I m trying to estimate gas for a swapTokensForExactETH
transaction with pancakeswap contract (similar to uniswap one)
When i do, i get the following error: Error: Returned error: execution reverted null
After some research it appears i shouldn't give the value
field when building the transaction with this contract method. But when i don't provide this field, i get the following error: Error: Returned error: execution reverted: PancakeRouter: EXCESSIVE_INPUT_AMOUNT
, same goes if i put 0 in value
For reference:
- Using Binance Smart Chain
- 0.00024 BNB = 1 Token.
- I have 10 tokens in my wallet + 1 BNB.
- I have approved the token on Pancakeswap
- I did manage to use
swapExactETHForTokens
successfully already, but notswapTokensForExactETH
yet
// For your information:
// `CNF.wallet` = My wallet address
// `config.token.contract`= `0x00e1656e45f18ec6747f5a8496fd39b50b38396d` (BCOIN)
// `config.crypto.bnb.contract` = `0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c`
// `config.crypto.pancake.router.contract` = `0x10ED43C718714eb63d5aA57B78B54704E256024E`
var amountToGet = 0.00024; //exact amount of BNB that i want (equal to 1 token)
var amountOutMax = 5; //max amount of tokens i m willing to use to get above BNB amount
var data = contract.methods.swapTokensForExactETH(
web3_bsc.utils.toHex(amountToGet),
web3_bsc.utils.toHex(amountOutMax),
[config.token.contract, config.crypto.bnb.contract],
CNF.wallet,
web3_bsc.utils.toHex(Math.round(Date.now() / 1000) + 60 * 20), //deadline
);
var count = await web3_bsc.eth.getTransactionCount(CNF.wallet);
var rawTransaction = {
"from": CNF.wallet,
"gasLimit": web3_bsc.utils.toHex(CNF.gasmax),
"to": config.crypto.pancake.router.contract,
// "value":web3_bsc.utils.toHex(amountToGet),
// "value": 0,
"data": data.encodeABI(),
"nonce": web3_bsc.utils.toHex(count)
};
web3_bsc.eth.estimateGas(rawTransaction, (error, gasEstimate) => {
if (!error && gasEstimate) {
//continue...
}
else {
//error
}
});
Best Answer
You forgot to convert the amounts from ether to wei.
https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html#towei
Amounts sent in ethereum contracts are
uint256
, you must convert your floating pointether
value towei
unit, which is done by multiplying by the number of decimal points a token has.WBNB
and your token are using 18 decimals, so callYour
amountInMax
is "5", this means 0.00000000000000005 BCOIN, henceEXCESSIVE_INPUT_AMOUNT
is thrown.swapTokensForExactETH
swaps an unknown amount of Token into exact ETH.You shouldn't specify
value
since you're receiving ETH (BNB in this case) andvalue
sends ETH along to the contract.According to docs -
amountOut
is the amount of ETH to receive.amountInMax
is the maximum amount of input tokens that can be required before the transaction reverts.The
EXCESSIVE_INPUT_AMOUNT
error means that to match your requestedamountOut
(exact ETH), you must pay too much of Token, more than allowed inamountInMax
. This means you're not calculating enough slippage inamountInMax
to sustain normal price fluctuations.If you want this to work in real time, you should use UniswapSDK to calculate
inputAmount
using the Trade object, then add 0.1% for slippage and use that formaxInputAmount
.Make sure you have enough of Token in your wallet to cover this.