DEV Community

Seymour1948
Seymour1948

Posted on • Updated on

Deploy a ERC20 compliant contract. Part 3.

So welcome to part 3. In this part I was intended to finally deploy our contract in the live net as promise I notice there was still a couple of subjects I don't want to omit. Control version and a little bit more of testing. So let's do it. Working with a control version software garanties we have an historic of hour work that way if we need to go back it would be easier to do it. There are a lot more of advantages especially if you work in bigger projects and with other people contributing to the same project. But by now let's take just a very little glimpse of how could be work with it. We will be working wit Git, there are other control versions software but this is pretty much the standard. Just in case you don't have installed in your computer check it out

git --version
Enter fullscreen mode Exit fullscreen mode

if you obtain a confirming message giving you info about the version you have installed you are good to go otherwise follow these steps from the Git Homepage. After that if you introduce the last command again in the terminal it should show you the version you just installed.
In the terminal from our current project folder just type

git init .
Enter fullscreen mode Exit fullscreen mode

this will create a repository in that folder (thats what the dot means)
again if is your first time working with git you probably need to configure first two parameters your user and email with these to lines.

git config --global user.name "[name]"

Enter fullscreen mode Exit fullscreen mode
git config --global user.email "[email address]"

Enter fullscreen mode Exit fullscreen mode

if we type

git status
Enter fullscreen mode Exit fullscreen mode

We could see in red in our terminal which files are not part of the repo, if you remember at the very beginning hardhat ask you if create a .gitignore file in that file we tell to git which files should ignore, thanks to that we don't add to the repo files not needed or even more important files than contain info we want to keep private.
So let's add this files to the repo with

git add .
Enter fullscreen mode Exit fullscreen mode

if we type git status well see now our files in green this means they are prepare to be added permanently to the version history, we'll do that with

git commit -m "[descriptive message]"
Enter fullscreen mode Exit fullscreen mode

our message should be descriptive enough to let know anyone what we did here. Here we could say something like contract creation and firsts tests.
After that if we type

git log
Enter fullscreen mode Exit fullscreen mode

that will show our history with our first commit. Not bad as a first introduction let's add another tests to just get familiar with the workflow we should incorporate. Both testing and control version are a must and in my opinion we'll do your life easier as developer, so the sooner you get use to it the better.
In our test file we will add a couple of test.

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Glass", function () {
  let glassContract;
  let firtsAccount, secondAccount;
  beforeEach(async function () {
    [firtsAccount, secondAccount] = await ethers.getSigners();
    const GlassContract = await ethers.getContractFactory("Glass");
    const initialSupply = ethers.utils.parseEther("21000000");
    glassContract = await GlassContract.deploy(initialSupply);
    await glassContract.deployed();
  });
  it("Should return our contract", async function () {
    expect(glassContract).to.exist;
  });
  it("Should has an address", async function () {
    expect(ethers.utils.isAddress(glassContract.address)).to.be.true;
  });

  it("Should return amount balance of creator account ", async function () {
    expect(await glassContract.balanceOf(firtsAccount.address)).to.be.equal(
      ethers.utils.parseEther("21000000")
    );
  });
  it("Should return balance of second account ", async function () {
    await glassContract.transfer(
      secondAccount.address,
      ethers.utils.parseEther("11000000")
    );
    expect(await glassContract.balanceOf(secondAccount.address)).to.be.equal(
      ethers.utils.parseEther("11000000")
    );
  });
});

Enter fullscreen mode Exit fullscreen mode

You should notice that at the very beginning of describe function we declare two new variables and inside beforeEach we assign values to then using that method getSigners(). In the previous part we saw that Hardhat create for us a local network but also provides us with 20 accounts we can use for our testing. You can get this address directly with the following command in your terminal.

npx hardhat accounts
Enter fullscreen mode Exit fullscreen mode

This print the public addresses.
Have in mind this accounts are the same for everyone so be careful and don't use it for any other thing than just your tests.
So in this case with and array deconstruction we obtain just two of them. That will be enough for our new test. In the first one we check if the total balance of our minted tokens belongs to the first account. If you remember in our contract implementation we call the private function _mint which transfer this minted token to the address that deploys the contract. If we don't say any other thin in our test that would be the first account. So our first test will ensure the full amount of minted token are where they should be.
If you want just to see the test fail you can change the amount.
In the second test we are testing the transfer function in the contract. In reality we only should test our own code and this function that we inherit is way well test it but we'll get and idea how could be to test our own function if we add it some (very likely) to our bare-bone smart-contract. So if everything goes right the result is we are transferring part of our tokens to another address and they get reflected in its balance.

Ok, I think we are done with test. Till now we were deploying our contract to our local network. We were doing it in our testing phase but let see how we can do it in another way. This way will put us a step closer to our final deployment.
The first thing we will do is to launch manually our local network from terminal.

npx hardhat node
Enter fullscreen mode Exit fullscreen mode

Now that we had our app up and running let focus in our deploy script to do that we will modify the one is provided in our project. For that we will open a new terminal and copy the script provided

cp scripts/sample-script.js scripts/deploy.js  && code scripts/deploy.js -r
Enter fullscreen mode Exit fullscreen mode

Let's strip some of the comments, I recommend you read it first but we will end with something like this.

const hre = require("hardhat");

async function main() {
  const Glass = await hre.ethers.getContractFactory("Glass");
  const initialSupply = hre.ethers.utils.parseEther("21000000");
  const glass = await Glass.deploy(initialSupply);

  await glass.deployed();

  console.log("Glass deployed to:", glass.address);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Enter fullscreen mode Exit fullscreen mode

We are almost ready we have our local network running in another terminal we have our deploy script now is time to actually deploy our contract. To do that in our terminal...

npx hardhat run script/deploy.js
Enter fullscreen mode Exit fullscreen mode

we'll get our contract address but something is not right, if we take a look to the terminal where we launch our network nothing happened in there. The reason is because we didn't specify the network to deploy our contract and hardhat did like in our test and launch a local network for us. So let's fix that.

npx hardhat run script/deploy.js --network localhost
Enter fullscreen mode Exit fullscreen mode

This time we'll see our transactions of the contract creation in our terminal where we are running our local node

terminal node

So that's it for now we are closer I'm serious. Next article will be the one where we go live, but also we try hardhat console so I hope you stay with me till the end, or I should say beginning of our journey.

Top comments (0)