Yield Farming Tutorial — Part 3

Deploying Smart Contracts with Hardhat

Andrew Fleming
Coinmonks
Published in
7 min readJul 7, 2021

--

In case you missed them:

Introduction

Deploying smart contracts with Hardhat poses as a much different beast than Truffle. Truffle used their own Migrations folder with a “1_initial_migrations.js” file. The general approach for deploying contracts included setting a new file “2_deploy_contracts.js” and writing deployment scripts therein. Hardhat, out-of-the-box, opted not to apply the same approach. Instead, they offer their own tasks feature in conjunction with the user’s own scripts. For newer Solidity devs reading this, deployment scripts in Hardhat are your friends.

We’ll deploy the PmknFarm contract (links at the top) onto a local network, Ethereum’s Kovan test network, and Polygon’s Mumbai test network. Let’s get into it.

Dependencies/Prerequisites

.env

We will need both a web3 provider and an Ethereum wallet in order to deploy our contracts. In the PmknFarm root, type: touch .env

I provided a simple template. Simply add this to your .env file:

Now, let’s grab the private key.

Private Key

For simplicity, we’ll use MetaMask in this tutorial. If you don’t already have MetaMask installed, click on the link and follow their directions. Once installed, follow along with the pictures to fetch your private key.

Click on the MetaMask extension
Click on Account Options
Then, Account details
Finally, Export Private Key

Thereafter, you’ll be asked to provide your password. MetaMask will present you with your private key. Copy/paste this into the PRIVATE_KEY variable in the .env file.

API KEY

To acquire an API key (so our smart contracts can interact on a public network), we’ll grab one for free from Infura. Sign up with an email and confirm your email address. Click on the Ethereum tab and input the name of your project (“PmknFarm”). Click on the endpoints drop-down and select Kovan. Copy the endpoint here:

Paste the endpoint in your KOVAN_KEY variable inside your .env file. Next up: writing the scripts.

Scripts

Local Deployment

Writing deployment scripts will look quite familiar to the deployments ran in testing. Suffice to say, you’ve already done most of the work if you properly tested your code. The following scripts can act as a template for writing Hardhat scripts in your future smart contracts.

In the project root, create a directory called scripts and add the file deployFarm.ts:

mkdir scripts

touch scripts/deployFarm.ts

Next, add the skeleton of the deployment script with error handling:

Again, similar to the PmknFarm tests, let’s include the deployer of the contract, fetch the contract factories of our smart contracts, and assign their deployments. Afterwards, don’t forget to transfer ownership of the PmknToken contract to the PmknFarm contract. That way, the PmknFarm contract can mint PmknTokens to users when they withdraw their yield. This function should be called after both the PmknToken and PmknFarm contracts have been deployed.

I always include a console.log() for each new contract deployment. This allows for easy access to the addresses when integrating the smart contract backend with the frontend. I encourage you to do the same. The final deployment script for local deployment should look as follows:

Notice, I included the MockDai contract in this script. Including the mock contract frees you from the hassle of acquiring actual DAI in a mainnet fork. I recommend including mainnet forking into your testing toolbox; however, that is outside the scope of this tutorial (Hardhat offers a great tutorial for mainnet forking here). Next, run the script you just created in the terminal. Hardhat will automatically use the Hardhat network when a specific network is not specified.

npx hardhat run scripts/deployFarm.ts

If your terminal’s output looks similar to this, congratulations! You successfully deployed your smart contracts.

Kovan Deployment

The main difference between deploying onto Hardhat’s local network and an actual public network lies in how we construct the configuration. We need to the actual wallet from which we’ll deploy from; as, launching smart contracts costs gas. We’ll also need a node as our gateway to allow our smart contracts to interact within the network (a la the KOVAN_KEY).

If you’ve followed along up to this point, you should have your .env file set up — if not, see the beginning of the article. Next, we add the Kovan network to our networks key in the hardhat.config.ts file. Within the kovan key, we’ll need to include a url key and an accounts key. MetaMask does not include the 0x prefix in your private key; therefore, I choose to use template literals and insert the prefix that way.

We also need to import DOTENV’s config() function. This tells the runtime the location of our PRIVATE_KEY and KOVAN_KEY. The final, albeit minimal, configuration should look like this:

We’ll also need to switch the MockDAI contract in our script for actual DAI (kDAI) on the Kovan network. I’ve included the DAI Stablecoin address below.

const kDAI = "0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa";

Your script should look as follows:

You will need some Kovan ETH in your wallet prior to deployment. To acquire kETH, go to Gitter’s Kovan Faucet (they require only that you have a GitHub account).

Once you have some Kovan ETH, type in your terminal:

npx hardhat run scripts/deployFarm.ts --network kovan

*Also, be aware that since PmknFarm, as programmed in this series, only accepts DAI, you’ll need to acquire DAI on Kovan from the address we used. The easiest way to grab kDAI resides in opening a collateralized debt position (CDP) with MakerDAO. Click here to go to the Oasis dApp and open a CDP with your kETH.

Mumbai Deployment

If you’ve followed along with the Kovan deployment, this should prove to be exceptionally quick and easy. First, we’ll need to include an API key for the Mumbai network. Infura offers this; however, I’ve encountered a consistent error using their RPC at the time of this writing. Therefore, I encourage you to follow Matic’s documentation and use their provided RPC. Copy and paste the following into your .env file:

MUMBAI_KEY=https://rpc-mumbai.maticvigil.com

Next, add matic as a network key in your hardhat.config.ts file. Copy the url and accounts keys for Kovan, but include the MUMBAI_KEY in place of the KOVAN_KEY.

You’ll need to acquire some test MATIC for deployment. Their faucet is located here.

Finally, run the following script to deploy PmknFarm onto Mumbai:

npx hardhat run scripts/deployFarm.ts --network matic

*Also note, an official testnet DAI does not exist on Mumbai at the time of this writing. You’ll need to deploy a mockDAI contract just as we deployed to our local network in order to interact with your contract on Mumbai.

Conclusion

I hope this guide provided some insights with deploying smart contracts using Hardhat. Hardhat’s powerful plugin ecosystem offers developers an incredible amount of flexibility regarding testing and the deployment of smart contracts. I created a GitHub repo specifically for this series of tutorials here. Please reach out if you have any questions or issues. Thank you so much for reading! I hope this added value to you as a developer.

*Tips are greatly appreciated!
ETH address: 0xD300fAeD55AE89229f7d725e0D710551927b5B15

Join Coinmonks Telegram Channel and learn about crypto trading and investing

Also, Read

--

--

Andrew Fleming
Coinmonks

Writer, programmer, boating accident enthusiast