Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to fetch gas estimate when sending erc20 #307

Closed
ChuanqingYang opened this issue Jan 30, 2021 · 1 comment
Closed

Failed to fetch gas estimate when sending erc20 #307

ChuanqingYang opened this issue Jan 30, 2021 · 1 comment

Comments

@ChuanqingYang
Copy link

ChuanqingYang commented Jan 30, 2021

    let value : String = amount
    let walletAddress = EthereumAddress(wallet.address)!
    let toAddress = EthereumAddress(to)!
    // contractAddress
    let erc20ContractAddress = EthereumAddress("0x94a91f7f24738b2b49d0d009aA0801a280D05eBb")!
    let web3 = self.getETHWeb3()
    let keyStoreManager = self.getETHKeyStoreManagerWithWallet(wallet: wallet)
    web3.addKeystoreManager(keyStoreManager)
    let contract = web3.contract(Web3.Utils.erc20ABI, at: erc20ContractAddress, abiVersion: 2)!
    let amountA = Web3.Utils.parseToBigUInt(value, units: .eth)!
    var options = TransactionOptions.defaultOptions
    options.from = walletAddress
    options.gasLimit = .automatic
    options.gasPrice = .automatic
    options.nonce = .manual(try! web3.eth.getTransactionCount(address: EthereumAddress(wallet.address)!))
    let method = "transfer"
    let tx = contract.write(method,
                             parameters: [toAddress, "\(amountA)"] as [AnyObject],
                             transactionOptions: options)!
    print(tx)
    let result = try! tx.send(password: password)
    complete(result)

Here is my error code & description: -32000 execution reverted
Failed to fetch gas estimate

@ChuanqingYang
Copy link
Author

Finally i found what's wrong with my code

The amount what i'm set is ETH which is a wrong option.
let amountA = Web3.Utils.parseToBigUInt(value, units: .eth)!
ERC20 transation amount should be set what it's decimals be,use the below functions:
let amountA = Web3.Utils.parseToBigUInt(value, decimal: X)!
you should replace X with what contract requiered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants