Optimize Smart Contract Gas Usage

Barış ATALAY
Getwallet
Published in
5 min readDec 30, 2022

Tip 1: Avoid manipulating storage data

In our example;

Storage will be the Coordination Center,

Function Logic will be the Transporter.

Let’s talk about the typical day of a transporter. They have products that need to be delivered to 100 different locations. This transporter gets these addresses from the Coordination Center. Let’s look at the worst scenario first. If the transporter is not an efficient worker, he will go to the Coordination Center and get the first address, then leave to deliver the products. Then, because the doesn’t know the new delivery address, he returned to the center and demanded the new address. He did the same with the second address and continued this cycle until the 100th address.

The fuel cost of this transporter sounds scary, right?

So what’s the correct method?

Before starting the work, this transporter should have first gotten the complete address list from the Coordination Center and kept it with them instead of requesting information from the center for each of the 100 addresses.

Let’s think of this example as a loop.

We shouldn’t forget that memory or call data operations are always cheaper than Storage operations.

Execution cost: 71646 gas

Execution cost: 61659 gas

Bonus:) Let’s deactivate EVM gasoptimizations for the looping iteration operation.Thanks to our dear teacher Doğa for this great example.

Execution cost: 53181 gas

Optimization conclusions

WithoutGasOptimization Execution cost: 71646 gas

WithGasOptimization Execution cost: 61659 gas

WithDisableEvmGasOptimization Execution cost: 53181 gas

Tip 2: Pack your variables on Memory SLOT!

In Solidity data are kept on 256-bit Memory Slots. Data that are smaller than 256-bit are stored on a single Memory Slot. The data that don’t fit in a single slot are distributed to multiple slots. The cost of storage is GAS. Grouping variables properly will reduce the number of Memory Slots that our Smart Contract uses and thus reduce our GAS usage.

We have 3 variables. They are: 128-bytes, 256-bytes and 8-bytes

Let’s first arrange them poorly.

The first variable is 128-bytes, the second variable is 256-bytes and the third variable is8-bytes.

Memory Slot placement will be;

SLOT-1 : 128-bytes

SLOT-2 : 256-bytes

SLOT-3 : 8-bytes

Because of this poor arrangement we used up a 3rdSLOT. Let’s not forget that we need to pay the GAS fee of this while deploying our Contract.

Not let’s arrange these variables properly.

The first variable is 256-bytes, the second variable is 128-bytes and the third variable is8-bytes.

Memory Slot placement will be;

SLOT-1 : 256-bytes

SLOT-2 : 128-bytes | 8-bytes

Congratulations, you saved money :)

Tip 3: Delete variables that you don’t need

In the networks that use EVM infrastructure (I don’t know much about the other networks), if you delete a Storage variable or reset it to the default value, you will receive GAS return as a reward for clearing up the storage area of the previous assignment. Because unneeded data will increase the total network space, so you shouldn’t underestimate the space even a small variable takes. Every little drop counts!

Tip 4: Use Events

Data that are not required to be accessed through the chain can be stored with EVENT to save on gas.

Tip 5: Use Libraries

When public libraries are used in Smart Contract, the function’s byte code are not included in the contract, only the fee of the GAS used is paid. That’s why it may make more sense to put more complicated logics inside library. Operations carried out to the libraries are done with “Delegate Call” method. This also means that the used library has the same data and permissions as the called Smart Contract.

Tip 6: Avoid assigning values that You’ll never use

Remember the GAS cost in solidity? Let me remind you that assigning a variable costs GAS.GAS = MONEY :)

uint256 value is cheaper than uint256 value = 0.

Tip 7: Minimize on-chain data

The less data you put in on-chain, the less GAS cost you will have. Suppose that you are designing a dAPP. Every unnecessary data you store here will cause the calculation of more data both in the operations made by the Smart Contract owner and the users. For example, the unnecessary meta data in the Structs you create.

Tip 8: Use Mappings instead of Arrays

This one surprised me when I first heard of it, but when coding Solidity mapping operations are cheaper than array operations.

This is one of the reasons we love mapping :)

Tip 9: Use short reason strings

To understand why a smart contract is giving an error, you can add an error description with require statements (and you should do so).

But you should note that this uses space in the contract that is deployed. Require statement takes at least 256-bytes, so it is recommended to add an error description that will fit into 256-bytes. Because for any byte that doesn’t fit into 256-bytes, a new 256-bytes space will be provided.

Tip 10: Use Assembly

Find me on LinkedIn ,Github, and Twitter.

References :

https://twitter.com/DogaEtherOzcan
https://ethereum.github.io/yellowpaper/paper.pdf

--

--