Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: cannot estimate gas Pancake swap bot. #62

Open
tuxforensics opened this issue May 17, 2021 · 12 comments
Open

Error: cannot estimate gas Pancake swap bot. #62

tuxforensics opened this issue May 17, 2021 · 12 comments

Comments

@tuxforensics
Copy link

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
Copy link
Author

tuxforensics commented May 19, 2021

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 },

@MartinSchere
Copy link

I'm having the same issue

@MartinSchere
Copy link

Were you able to solve this?

@tuxforensics
Copy link
Author

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
Copy link
Author

tuxforensics commented May 20, 2021 via email

@MartinSchere
Copy link

But you are able to call getAmountsOut?

@tuxforensics
Copy link
Author

tuxforensics commented May 20, 2021 via email

@MartinSchere
Copy link

MartinSchere commented May 20, 2021

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

@tuxforensics
Copy link
Author

tuxforensics commented May 20, 2021 via email

@vlbhtcno1
Copy link

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

@nictengkk
Copy link

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
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants