Getting Started with Polygon: Deploying Your First Smart Contract On PoS

Updated at: January 5, 20257 Mins Read

Author: QuillAudits Team

Ready to dive into the world of deploying smart contracts on Polygon? Polygon, with its Ethereum compatibility, offers faster transactions and lower costs, making it a great choice for your projects.

Whether you're a seasoned developer or just starting out, this guide will walk you through the essentials using Hardhat, Remix, and Replit.

So Let's get started.


What is Polygon?

Polygon, formerly known as Matic Network, is a Layer 2 scaling solution for Ethereum, designed to improve its scalability and usability without compromising security. It offers a framework for building and connecting Ethereum-compatible blockchain networks.

By handling transactions off the main Ethereum chain, Polygon reduces congestion and lowers transaction costs, making it a popular choice for developers. It supports various scaling solutions like Plasma, sidechains, PoS, and zk-Rollups and is fully compatible with existing Ethereum dApps and smart contracts.

Polygon's ecosystem boasts a range of dApps, DeFi platforms, and NFT projects, leveraging its high throughput, low fees, and robust security features.


What You Will Do

You'll learn how to deploy a Solidity smart contract on the Polygon network using three different tools: Hardhat, Remix, and Replit.

Each tool offers a unique approach to deploying contracts, providing a comprehensive understanding of various deployment methods.


Using Hardhat

  1. Setup Hardhat Project: Initialize a Hardhat project and install dependencies.
     
  2. Write a Smart Contract: Create and write a smart contract in Solidity.
     
  3. Compile the Contract: Use Hardhat to compile the smart contract.
     
  4. Deploy to Polygon Amoy Testnet: Deploy the contract using a script and connect to the Polygon Amoy testnet.
     
  5. Verify the Contract: Verify the deployed contract on Polygonscan.
     

Using Remix

  1. Create a File on Remix: Use Remix IDE to create and manage Solidity files.
     
  2. Write a Smart Contract: Write a smart contract in the Remix IDE.
     
  3. Compile the Contract: Compile the contract directly in Remix.
     
  4. Deploy to Polygon Amoy Testnet: Connect MetaMask to the Amoy testnet and deploy the contract. 
     
  5. Verify the Contract: Verify the contract on Polygonscan.
     

Using Replit

  1. Create a Replit Account: Sign up and create a Replit account.
     
  2. Create a Repl Environment: Set up a Solidity starter project.
     
  3. Write a Smart Contract: Use Replit to write your smart contract.
     
  4. Deploy to Polygon Amoy Testnet: Deploy the contract using Replit’s deployment tools.
     
  5. Verify the Contract: Verify the deployed contract on Polygonscan.
     
  6. Publish to Replit Profile: Publish your project on Replit for others to see.
     

Prerequisites

Regardless of the tool you choose, make sure you have the following prerequisites:

  1. MetaMask Wallet: Install MetaMask and create a new account.
     
  2. Polygon Configuration on MetaMask: Configure the Polygon Amoy testnet on MetaMask.
     
  3. Testnet Tokens: Obtain test MATIC tokens for gas fees.
     
  4. Basic Knowledge of Solidity: Understanding the basics of Solidity and smart contract development.
     

Step 1: Setting Up the Project

  1. Create a New Directory for Your Project

    Open your terminal and run the following commands:

    mkdir polygon-hardhat cd polygon-hardhat npm init -y
  2. Install Hardhat

    Install Hardhat and its dependencies:

    npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox dotenv
  3. Create a Hardhat Project

    Initialize a new Hardhat project:

    npx hardhat

    Choose "Create a basic sample project" and follow the prompts. This will create a sample project with a basic configuration.
     

    1
     

Step 2: Writing the Smart Contract

In the contracts folder, create a new file named HelloWorld.sol with the following content:

3

 

Step 3: Configuring Hardhat

  1. Update hardhat.config.js

    Edit the hardhat.config.js file to include the Polygon Amoy testnet configuration:

    3

  2. Set Up Environment Variables

    Create a .env file in the root directory of your project and add your private key and Polygonscan API key:

    PRIVATE_KEY=your_private_key
    POLYGONSCAN_API_KEY=your_polygonscan_api_key
     

    Step 4: Compiling the Contract

    Compile your smart contract using Hardhat:

    npx hardhat compile

     

Step 5: Deploying the Contract

  1. Create a Deployment Script

    In the scripts directory, create a new file named deploy.js with the following content:

    2

  2. Deploy to Polygon Amoy

    Deploy your contract by running:

    npx hardhat run scripts/deploy.js --network polygon_amoy

     

Step 6: Verifying the Contract

Verify your contract on Polygonscan:

npx hardhat verify --network polygon_amoy <your_contract_address> "Hello, Polygon! From Quilla"

 

Step 7: Compiling the Contract

First, install Hardhat by running:

npm install --save-dev @nomicfoundation/hardhat-toolbox

Then, compile your contract with:

npx hardhat compile

 

Step 8: Testing the Contract

To test your contract using Hardhat, simply run:

npx hardhat test

You should see an output similar to this:

2

Source

 

Deploying to Polygon PoS Mainnet

To deploy directly to the Polygon PoS mainnet, you only need to make a few changes in the Hardhat configuration:

  1. Update hardhat.config.js for Mainnet

    Replace the polygon_amoy network configuration with the following for the Polygon PoS mainnet:

    1

     

  2. Deploy to Polygon PoS Mainnet

    Deploy your contract by running:

    npx hardhat run scripts/deploy.js --network polygon

     

  3. Verify the Contract on Polygon PoS Mainnet

    Verify your contract on Polygonscan:

    npx hardhat verify --network polygon <your_contract_address> "Hello, Polygon! From Quilla"

     

