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

The "Create Your First Smart 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 the project folder and installs hardhat which is the node module used to compile the contracts. The first Hello World contract is coded and compiled. Some common compile errors are also discussed in this segment.

Preview
Close

Transcript from the "Create Your First Smart Contract" Lesson

[00:00:00]
>> All right, so we're gonna just get started right away and I want you to do these following steps. We need to make a directory where we're gonna put everything. I'm gonna choose the name JPEG degens because that just feels like just the right level, of what I wanna call this so I'm gonna call it JPEG degens.

[00:00:14]
Just go into the directory and then we're going get init, yarn init, and then we're add hardhat. So you can do these however you want to do these. I'm just gonna copy this, paste those all in, and there we go. And now remember, whenever you're working with a project that involves Node.

[00:00:30]
One of the things that you know you're doing right is when you use a lot of libraries. So the bigger our library project or the the bigger our Node modules folder becomes, the more we're winning. So just remember that, right now we've added some things. But I feel like we could have added more.

[00:00:47]
This is kinda a weak attempt at our very first addition. And so once you get these three things in here, you have inited git, you have yarn going, you have added hardhat. We need to execute hardhat. If you're not familiar with npx, it's like a co-utility with NPM that came out not too long ago.

[00:01:04]
Where it just effectively adds to your path, the bin folder inside your Node modules. So you're able to call into the things that you've just NPM installed. It's a nice little module so it also helps prevent people globally installing things. All right, is everyone here? Has everyone made it to this point?

[00:01:21]
Awesome, so let's go and let's create an empty hardhat config dot JavaScript. Yes, dot, JavaScript, the lessor of the friendly TypeScript nature. All right, so the typical structure of smart contracts follows this. It's gonna have a contracts folder. This is where you're gonna put all your solidity code.

[00:01:43]
It's gonna have a scripts folder. This is where you put all your deployment code. And then it's gonna have a test folder. And this is where you put all your test code. You really do wanna try to keep this, this kinda seems to be a standard. And Hardhat does some auto assuming, like if you run, compile, it's gonna go through your contract folder and compile each one of the files and pull out all the different contracts you have named in there.

[00:02:08]
Second, if you run npx hardhat test, it's gonna go to your test folder and run through that. So it is kind of important to keep the structure. The scripts thing, you could probably rename it to what you want. But that just seems to be kinda convention more than anything else.

[00:02:24]
And HardHat is the tool we're gonna be using for building and deploying. It adds a lot of really nice features to a third party library called ethers. And it just makes life easy. So we're just gonna be using that the whole time. There's some other ones, there's truffle and stuff, but I find HardHat to be kind of the completest solution out of them all.

[00:02:43]
And I also really like testing, I don't know about you but since I get everything first try I usually don't need testing. But I do testing cuz I wanna show everyone how important it really should be if you don't first try all the things. And lastly of course don't forget the gitignore because honestly a gitignore is very important and put in Node modules, right?

[00:03:05]
You definitely want that in there or else your life will be hellish if you try to open up stuff. All right, so the first thing we're gonna do is we're gonna create the HelloWorld contract. It is a .sol, no it does not mean shit out of luck, it means Solidity, which is the programming language of Aetherium.

[00:03:21]
And so we're gonna create a contracts folder and we're gonna create a file called helloworld.sol. You can call the file whatever you want, the file name has no meaning or bearing on how it gets compiled. As long as it ends with .sol that's the only thing you really need to do.

[00:03:37]
So let's go back here, create a nice folder called contracts, go into here, create HelloWorld.sol. It's kinda Java convention, right? You just name the file after the primary item inside of it. And the very first thing you're gonna do when you do a Solidity contract is you need to tell the compiler or you need to tell Solidity, what compiler you expect.

[00:04:03]
And so they do this pragma statement. Now for anyone that did some C++ and Windows era, the pragma is kinda, it feels like some sorta pre compiler step. That's effectively how I look at it so I just say hey pragma solidity 080. I expect at least that compiler level, we're gonna put this at the top of every one of our contracts.

[00:04:25]
So you don't really have to understand what it's doing other than it's kinda like a package dot JSON. It's just specifying which version we want to use. After that we're gonna go contract HelloWorld. So you can probably make a quick analogy right away that a contract looks a lot like a class, right?

[00:04:46]
And really that's how it operates, honestly. It's gonna be simply a state container with some functions to mutate it. So it's effectively a class and it even does contain some classlike semantics. It has a constructor that will be executed only upon deploying to the network once. So every time you deploy your contract, that constructor gets executed one time.

[00:05:09]
So it does feel very analogous to classes, but we're not even gonna do that. We're gonna type in function hello, and we're gonna do public, pure, returns, string memory, return HelloWorld. Now anyone that's familiar with programming understands every single thing about what I've typed except for the word pure and memory.

[00:05:34]
Those two things are probably your only questions you have. We will get to those, those things will be answered. But it's good just to understand that, right? I assume everybody understands that this is a function hanging off the HelloWorld contract. Anyone can call it, it returns a string, and that's the string that it returns.

[00:05:52]
These two, we'll just put a pin in it, as that one guy said in that movie Bolt, all right? So now that we have that, we have finished our first contract. Pretty awesome, right? So I have a little link if anyone just wants to copy and paste it, you can do it right there.

[00:06:09]
We're gonna compile the contract now. So open up a new terminal, and you can go npx hardhat compile and by doing that it's gonna compile your contract. But first it's gonna do some resolving, and it's gonna make sure it has some information, apparently. I just realized that I'm executing it in the wrong folder, jpg degens, I forgot to do that, there we go.

[00:06:31]
And now npx hardhat compile. I wanted to make that mistake for you and you'll immediately get this error probably. Now it's gonna say something along the lines of solidity version pragma statement in these files do not match any configured compilers. Why is that? Well, it's actually pretty simple.

[00:06:48]
If you go to your hardhat.config, you'll notice that your hardhat.config, which is the thing that drives your hardhat experience. Specifies 073, so you get to specify the exact compiler you want in hardhat. But your contracts get to specify what they want to use. So we can't compile because we've only specified a single version.

[00:07:09]
So we're gonna actually do this, we're gonna specify 810. That's the latest version. Unfortunately, the versions of Solidity don't really respect semantic versioning. They added features and they removed features in four to five. But they never do a major version update when they completely remove API. So just remember every minor version could be a surprise and you lose things, just as a warning, it's a little unfortunate.

[00:07:34]
So now if we go back and we compile, we will compile with a single warning. Well, technically two warnings, but you don't have to worry about that one. The thing that you can worry about later is that we didn't put a license identifier. Technically, the first line of any code should be a license identifier.

[00:07:54]
I don't think that that's gonna really give you much right now for today. But we will copy this, go back here, open up our nice HelloWorld. And I can just put this in here and put in MIT, right, there we go. And if I were to go back here and compile, it goes away.

[00:08:09]
Does that really help us today, no. So we're gonna always have that warning every single time. Not a big deal.

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