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

The "Alchemy Test Network" 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 demonstrates how Alchemy is used to deploy the contract to a live test network. In this case, the test network is Rinkeby. An Alchemy API endpoint is specified in the hardhat.config.ts file. MetaMask then needs an authorized account for Rinkeby.

Preview
Close

Transcript from the "Alchemy Test Network" Lesson

[00:00:00]
>> I'm gonna undo these cuz we don't need to do that, this part is fantastic, this is kind of like where the magic happens, okay? Everyone ready for the magic to happen? All right, I've done a couple things, first, I've created a private file. You're not allowed to see what's in that private's file, okay?

[00:00:15]
It contains some information. So I'm gonna just simply source the private file, awesome. I now have some environmental keys in here. I have a key to a provider that runs on a test network, it's called Alchemy, if you've ever been to Alchemy. Alchemy effectively, let's say, okay, might expose them.

[00:00:33]
I dox myself all the time, so it effectively provides a way into the network, and you need a way to provide yourself into the network. Now what you could do is you could use a thing called Geth and actually create an Ethereum node that connects to a real network on your machine and make requests yourself.

[00:00:51]
Well, I don't do that, instead, most people just simply use a provider that's already out there. There's one called Alchemy, there's another one called Infura, you can find out a bunch of different ones. Alchemy seems to be a pretty popular one, so I just chose that. So I have an Alchemy key and then I also have a Rinkeby private account or wallet.

[00:01:10]
So if we look at right here, if I open up my MetaMask, you'll notice that I have, let's switch over to Rinkeby, and you'll notice that I have this one. I have a decent amount of Rinkeby, I actually had someone try to pay me for some of the Rinkeby when it was really hard to get.

[00:01:24]
I almost made money off of fake money, I mean, even faker money than fake money, it was like super fake. And it was very fantastic, I was very excited about it. But there we go, I have some fake Ether on a network called Rinkeby. Which means now that I've sourced, I have those private key informations inside of here, I'm gonna cat out one thing from something behind me, what is it?

[00:01:46]
Yeah, rinkeby.json, I knew that, rinkeby.json. I'm gonna copy this little piece of code, you guys don't need to do this cuz obviously you haven't set up all the accounts and all that. I'm gonna copy this, I'm gonna go to our hardhat config and under our networks, I'm gonna include this, rinkeby.

[00:02:02]
This is how you communicate with Rinkeby. Now notice we still have everything else the same, I'm just saying, hey, here's the URL to the provider, here's the list of accounts that you're gonna use. So instead of having our 20 hydrated accounts from the local network, I'm now providing the list of accounts for it to use by using my private key.

[00:02:22]
And so there we go, we have these two things right here. I'm gonna do this now, this is where things are gonna get a little bit interesting. Just so I don't screw myself like I did last night, I'm also gonna go like this, console.log(counter.address). I'm gonna print my address cuz if you forget to print your address, well, I can't contact it.

[00:02:40]
So then you have to deploy it again. So now that we've done that, I'm gonna go npx hardhat, I'm gonna launch my counter but this time, I'm gonna erase localhost and put in rinkeby. There we go. We are now putting it onto an actual test net that will exist tomorrow, this contract will go on and live forever.

[00:02:58]
Now it's gonna take a little bit longer than our previous items because it's actually contacting something that's not just simply on our localhost. So it has to go through all this stuff. There we go, I got an address out, pretty exciting, right? At least I'm pretty excited. Also notice something kind of funny.

[00:03:16]
Our counter is zero, last time we did that, our counter was one, why? Cuz we were doing a node request to a node that existed on our machine and we were able to catch state's transition because it could happen in a single step. Now we're on a real network, state transitions don't happen in a single step anymore, right?

[00:03:38]
So it takes longer, all right? So now we do that fun game again. I'm going to the environment, erasing that, putting in our new one, go to webpack, kill that, relaunch webpack. And as this thing launches, it'll take a second. I go here, we are now at one.

[00:03:51]
Also notice that it took a second. Do you see that kinda how it took a moment? We're now out there contacting it. And how are we contacting Rinkeby? It's cuz I took MetaMask and I set it to the Rinkeby network, that is it. So notice we didn't change any of our frontend code.

[00:04:07]
I'm going to increment it, it's now running on somewhere else. We have the wrong account hooked up to it, so this exciting part just got a little bit less exciting, not so happy about that. So what we're gonna do is we're gonna go here, I am gonna take this, I'm gonna connect to my network.

[00:04:21]
There we go, connected, beautiful, awesome, do that. I'm gonna refresh it just in case. Let's increment this thing, and there we go, we have this. We're gonna confirm the transaction, and now we're going to wait. So this transaction is happening on a real test network. We could go to Ether scan, we could actually find this information on Ether scan for it.

[00:04:40]
So it has happened and we just got the increment right there. So we actually, look at that, look how sweet that is. Okay, I think this is the coolest part. I don't see very many smiles or cheers or gasps, but this was awesome. We went from our machine all the way out into the, I don't know where Rinkeby exists.

