Transcript from the "Create a Deployment Script" Lesson
>> And so we have executed hard hat test. We can see that it's it's passing. This is fantastic, right? And let's go down. So we saw this fantastic. We tested the method we actually did get the string out. We saw that it was working and just to prove to ourselves that we're not just making things up.
[00:00:21] Let us go over here. And let us go hello, front end masters and then rerun our test. And what we are gonna see is that it now fails because we were expecting hello world so we really genuinely are talking to this contract. There is no smoke and mirrors at this point.
[00:00:39] We are talking to this contract we have deployed it we have executed a function on it but there's probably some questions about this I think we kind of answered a lot of the questions right now is there any specific questions that we want to go over that someone has about this process so far.
[00:00:55] My question is did it create like some sort of test network.
>> How does that work?
>> So did we create a test network? What just happened there? Well, good thing it leads into the right the next thing so what just happened? What did we actually do there?
[00:01:09] Now I have explained some of the things about how we let's see where are you? Hello World? Yes, I didn't add that to that. There we go. We didn't like I explained how this works I explained how these things kind of work but notice a, that we now had a function called Hello.
[00:01:27] How did we get that generated function because that's the name of the function we use on our contract obviously that is a proxy function uses all the information that I showed you from here to generate proxy functions here that actually communicate with the network. But on top of that, where was this network at?
[00:01:44] Well, one of the beautiful reasons why I love Hardhats so much is when you run a test file it actually will create it will launch a kind of a private network. Run your test take down the network. So you just get this quick a femoral network that gets destroyed and or built and destroyed really quickly.
[00:02:02] So that way you don't have to worry about state you don't have to worry about anything persisting you don't have to worry about any kind of outside environmental factors that will make your test fail? Because nothing's worse than a test that relies on some network data. Because every now and then it breaks.
[00:02:17] People are asking you why it turns out, had nothing to do with you. So, that is what effectively has happened here is we are able to communicate with a network that existed for a moment and left. So let's do this again. But let's instead of writing a test, let's write a deployment script, meaning we're gonna actually deploy this to a network.
[00:02:42] Like a real network, not a fake network, but like a real network as real as it gets. So let's go into our route and let's create a directory called Scripts inside of scripts let's create a deploy dash Hello, whatever you want to name it, just some typescript file that is comfortable for you.
[00:03:00] And then of course, at the top of the TypeScript file, we put in that same nomic labs because I just like really nice auto completion without a bunch of complaining. Now today we'll probably be putting in some amount of TS ignores and if about 50% of the time I do that, it's actually incorrect and, Typescripts helping me, but we'll see what happens here.
[00:03:21] All right. So there we go. We have our ethers kind of additional functions added. So now we just need the import ethers from Hardhat. Again just as before and let's create an async function called Deploy and inside of here let's deploy our Hello World contract. Now I could just copy it from our test but just because it's really good to do something a few times let's just rewrite that.
[00:03:47] So first we get the Helloworld contract equals await ethers get contract factory. And then the name of your contract, not the name of the file that it's in the name of the contract once you've done that, and make sure you also await it, because it's reading something from the disk.
[00:04:04] So it's asyc. Next we actually create the contract itself by calling helloworld.deploy and then we wait for it to be deployed await hello.deployed. And once we do that let's just return the Hellocontract. There we go. I haven't put any return types and all that. That's for production based code.
[00:04:28] We're not doing production based code here. So now we're just going to deploy the Hello World contract. Let's create a second function we'll call out say hello, right and it's going to take in the Hello World contracts. And they'll simply go, wait, hello I could have done this in the first one, but it's kind of nice to separate them out.
[00:04:45] We'll use it more often. A wait, hello, hello. And then of course go console dot log, say hello. And then there we go. Two spaces, one space. Beautiful. And then of course at the very bottom, we're simply gonna go deploy then say hello. So, we do each one of those at a time.
[00:05:06] Now, notice typescripts kinda complaining right here. We're gonna have some implicit anys. It's just gonna happen from time to time. I'll leave this up here for a moment so everyone can get caught up. But notice we're doing the exact same thing we did in the test. We get the contract factory.
[00:05:21] It's not a contract itself it just contains all the information it needs to deploy it to a network. We call this convenience function called deploy. It's going to build up a JSON request that it's gonna JSON our RPC there we go. I was like, wait a second. It's gonna RPC it over to some network.
[00:05:43] That network will then begin the transaction. When it's done doing enough confirmations on that network, we'll be considered deployed, we'll return this out, and then now we'll actually execute a function on it. Now, remember the extra function we're calling right here is the literal function that is right here.
[00:06:20] Cause that's just the easiest way to think about it, is try to relate it to things you already understand. It's just like ,any sort of API you've written before. All right, so there we go. So now we should be able to go NPX, hard hat run scripts. Deploy then we're gonna go network local host we're gonna just run it locally right we're going to tell it where to run.
[00:06:43] So I'll let this sit up for one second as everyone gets caught up this is very important if you don't do this, it's gonna use that ephemeral network. And this is where you can also replace the word local host, say with Rank B, Main Net, any of the other networks that do exist and you will actually go and deploy it on there.
[00:07:00] But of course, at that point, you need a theorem on those networks to be able to deploy stuff. So for now, we'll just do it to our local net and when I press enter what happens? Well of couse it fails because we have an implicit any that's it my guess right off the RIP yep okay so I probably should have you know probably some TS config to avoid that.
[00:07:23] So let's just have a quick TS ignore I believe that is correct. And then we can redo this and then now it should fail properly. There we go. We've properly failed this time. So what happens here it says cannot connect to the network, local host so what's going on there.
[00:07:41] Well, we get this error. What is the cloud, it's just someone else's computer, you've specified someone else's computer as your own computer. So therefore, we need our own network on this computer. So let's go apx hard hat node, it will deploy a Ethereum network on your local computer that you will be able to run.
[00:08:04] And t has a bunch of accounts already created for you that each have 10,000 ETH your rich time to retire and now you can interact with this network. So let's open up a new terminal and re-execute our deployments script now that we have a node launched on our local machine.
[00:08:22] When we do that, you're gonna see a couple things happen here. First, you're gonna see hello front and masters which is quite fantastic. But then let me make sure I'm going in with, okay, good. I'm going along my pre designed path here. If we go over to our node we launched, what you'll see is that there's actually a couple transactions that happen here.
[00:08:43] Right? The first one is an eth send transaction. It deploys a contract called hello world. This is our contract address. A contract address is no different than a wallet address. It's just 20 bytes of data. You know zero through F. It says hey here's the transaction ID here's who sent it.
[00:09:00] So who is this person that sent it well let's just remember these last 42266 let's go all the way up to the top and if you look at the first account it's 2266. So they're using the test account zero to be the one that's paying for the deployment of SED network.
[00:09:17] We'll go back down and then what else can we grab, hey it was block number one it was the first thing executed on this network and then you'll see that we do something different right here which is we do an eth call it's not a transaction. You'll notice that there is no gas used, there's no block number two, it simply doesn't eth call it calls out to hello-world Hello from account zero to 2 the contract we just deployed aa3 you can see aa three here ,aa three here, one nice part about using this little test network is that if you deploy a contract every first contract that you deploy has the same address.
[00:09:53] Every one of these 20 private accounts all have the same address because it's the same network being launched every single time and so there is something nice about having those things just be consistent so you can see them go okay yeah that's correct that's what I was expecting it'll be even more convenient as we get into meta mask and using things from the browser.
[00:10:12] So there we go. That is what happened. Does anyone have any questions about kind of what happened, so far, because this is a bit more unusual than your standard development, right? So we do exactly what we did in the test world we get our contract factory or we get a contract factory from Hello World.
[00:10:30] Remember that is the name of your contract, not the name of the file that contains the contract. We happen to name the same thing, a bit confusing. Second, we deploy it. Third, we wait for it to be deployed, then we return it, after that we can call functions on it that exists on our contract because he, or a hardhat, or ethers.
[00:10:49] I think it's actually ethers does this They use the contract ABI, which is inside that contract file that was generated by Hardhat a uses that to generate methods on this contract object that are just effectively proxies for what is on the chain. So somehow we haven't kind of gone over that eths knows how to communicate to whatever chain we're going to.
[00:11:13] So somehow it did know when were to deploy this, obviously some of the hints are when we did our deployment. We did dash dash network, local host. So there's some extra information obviously going on here. That Hardhat is providing two ethers that we're currently not providing. We will have to kind of break that facade here shortly but for now it just knows because of Hardhat and so we're gonna be doing this a bunch so if you want to convenience function this out you can now if you use Vim and it happens to be Neo Vim and you happen to have one of the sweet plugins I built, you can extract function you can call it foo and boom, look at that I got the bug all taken out, you can call it async and there it goes.
[00:11:54] Look at that look at how that just wonderfully worked. Okay, I mean I think it's cool, you don't have to think it's cool but I think it's fantastic. Having almost as good of tools as IntelliJ almost, and by almost I mean definitely not as good of tools as IntelliJ Uses fun little plugin called tree sitter, while we're waiting, I assume, are we good to go?
[00:12:23] Awesome. All right. So there we go. So let's continue on. So we're gonna go even deeper. I know we barely I feels like we haven't explained a lot of things. We keep on moving on to new things which is kind of hard to always do something new over and over again because you feel like you don't quite understand it.
[00:12:42] You probably tell me not to do it. We got a question, telling me not to do it.
>> Are those private keys switched out every time you run Hardhat so if you relaunch your app on your computer is it a fresh set of keys each time or. No it's the same keys every time, it's the same accounts every time.
[00:13:00] That's why you always get the same contract address if you launch them in the same order because it's a network that's fairly predictable it just is there's no outside influence that network obviously can't be you know reach for I'm sure you could make it reach from the outside but it's just effectively your own private instance of a network starting at block zero and so they just precede it with 20 accounts.
>> So if you have like a really complicated smart contract does it cost more guests to execute that.