Technical Details on Gas on Ethereum

I’ve been working over the last year off and on on different smart contracts.  What follows are some technical details I sent to some of my colleagues here at Cisco regarding gas price and gas limits.  Two important parts of Ethereum that are not understood very well: Units and Gas.

Units

1 Ether in Ethereum is divisible up to 18 decimal units.  The smallest unit is called a wei.  Thus, 1 wei  followed by 18 zeros equals 1 ether.  So:
The other important unit to know is a gwei.  A gwei is halfway between 1 ether and 1 wei.

In solidity everything is stored in wei values.  There are no fractions nor decimals, we just work with integers.  So by having this long zero values provide a way to have decimals without having decimals.

There are several functions I’ve used in some recent test cases that use this conversion as it is much easier.  Here are some examples:

Essentially, the fromWei function lets you format a value from wei to something else, like ether.  The toWei let’s you convert from ether to wei without having to write out all the zeros.  In Ethereum we have this whole other BigNumber class to deal with.  BigNumber is the class that allows us to store these giant 18 digit numbers as well as manipulate them.   It’s important to understand the units before understanding how gas works.

Gas

Gas is a unit or number of steps to be execute for your contract. Gas has fixed number of units for a specific operation/computation, this is fixed by Ethereum community. For example to add two numbers EVM consumes it may consumes 3 gas units. You set a Gas Limit in your transaction to tell how many steps you are willing to take.  The unit of gas is not in Ether, Gwei, or Wei.  It’s just an integer representing a number of steps.

To get basic fees to get some idea of how much gas, note the following:

  • 32k gas to create a contract.
  • 21k gas for normal transaction
  • 200 gas * 1 byte of byte code.
  • Transaction data costs 64 for non-zero bytes and 4 for zero bytes
  • Constructor cost

See this response on Ethereum Stack Exchange.

The limit of computation per block is not constant. Miners can set this.

VALUE field – The amount of wei to transfer from the sender to the recipient, this is the value we are going to send to either another user or to a contract.

GASPRICE value, representing the fee the sender is willing to pay for gas. One unit of gas corresponds to the execution of one atomic instruction, Gasprice is in wei. Wei is a smallest unit. (1 wei = 10^-18 ether or 10^18 wei = 1 ether. Because it is quite high, we use gwei.  1 gwei = 1,000,000,000 wei.

Total cost of transaction is Gas Limit * Gas Price

Gas price fluctuates. During normal times:

  • 40 GWEI will always get you in next block
  • 20 GWEI will get you in the next few blocks
  • 2 GWEI will get you in the next few minutes.

Depends on what minors are willing to take.

To see the current price for a unit of gas you can run:

The new function in web3.js 1.0.0 (still beta as of this writing) requires a callback:

(the callbacks are always error, response in 1.0.0 so in the new stuff you’ll get two values printed in the console log)

This will be about 20 Gwei. Or you can look to see what the Gas price is at the ETH Gas Station. The current price shown at the ETH Gas station is 11 Gwei (std) and 9 Gwei (safe low).  Creating contracts seem to cost a little more.

In practice when we’ve created contracts we’ve seen that we can use the estimateGas function to tell how much for storing the bytes:

The callback function for the contracts we’ve been working on shows the gas limit (or amount of gas) is about 645234.  But this is only the cost of storing the contract code on the blockchain.  You need to add more gas limit for creating the contract, for passing parameters, etc.  We’ve found that by adding 80000 more to our gas Limit the contract goes through.