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
  • [00:00 - 00:13] 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.

    [00:14 - 00:21] 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.

    [00:22 - 00:29] Actually, our code will be executed on thousands of computers. We have to ask, what happens if we wrote malicious code?

    [00:30 - 00:40] 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.

    [00:41 - 00:50] 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.

    [00:51 - 00:59] 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.

    [01:00 - 01:03] So what can we do? The EVM gives resource constraints on our programs.

    [01:04 - 01:11] 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.

    [01:12 - 01:16] The constraint on running our programs is called gas. That gas is a unit.

    [01:17 - 01:24] Let me explain. Remember that the EVM runs bytecode operations called opcodes, not our solidity code.

    [01:25 - 01:35] 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.

    [01:36 - 01:43] But the gas cost for each opcode is essentially fixed. It's always going to cost three gas to add two numbers together.

    [01:44 - 01:57] 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.

    [01:58 - 02:01] We pay in ether. So how do we convert between the two?

    [02:02 - 02:26] 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.

    [02:27 - 02:35] 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.

    [02:36 - 02:43] It's 374 miles, but let's round up to 400. My camper gets 10 miles to the gallon.

    [02:44 - 02:49] It's 400 miles, so 400 divided by 10. I'll use 40 gallons of gas on my trip.

    [02:50 - 02:55] But how much will I pay for those gallons? Well, it depends on the price of gas.

    [02:56 - 03:11] 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.

    [03:12 - 03:16] How much gas will I use on my way home? The same amount.

    [03:17 - 03:29] 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.

    [03:30 - 03:40] 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.

    [03:41 - 03:48] What might change is how much that gas costs. So there are two things we need to figure out.

    [03:49 - 03:57] One, how much gas does our contract call or transaction use? And two, how much ether will that gas cost?

    [03:58 - 04:04] 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.

    [04:05 - 04:11] If you use Remix, it will provide those estimates in the UI. But it's important to remember that these are just estimates.

    [04:12 - 04:24] 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.

    [04:25 - 04:30] Let's say that our transaction costs are 20,000 gas to run. How much are we going to end up paying in Ether?

    [04:31 - 04:42] 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.

    [04:43 - 04:54] 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.

    [04:55 - 05:05] 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.

    [05:06 - 05:11] We can do this in our Geth console like this. So our estimate is 20,000.

    [05:12 - 05:16] That's the estimated gas used by our transaction. And our gas price is 25 Gigaway.

    [05:17 - 05:25] And remember, the miners set this price. We can estimate the total cost in Way by multiplying the gas price times the estimate.

    [05:26 - 05:32] 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.

    [05:33 - 05:35] So let's convert it to Ether. There we go.

    [05:36 - 05:45] 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.

    [05:46 - 05:56] 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.

    [05:57 - 06:05] 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.

    [06:06 - 06:14] 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.

    [06:15 - 06:27] 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.

    [06:28 - 06:36] 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.

    [06:37 - 06:42] This makes sense from the miners perspective. They still had to use their resources to process your transaction.

    [06:43 - 06:50] 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.

    [06:51 - 06:58] 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.

    [06:59 - 07:13] 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.