[00:04:56]
It exists in a decentralized way, right? On hundreds of machines out in the world. And we just put that state changing transaction that will live effectively forever as long as Rinkeby does that will be there forever. You in a year from now, if it's still up, can take this contract address and increment it for fun, right?

[00:05:14]
If someone's at home doing it right now, if they're fast enough to do it, they would probably increment it in front of me and it would start going up, right? That's how awesome this is, is that it actually worked. We actually launched it somewhere else and changed nothing about our frontend, because the provider is what abstracts that communication to the network.

[00:05:33]
Okay, well, I mean, I thought that was awesome. So I can paste in the contract address. If anyone wants to increment it, we'll come back and see how many increments we have. It's on the Rinkeby network, so you'll have to have a Rinkeby account. You'll obviously, have to be able to write that deploy code.

[00:05:49]
Kind of good little exercise to not deploy it, but to just simply talk to it, do all that kind of fun thing. There's a lot we just did here, is there any specific questions?
>> Can you in Ethereum contract, it can really just have any arbitrary effect on anything, then it doesn't necessarily have to do with monetary transactions or moving Ethers around?

[00:06:14]
>> That is exactly right. That's why I'm so excited about this, cuz right now it's hyper inefficient, it's not that great. Every technology that's ever been introduced, its first version has always sucked. That's just the reality of the life, right? I mean, think about cell phones, they weren't that awesome, they got awesome, they've been more awesome.

[00:06:33]
First Internet, who had 14.4, right? Who had the dial-up sounds, right? That sucked, but it was awesome at the exact same time. And so the same thing with this, is it's super expensive, it's ridiculously expensive, it's not worth it at all expensive. It needs to come down by at least 1,000 fold for it to be even partially useful in any sort of real context.

[00:06:56]
But we're not doing anything with JPEG art, we're not doing anything with moving Ethers around. We're just simply creating functionality and using it like AWS Lambda. And that is what's really cool about this, is that it's a decentralized censorship resistant network in which you can put things up that execute.

[00:07:15]
And to me, that's a pretty novel or neat idea. Now, will it pan out in the future? Like I said, I have no strong stance either way cuz I can't read into the future, but I just think it's a very exciting thing. And so, hopefully, you're feeling that excitement kinda rise cuz that's how I felt at this point when I launched onto Rinkeby, cuz I originally wasn't gonna do that.

[00:07:35]
But I thought, okay, I'm gonna give it just a quick try, maybe it'll work out, this was two days ago. And I just did it first try and I was like, holy shit, that really worked way easier than I thought it was going to. Cuz every time I've done Rinkeby before that, it was always I had so much setup and all that.

[00:07:49]
But now that I'm just slightly familiar with it, just blazed right through and it worked, and I was like, wow, that was sweet. All right, Mark.
>> So Alchemy provisions a node for you to use, does that cost anything to use?
>> There's the free to use plan, it has quite a few cycles on the free to use plan, so I just use that, right?

[00:08:07]
Eventually, yes, you will have to pay for any provider as you become a larger and larger place. If you were to try to launch something that does a lot of transactions, that does a lot of testing, you will eventually have to pay for something. But for now, easy to use, free first using little bits.

[00:08:22]
It's kind like the lowest plan on Heroku, exact same thing.
>> So nobody can stop us from counting now?
>> No one, nobody can stop us from counting. [LAUGH] It'll be censorship forever. There we go, let me go over to this thing, and I'll throw in that, there we go, fantastic.

[00:08:42]
By the way, there's the Rinkeby address to our contract, you can call account. If you want to do that just put it in the chat, you can always do this. There we go, that's the interface. If you just wanna use that, you should be able to start up in Ethers.

[00:08:55]
Plug that little interface in there, do that address, and just start calling it if you want to.
>> Is there a linter or anything like that for those interfaces?
>> So those interfaces, that's why I just use hardhat, they generate those interfaces for me. But on the other side, I don't know if there is something that does it from the TypeScript side.

[00:09:16]
I'm sure because, I mean, let's just be real here. If you look at the code, let's see, what is it? It's artifacts, this, there we go. Oop, not that one, art, I ignored that artifacts thing, sorry, hold on. If we just look at this right here, not that one, this one.

[00:09:31]
This take me an hour to write a generalized version to be able to generate out these interfaces such that they'd be attached to objects and returned back, right? It's not very hard, you have all the information. So it definitely can be done, I'm sure it probably already is done, I just don't know about it.

[00:09:48]
The reality is I write very little. When it comes to the website side of things, I try to avoid as much as I can with this stuff, but there we go. You should easily be able to write this stuff out. You already know it's a type event, so you need to mutate the filters object with a little extra layer, boom.

[00:10:03]
So if it doesn't already exist, it'd be easy to exist, you'd get 100,000 stars on GitHub. And everyone knows that every star you get increases your pay by 0.1%, so it's very important to have a lot of stars on GitHub.

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