The cost of running a smart contract
Tags: nft, crypto, smartcontract
Published: 2022-02-08 19:32:21

For [Loot from a Chest] (https://lootfromachest.com), we decided to look into creating a custom smart contract because we need some functionality that is not provided by existing smart contracts. Writing and running a smart contract was a black box for us. None of us had done this before. There were open questions that we wanted answers to quickly

- How does it work exactly?
- Who are the parties involved and how do they interact with each other?
- What does it cost to run a smart contract?

Questions 1 and 2 will be covered in another article soon. In this article, let us take a look at the costs associated with running a smart contract. 

# What is a smart contract?

A smart contract is a program that runs entirely on a blockchain. The data involved is also stored on the blockchain. With this program, you can do anything you want. Conduct voting, run decentralized finances, and of course, run a marketplace selling goods like NFTs. There are many guides that show you how to write these programs. Few of them tell you what happens afterwards when you want to deploy them on the blockchain.

Let's take a look at the Ethereum blockchain and try to keep things simple. The basic unit of Ethereum is called a block and it is a stack of transactions with a hash of the previous block.  So these blocks are linked together like a chain (hence the name) and form the basis of the fraud prevention we love about blockchain technology. To perform such a transaction, we need to expend computational energy. We call this cost `gas`. The `gas price` is more or less determined by  supply and demand. The higher the demand, the higher the gas pric. In fact, we often see high amplitudes in a short time. If we want to determine the cost of creating a smart contract, depends on how much `gas` we have to spend to execute the transactions that are involved as well as the `gas price` at the time when they happen. 

Fortunately, Ethereum is open source and we can read all about it in the [Ethereum Yellow Paper](https://ethereum.github.io/yellowpaper/paper.pdf). On page 27, we can see the amount of `gas` needed for certain operations. The following operations are required to create a smart contract.

- G_txcreate = 32000 `gas`
- G_transaction = 21000 `gas`

So at least 53000 `gas` to create a smart contract. Yes, this is the minimum amount of gas. You also have to pay `gas` for every byte of data your program requires. Ethereum uses what are called slots of 256-bit size for this purpose. To store data in a slot, 20000 `gas` is required. This results in 640.000 `gas` for 1kB of data (256 bits = 32 bytes; 1kB = 1024 bytes = 32 * 32 bytes). There are several ways to determine the size of your smart contract. 

# Determining the size of the contract

There are many ways to calculate the size of your smart contract. We create one using [hardhat](https://www.npmjs.com/package/hardhat). This tool has a plugin called [hardhat-contract-sizer](https://www.npmjs.com/package/hardhat-contract-sizer). This outputs the size of the contract in kilobytes. For Loot from a Chest this was 21.045kB. Another method (i.e. measuring the byte size of the deployed contract binaries) gave 23.13kB. So there might be some fuzziness. 

# Putting it all together

Finally, to calculate the cost of creating the smart contract we need to determine the `gas price` and the Ether price. The Ether price tells us how many dollars we need to spend to buy the Ether. For this, there are many trackers that determine these both prices in real time and historically. At the time of writing the `gas price` was 2822700013 `wei` (or 0.000000002822700013 `ETH`) and the Ether price was $3018.38 per Ether. Now we can do the math:

```
const gas = 32000 + 21000 + 21.045 * 640000;
const gasPrice = 0.000000002822700013;
const etherPrice = 3018.38;

const price = gas * gasPrice * etherPrice;
console.log(price) // -> 115.20548267230792
```

To deploy our very basic smart contract, we have to pay $115.21. For a more sophisticated contract that we have in mind for Loot from a Chest, we will probably have to spend much more. I hope this helps you get a better sense of the costs involved in creating a smart contract. We have not talked about the costs that will be incurred if you do other transactions (such as buying your NFTs). The math is the same. You just need to figure out what the transactions are involved and what data and operations make up those transactions.

**One last tipp**: In the Ethereum RPC, there is a method called [eth_estimateGas](https://www.quicknode.com/docs/ethereum/eth_estimateGas). You can write a program that uses this function and embeds a gas price and Ether price tracker to determine the cost in advance. This is basically an automated version of what we explored in this article.

# Final thoughts

Running a smart contract can be an expensive thing to do. There are existing ones out there that you could look into. Marketplaces like Open Sea or Rarible are based on very sophisticated smart contracts with advanced features like royalties. If you do not need additional features to run your project, you should use them. If you do, you should look at side chains like Polygon or alternatives like Solana. They run on less gas and are therefore cheaper to work with. But keep in mind that they are not as mature compared to the Ethereum Blockchain. Also, most of the NFT community is on Ethereum, which means you have a smaller audience to sell to or have to convert to your chosen blockchain!

If you have not checked out "Loot from a Chest" yet, you really should :) It is a collection of 10000 procedurally generated swords in a fantasy world. Join our [Discord](https://discord.gg/aS3vQcQQUC) and visit our [Website](https://lootfromachest.com).

_Feel free to leave comments. I hope this helps you in your digital endeavour. If not, drop me a line and I'll be happy to help :)_