I want to estimate gas cost for a smart contract function. It's a kind of deposit function which takes USDT tokens from user wallet to the pool. So I should call the approve() function first to increase allowance and then send second transaction for deposit().
This is the source code. (* It works well.)
await tokenContract.methods.approve(poolAddress, amount).send({from: account});
await poolContract.methods.deposit(amount).send({from: account});
But when I try to estimate gas cost of second transaction like this:
await tokenContract.methods.approve(poolAddress, amount).send({from: account});
await poolContract.methods.deposit(amount).estimateGas({from: account});
estimateGas() fails with this error:
Error: Error: Internal JSON-RPC error.
{
"code": 3,
"message": "execution reverted: ERC20: transfer amount exceeds allowance",
"data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002845524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e6365000000000000000000000000000000000000000000000000"
}
What's the reason? I already called approve function before calling deposit.estimateGas(). When sending the normal transaction like first example, it works well but estimateGas() function doesn't work.
I tried one more thing. When I give 15 seconds of timeout between first function(approve) and second function(deposit.estimateGas), it works surprisingly. So in conclusion, it says that estimateGas() function doesn't work with the transactions in the same block itself.
Is it correct? If so, why?
approve
, so your allowance is probably0