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.
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.
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.
“QuillAudits provided security enhancements for projects, earning positive feedback for their prompt high-quality service as an auditing partner“
Regardless of the tool you choose, make sure you have the following prerequisites:
Create a New Directory for Your Project
Open your terminal and run the following commands:
mkdir polygon-hardhat
cd polygon-hardhat
npm init -y
Install Hardhat
Install Hardhat and its dependencies:
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox dotenv
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.
In the contracts
folder, create a new file named HelloWorld.sol
with the following content:
Update hardhat.config.js
Edit the hardhat.config.js
file to include the Polygon Amoy testnet configuration:
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
Compile your smart contract using Hardhat:
npx hardhat compile
Create a Deployment Script
In the scripts
directory, create a new file named deploy.js
with the following content:
Deploy to Polygon Amoy
Deploy your contract by running:
npx hardhat run scripts/deploy.js --network polygon_amoy
Verify your contract on Polygonscan:
npx hardhat verify --network polygon_amoy <your_contract_address> "Hello, Polygon! From Quilla"
First, install Hardhat by running:
npm install --save-dev @nomicfoundation/hardhat-toolbox
Then, compile your contract with:
npx hardhat compile
To test your contract using Hardhat, simply run:
npx hardhat test
You should see an output similar to this:
To deploy directly to the Polygon PoS mainnet, you only need to make a few changes in the Hardhat configuration:
Update hardhat.config.js
for Mainnet
Replace the polygon_amoy
network configuration with the following for the Polygon PoS mainnet:
Deploy to Polygon PoS Mainnet
Deploy your contract by running:
npx hardhat run scripts/deploy.js --network polygon
Verify the Contract on Polygon PoS Mainnet
Verify your contract on Polygonscan:
npx hardhat verify --network polygon <your_contract_address> "Hello, Polygon! From Quilla"
Open Remix IDE
Visit Remix IDE.
Create a New File
Click on the "New File" button and name it HelloWorld.sol
.
Copy and paste the following code into your new HelloWorld.sol
file:
Go to the Solidity Compiler Tab
Select the Solidity compiler version 0.5.10
.
Compile the Contract
Click the "Compile HelloWorld.sol" button. You should see a green checkmark indicating a successful compilation.
Setup MetaMask
Install MetaMask if you haven't already. MetaMask Download.
Add the Polygon Amoy Testnet to MetaMask
Open MetaMask and click on the network dropdown menu. Select "Add Network" and input the following details:
https://rpc-amoy.polygon.technology
80002
https://amoy.polygonscan.com
Get Test MATIC
Request test MATIC from the Polygon Amoy Faucet.
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.
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.
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
Verify on Polygonscan
Navigate to your contract’s page on Amoy Polygonscan and click on "Verify and Publish".
Add Polygon Mainnet to MetaMask
In MetaMask, click "Add Network" and input the following details:
https://polygon-mainnet.g.alchemy.com/v2/{your-api-key}
137
https://polygonscan.com
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.
Verify on Polygonscan
Verify your contract on Polygonscan as you did with the Amoy testnet.
Log In or Create an Account
After logging in, you will see a dashboard where you can view and create projects.
Create a Solidity Starter 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.
Install Packages
Click "Run" at the top to install all relevant packages and start the contract deployment UI.
Check Deployment
Navigate to Polygonscan to search for your account, view your deployed contract, and copy your account address.
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.
Replit allows you to publish your projects to your personal profile for others to explore, interact with, clone, and collaborate.
Here is a Video Tutorial on How to Write & Deploy a Smart Contract on Polygon
We've audited top DApps and DeFi protocols, ensuring they remain secure and Free from Hacks. Check out their audit reports to see our impact.
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.
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!
Your weekly dose of Web3 innovation and security, featuring blockchain updates, developer insights, curated knowledge, security resources, and hack alerts. Stay ahead in Web3!