question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed to fetch gas estimate when sending erc20

See original GitHub issue

Trying to send an erc20 token but getting a Web3Error “Failed to fetch gas estimate”. Here is a snippet of my code:

   let amount = Web3.Utils.parseToBigUInt(tokenAmount, units: .eth)
        var options = TransactionOptions.defaultOptions
        options.gasPrice = gasPrice // is calculated as follows web3Main.eth.getGasPrice()
        options.gasLimit = gasLimit //set to BigUInt(50000)
        options.from = EthereumAddress(from)
        options.to = ethToAddress
        options.value = amount

      guard let myTransaction = contract.method("transfer", parameters: [ethToAddress, amount] as [AnyObject], extraData: Data(), transactionOptions: options) else {
            throw Web3Error.transactionSerializationError
        }
        
        let myRes = try myTransaction.send(password: "PASSWORD", transactionOptions: options)

Produces error on the last line. Any ideas?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
WarTech9commented, May 16, 2019

The problem here is when sending ERC20 tokens the options.value field should not be set. That field is supposed to be used for sending ETH. Notice the amount is already specified in the parameters array. So just delete the line options.value = amount.

This should probably be fixed in the example in the documentation as that sets the options.value field which will always result in this error.

1reaction
tuanYasuocommented, Aug 1, 2019

Please fix it in document for everyone! thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed to fetch gas estimate when sending erc20 #147 - GitHub
Trying to send an erc20 token but getting a Web3Error "Failed to fetch gas estimate". Here is a snippet of my code: let...
Read more >
Why did my transaction fail with an "Out of Gas" error? How ...
Cause. The "out of gas" error occurs when all the gas you allotted for the transaction is consumed before the transaction could complete....
Read more >
How to fix "Gas estimation failed error" when transferring ...
I have a simple ERC20 contract which is ERC20Detailed, ERC20Mintable and Ownable. On deployment I want: Add Minter to a new account, ...
Read more >
Changelog - CocoaPods
Error in Documentation Examples: "Failed to fetch gas estimate" #421; BSC Network Transaction #418; How to sign and send ERC20 token #413; Example...
Read more >
Solidity/Web3 Error : We were not able to estimate gas. There ...
Solidity/Web3 Error : We were not able to estimate gas. There might be an error in the contract and this transaction may fail....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found