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

The "Counter Contract" 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 creates a counter contract as well as a deploy script. Unlike the hello world example, this contract changes state. The change in state creates a new block and triggers a gas fee.

Preview
Close

Transcript from the "Counter Contract" Lesson

[00:00:00]
>> Let's build our second contract. So we're gonna do a counter contract. This time we're gonna mutate state, we're gonna actually build something slightly more complicated. Every single time we call a function. We're going to increment a number by one. Second, we're gonna also import this little thing at the top of our function every time, import hardhat slash console.sol.

[00:00:21]
So this will help us cancel out from our smart contract. This is a hard hat piece of functionality, yet another reason why hardhat is so dang fantastic. So let's create our counter contracts. Now I do not have the code for the counter contract up here, cuz at this point hopefully everyone, we can follow along for the most part.

[00:00:41]
If we need it, I can update it as quickly as possible, but we're gonna go and we're gonna create inside of our contracts, Counter.sol. We'll do exactly as we did before, we'll put the pragma statement at the top of it and tell the compiler, hey, we want to use at least version 8 of the compiler.

[00:01:01]
You can actually put a range in here, you can say hey I can use from ranges four to 8, I just specify the latest one the latest and the greatest. And then next we're gonna do an import of hardhat slash console.sol. And then of course, we're gonna go create a contract called counter.

[00:01:21]
I'll give everyone a moment to kinda get caught up once I get all this done. We're gonna create a private member, or not a private member, just a member called counter. So uint counter. This is obviously a 256 bit number, very large. Do we need that size? Well, there's some benefits to using that size which we'll go over later, then we're gonna create a single function called count.

[00:01:43]
And it's gonna be public, and it's gonna return a uint, and it's gonna go counter ++, everyone's familiar with the post increment operator. Then we're gonna console.log out. Counter is now, counter, and return the counter. So there we go, we have just done a state mutating function. We have a member on the contract called counter, so we have identified some memory that we want to mutate.

[00:02:11]
We've referenced it inside of a function, we've added one to it, we print it out and we return it. So based on some previous things I will be adding these to the repo that I have done. So here counter. So if you go to the jpgdegen1, you'll have it.

[00:02:35]
But there we go. We now have our counter done. And so we need to make a new deploy script. The deploy script is gonna do pretty much the exact same thing that we did before. But now we're gonna be deploying the counter. If it makes it easier, I can try to add this to the repo, or to the actual scripts that we're doing.

[00:03:02]
So let's see, do I have the contract code? I do have the contract code on here. I do. Look at this. I guess I was smart. I do have the contract code along with the deploy script and the sample output. So look at that. So if you don't wanna just mash all the buttons, you can, everything's already available up there.

[00:03:18]
Fantastic, right? Sorry. I surprised myself, I impress myself sometimes, I'm kidding. And I like that. All right, anyway. So let's create a new deploy script, let's call it deploy-counter TypeScript, we're gonna do the exact same deployment method we did before. Now I could go and copy it. But like I always say it's good to kinda run through things a few times when you're brand new.

[00:03:40]
It makes it really stick inside your head. So the first thing we got to do, we got to make sure that our ethers object has those extra methods on it. Next, we need to import ethers from hardhat. I mean, if you want, you can go and copy it doesn't really matter to me.

[00:03:56]
And then next we need to write our deploy script. So just like the other time we're gonna do async function deploy. We're gonna grab our counter from the file system that hardhat has already generated. So const Counter = await ethers.getContractFactory Counter the name of our contract. After we do that let's create our actual contract on the network, await counter.deploy.

[00:04:26]
And of course we wait for it to be done, await counter.deployed. Return counter, exact same code again. You're gonna see this code so many times today, right? Is the exact same thing over and over again. Read the contract from file, deploy it to the network, wait for it to be done being deployed, return the contract.

[00:04:49]
So now that we have that let's create a little fun function called count, why not? And it will take in the counter as the contract. And we're gonna go like this, we're gonna go await or let's go console.log Counter counter.count. Of course, we gotta await that, or else we won't get the exact item in there.

[00:05:11]
And so there we go. And exactly like before, we're gonna do deploy, then count. Fantastic. We do the deploy, we wait for that promise to be done, then we do the counter count. And so theoretically, this should return us one the first time we execute this, correct? We deploy the script, then we get the counter, we count it once.

[00:05:34]
We should even be able to go to our node, see that it counted once because we put that console dot log in there, and we should get back the number 1. So let's go, opp, I gotta make sure my Python simple servers up. So let's go here and do an npx hardhat run just like before, and we're gonna do scripts, but instead of deploy-hello, let's do deploy-counter.

[00:05:57]
There we go. And of course the network is local host. Again, so it will be doing this a bunch and you'll be kind of doing this a lot if you do any sort of Ethereum development, is that you always have to do this little scripts deploy. Passing the network you want, or else it uses the ephemeral network that it can set up, and tear down right away.

[00:06:18]
So when we run this, one or two things is gonna happen. Either I did this first try, or we're gonna do it first try next time, all right? So fantastic. A lot of things just happened right here. So let's go up to the output. Now notice it says counter, which is what we put in the JavaScript.

[00:06:33]
And then it gave you this big transaction. So what happened with this transaction? Well, remember how I talked about there's two types of calls on the Ethereum networks, those that use gas and those that do not. Well, we changed the state of the network by adding one to a member of a contract.

[00:06:54]
Therefore this is a transaction, this is not a call to the network. Therefore we're not getting data out of this, because when is the data considered set? Well, when a certain number of transactions have happened after ours and certain amount of blocks have been processed. And some people like to do 32 blocks, that's considered a set in stone action.

[00:07:17]
Some people can be fine with 6, 12, whatever. But nonetheless, what is considered done? It's kind of always a little bit of a confusing notion, right? And so that's why you don't get data out of a state changing transaction. Now we can jump over to our node and we can see that exact same thing.

[00:07:34]
First, we see that we deployed our contract. It is at this location, it used this address, it was block number 2. Now if we go up just a little bit more, you'll notice that our eth calls to our HelloWorld, never calls a new block, cuz it's purely reading state.

[00:07:51]
Whereas now when we go down to our count, it actually caused a new block because it had a transaction that had to be processed. So got added to the next block. It also used to this amount of gas. There you go. You can see all the basic information who sent it, who received it, okay?

[00:08:08]
So something's happened here. This is different than what it was before. And lastly, you'll notice that we see our hardhat counter right here. This is very, very useful for debugging. I wanted to put that in there because it's just super, super, super nice to have but nothing too surprising just happened there.

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