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

Error in Documentation Examples: "Failed to fetch gas estimate" #421

Closed
jimmyneutront opened this issue Dec 8, 2021 · 1 comment
Closed

Comments

@jimmyneutront
Copy link
Contributor

I'm running a very simple test that is almost entirely copied from the documentation examples here. I start a local Hardhat node, import 2 Ethereum accounts using the private keys of two pre-funded accounts set up by Hardhat, and then attempt to send 1 ETH from the first account to the second. Again, the code with which I am trying to do this is almost entirely copied and pasted from the documentation examples linked above. I have tried fetching web3swift both from the "master" branch and the "develop" branch; the error occurs either way.
Prerequisites:
Open an Xcode project that depends on web3swift, create a new unit test class and add web3swift as a testable import.
Steps to Reproduction:
Paste the following function into the newly created unit test class:

func testTransferEth() {

    //Restore Hardhat account #1
    let password_one = "web3swift"
    let key_one = "59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d" // Some private key
    let formattedKey_one = key_one.trimmingCharacters(in: .whitespacesAndNewlines)
    let dataKey_one = Data.fromHex(formattedKey_one)!
    let keystore_one = try! EthereumKeystoreV3(privateKey: dataKey_one, password: password_one)!
    let keyData_one = try! JSONEncoder().encode(keystore_one.keystoreParams)
    let address_one = keystore_one.addresses!.first!.address
   
    //Restore Hardhat account #2
    let password_two = "web3swift"
    let key_two = "5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a" // Some private key
    let formattedKey_two = key_two.trimmingCharacters(in: .whitespacesAndNewlines)
    let dataKey_two = Data.fromHex(formattedKey_two)!
    let keystore_two = try! EthereumKeystoreV3(privateKey: dataKey_two, password: password_two)!
    let keyData_two = try! JSONEncoder().encode(keystore_two.keystoreParams)
    let address_two = keystore_two.addresses!.first!.address
    
    //Establish connection to Hardhat node
    let endpoint = "http://192.168.1.12:8545"
    let web3 = web3(provider: Web3HttpProvider(URL(string: endpoint)!)!)
    let keystoreManager = KeystoreManager([keystore_one, keystore_two])
    web3.addKeystoreManager(keystoreManager)
    
    //Transfer 1 ETH from account #1 to account #2
    let value: String = "1.0" // In Ether
    let walletAddress = EthereumAddress(address_one)! // Your wallet address
    let toAddress = EthereumAddress(address_two)!
    let contract = web3.contract(Web3.Utils.coldWalletABI, at: toAddress, abiVersion: 2)!
    let amount = Web3.Utils.parseToBigUInt(value, units: .eth)
    var options = TransactionOptions.defaultOptions
    options.value = amount
    options.from = walletAddress
    options.gasPrice = .automatic
    options.gasLimit = .automatic
    let tx = contract.write(
        "fallback",
        parameters: [AnyObject](),
        extraData: Data(),
        transactionOptions: options)!
    let password = "web3swift"
    let result = try! tx.send(password: password)

}

Run the unit test.

Expected Result:
1 ETH is successfully transferred from the first account to the second account.

Actual Result:
The following error is thrown:

    iosAppTests/TestClass.swift:67: Fatal error: 'try!' expression unexpectedly raised an error: web3swift.Web3Error.processingError(desc: "Failed to fetch gas estimate")
    2021-12-08 00:32:34.572536-0500 iosApp[9023:569768] iosAppTests/TestClass.swift:67: Fatal error: 'try!' expression unexpectedly raised an error: web3swift.Web3Error.processingError(desc: "Failed to fetch gas estimate")

Any idea what is going wrong here?

Environment:
OS: macOS Monterey 12.0.1
Xcode: 13.1 (13A1030d)
web3swift: freshly installed from 'develop' branch

@jimmyneutront
Copy link
Contributor Author

I was able to resolve the issue by replacing
options.gasPrice = .automatic
options.gasLimit = .automatic
with
options.gasPrice = .manual(BigUInt(875000000))
options.gasLimit = .manual(BigUInt(30000000))

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

1 participant