Check out a free preview of the full A Tour of Web 3: Ethereum & Smart Contracts with Solidity course

The "Gas Overview" Lesson is part of the full, A Tour of Web 3: Ethereum & Smart Contracts with Solidity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen explains that the term "gas" refers to the transaction cost. The wei is the fundamental unit of ether. The gwei, or Gigawei is the typical measurement for gas.


Transcript from the "Gas Overview" Lesson

>> Yes, someone did incremented at least four times while we were out. So that's actually pretty fantastic. We saw we got some of that done. All right, so this is probably one of the more important little things you need to put in your brain. I'm just gonna go over some stuff, no need to program we're just gonna see how it works.

Effectively it's just talking about gas. Gas is really expensive. You've probably heard this term if you're familiar enough with ethereum, it's called gwei is the standard unit for gas which is actually a non standard unit. The standard unit for ethereum is called wei one we is the fundamental unit of ether effectively, it's like our pennies every dollars 100 pennies effectively.

There's 10 to the nine wei and a gwei, a gwei is not a giga chat. It is a giga wei it's a billion weis, ethers just simply 10 to the 18 weis. So when you own an ether you own 10 to 18 weis, gas is measured in these more fundamental units.

Gas tends to be measured in a giga wei, are we have just called gwei from here on out. So there's two things you need to understand when it comes to gas prices. First thing is virtual machines. So I wanna just do a quick drawing on a virtual machine just in case people don't understand.

And effectively when you think about it contracts are just simply a series of but, I got to take a quick break here. Did you see how straight that line is? I mean, that was free hand drawn and FDE material there. Contract is just simply a series of byte codes that need to be executed one at a time.

So you can imagine that if you were to write a program that takes in text and does what the text says, you would effectively have a do while loop because that is the best loop of all time. You'd have a do while loop that inside of it would have a switch statement that would walk through your so called program.

If you ever did advent of code in 2019, they had an ENT machine in which you would actually do effectively this. And it would hop around inside of your program until it gets to an endy condition, and then it would leave. And that's really all a virtual machine is.

So when I write something that looks like x + 3. What's actually happening is that there's a something called an add being executed, a lot like assembly, right, there's an add. Maybe it has to load in the x for memory and then it has to put in a constant and then it probably needs to go and store this value in something else.

And so that's effectively what a virtual machine is, is that it takes in something that's not executable and executes it. JavaScript was originally, purely just an interpreted language, or it actually had a virtual machine underneath it. If you look at JavaScript core code from 2012, you will literally see one gigantic switch statement, I used to have to play around in it at Netflix.

And that's all it was. It was just one gigantic statement. And you just hopped around in there while the JavaScript was executing, and eventually it hit an exit condition. And you'd be out of the gigantic do while switch loop, right? They used go-tos actually, so it's just switch and it used go-tos to hop around inside the switch.

A little bit dangerous to do go-tos but it was a lot of fun. And so that's all a virtual machine is, it interprets these things one at a time. Every single action in a theorem comes with a cost. So we can actually open up this little link that I put in there.

And you can see right away an add operation requires three units of gas. And so that's, it's a hard number. So every single time you do an add, you're paying three units of gas. If you do, say a store, it could cost a lot more, if you wanna store something onto the network or onto your contract, you can store a single word or 256 bits for approximately 20,000 units of gas.

Now that doesn't mean you're paying just simply 20,000 gwei, you're paying 20,000 units of gas and so what is the other side? What is the cost of gas? Well, I can just go in here and go cost of open up. That's cost of storage cost of eth gas.

Go here, it'll probably open up ETH gas station, usually good way to go. It should say somewhere around in here. All right, this one doesn't give me just the gwei one. There's one that just gives you the gwei one, gosh. Ether scan, there we go. It's 205 gwei, meaning that if we're to do this an add costs three gas units right now costs 205 gwei per gas unit, and the cost of one ether is $4,000 divide that of course by 10 to the nine.

And what do you get a single add operation right now cost 0.02. So if you were to add 50 items right now, it would cost $1. So, that's expensive, right? This is why a theory I'm struggling in these areas. So I have the little thing broken down right here.

This is why I did tend to the nine, not 10 to the 18. Because remember, it's in gwei, it's not an ethers. So he times it by that to make it into ethers multiplied by the cost of ethers divided by wei or you can just simply reduce the formula to being this.

So, if you're not familiar with maths there you go, that's an easy way to do it and so it's actually pretty expensive.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now