Better Programming

Advice for programmers.

Follow publication

How to Deploy a Smart Contract in 5 Minutes

Sicong Zhao
Better Programming
Published in
3 min readFeb 9, 2022
The header image of HOW TO DEPLOY A SMART CONTRACT IN 5 MINTUES?

There are many ways to deploy a smart contract. In this article, I will share the way to deploy a smart contract using hardhat. There is no pre-requisite required.

5 steps to deploy a smart contract

  1. Set up the environment
  2. Initiate a hardhat project
  3. Configuration
  4. Code for Deployment
  5. Deploy

Step1. Set up the environment

Step1, run the code in your terminal.

We start by creating a project folder, and use npm init -y to generate an npm project. -y means say yes to every prompt.

Then we install the hardhat library. Hardhat is an Ethereum development environment. It helps to test, compile, deploy and debug smart contracts.

Remember to install all dependencies based on your project. For example, in the case of an NFT smart contract, you might want to add npm install @openzeppelin/contracts .

Step2. Initiate a hardhat project

Stay in the project folder, and use the command npx hardhat to create a basic sample hardhat project. Choose the default option for every prompt question. Your terminal will look like this:

Terminal output for ‘npx hardhat’ command.
Fig.1 Terminal output for ‘npx hardhat’ command

Now in your project folder, there are many folders with a sample smart contract under contract folder. Let’s delete the following files:test/sample-test.js, script/sample-script.js, contracts/Greeter.sol.

And then save your smart contract under the\contracts folder.

Step3. Configure network and private key

Let’s use Mumbai, Polygon’s Testnet as for demonstration. Open hardhat.config.js and replace the existing code with the following code.

Make sure you choose the correct solidity version, add your private key and Mumbai RPC. You can find a list of RPC in the official document. Since this file contains your private key, you might want to keep it on your local machine.

You can configure other networks in the networks section, and in step5 you can choose which network to use.

Unfamiliar with RPC? I was also confused. This article helped me out. In short, an RPC is like an API address in Web2.0.

Step4. Code for Deployment

Now we set up the environment and configuration file, let’s work on the code that will help deploy the smart contract.

Create a file deploy.js under \scripts folder, and copy the following content:

The code is self-explanatory. Essentially we just use hre.ethers to get our contract and deploy it. Here are some tips:

  • In line 2, your-contract-name should not contain ‘.sol’. If your contract filename is myContract.sol, just use myContract here.
  • In line 3, provide all parameters required by the constructor of your smart contract.
  • From lines 7 to 11, you can try to interact with your smart contract by invoking its functions.

Step5. Deploy

The last step is the easiest, just use the command: npx hardhat run scripts/deploy.js --network mumbai

The expected terminal output will look like this:

Fig.2 Terminal output for contract deployment command
Fig.2 Terminal output for contract deployment command

Remember your smart contract address, you will use it when building the front end.

Parting Words

There you have it! I hope this tutorial helped you deploy your smart contract. Please feel free to leave a comment if you have any questions or suggestions. If you want me to write other topics about blockchain development, please also let me know!

Want to Connect?Please feel free to reach out (my LinkedIn) if you have any questions, feedback, or even just a random chat.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sicong Zhao
Sicong Zhao

Written by Sicong Zhao

Founder of Learniverse / Duke Alumni / Former Data Scientist @Credit Suisse / Former Computer Vision Fellow @Etsy /Former Product Designer @Baidu.

No responses yet

Write a response