xclud / web3dart

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

NFT(ERC721) estimateGas? #44

Closed ttb-inc closed 2 years ago

ttb-inc commented 2 years ago

Hi,

To calculate gasLimit in real time, we are going to use estimateGas. However, the returned gasLimit value is too small. (ex, early 20,000, not 21,000 ^^) If you actually send it, you will get over 50,000. Is the method I used correct? How can I get estimateGas for ERC721? thank you

...
final contractFunction = contract.function('safeTransferFrom');
        return await client.estimateGas(
          sender: EthereumAddress.fromHex(from),
          to: EthereumAddress.fromHex(to),
          data: contractFunction.encodeCall([EthereumAddress.fromHex(from), EthereumAddress.fromHex(to), tokenId, Uint8List.fromList([])]),
          gasPrice: EtherAmount.inWei(gasPrice),
          amountOfGas: gasPrice,
          value: EtherAmount.inWei(BigInt.from(1)),
        );
...        
mrtnetwork commented 2 years ago

your code is ok. change your rpc url

TheGreatAxios commented 2 years ago

@ttb-inc Is it working? if not, what network are you using?

ttb-inc commented 2 years ago

@ttb-inc Is it working? if not, what network are you using?

@TheGreatAxios It still doesn't work.

[Network] 80001 (Polygon Testnet(Mumbai)) (https://docs.polygon.technology/docs/develop/network-details/network/)

[RPC Endpoint] RPC Endpoint tested two things.

  1. https://rpc-mumbai.matic.today/
  2. Supported by Moralis. (https://speedy-nodes-nyc.moralis.io/[API_KEY]/polygon/mumbai) Both of the above result in the same result.

Thank you.

TheGreatAxios commented 2 years ago

@ttb-inc Please send your code here, I will take a look. Currently, I am having no issues estimated gas on Mumbai; so it may be something related to your setup/config.

NOTE: DO NOT UPLOAD ANY API KEYS when you upload your code (Just a reminder :)) I see you commended the one out above

ttb-inc commented 2 years ago

@ttb-inc Please send your code here, I will take a look. Currently, I am having no issues estimated gas on Mumbai; so it may be something related to your setup/config.

NOTE: DO NOT UPLOAD ANY API KEYS when you upload your code (Just a reminder :)) I see you commended the one out above

@TheGreatAxios Thank you for answer.

...
final contractFunction = contract.function('safeTransferFrom');
        return await client.estimateGas(
          sender: EthereumAddress.fromHex(from),
          to: EthereumAddress.fromHex(to),
          data: contractFunction.encodeCall([EthereumAddress.fromHex(from), EthereumAddress.fromHex(to), tokenId, Uint8List.fromList([])]),
          gasPrice: EtherAmount.inWei(gasPrice),
          amountOfGas: gasPrice,
          value: EtherAmount.inWei(BigInt.from(1)),
        );
...        

If there is no problem with the above code, I will check it again.

Let me create testable code. Thank you.

TheGreatAxios commented 2 years ago

@ttb-inc Is this for an ERC-1155 or an ERC-721? Also, can you please provide the error.

Thanks

ttb-inc commented 2 years ago

@TheGreatAxios Thank you for answer.

This is my fault. The same to: address was used. I separated the to: address, and it works fine. (contract address, wallet address)

...
final contractFunction = contract.function('safeTransferFrom');
        return await client.estimateGas(
          sender: EthereumAddress.fromHex(from),
          to: EthereumAddress.fromHex(contract address),
          data: contractFunction.encodeCall([EthereumAddress.fromHex(from), EthereumAddress.fromHex(wallet address), tokenId, Uint8List.fromList([])]),
          gasPrice: EtherAmount.inWei(gasPrice),
          amountOfGas: gasPrice,
          value: EtherAmount.inWei(BigInt.from(1)),
        );
...        

The gasLimit is similar to 90,000. (real 87,933)

Thank you very much. Have a good day.