Estimating Gas in Ethereum

Bernard Peh
Jun 1, 2018 · 2 min read

Many Ethereum wallets such as myetherwallet will tell you how much gas is required before you make a transaction. Sending Ethereum to another user account will cost 21000 but sending data requires some extra calculation. What if we don’t have the luxury of a UI and have to write a script for it?

Let’s say we want to calculate the gas required to contribute to an ICO by calling the “transfer(address,uint256)” method.

There are a few ways we could do it. The most direct way is to paste the full solidity code in the remix browser and simulate the transfer function. Another way is to change from Ether to the payment token in myetherwallet and it will tell you how much gas it requires to send the token to another person. Other Ethereum wallets would work the same way as well.

If you are a developer and know the ABI of the Token contract, the easiest way is to call estimateGas on the contract function.

// possible to get from remix or compile it yourself if you have the code
let abi = [ { … } ]
const contract = web3.eth.contract(abi).at(token_address);let result = contract.transfer.estimateGas(to_address, token_val, {from: my_account});console.log(result)

However if you don’t have the ABI and the only thing you know is that its an ERC20 token, you could still estimate the gas required based on the fact that you know there must be a “transfer” function. I hope the code below is self explanatory.

// Set recipient address and transfer value first.
let to_address = sender_address
let transfer_val = sender_val
// function signature is the first 4 bytes of the sha3 hash
let function_signature = web3.sha3('transfer(address,uint256)').substring(0,10)
// we have to make the address field 32 bytes
let address_param = '0'.repeat(24)+to_address.substring(2)
// likewise, we have to make the transfer value 32 bytes
let transfer_value_param = web3.toHex(web3.toBigNumber(transfer_val*Math.pow(10, 18)).toNumber())
let transfer_value_prefix = '0'.repeat((66 - transfer_value_param.length))// combining the function sig and all the arguments
let transfer_data = function_signature + address_param + transfer_value_prefix + transfer_value_param.substring(2)
// We are ready to estimateGas with all the data ready.
var result = web3.eth.estimateGas({
from: from_address,
// token contract address
to: contract_address,
data: transfer_data,
});console.log(result)

Knowing how much gas allows you to calculate the transaction fees (since you can set the gas price manually) before the actual transaction which is always good to know.

That’s it. Happy coding.

Bernard Peh
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade