Understanding the Concept of Gas

The course is part of this learning path

Start course
1h 1m

This course covers Ethereum denominations, the ABI array, Ethereum networks, private/public key cryptography, and much more!


In the previous lectures we have talked a lot about it already. And in this lecture, I finally want to dive right into it. That's gas- gas costs and everything that revolves around the operational costs of the EVM, the Theory Virtual Machine. And for this I have prepared a little example and also in the background I've opened the Ethereum Yellow Paper which we're going to dive into a little bit in a few seconds. Now, if you run any kind of transactions on the Ethereum blockchain then it will cost you what is called gas. And gas is nothing else than a detached way of telling you you have to pay something in order to get your transaction processed. Now let's just run our Game Manager here, let's just deploy it again like we did before going to deploy it. And I just right here open the first transaction. And then you see there is a transaction cost and there's an execution cost. And you can see with any transaction that happens out there that there is a gas cost accruing when the transaction is mined and that has a particular reason. Now, let's have a look what happens when we are hitting start new game. So, we're going to set off 0.1 ether and we start a new game. And then we need around 1,184,542 gas in order to get that transaction mined on our blockchain here. How does this work? Why does it need exactly 1,184,542 gas because if I'm going to do this again, then it needs again 1,184,542 gas. So, it kind of stays the same. How does this happen? When we hit the debug button and we go into the debugger, then we see all the OP codes, all the operations that are executed on the Ethereum blockchain. So, when I hit my scroll bar here and I see I'm going through a couple of different operations here, PUSH4, PUSH2 And then maybe somewhere we have a CODECOPY and maybe we have a POP and so on. And those operations they cost gas and on the right side here you even see the amount of gas this current operation which is selected over here on the left just cost right now. But how does this debugger know how much gas this costs? And this is something that is written down in the Ethereum Yellow Paper and I'm going to open this right now. I'm here in the Ethereum Yellow Paper and you can find it either if you google just the Ethereum Yellow Paper, it will pop up immediately or you go to And then you have to go down to Appendix G that's around page number 25. So, it doesn't have any page numbers directly here, but my pdf US says it's on page number 25, then you find the Appendix G. And here you find the amount of gas different gas groups need. So, there is a G_zero cost, zero gas, nothing paid, G_base costs two gas, G_very low cost three G_low costs five gas, G_mid cost eight gas. But it doesn't really connect yet to these actual up codes, so where does the Up codes come in? Now, there's a specific group of Up codes and they're grouped together. So, you can see the different Up codes if I scroll around here, you can keep the gas cost in, pay attention to the gas cost here. The gas costs stay the same even though the Up codes change, so it's based on a group of gas costs that cost free gas very low. And if you scroll down in the Yellow Paper a little bit, then you find on the Appendix H, your Appendix H on page # 26, a little bit lower. You find the actual Up codes, and to which group they belong to. So, the zero gas costs stop return and revert base, the two gas cost Address, Origin, Caller, Caller value, Codesize and so on. So, these are Up codes, these are not specifically solidity commands or anything. They are really compiled solidity when they are executed by the EVM. Very low, which is then free gas cost Add, Sub, Not and so on and also Push, Dup and Swap. So, the ones that we had here, the PUSH, DUP you see them here, they cost free gas and so on. And there are a couple of more especially, I want to direct your attention towards the Up codes the groups here for, where is it? SSTORE and this depends on the amount of data you want to store. So, when you store any value in your storage variables especially strings are kind of particularly problematic here, then you pay quite a lot of gas, 20,000 gas for when the storage value is set from non zero to non-zero from zero. So, it was zero before like an empty string or an empty integer, and then it's set to a non zero value. And that one, the reason why this is so high is because storage on the Ethereum blockchain is kind of complicated because it clutters the blockchain and if you try to store just one megabyte you, I think at the moment it costs you 14,000 dollars around. I might be wrong on that one, but it definitely costs you a lot of money. So, it's always good to keep the storage cost as low as possible. And then there is also a base value for every transaction and that's 21,000. So, if you have a look in remix and you're wondering where the transaction costs and execution cost comes into place, the execution cost is the cost for executing the smart contract, the actual code. And the transaction cost is the execution costs including any overhead. So, when you send data to the blockchain then this Input field will be populated here and there is called data what is called Inside and that takes up some storage and that costs you something, every byte that is stored here costs something. And the transaction base costs something between 1,000. And this makes up a little bit more than the actual execution cost. And if you just want to know the overhead, then you take the transaction costs minus the execution costs you have the overhead. For me, in practice, mostly this value represents for me just can I execute the smart contract on the real blockchain or can I not because on the real blockchain you have a block gas limit. So, a transaction can not take- at the moment cannot use up more than 8 million gas. So, you're kind of limited there and that means you're smart contracts are mostly not very complex. So, the game that we've written is already one of the most complex things that you can run at the moment. You have to find other ways to architecture around that you have either more smart contracts that are then later on linked together and not directly during compilation. So, when I do this import and import here, then it will copy the- the contents inside here and compile everything together. That takes a lot of gas deploying this smart contract and I deploy here. I had to raise the gas limit to 5 million if you remember back. So, this is kind of how the gas comes together. Now, the question is how do you pay actual money for the gas? It's still not a connection here. Now for this reason I want to head over to and it has a new design right now. But I can still go here to the message missing the Vintage My Ether Wallets are click here go back. So, I guess if you go to you might be lucky if you can follow along. If you cannot, just follow this video and keep in mind we are using for our transaction here 1,184,542 gas. How do you pay ether here for the gas? And that is kind of connected through the- what is called the gas price. So, if you want to have a transaction recorded in the blockchain, then you have to pay for the miner a certain fee in order for him to process your transaction. And for this one here you pay the gas price which is like a transaction fee, very much like on the Bitcoin network. If you've ever done anything with it or if you go maybe to a bank teller and you queue up with other people, but you tell the bank teller, "Look, I know the other people will give you 10 cents for working on their bank book and I give you 20 cents." Now guess who the bank teller is going to choose first, the one with the highest fee that they're going to pay him. And that's working very much the same here on the Ethereum blockchain, and you pay usually between one Gwei and or 100 Gwei depending on how fast you want to have that transaction baked into the blockchain. So, if the network is congested and there is a lot of transactions outstanding and on the Ethereum blockchain, a block is mined roughly every 10-20 seconds, on average every 14-15 seconds, so if there's a lot of transaction outstanding, and you want to have it really quickly included in the next block, then you pay a higher gas price and that's going to be, I guess I personally never paid more than 20 Gwei and it can get very, very very expensive, so don't underestimate the gas price. And the fee that you pay at the end in ether is going to be the 20 Gwei times the amount of gas it needs to process. So, if you have 1,184,542 gas times 20 Gwei, then we will calculate now how much gas it's going to need and how much ether it's going to cost you. So, in our case we have 1,184,542 times 20, mega- giga, and that is 2.36- 2.36 with 16 zeros behind. And if you go to and we have this is in Gwei. So, 236 and 14 zeros behind and 1, 2, 3, 4, 5, 6, 7,  8, 9, 10, 11, 12, 14, And that was Gwei. So, we pay around 0.00236 ether for that transaction if we are going to pay 20 Gwei to the miner. So, this is kind of the connection how the Gwei, how the gas, and how the transaction cost comes together. That's the deepest I wanted to go with the concept of gas. You know now that every transaction costs gas and how the transaction costs and execution cost comes together, especially with the the Up codes that are over here processed by the EVM once the transaction is mined and baked into a block, so that is very important. And if you can make this connection and if you know that  Gwei is going to be the denominator for paying the gas price, then you're already good to go to understand most of the concepts of gas that are around there. All right, in the next lecture we're going to talk a little bit more about events and how they work on a deeper level. We're going to implement the last part of our smart contracts before we then go into local development with Truffle and Web3, where it gets, again, very exciting, and I'll see you in the next lecture.


About the Author

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.

Covered Topics