LAB: Deploy NFT Contract To Ethereum
Transcript
Toggle full transcript
Hello. In this series of labs, we're going to mint an NFT on the ImmutableX blockchain. In this first lab, we're going to take the first step, which is to deploy an NFT
contract to the Ethereum blockchain. The first step is to get the source code that we have developed to guide you through this lab.
You can do that by going to the GitHub link in the resources section for this lab and you'll find yourself here.
What we need to do is to clone this repository on our computer. So to do that, we can go to Code. We can copy the URL of the GitHub checkout.
And then you can use your favorite Git client to actually clone that repository. In this case, I'm going to use the command line and just issue the git clone command.
Once you've done that, you just need to open the folder that has been cloned and then
open your favorite IDE. In this case, I'm going to use VS Code. Now the instructions to follow this lab are in the
Readme.md file. If you're familiar with Markdown, you can look at the file by itself or if you are using VS Code or some other editor that supports it, you can actually
view the preview of that file and see it in a nicer format. Alternatively, you can actually view it in that
format by scrolling down on the Git repository in GitHub as well. So in this first lab we're going to deploy
the smart contract to Ethereum that is then going to be used by ImmutableX to mint our interface.
First and foremost, to get started, we just need to make sure that we have our dependencies. So if you pull open a command line terminal.
I'm just going to do it within VS Code and we just type in: node --version, that'll tell us if we're
running Node.js. And similarly, if we can go: npm --version, then that tells us that we're running NPM and that we have a path variable set up so that we can just execute those commands directly.
Once we've validated that we can actually run npm. We simply need to run the npm install command.
And what this is going to do is it's going to install all of the dependencies that this lab needs to run.
Once that's happened, then we're basically ready to get into it.
Now, one of the key ways that we're going to be configuring the parameters of this exercise, as we go, is
by using what's called a .env file. We've provided a example file, .env.example. In order to use that, we just need to copy that to .env, now
we've helpfully provided a cross-platform command that you can execute and this will simply copy our .env.example file to .env.
And you'll note here that it's greyed out. That's because it's in the .gitignore file so you don't accidentally
commit it. So we're going to be running through this file as we go through each of these labs and populating values in it.
The first set of values need to be these two here, which are essentially going to be the Ethereum account, including the private key
that we're then going to use to actually deploy the smart contract to a theory. Again, we've got a helpful command here that you can execute.
This is just running some code that we can say is here inside scripts slash generate random key ducts.
If you want, you can control + click on that to see what's going on. We're essentially just pulling out this wallet library from ethereumjs-wallet,
which is one of the dependencies that got installed by npm install and we're calling wallet.generate(). This is an offline operation and it produces
a random private key that represents an Ethereum account. And you can see here we've just output to the command line
the values that we need to paste into our .env file. So if you copied that value from your command line, you should be able to go
into .env and replace the corresponding lines there and then save that file. It's worth pointing out that if you want, you can add the address and private
key of your own Ethereum wallet, although it's worth pointing out that you want to be careful with how you handle your private key to make sure
that it doesn't get leaked. The next step then is to set our provider API keys.
There's two third party services that we're going to be using to do this. The first is Etherscan and the second one is Alchemy.get repot
with Etherscan, this is going to allow us to verify a contract and make the source code public. It just provides a better developer experience.
To do this, we're going to need to generate an API key. So you need to go to their website and log in. Now, in my case, I've already created an account and I'm already logged in.
But if you happen to create an account for the first time, you simply just need to go to the API Keys page, which you can see in the menu here.
Or alternatively, once you're logged in, if you click on that link, then that will take you to the right page. Once you're here, you simply need to create an API key.
So if I click add, we type in the name of the project. I'm just going to call this IMX Minting. I'm going to create the API key and then simply just need a copy of that API
key and put it inside our .env file against the ETHERSCAN_API_KEY line.
The next one we need is Alchemy. And what we do with Alchemy is we use it as an Ethereum
node that we can send transactions to. And this just means that we don't need to go through the hassle of standing up at Ethereum nodes on our local machine or elsewhere.
So to do this similarly, we'll need to create an account with Alchemy. So if we click on the link. That takes us to the Alchemy dashboard and it will make you
log in or sign up for an account. Now, as before, I'm actually already signed into my account here.
Now, what we need to do is we just need to go to Apps and then Create App. And we just need to give it a name. So again, I'm just going to call it IMX Minting.
If we want, we can give it a description. The chain that we're going to be connecting to is Ethereum and the network,
in this case, that we're going to be connecting to is Goerli, which is the Testnet. So if I click Create app, then that creates the app for me and we can see it down here.
And I simply just need to click on View key and the API key is at the top here so I can click copy.
If we go back to VS Code or your editor choice, we simply just need to paste that value that we just copied into the ALCHEMY_API_KEY
line in the .env file and then site our .env file. The next step is an optional one. If we want, we can update the contract details.
So here we've got a sample CONTRACT_NAME called My Contract, in this case, and a CONTRACT_SYMBOL, MC.
So in this case, maybe I'll change it to My IMX Contract and we'll call it MIC. We're going to save that.
The third value here is the CONTRACT_METADATA_URI. This needs to be a URL that points to an API that when you
append it with slash + the token ID, so like /1, /2, /3, etc. it returns an ERC-721 compatible metadata payload that describes the metadata of that NFT.
Now we've provided a sample metadata URI, that you're welcome to use, which will automatically return compliant payloads
for you so that you don't have to worry while doing this lab about trying to also generate an API.
The next step and, the last one before doing the actual deployment, is that we actually need to fund a randomly generated Ethereum wallet with some ETH
on the Goerli network. And this is so that we can actually pay for the gas fees to deploy that contract. Now we've included a URL to the Goerli faucet, which is run by Alchemy.
And in order to use this, you just need to log in with the same Alchemy account that you just used.
In this case, I'm already logged in. And then we pretty much just need to put our wallet address that has been generated. So in this case, if we go back to our .env file and we copy
the OWNER_ACCOUNT_ADDRESS value and we paste that in there and then hit Send Me ETH...
And, now we can say that we've been funded. So now that we've done all of that, we can actually do the last step, which is to deploy
the contract. Again, we've included a helpful command here that you can execute.
Now, in this case, what's happening is it's using HardHat to deploy our contract, using this deploy/asset.ts
file to the Goerli network. And you can see here that it's actually compiled the Solidity files
into the bytecode and then deploying the contract using the account that we configured in the .env file.
And once that's all done, then we can see we've got a success message. We've got a link to see the contract on Etherscan, and we've got a instruction here to update our .env file with the contract address that we just
deployed, which is actually step eight. So let's just start by doing that and get that out of the
way. If we scroll down here, we can see there's a contract address waiting to be filled in before we dive in and have a look on the scan.
Let's just have a quick look at this file here. Essentially, we're importing some commands from HardHat,
and what we're doing is we're getting some environment variables, the ones that we've been filling in in our .env file. We're getting a contract factory to deploy the asset contract,
which if we go in our contracts folder here, we can see there's an asset.sol file. So this is the Solidity contract that we are deploying.
We can see here that it extends ERC-721 and also the IMX Mintable interface.
Other than that, the contracts are pretty straightforward. It's just extending and calling some of those underlying functions. If we go back to our asset.ts, we can see
that we then just call asset.deploy() and we pass in all of the values that are expected in the constructor
of our smart contract. And then we essentially just run it. So it's a reasonably straightforward command.
The final thing then to look at is what does it look like on Etherscan? So if we click through, we can see here we've got our transaction.
Now if you click that link straight away, sometimes it might take a little while to appear, so just keep retrying. And we can click in and have a look at the high level transaction
details. We can see that it was issued from our account that we randomly generated.
We can see what the transaction fee and the gas price were. If we go back and we click on Contract, then we can see the byte code for that contract.
So there we have it. We've completed our first lab. We've set up our environment. We've created an account on Ethereum.
We've funded it with ETH from the Goerli Testnet and then we've deployed our ImmutableX compatible contract
to that Goerli Testnet and captured the contract address. I'll see you in the next lab where we can start interacting with ImmutableX.