How to reduce Gas cost in Smart contract?

Vishwas Anand Bhushan
4 min readFeb 13, 2018

--

Well, to be frank its very difficult to do so!

Let me give you an idea, what I am talking about. As we know, in Etheruem, we can write smart contracts (codes that are executed on EVM). So for every line of execution of code on EVM — Ethereum Virtual Machine, one has to pay ‘gas’ — a smaller unit of Ether, to the network.

Basically, what happens, when you compile a solidity code, a .sol file, it gets transformed into byte code to be executed on EVM. You can see this in action by clicking details button under compile tab in remix browser (see the screen shot below).

Notice you also get opcodes (which is operation codes, a assembly level language, basically instructions to be executed on the machine.). Executing these instructions, consume gas.

For instance, an addition operation that sums up the top two items of the stack takes 3 units of gas. It is worth noting that real smart contracts consist of lots of operations and some operations consume much more gas than the addition operation.

  • Stack operations (e.g., POP, PUSH), arithmetic operations (e.g., ADD, SUB), bit wise operations (e.g., OR, XOR), and comparison operations (e.g., LT/GT) are cheap because being a stack-based virtual machine, EVM favors such stack-related operations.
  • Loading a word (i.e., 256 bits) from the memory (e.g., MLOAD) or saving a word to the memory (e.g., MSTORE) are also cheap. For those who don’t know what memory is in solidity, please refer to the solidity document. And search for data allocation (3 types basically, storage, memory, calldata).
  • It is worth noting that the gas consumption will be multiplied if many words in memory are read or written.
  • Loading a word from the storage (i.e., SLOAD) or saving a word to the storage (i.e., SSTORE) are expensive.
  • Memory is temporary but the storage is persistent memory.
  • A SSTORE operation costs 20,000 units of gas if the storage word is set to non-zero from zero; otherwise, it costs 5,000.
  • EVM has a number of block chain-specific operations which are very expensive, such as BALANCE, CREATE and CALL.
  • A conditional jump (i.e, JUMPI) is more expensive than an unconditional jump (i.e., JUMP).

Check out this link for more reference.

So the biggest question is, how can we write these contracts, so that it won’t consume much gas?

Following are the ways by which we may avoid gas devour :

  1. Understanding of EVM’s instructions properly : if we know these instructions and understand their usage, we can definitely write our codes in such a way that it will consume less gas.
  2. Using data location (memory , storage and stack)wisely : Whenever we use data locations, we have to use them wisely otherwise we may end up loosing gas.
  3. We should avoid loops and recursions.
  4. We should also focus on amount of data read or written.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response