web3swift: Failed to fetch gas estimate when sending erc20

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?

About this issue

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

Most upvoted comments

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.

Please fix it in document for everyone! thanks