Project Setup Using Hardhat & Truffle — part 4

Shih-Yu Hwang
Coinmonks
7 min readDec 15, 2021

--

Photo by Arnold Francisca on Unsplash

Now that we have a smart contract, let’s deploy it to the blockchain

Deployment on Truffle

  • In the terminal make sure we are in the truffle-example directory

Before we can deploy our contract, we first have to add a new file under the migrations directory

Create a new file under the migrations directory and prefix the name of the file with “ 2_” and name it 2_NumberChanger.js

  • Inside 2_NumberChanger.js we will write the following script

We defined a variable called NumberChanger, but this can be any name you wish. Inside the variable, we call the artifacts.require method and pass in the name of our contract to interact with(not the name of the file), which in this case is also called ”NumberChanger”

Afterwards, we export the function that would deploy the contract

To learn more about how migrations work, go here Truffle Migration

Now that we are setup, lets deploy our contract, run the following in the terminal

And you should get the following results

Once compiled, there should be a folder created called build

This is where your contracts ABI is so that the front end can interact with it and the byte code is create for the EVM

Now that we have compiled our contracts, we can deploy it. For this example, we will deploy it to the localnet, Ganache.

First in the terminal, run the following command

You should now see the following:

Now that we have our local blockchain running, we can now deploy our contract

Running the following code in the terminal and you should get the following result

truffle migrate is similar to truffle deploy You can find out more information on this topic here Truffle Migration

You can see that truffle migrate compiles the contract and the deploys it. You can just use truffle migrate to compile and deploy your contract with one command

The contract is now deployed, can you can see the contact addresses

One last thing is to move the build folder. By default, it will be created in the root directory, but since we will be interacting with in from the front end with React, it should be located in the src folder.

In the truffle-config.js file, add the following code before module.exports

place the following code under the module.exports

The result should look like the following:

Now when we redeploy our contract, the build folder should be created inside the src folder

Deployment on Hardhat

In the terminal make sure we are in the hardhat-example directory

Before we can deploy our contract, we first have to add some code for hardhat to deploy our contract

Go to the scripts folder and rename the file sample-script.js to deploy.js

Next, under the line: console.log(“Greeter deployed to:”, greeter.address);, type the following code:

What this code does is create a new variable that will take the NumberChanger contract and deploy it. Once the contract is deployed, it will log out in the terminal the address of the deployed NumberChanger contract.

In Hardhat, you can put all the contracts to be compiled and deployed in the same file, which is located in the scripts folder.

The code should look like the following with the default Greeter contract provided by Hardhat.

Run the following in the terminal

and should get the following output:

Once compiled, there should be a folder created called artifacts

This is where your contracts ABI is so that the front end can interact with it and the byte code is create for the EVM

Now that we have compiled our contracts, we can deploy it. For this example, we will deploy it to the localnet provided by Hardhat

To specify the network to deploy to, use the following command in the terminal npx hardhat run scripts/deploy.js — network <network name>

Run the following command in the terminal:

You should now see the following output:

Now we are given the address to our contracts.

One last thing is to move the artifacts folder. By default, it will be created in the root directory, but since we will be interacting with in from the front end with React, it should be located in the src folder.

In the hardhat-config.js file, place the following code under the where the version of the solidity compiler version is declared.

The code should look like the following:

Now that we set the path, delete the artifacts folder in the root directory and redeploy it with npx hardhat run scripts/deploy.js and it should show up in the src folder.

Finally we will run a local blockchain with the following command that Hardhat provides

This creates a list of 20 accounts with their private keys and is pre-funded with test Ether.

In part 5, we will build a simple front end in react and interact with our contract.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also, Read

--

--