I have this big doubt about how the EVM works,

Let's say I have an Ether balance of 1000000000000000000 in Wei (1 Ether) and I want to pass ALL of it to a payable function, let's call the function buy():

Contract.buy({ from: '0x0...', value: 1000000000000000000 });

Will that work? meaning; Will that in itself deduct GAS costs?

Or, on the contrary... I have to estimate Gas cost and deduct it myself from the Ether I'm sending? for example, If I estimate 50000 in Gas, I'd do it like this:

Contract.buy({ from: '0x0...', value: 999999999999950080, gas: 50000 });

Which approach is the correct one and Why?

Thanks in advance!

EDIT (Code in Swift):

    var options = Veev.options(gas: nil, from: addressFrom)
    let contract = web3.contract(contractABI, at: EthereumAddress(Rinkeby.address))
    let parameters = [EthereumAddress("0x0.."), amountToBuy] as [AnyObject]
    guard let estimatedGas = web3.contract(contractABI, at: EthereumAddress(Rinkeby.address))?.method("buy", parameters: parameters, options: options)?.estimateGas(options: nil) else { return }
    options.gas = estimatedGas
    let value: BigUInt = BigUInt(ether) - BigUInt(estimatedGas) * BigUInt(5000000000) - 1 // 5000000000 is gas Price
    options.value = value
  • updated ans check. You can transfer all eth by specifying gasPrice 0. Feb 14, 2018 at 6:08
  • Thanks, do explain. If I set gasPrice to 0, do I still have to use this formula: 1 ether - 55000 * 1 gwei?
    – jlstr
    Feb 14, 2018 at 23:19

2 Answers 2


The latter. The total ether required to make a transaction is amount + gasLimit * gasPrice (amount being how much ether you're sending). So you need to set aside enough ether to cover the maximum the transaction may cost for gas.

In your example of needing 50000 in gas, you'd want to send, say, 1 ether - 50000 * 1 gwei and specify a 1 gwei gas price.

  • Thank you very much! I really needed to hear commentary from a pro.
    – jlstr
    Feb 14, 2018 at 2:50
  • I have tried very hard to put this into code, but I'm still getting the following error: "insufficient funds for gas * price + value". It seems it's a more or less 'common' error, Any ideas about what I'm doing wrong, sir? Thanks.
    – jlstr
    Feb 14, 2018 at 22:55
  • Please share your code.
    – user19510
    Feb 14, 2018 at 22:57
  • 1
    Perhaps you're leaving it unspecified and letting something else choose, but you'll definitely need to know what gas price is going to be used so you can do the calculation properly.
    – user19510
    Feb 14, 2018 at 23:31
  • 1
    I'd phrase it as balance - gasLimit * gasPrice, but yes. (You're setting the gas limit according to the estimated gas, which is an appropriate thing to do.)
    – user19510
    Feb 15, 2018 at 0:00

Can I send ALL my Ether to a Payable function? or should a little be saved to pay for Gas?

Yes you can, by setting gasPrice is 0. But in real time we don't know how much time it will take to execute, It depends on situation. I seen in etherscan some of the transaction's value is 0.

Refer following link for the same: https://etherscan.io/tx/0x173fd250d63a62bb407f214ff924b49e1c90593572eab1a8c523783f2aa58ba3

In your example of you can estimate gas for buy() function using Contract. buy.estimateGas({ from: '0x0...', value: 1000000000000000000}) will tell how much gas will consume for a method. So no need to assume, but some dev's are complaining its not returning accurate value. Better to add 5-10% extra value.

Thats for safer side in above method lets assume its your estimated 55000 gas for exec buy(), then you need to send value to method is 1 ether - 55000 * 1 gwei

  • Thank you very much, using the formula above (let's say it's 3ETH): 3 ether - 80000 * 1 gwei, I'm getting the following error: "insufficient funds for gas * price + value" (My estimated Gas is 72000 and I round it up 80000). Any idea How to fix it?
    – jlstr
    Feb 14, 2018 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.