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
}
});