DEV Community

Seymour1948
Seymour1948

Posted on

Deploy a ERC20 compliant contract. Part 4.

So we are in part 4 and let's go straight to our task in the last part we hub or local network node up and running and our contract deployed in it. So let's interact with it through the hardhat console, to do that in our terminal write.

npx hardhat console --network localhost
Enter fullscreen mode Exit fullscreen mode

And we are in. We are inside a node-JS console, that means we can write some commands there. First thing we will do is attach to our currently deployed contract. First we call our contract factory like we did in our test or in our deploy script.

const Glass = await hre.ethers.getContractFactory("Glass");
Enter fullscreen mode Exit fullscreen mode

but now instead of deploy a new contract instance we attach to the one we already deploy. For that you need to rescue from the info published in your node running the address of the contract and with that we write.

const glass = Glass.attach("0x5fbdb2315678afecb367f032d93f642f64180aa3");
Enter fullscreen mode Exit fullscreen mode

Now we are ready to do some interactions but to ease things a little bit let get the accounts we already know to play a little bit with it.

const accounts = await hre.ethers.getSigners();
Enter fullscreen mode Exit fullscreen mode

and with that we can check a few things in the current contract.

await glass.balanceOf(accounts[0]);
Enter fullscreen mode Exit fullscreen mode

with that we get the current balance of the first account (the one with we deployed the contract).
and lets transfer some tokens to the second account like we did in our testing.

await glass.transfer(accounts[1].address, "1000000");
Enter fullscreen mode Exit fullscreen mode

If we check balances again in both accounts we will see that the tokens where rightly transferred. So that's it, this is just another way to check everything is in order.

Now is the time all we were expecting let's deploy our "battle tested" contract to a real network. To do that we will need to get access to this network. In order to do that we could run our own node of the network. That's what we did in our local network, but in this case we will try to get access through one of the providers that give you access to one of their nodes. There are several companies doing that in this case and having in mind we will be connecting to polygon we'll use Alchemy service. https://www.alchemy.com/
In their website we'll need to create and account to get access to the dashboard and in there push the upper right button "create app". In the panel displayed we should fill some details
Alchemy app creation
Like you see, we set to development mode we select polygon as our network but instead using the mainnet we will be using Mumbay test net. That means we won't need right now to use real money to deploy our contract. Really convenient specially if we are still testing things.
Now that we have created our Alchemy app we can click in the last column button in our right called view key and from there we copy the http address we are provided. This is let's call it our access point and next thing we need to do is add this info to our project. But we need to have something in mind this is sensible info we need to store in a way it never leaves our local development enviroment to do that we'll do the following. We are gonna create a js file call secrets.js in our project. This file needs to be excluded for example from our control version system so let's open a special file hardhat create for us at the very beginning .gitignore add add secrets.json to the list of ignored files.

.gitignore

After that we will complete our secrets.json with this info.

{
  "url": "https://polygon-mumbai.g.alchemy.com/v2/[API KEY]",
  "mnemonic": "[seed-phrase]"
}
Enter fullscreen mode Exit fullscreen mode

The url is the one we obtain from Alchemy and the second element call mnemonic refers to the seed we use to generate an address. We don't have a working address yet, hardhat provide us with 20 of them but obviously these address are not suitable for our next steps in order to do that we'll need to create a new one in metamask. If you don't have metamask still installed in your browser go to metamask.io and install it, if you already have metamask I advise you to install in another browser (chrome, firefox or brave are your options) and create a new account you reserve for development. In the process of creation you will be provided with 12 word, add them to the mnemonic.

So at this point we have our sensible data in a separated file that will never be included in our control version and we have a new fresh account created using our metamask wallet.
But still we need to let know hardhat how to access this data, to do that we will make some modifications to the hardhat.config.js file. We will strip the not needed info at this point

require("@nomiclabs/hardhat-waffle");
const { url, mnemonic } = require("./secrets.json");

module.exports = {
  solidity: "0.8.4",
  networks: {
    hardhat: {
      chainId: 1337,
    },
    mumbai: {
      url: url,
      accounts: { mnemonic },
    },
  },
};
Enter fullscreen mode Exit fullscreen mode

What we are doing here is import data from our secrets.json file and we inject this info to our new network.
We are just to one single step of deploy our contract but we will need some Matic the currency of the network we are deploying our contract so we need to get some. We can get some from this faucet:

faucet
We will introduce our public address here and that's it, in just a little time we'll see how we receive this test tokens in order to operate in mumbai network. In order to see that in our metamask wallet we need to set this network in the wallet we can do this easily using the service provided by Chainlist. From their website we can connect our metamask wallet search for Mumbai test net and add it. That will allow us to check our current balance.
And now the moment we were waiting for. The following simple line in our terminal will do the trick.

npx hardhat run scripts/deploy.js --network mumbai
Enter fullscreen mode Exit fullscreen mode

After that if everything goes fine. It should we'll receive the address of the contract we just deployed with the huge difference the contract is not just in our local network but in a "real" one.
Let's check the test net explorer of the network. Navigate to the web and introduce the address of our contract. And you should see our just deployed contract right there.
We can explore the details of the transaction if we will. Check how the new minted tokens of our contract were transferred to our account.
We can even add then to the metamask wallet to make them visible.

Import tokens
In the lower part of the interface just click import tokens and introduce again the address of our contract and that's it we have our token balance. From there we could send to another address if we will. And that's it, I hope this series of articles help you to have a better understanding of the task can involve to deploy a contract. It's obviously a simple pet project but I tried to don't omit some relevant task you should do in your next projects, like testing using a local environment, aid in hard tested libraries or contracts, use control version, secure sensible info, and of course how to bring all from your local environment to a live net.
In future articles I will try to keep using this approach. I hope to see you there.

Top comments (0)