Smart Contract Optimization Part-1 : Understanding the basics

Atharva Paliwal
Coinmonks
4 min readJan 14, 2023

--

Do you wonder why smart contract developers are heavily paid across the world?

Well let me give you a very simple answer — because they are the ones who save a lot of money by writing a secure, bug free and a optimized smart contract which consumes least amount of gas fees. If all this points are ignored by smart contract developers then this might cost the owner of the smart contract a huge amount of money because once that’s put up on the blockchain it can’t be changed.

So, let us try to understand some of the optimization techniques for the smart contracts that will take us closer to becoming a better smart contract developer.

Factors affecting smart contract optimization

In this article series we are going to cover some of the factors that we need to know for optimizing our smart contracts. The factors that we will cover are-

  1. Opcode
  2. State Change
  3. Transactions
  4. Memory

We will learn about them as we proceed ahead in the article but before that we must know some pre requisites.

What is transaction fees and how it is calculated?

Before proceeding ahead with the article, I think it is important to know how is the transaction fees calculated-

Transaction fees = (Gas Consumed * Gas Price) * (Ether Price) / 10⁹ gwei

Note : 1 eth = 10⁹ gwei

If you see the formula, you can notice that the only parameter that is in our hand and that we can control is Gas consumed. The other parameters like ether price and gas price are not in our control. Let us take an example to understand about this more-

Transaction Fees calculation

So, I took an transaction from etherscan and tried calculating the transaction fees for the transaction with the price of 1 ethereum at that time being 1411.17$. If you see the gas consumed i.e. 21000, gas price in gwei i.e. 16.26 gwei. Putting all this values in the formula gives me the answer as 0.48$ which matches with the output in etherscan.

Exercise : I would highly recommend that you try calculating the transaction fees and do let me know if you get the desired output. You can tag me on twitter with the screenshot of your answer. I would appreciate it. If you face any difficulties do let me know there !!

One more interesting thing to note is the minimum usage or gas consumption in any ethereum transaction is 21000, it can’t be less than that. More the value of gas consumed, more will be the transaction fees , this is why it is necessary to understand some optimization techniques for our smart contracts that will help us reduce the fees.

What is Ethereum Block Size & Limit ?

Ethereum Block limit is 30 million gas. You must be wondering what is this 30 million gas? and why the block limit is not in MBs/KBs. Well to create more confusion let me tell you that Bitcoin has a block limit of 1MB.

Let me clear your confusion, Ethereum founders decided to keep a limit based on gas because they wanted to keep less load on the network to restrain people from doing heavy transactions.

So, calculating the number of transactions per block in an ideal case where gas consumed for each transaction is 21000 gives us —

transactions per block = 30 million gas / 21000 = 1428

So we can see ideally 1428 transactions can be fitted into a block but we know that every smart contract can’t have 21000 as gas consumed and if there are contracts that consume more gas then less transactions will be added to the block and as a result miner will not tend to mine the block as he will get less reward for his mining work.

What is Transaction & Execution cost?

Ethereum Yellow Paper

Execution cost is the subset of transaction cost i.e. Execution cost can be transaction cost but transaction cost cannot be execution cost.

Execution cost

Execution cost is the cost that is related to the internal storage and manipulation of the contract.

They are based on the cost of computational operations which are executed as a result of the transaction.

Transaction cost

Transaction cost is based on the cost of sending data to the blockchain. Since Execution cost is a subset of Transaction cost, it will also be added in Transaction cost.

So, this is it for this article i.e. the Part-1 of smart contract optimization series. This article was a short article to introduce you to some of the basic terms that I will be using in my upcoming article for the series so that you don’t get stuck while understanding the optimization techniques. I will cover the factors in depth in the next article of the series. So stay tuned!!

Read Part-2 here!!

If you enjoyed reading this article, please do leave a clap or a comment and you can also connect with me on LinkedIn and Twitter. You can follow me on Medium as I keep writing about some of the new and less known things about Blockchain.

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

--

--

Atharva Paliwal
Coinmonks

Blockchain Developer (R&D) at Persistent Systems | Writes about new and less known things that you may want to add to your skills portfolio ✍️💫