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

Is there any function to calculate minimum amount needed to perform ethereum transaction? #134

Closed
DiwakarThapa opened this issue Mar 25, 2019 · 1 comment
Labels
question Further information is requested

Comments

@DiwakarThapa
Copy link

Hello,
I wanted to know is there any function in web3swift to validate minimum amount required to make transaction ?

@TonioMacaronio
Copy link
Collaborator

TonioMacaronio commented Mar 25, 2019

Hi, @DiwakarThapa
Think you need estimateGas function. It estimates a minimal amount of gas required to run a transaction. To do it the Ethereum node tries to run it and counts how much gas it consumes for computations. Setting the transaction gas limit lower than the estimate will most likely result in a failing transaction.
Usage example

let web3 = Web3.InfuraMainnetWeb3()
let sendToAddress = EthereumAddress("recepient_address")
let keystore = try! EthereumKeystoreV3(password: "")
let keystoreManager = KeystoreManager([tempKeystore!])
web3.addKeystoreManager(keystoreManager)
let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)
guard let writeTX = contract?.write("fallback") else {return} // you can use any other tx method
writeTX.transactionOptions.from = tempKeystore!.addresses?.first // its your address
writeTX.transactionOptions.value = BigUInt("1.0", .eth) // value in eth
let estimate = try writeTX.estimateGasPromise().wait()
print(estimate) // here you'll get gas estimation

Hope this will help you

@TonioMacaronio TonioMacaronio added question Further information is requested wait for reply labels Mar 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants