eattheblocks icon indicating copy to clipboard operation
eattheblocks copied to clipboard

Error: cannot estimate gas Pancake swap bot.

Open tuxforensics opened this issue 4 years ago • 12 comments

I am looking to add .gasLimit and or .gasPrice to solve the estimate gas error..

I get the WBNB approval to work but then the script errors with estimate gas during the swap

tuxforensics avatar May 17 '21 15:05 tuxforensics

This works

const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [tokenIn, tokenOut], addresses.recipient, Math.floor(Date.now() / 1000) + 60 * 3, // 3 minutes from the current Unix time { gasPrice: 5, gasLimit: 210000 } );

But now getting

code: -32000, response: '{"jsonrpc":"2.0","id":5,"error":{"code":-32000,"message":"transaction underpriced"}}\n', transaction: { nonce: 11, gasPrice: BigNumber { _hex: '0x05', _isBigNumber: true }, gasLimit: BigNumber { _hex: '0x033450', _isBigNumber: true },

tuxforensics avatar May 19 '21 16:05 tuxforensics

I'm having the same issue

MartinSchere avatar May 19 '21 23:05 MartinSchere

Were you able to solve this?

MartinSchere avatar May 19 '21 23:05 MartinSchere

this gets me past the estimated gas issues


const mygasPrice = ethers.utils.parseUnits('0.000000005', 'ether');
  
  const tx = await router.swapExactTokensForTokens(
    amountIn,
    amountOutMin,
    [tokenIn, tokenOut],
    addresses.recipient,
    Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time
    {
        gasPrice: mygasPrice,
        gasLimit: '162445'
    }
  );

tuxforensics avatar May 20 '21 01:05 tuxforensics

I am close

https://ethereum.stackexchange.com/questions/99320/noob-troubleshooting-failed-bsc-transaction-using-ethers-js-and-pancakeswap-v2-r

On Wed, May 19, 2021 at 6:10 PM Martín Schere @.***> wrote:

Were you able to solve this?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jklepatch/eattheblocks/issues/62#issuecomment-844557105, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATE52OBXMIDTW3UFJROTWWDTORANPANCNFSM45AWF6AQ .

tuxforensics avatar May 20 '21 02:05 tuxforensics

But you are able to call getAmountsOut?

MartinSchere avatar May 20 '21 12:05 MartinSchere

No I don’t care about that. I have hard coded the gas

On Thu, May 20, 2021 at 7:43 AM Martín Schere @.***> wrote:

But you are able to call getAmountsOut?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jklepatch/eattheblocks/issues/62#issuecomment-845076941, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATE52OHY6F3JA34VFNUC4EDTOT7W3ANCNFSM45AWF6AQ .

tuxforensics avatar May 20 '21 12:05 tuxforensics

Yes, but when I run your code I get the error in getAmountsOut like I do when I run mine. The gas is not the issue here, I think the error is misleading

MartinSchere avatar May 20 '21 13:05 MartinSchere

You need to add BNB and WBNB in your test wallet


const ethers = require(‘ethers’);

const addresses = { WBNB: ‘0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c’, BUSD: ‘0xe9e7cea3dedca5984780bafc599bd69add087d56’, factory: ‘0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73’, router: ‘0x10ed43c718714eb63d5aa57b78b54704e256024e’, recipient: ‘’ }

//First address of this mnemonic must have enough BNB to pay for tx fess

const privateKey = ‘’;

const mygasPrice = ethers.utils.parseUnits(‘5’, ‘gwei’);

const provider = new ethers.providers.WebSocketProvider(‘wss:// muddy-young-frost.bsc.quiknode.pro/’); const wallet = new ethers.Wallet(privateKey); const account = wallet.connect(provider);

const factory = new ethers.Contract( addresses.factory, [‘event PairCreated(address indexed token0, address indexed token1, address pair, uint)’], account ); const router = new ethers.Contract( addresses.router, [ ‘function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)’, ‘function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)’ ], account );

const wbnb = new ethers.Contract( addresses.WBNB, [ ‘function approve(address spender, uint amount) public returns(bool)’, ], account );

console.log(Before Approve); const valueToapprove = ethers.utils.parseUnits(‘0.1’, ‘ether’); const init = async () => { const tx = await wbnb.approve( router.address, valueToapprove, { gasPrice: mygasPrice, gasLimit: ‘162445’ } ); console.log(After Approve); const receipt = await tx.wait(); console.log(‘Transaction receipt’); console.log(receipt); }

factory.on(‘PairCreated’, async (token0, token1, pairAddress) => { console.log(after factory.on:); console.log( New pair detected ================= token0: ${token0} token1: ${token1} pairAddress: ${pairAddress} );

//The quote currency needs to be WBNB (we will pay with WBNB) let tokenIn, tokenOut; if(token0 === addresses.WBNB) { tokenIn = token0; tokenOut = token1; }

if(token1 == addresses.WBNB) { tokenIn = token1; tokenOut = token0; }

//The quote currency is not WBNB if(typeof tokenIn === ‘undefined’) { return; }

//We buy for 0.1 BNB of the new token //ethers was originally created for Ethereum, both also work for BSC //‘ether’ === ‘bnb’ on BSC console.log(line 87); const amountIn = ethers.utils.parseUnits(‘0.01’, ‘ether’); const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]); //Our execution price will be a bit different, we need some flexbility const amountOutMin = amounts[1].sub(amounts[1].div(10));

console.log(line 92); console.log( Buying new token ================= tokenIn: ${amountIn} ${tokenIn} (WBNB) tokenOut: ${amountOutMin} ${tokenOut} ); console.log(line 101);

const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [tokenIn, tokenOut], addresses.recipient, Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time { gasPrice: mygasPrice, gasLimit: 162445 } ); console.log(line 117); const receipt = await tx.wait(); console.log(‘Transaction receipt’); console.log(receipt); });