Deploying a Smart Contract Using Remix


Getting Started with Remix IDE

  1. Open Remix IDE

    Visit Remix IDE.

  2. Create a New File

    Click on the "New File" button and name it HelloWorld.sol.

Writing the Smart Contract

Copy and paste the following code into your new HelloWorld.sol file:

carbon (53).png

 

Compile the Smart Contract

  1. Go to the Solidity Compiler Tab

    Select the Solidity compiler version 0.5.10.
     

  2. Compile the Contract

    Click the "Compile HelloWorld.sol" button. You should see a green checkmark indicating a successful compilation.
     

    injected-provider
    Source
     

Connect to the Polygon Amoy Testnet

  1. Setup MetaMask

    Install MetaMask if you haven't already. MetaMask Download.
     

  2. Add the Polygon Amoy Testnet to MetaMask

    Open MetaMask and click on the network dropdown menu. Select "Add Network" and input the following details:

    • Network Name: Polygon Amoy Testnet
    • RPC URL: https://rpc-amoy.polygon.technology
    • Chain ID: 80002
    • Currency Symbol: MATIC
    • Block Explorer URL: https://amoy.polygonscan.com
       
  3. Get Test MATIC

    Request test MATIC from the Polygon Amoy Faucet.
     

Deploy the Smart Contract

  1. Connect Remix to MetaMask

    In Remix, go to the "Deploy & Run Transactions" tab. Select "Injected Web3" in the Environment dropdown. MetaMask will prompt you to connect; accept the connection.
     

  2. Deploy the Contract

    Ensure your contract is selected, then click "Deploy". MetaMask will prompt you to confirm the transaction. Once confirmed, your contract will be deployed to the Amoy testnet.
     

Verifying the Contract

  1. Flatten Your Smart Contract

    Use a tool like truffle-flattener or sol-merger to flatten your contract if it contains multiple files:

    sol-merger "./contracts/*.sol" ./build

     

  2. Verify on Polygonscan

    Navigate to your contract’s page on Amoy Polygonscan and click on "Verify and Publish".

    • Select "Solidity (Single File)" for compiler type.
    • Choose the appropriate compiler version.
    • Select the license type.
    • Paste your flattened contract code into the verification form.
    • If optimization was enabled during deployment, ensure you select the corresponding option.

       

Deploying to Polygon Mainnet

  1. Add Polygon Mainnet to MetaMask

    In MetaMask, click "Add Network" and input the following details:

    • Network Name: Polygon Mainnet
    • RPC URL: https://polygon-mainnet.g.alchemy.com/v2/{your-api-key}
    • Chain ID: 137
    • Currency Symbol: MATIC
    • Block Explorer URL: https://polygonscan.com
  2. Deploy to Mainnet

    Ensure your MetaMask wallet has sufficient MATIC for gas fees. Repeat the deployment steps in Remix, selecting "Injected Web3" for the Polygon Mainnet.

  3. Verify on Polygonscan

    Verify your contract on Polygonscan as you did with the Amoy testnet.
     

Deploying a Smart Contract Using Replit


Prerequisites

  • Replit Account: Create an account at Replit.
  • MetaMask Wallet: Install MetaMask and create a new account for testing.
  • Polygon Configuration on MetaMask: Configure Polygon Amoy testnet on MetaMask.
  • Testnet Tokens: Obtain test MATIC tokens for gas fees.

 

1

Working with a Repl

  1. Log In or Create an Account

    After logging in, you will see a dashboard where you can view and create projects.

  2. Create a Solidity Starter Repl

    • Click on "+ Create Repl" from the left panel or the top right corner.
    • Select the Solidity starter (beta) template.
    • Give your project a title and click on "+ Create Repl".

    The Solidity starter repl includes a browser-friendly interface built using the Web3 Ethereum JavaScript API, allowing you to deploy and interact with contracts.

     

     

    solidity.png

     

Deploy on Polygon Amoy Testnet

  1. Install Packages

    Click "Run" at the top to install all relevant packages and start the contract deployment UI.

  2. Connect MetaMask
    • Connect your MetaMask wallet to the web interface and switch to the Amoy testnet.
    • Click "Connect wallet," select your account, and choose "Connect."
  3. Deploy the Contract
    • Select the contract you want to deploy from the dropdown list.
    • Click "Deploy."
    • Confirm the transaction in the MetaMask popup window.
       

Verifying and Testing Your Contract

  1. Check Deployment

    Navigate to Polygonscan to search for your account, view your deployed contract, and copy your account address.

  2. Interact with the Contract

    After deployment, the contract will appear as expandable boxes below the dropdown box in the UI. Expand the boxes to interact with the contract functions.

     

Publish to Replit

Replit allows you to publish your projects to your personal profile for others to explore, interact with, clone, and collaborate.

  1. Publish Your Project
    • Select the project title at the top of the screen.
    • Complete your project name and description.
    • Click "Publish."

Here is a Video Tutorial on How to Write & Deploy a Smart Contract on Polygon



Final Thoughts

By following these steps, you can easily deploy your smart contract on both the Polygon Amoy testnet and the Polygon PoS mainnet. The key differences lie in the network configurations and endpoints, but this guide ensures a smooth transition from testnet to mainnet deployment.

Before you deploy on the mainnet, always prioritize security. You can use our QuillShield, an AI agent that checks your code for vulnerabilities and even fixes them.

1

This tool can be incredibly handy. And of course, it's crucial to have your contract audited before the final deployment to ensure everything is secure.

Happy coding!

 

Subscribe to our Newsletter

Get Pure Alpha Straight to Your Inbox. Miss this, and you’re missing out. Insider Secrets - Delivered Right to You. Subscribe now.

Telegram