web3dart estimating gas fee exception

930 views Asked by At

I'm trying to estimate the gas fee for the ERC20 token transaction - in this case transferring DAI from one address to another (on Mumbai).

The code for estimating the gas fee:

final contract = DeployedContract(ContractAbi.fromJson(abi, token.id),  // 'dai'
          EthereumAddress.fromHex(token.contractAddress)); // 0xcB1e72786A6eb3b44C2a2429e317c8a2462CFeb1

final transferFunction = contract.function('transferFrom');

final transaction = Transaction.callContract(
  contract: contract,
  function: transferFunction,
  parameters: [
    EthereumAddress.fromHex(address),  // 0x2970C7181450B6c13071131f0005ccA18436c12B
    EthereumAddress.fromHex(recipientAddress),  // 0xc7c6BAEA62Ff6BBAca799156CC4e9f50BC9e8060
    10000000000000,  // 0.001 Dai
  ],
);

final fee = await _client.estimateGas(
  to: transaction.to,
  value: transaction.value,
  data: transaction.data,
);

However, I'm getting RPCError: got code 3 with msg "execution reverted: Dai/insufficient-allowance". The sender address holds enough tokens (approx. 0.0038 Dai). I have tried first to call approve with the same amount but I'm getting a different exception RPCError: got code -32000 with msg "already known". and the gas estimation still fails.

Any ideas on how to estimate correctly with web3dart?

1

There are 1 answers

0
Arun Xena On

Try adding sender to estimateGas function,

  final fee = await _client.estimateGas(
      sender: EthereumAddress.fromHex(fromAddress),
      to: transaction.to,
      value: transaction.value,
      data: transaction.data,
    );