Deploy Smart Contract On Polygon
This guide covers adding the network to a browser wallet, then deploying a simple contract using Foundry and Hardhat — pick whichever you prefer.
Last updated
Was this helpful?
Was this helpful?
# Install foundryup
curl -L https://foundry.paradigm.xyz | bash
# Install forge, anvil, cast, and chisel
foundryup# Initialize a new project
mkdir Poly-deploy && cd Poly-deploy
forge init// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.30;
contract HelloPolygon {
function hello() external pure returns (string memory) {
return "Hello, Polygon!";
}
}# Set environment variables
export PRIVATE_KEY=0x<your_private_key>
export POLY_RPC_URL=https://go.getblock.io/<ACCESS-TOKEN>/
# Deploy contract
forge create HelloEthereum \
--rpc-url $POLYGON_RPC_URL \
--private-key $PRIVATE_KEY \
--broadcast# Verify the contract on Polyscan (Mainnet — chain-id 137)
forge verify-contract <contract_address> \
src/HelloPolygon.sol:HelloPolygon\
--chain-id 137 \
--polyscan-api-key $POLYSCAN_API_KEY# Initialize project and install Hardhat
mkdir poly-deploy && cd poly-deploy
npm init -y
npm install --save-dev hardhat
npx hardhat initrequire("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.30",
networks: {
mainnet: {
url: process.env.POLY_RPC_URL,
chainId: 137,
accounts: [process.env.PRIVATE_KEY],
},
amoy: {
url: process.env.POLY_RPC_URL,
chainId: 80002,
accounts: [process.env.PRIVATE_KEY],
},
}
};export PRIVATE_KEY=0x<your_private_key>
export POLY_RPC_URL=https://go.getblock.io/<ACCESS-TOKEN>/
export POLYSCAN_API_KEY=<your_polyscan_v2_api_key>// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.30;
contract HelloPolygon {
function hello() external pure returns (string memory) {
return "Hello, Polygon!";
}
}const hre = require("hardhat");
async function main() {
const contract = await hre.ethers.deployContract("HelloPolygon");
await contract.waitForDeployment();
console.log("Deployed to:", await contract.getAddress());
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});# Compile
npx hardhat compile
# Deploy to Amoy (recommended first)
npx hardhat run scripts/deploy.js --network amoy
# Or deploy to Mainnet when ready
npx hardhat run scripts/deploy.js --network mainnet# Verify on the network you deployed to
npx hardhat verify --network amoy <contract_address>