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