xclud / web3dart

Ethereum library, written in Dart.
https://pub.dev/packages/web3dart
MIT License
180 stars 96 forks source link

Estimating gas fee exception #29

Open 3ph opened 2 years ago

3ph commented 2 years ago

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?

muhammadsaddamnur commented 2 years ago

@3ph i have same issue, are you have fixed this issue?

3ph commented 2 years ago

Yes, I have changed the structure of the call which seemed to have fixed it:

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

    final fee = await _client.estimateGas(
      sender: EthereumAddress.fromHex(address),
      to: EthereumAddress.fromHex(recipientAddress),
      data: transferFunction.encodeCall([
        EthereumAddress.fromHex(address),
        EthereumAddress.fromHex(recipientAddress),
        amount,
      ]),
    );
ttb-inc commented 2 years ago

When checking the ERC721 token, it seems that the correct value cannot be obtained. (The value varies a lot.) Has anyone checked?

Thank you.