Skip to main content
By the end of this tutorial, you will have compiled a Solidity smart contract, run its test suite, and deployed it to the Polygon Amoy testnet using Hardhat. Hardhat is an Ethereum development environment for deploying smart contracts, running tests, and debugging Solidity locally.
This tutorial follows the Hardhat Quickstart guide and adapts it for Polygon.

Prerequisites

Step 1: Create a project and install Hardhat

  1. Create an npm project:
  2. Install Hardhat:

Step 2: Create a sample project

Run npx hardhat in your project folder: img Choose the JavaScript project option. Hardhat will scaffold a sample project with a contract, tests, and deploy scripts.

Step 3: Review the sample contract

The contracts folder contains Lock.sol, a simple contract that lets the owner withdraw funds only after a specified time period.

Step 4: Configure Hardhat for Polygon Amoy

  1. Open hardhat.config.js.
  2. Create a .env file in the project root.
  3. Add your private key and a Polygonscan API key to .env. Generate a Polygonscan API key by creating an account.
Replace the contents of hardhat.config.js with:
This config uses DOTENV for environment variables. Install dotenv along with the other required packages:
Find more information about dotenv on npm.

Step 5: Compile the contract

Install the Hardhat toolbox, then compile:

Step 6: Run the tests

Expected output: img

Step 7: Deploy to Polygon Amoy

Run the deploy script from the project root:
The contract deploys to the Polygon Amoy testnet. Check the deployment status at https://amoy.polygonscan.com/. You have successfully deployed the Lock smart contract to Polygon Amoy. You can now interact with it through Polygonscan or your own frontend.
Verifying your contract makes its source code publicly visible on Polygonscan. Run:
For contracts with complex constructor arguments, see the hardhat-etherscan plugin docs.