Buy Tokens on Pancake Swap
Closed this issue · 10 comments
Hi
I'm using your code for 1 month but today it's not working and I don't know why. Whenever I try to buy with code transactions fail with 'INSUFFICIENT_INPUT_AMOUNT'. Could you look at this problem? Thanks.
Hi,
I could try to help. Can you show me the transaction that is failing?
Also have you tried to buy from pancakeswap.finance and check the transaction from pancakeswap.finance and then simulate it with the web3 code?
it could be my bad. I'm gonna change it and write you back. But also I wonder how could I specify the slippage tolerance with percentage ?
Bro got your answer. Yesterday I changed a code a little bit and it was working but after woke up today, my transactions fail with Error: Transaction has been reverted by the EVM
is that problem of mine ?
failed transaction hash : https://bscscan.com/tx/0x62770c39fb95de9e8af2559195b1ffa5114864263d0bf7f52603c045f054e884
var fs = require('fs')
var Tx = require('ethereumjs-tx').Transaction;
var Web3 = require('web3')
var Common = require('ethereumjs-common').default;
let rawData = fs.readFileSync('data.json');
let progData = JSON.parse(rawData);
var web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed.binance.org/'))
var BSC_FORK = Common.forCustomChain(
'mainnet',
{
name: 'Binance Smart Chain Mainnet',
networkId: 56,
chainId: 56,
url: 'https://bsc-dataseed.binance.org/'
},
'istanbul',
);
// SPECIFY_THE_AMOUNT_OF_BNB_YOU_WANT_TO_BUY_FOR_HERE
var originalAmountToBuyWith = '0.001' + Math.random().toString().slice(2,7);
var bnbAmount = web3.utils.toWei(originalAmountToBuyWith, 'ether');
var targetAccount = progData.mainAccount;
var res = buyOnlyone(targetAccount, bnbAmount);
console.log(res);
async function buyOnlyone(targetAccount, amount) {
var privateKeyBuffer = progData.privateKey;
var privateKey = Buffer.from(privateKeyBuffer,'hex');
var amountToBuyWith = web3.utils.toHex(amount);
var tokenAddress = '0x9a306e11d571caa7760551d97b2b990ee47ff7ca'; // ONLYONE contract address
var WBNBAddress = '0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c'; // WBNB token address
var amountOutMin = '100' + Math.random().toString().slice(2,6);
var pancakeSwapRouterAddress = '0x10ed43c718714eb63d5aa57b78b54704e256024e';
var routerAbi = JSON.parse(fs.readFileSync('pancake-router-abi.json', 'utf-8'));
var contract = new web3.eth.Contract(routerAbi, pancakeSwapRouterAddress, {from: targetAccount});
var data = contract.methods.swapExactETHForTokens(
web3.utils.toHex(amountOutMin),
[WBNBAddress,
tokenAddress],
targetAccount,
web3.utils.toHex(Math.round(Date.now()/1000)+60*20),
);
var count = await web3.eth.getTransactionCount(targetAccount);
var rawTransaction = {
"from":targetAccount,
"gasPrice":web3.utils.toHex(5000000000),
"gasLimit":web3.utils.toHex(290000),
"to":pancakeSwapRouterAddress,
"value":web3.utils.toHex(amountToBuyWith),
"data":data.encodeABI(),
"nonce":web3.utils.toHex(count)
};
var transaction = new Tx(rawTransaction, { 'common': BSC_FORK });
transaction.sign(privateKey);
var result = await web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'));
console.log(result)
return result;
}
found it its about gas price no problem thanks.
Hi @cem-altuner , I have a same error. Can you sharing how to fixed? thanks!
@thanglv1302 If you are referring to https://bscscan.com/tx/0x91299941c2da1ede382a8bf7909072da6f76843b310a6807a8dbe112b2476133
The problem is different. There you try to sell tokens that you don't have. You first need to buy them in order to sell them later.
@religion-counter, thank mate, I have a comment, please give me your answers, thanks!
1196977#commitcomment-54339907
@religion-counter I get error too Transaction has been reverted by the EVM
https://bscscan.com/tx/0x71dea3762493a85041d65948d7edb67fd99d602cb439f03aecbfc893ec7fe43d
@religion-counter I get error too
Transaction has been reverted by the EVM
https://bscscan.com/tx/0x71dea3762493a85041d65948d7edb67fd99d602cb439f03aecbfc893ec7fe43d
Hi @microooji ,
In the contract you need to set the Pancakeswap router address: 0x10ed43c718714eb63d5aa57b78b54704e256024e
You have set the contract for the pancake pair, not the pancake router. You can try to manually execute the transaction with the browser and then try to simulate it with web3.
Regards