init();

On Thu, May 20, 2021 at 8:18 AM Martín Schere @.***> wrote:

Yes, but when I run your code I get the error in getAmountsOut like I do when I run mine

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jklepatch/eattheblocks/issues/62#issuecomment-845113014, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATE52OG5D6D6O5ZU2HRS5ETTOUD27ANCNFSM45AWF6AQ .

tuxforensics avatar May 20 '21 18:05 tuxforensics

this gets me past the estimated gas issues


const mygasPrice = ethers.utils.parseUnits('0.000000005', 'ether');
  
  const tx = await router.swapExactTokensForTokens(
    amountIn,
    amountOutMin,
    [tokenIn, tokenOut],
    addresses.recipient,
    Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time
    {
        gasPrice: mygasPrice,
        gasLimit: '162445'
    }
  );

Thank you. It very helpful for me :) I'm a developer but stuck after 2 hours research

vlbhtcno1 avatar Jun 01 '21 18:06 vlbhtcno1

You need to add BNB and WBNB in your test wallet *************** const ethers = require(‘ethers’); const addresses = { WBNB: ‘0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c’, BUSD: ‘0xe9e7cea3dedca5984780bafc599bd69add087d56’, factory: ‘0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73’, router: ‘0x10ed43c718714eb63d5aa57b78b54704e256024e’, recipient: ‘’ } //First address of this mnemonic must have enough BNB to pay for tx fess const privateKey = ‘’; const mygasPrice = ethers.utils.parseUnits(‘5’, ‘gwei’); const provider = new ethers.providers.WebSocketProvider(‘wss:// muddy-young-frost.bsc.quiknode.pro/’); const wallet = new ethers.Wallet(privateKey); const account = wallet.connect(provider); const factory = new ethers.Contract( addresses.factory, [‘event PairCreated(address indexed token0, address indexed token1, address pair, uint)’], account ); const router = new ethers.Contract( addresses.router, [ ‘function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)’, ‘function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)’ ], account ); const wbnb = new ethers.Contract( addresses.WBNB, [ ‘function approve(address spender, uint amount) public returns(bool)’, ], account ); console.log(Before Approve); const valueToapprove = ethers.utils.parseUnits(‘0.1’, ‘ether’); const init = async () => { const tx = await wbnb.approve( router.address, valueToapprove, { gasPrice: mygasPrice, gasLimit: ‘162445’ } ); console.log(After Approve); const receipt = await tx.wait(); console.log(‘Transaction receipt’); console.log(receipt); } factory.on(‘PairCreated’, async (token0, token1, pairAddress) => { console.log(after factory.on:); console.log( New pair detected ================= token0: ${token0} token1: ${token1} pairAddress: ${pairAddress} ); //The quote currency needs to be WBNB (we will pay with WBNB) let tokenIn, tokenOut; if(token0 === addresses.WBNB) { tokenIn = token0; tokenOut = token1; } if(token1 == addresses.WBNB) { tokenIn = token1; tokenOut = token0; } //The quote currency is not WBNB if(typeof tokenIn === ‘undefined’) { return; } //We buy for 0.1 BNB of the new token //ethers was originally created for Ethereum, both also work for BSC //‘ether’ === ‘bnb’ on BSC console.log(line 87); const amountIn = ethers.utils.parseUnits(‘0.01’, ‘ether’); const amounts = await router.getAmountsOut(amountIn, [tokenIn, tokenOut]); //Our execution price will be a bit different, we need some flexbility const amountOutMin = amounts[1].sub(amounts[1].div(10)); console.log(line 92); console.log( Buying new token ================= tokenIn: ${amountIn} ${tokenIn} (WBNB) tokenOut: ${amountOutMin} ${tokenOut} ); console.log(line 101); const tx = await router.swapExactTokensForTokens( amountIn, amountOutMin, [tokenIn, tokenOut], addresses.recipient, Math.floor(Date.now() / 1000) + 60 * 20, // 20 minutes from the current Unix time { gasPrice: mygasPrice, gasLimit: 162445 } ); console.log(line 117); const receipt = await tx.wait(); console.log(‘Transaction receipt’); console.log(receipt); }); init(); On Thu, May 20, 2021 at 8:18 AM Martín Schere @.***> wrote: Yes, but when I run your code I get the error in getAmountsOut like I do when I run mine — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <#62 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATE52OG5D6D6O5ZU2HRS5ETTOUD27ANCNFSM45AWF6AQ .

Hi, I am facing the same issue, and I have transferred a small amount of WBNB and definitely have BNB in my wallet. However, I am still getting this error. Currently setting mygasFee as const mygasPrice = ethers.utils.parseUnits("0.000000005", "ether");. Contract is getting approved but the swap does not go through. Any advise?

Error: replacement fee too low (error={"code":-32000,"response":"{\"jsonrpc\":\"2.0\",\"id\":7,\"error\":{\"code\":-32000,\"message\":\"replacement transaction underpriced\"}}\n"}, method="sendTransaction", reason: 'replacement fee too low', code: 'REPLACEMENT_UNDERPRICED', error: Error: replacement transaction underpriced

nictengkk avatar Jun 12 '21 09:06 nictengkk

Okay I added different gas prices for the approval and swap request, and the transaction went through, but I am still receiving a Error: transaction was replaced (cancelled=true, reason="replaced", replacement= error, not sure if I should be concerned?

nictengkk avatar Jun 12 '21 09:06 nictengkk