Gas

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Million Ether Homepage course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Million Ether Homepage, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Million Ether Homepage

There's one major constraint of EVM smart contracts that we've swept under the rug so far, and that is the issue of gas. So far we've acted as if we have infinite ether and unlimited computing resources available to us. But in reality, this isn't the case. When we write Solidity Smart Contracts, we're writing code that will be executed on someone else's computer. Actually, our code will be executed on thousands of computers. We have to ask, what happens if we wrote malicious code? For example, what if we wrote code that went into an infinite loop and submitted it to the network? If every node on the network were to fall into an infinite loop, the entire Ethereum network would halt. Could we analyze a program before we run it and check and see if it will terminate? As you probably already know, this is referred to as the halting problem, and the answer is no. Writing problem is undecidable over Turing complete languages. That is, the only way to tell if a program will finish is to actually run it. So what can we do? The EVM gives resource constraints on our programs. Essentially, it costs you money or ether to run any program. If you make your program run for a long time, you'll pay a lot of ether. The constraint on running our programs is called gas. That gas is a unit. Let me explain. Remember that the EVM runs bytecode operations called opcodes, not our solidity code. Each opcode costs a certain amount of gas to run. Some opcodes are cheap like adding two numbers together, and some opcodes are very expensive, like storing data on the blockchain. But the gas cost for each opcode is essentially fixed. It's always going to cost three gas to add two numbers together. When we submit a transaction that calls a function on our smart contract, the total gas spent will be the sum total of the gas for each of our opcodes that was run . But we don't pay in gas. We pay in ether. So how do we convert between the two? Well, the miners decide the price per gas that they'll accept. So for our example, if the price is five-way per gas, and remember, a way is the smallest unit of ether, so if the price is five-way per gas, and we add two numbers together, which costs three gas, then we'll pay five times three, fifteen-way for that operation. It can be a little confusing at first, so maybe an analogy will help. Say that I have a gas-guzzling camper, and I want to drive from LA to San Francisco. It's 374 miles, but let's round up to 400. My camper gets 10 miles to the gallon. It's 400 miles, so 400 divided by 10. I'll use 40 gallons of gas on my trip. But how much will I pay for those gallons? Well, it depends on the price of gas. So let's say the price is the same at every gas station along the way, and I pay $4 per car, so my cost will be $160 to make that trip. And let's say during my stay there's a shortage, and the gas prices have shot up to $6 a gallon. How much gas will I use on my way home? The same amount. It's the same distance back, and my car gets the same gas mileage, so we're going to use the same number of gallons of gas. How much gas I'm going to use doesn't change, just because the price changes. It's the same way with the gas market in Ethereum. Your program will use the same amount of gas no matter what the price is, because it has the same upcodes. What might change is how much that gas costs. So there are two things we need to figure out. One, how much gas does our contract call or transaction use? And two, how much ether will that gas cost? Let's look at each of these separately. So it provides tools to estimate how much gas a particular method call or transaction will use. If you use Remix, it will provide those estimates in the UI. But it's important to remember that these are just estimates. Without running the code, you can't know precisely. And even if you run the code locally and measure how much gas was used, the state may change by the time you submit your real transaction, which could change the gas costs. Let's say that our transaction costs are 20,000 gas to run. How much are we going to end up paying in Ether? The easiest way to estimate gas costs is to find a website that publishes the current price. As I'm recording this, ETH stats.net and ETH gas station.info both publish this information. However, if those sites don't work, check the comments for a new list of sites. But the idea is that if you find the gas price on that page, that price may not be denominated in Ether nor Way. At the moment, I see that it's denominated in Gigaway, which is a billion Way. To calculate the cost of our transaction, we multiply the gas used times that price per gas. We can do this in our Geth console like this. So our estimate is 20,000. That's the estimated gas used by our transaction. And our gas price is 25 Gigaway. And remember, the miners set this price. We can estimate the total cost in Way by multiplying the gas price times the estimate. We'll use the Web3 two-way function to convert our gas price. That's a large number and it's difficult to understand in this format. So let's convert it to Ether. There we go. And if you want to convert into your local fiat currency, you can just multiply this number times the cost of one ETH. But say for example that an ETH costs 75 US dollars. Then we just multiply what we calculated before times ETH and USD. So if an ETH was 75 US dollars, then the cost of this transaction would be just under 4 cents. Or if the cost of an ETH was 500 dollars, then the cost of this transaction would be the equivalent of 25 cents. So that's the basics of calculating gas costs. One thing you should also know is that blocks have a total gas limit. At the time I'm recording this, the limit is between 4 and 6 million depending on the day. That's the total gas used by all transactions that block need to total less than 4 to 6 million gas. When we send transactions, we have to specify how much we're willing to spend on gas to run that transaction. And if you don't send along enough gas, your transaction will fail with an out of gas error. You should also know the gas that's spent is not returned to you. This makes sense from the miners perspective. They still had to use their resources to process your transaction. So they'll keep the gas fees even though your transaction failed. A more advanced issue with gas comes up when we have our contracts calling other contracts. The idea is that we sometimes want our contracts to forward along some spare gas to a neighboring contract. But we'll talk about this when we get there. To recap, to calculate your gas costs, estimate the cost of your transaction by using tooling like remix or solek or by running a test transaction locally. the market gas price, and then multiply the gas price times the gas you'll spend.