Deploying to Multiple Blockchain Networks¶
This guide explains how to deploy the DTA Provenance smart contracts to various blockchain networks including Polygon and Arbitrum mainnets and testnets.
Overview¶
The blockchain implementation supports multiple networks:
- Polygon (Mainnet) - Low-cost, Ethereum-compatible L2
- Polygon Amoy (Testnet) - Free testnet for Polygon (replaces Mumbai)
- Arbitrum One (Mainnet) - High-performance Ethereum L2
- Arbitrum Sepolia (Testnet) - Free testnet for Arbitrum
- Localhost - Local development with Hardhat Network
Prerequisites¶
Required¶
- Node.js v16+ and npm
- A crypto wallet (MetaMask recommended)
- Private key for deployment account
Optional (for verification)¶
- Polygonscan API key (free from polygonscan.com)
- Arbiscan API key (free from arbiscan.io)
Setup¶
1. Install Dependencies¶
This will install:
- hardhat - Development framework
- @nomicfoundation/hardhat-verify - Contract verification plugin
- dotenv - Environment variable management
- ethers.js - Ethereum library
- @openzeppelin/contracts - Secure contract libraries
2. Configure Environment¶
Copy the example environment file:
Edit .env and add your configuration:
# Your private key (WITHOUT the 0x prefix)
PRIVATE_KEY=your_64_character_private_key_here
# Polygon Configuration
POLYGON_RPC_URL=https://polygon-rpc.com
POLYGON_AMOY_RPC_URL=https://rpc-amoy.polygon.technology
POLYGONSCAN_API_KEY=your_polygonscan_api_key
# Arbitrum Configuration
ARBITRUM_RPC_URL=https://arb1.arbitrum.io/rpc
ARBITRUM_SEPOLIA_RPC_URL=https://sepolia-rollup.arbitrum.io/rpc
ARBISCAN_API_KEY=your_arbiscan_api_key
# Optional: Enable gas reporting
REPORT_GAS=false
Security Warning
- NEVER commit your
.envfile to version control - NEVER share your private key
- Use a separate wallet for testing with minimal funds
- The
.env.examplefile is safe to commit (no secrets)
3. Get Test Tokens¶
For testnet deployments, you need free test tokens:
Polygon Amoy Testnet¶
- Get MATIC from Polygon Faucet
- Select "Polygon Amoy" network
- Enter your wallet address
- You'll receive ~0.5 test MATIC
Arbitrum Sepolia Testnet¶
- Get Sepolia ETH from Sepolia Faucet
- Bridge to Arbitrum Sepolia via Arbitrum Bridge
- Or use Chainlink Faucet for Arbitrum Sepolia directly
4. Get RPC URLs (Optional but Recommended)¶
For better reliability, use dedicated RPC providers:
Alchemy (Recommended)¶
- Sign up at alchemy.com
- Create apps for each network you need
- Copy the API keys and update
.env:
POLYGON_RPC_URL=https://polygon-mainnet.g.alchemy.com/v2/YOUR_KEY
POLYGON_AMOY_RPC_URL=https://polygon-amoy.g.alchemy.com/v2/YOUR_KEY
ARBITRUM_RPC_URL=https://arb-mainnet.g.alchemy.com/v2/YOUR_KEY
ARBITRUM_SEPOLIA_RPC_URL=https://arb-sepolia.g.alchemy.com/v2/YOUR_KEY
Infura (Alternative)¶
- Sign up at infura.io
- Create projects for each network
- Update
.envwith Infura URLs
Deploying Contracts¶
Local Development¶
Start a local Hardhat node:
In another terminal, deploy:
Testnet Deployment¶
Polygon Amoy¶
Arbitrum Sepolia¶
Mainnet Deployment¶
Mainnet Warning
Mainnet deployments cost real money. Double-check: - Contract code is audited - You have enough tokens for gas - You're deploying to the correct network
Polygon Mainnet¶
Arbitrum One¶
Contract Verification¶
Verification makes your contract source code publicly available on block explorers.
Automatic Verification¶
Add --verify flag or set VERIFY=true:
# During deployment
npx hardhat run scripts/deploy-with-verify.js --network polygonAmoy --verify
# Or with environment variable
VERIFY=true npm run deploy:polygon-amoy
Manual Verification¶
If automatic verification fails:
# Get contract address from deployments/{network}.json
npx hardhat verify --network polygonAmoy 0xYourContractAddress
Check Verification Status¶
Visit the block explorer:
- Polygon: polygonscan.com
- Polygon Amoy: amoy.polygonscan.com
- Arbitrum: arbiscan.io
- Arbitrum Sepolia: sepolia.arbiscan.io
Deployment Tracking¶
Deployment information is saved in blockchain/deployments/{network}.json:
{
"address": "0x1234567890abcdef...",
"network": "polygonAmoy",
"chainId": 80002,
"deployer": "0xYourWalletAddress...",
"timestamp": "2024-02-09T12:34:56.789Z",
"blockNumber": 12345678,
"txHash": "0xTransactionHash...",
"verified": true,
"verifiedAt": "2024-02-09T12:35:30.123Z"
}
Interacting with Deployed Contracts¶
Use the interact script with any network:
# Polygon Amoy
npm run interact:polygon-amoy
# Arbitrum Sepolia
npm run interact:arbitrum-sepolia
# Polygon Mainnet
npm run interact:polygon
# Arbitrum One
npm run interact:arbitrum
The script automatically:
1. Reads the deployed contract address from deployments/{network}.json
2. Connects to the existing contract
3. Runs example transactions (register provenance, verify, etc.)
Network Comparison¶
| Network | Type | Chain ID | Avg Gas Price | Tx Cost* | Block Time | Finality |
|---|---|---|---|---|---|---|
| Polygon | Mainnet L2 | 137 | ~30 gwei | ~$0.01 | 2s | 128 blocks (~4 min) |
| Polygon Amoy | Testnet | 80002 | Free | Free | 2s | ~30s |
| Arbitrum One | Mainnet L2 | 42161 | ~0.1 gwei | ~$0.01 | 0.25s | 1 block |
| Arbitrum Sepolia | Testnet | 421614 | Free | Free | 0.25s | ~30s |
| Ethereum Mainnet | Mainnet L1 | 1 | ~50 gwei | ~$5-50 | 12s | 15 blocks (~3 min) |
* Approximate cost for registering a provenance record at current prices
Cost Estimates¶
Registration Transaction (~100,000 gas)¶
| Network | Gas Price | ETH Price | Cost per Tx | Cost per 1000 Tx |
|---|---|---|---|---|
| Polygon | 30 gwei | $0.80 MATIC | $0.0024 | $2.40 |
| Arbitrum | 0.1 gwei | $2,000 ETH | $0.02 | $20 |
| Ethereum | 50 gwei | $2,000 ETH | $10 | $10,000 |
Cost Optimization
- Use L2s: Polygon and Arbitrum are 100-500x cheaper than Ethereum mainnet
- Batch operations: Register multiple records in one transaction
- IPFS storage: Store full metadata off-chain, only hash on-chain
- Monitor gas: Deploy during low-traffic times
Troubleshooting¶
"Insufficient funds" Error¶
Make sure your wallet has: - Enough tokens to pay for gas - A small buffer (add 20% extra)
Check balance:
# Your wallet address is shown at deployment start
# Visit network block explorer and search for your address
"Nonce too high" Error¶
Your local nonce is out of sync:
"Invalid API Key" Error¶
Check your .env file:
- API keys are correct
- No extra spaces or quotes
- File is named .env (not .env.example)
"Network not supported" Error¶
Ensure:
1. Network is configured in hardhat.config.js
2. You're using the correct network name
3. RPC URL is accessible (test with curl)
RPC Connection Issues¶
Try these solutions:
- Use Alchemy/Infura: Public RPCs can be unreliable
- Check rate limits: Free tiers have request limits
- Wait and retry: RPC might be temporarily down
- Switch RPC: Try alternative public endpoints
Best Practices¶
Development Workflow¶
- Local First: Test thoroughly on Hardhat Network
- Testnet Deploy: Deploy to testnet (Amoy/Sepolia)
- Testnet Test: Run full test suite on testnet
- Audit: Get professional audit for mainnet
- Mainnet Deploy: Deploy to production network
Security Checklist¶
- [ ] Private key stored securely (never in code)
- [ ] Contract code audited by professionals
- [ ] Tests passing with 100% coverage
- [ ] Upgradability considered (if needed)
- [ ] Access controls properly configured
- [ ] Emergency pause mechanism tested
- [ ] Deployment wallet has minimal funds
Gas Optimization¶
// Good: Batch operations
function batchRegister(
string[] calldata names,
string[] calldata uris,
bytes32[] calldata hashes
) external {
for (uint i = 0; i < names.length; i++) {
_registerProvenance(names[i], uris[i], hashes[i]);
}
}
// Avoid: Individual transactions
// Each call costs base tx fee (~21,000 gas)
Monitoring¶
After deployment, monitor:
- Transaction status: Ensure confirmations
- Gas usage: Track actual vs estimated costs
- Contract events: Set up event listeners
- Error rates: Monitor failed transactions
Advanced Topics¶
Multi-Sig Deployment¶
For production, use a multi-sig wallet:
- Deploy Gnosis Safe on target network
- Fund the Safe with deployment funds
- Create deployment transaction in Safe
- Collect required signatures
- Execute deployment
Upgradeable Contracts¶
Consider OpenZeppelin's upgradeable contracts:
Use UUPS or Transparent Proxy pattern for upgradeability.
Custom Networks¶
Add new networks in hardhat.config.js:
networks: {
customNetwork: {
chainId: 12345,
url: "https://rpc.custom.network",
accounts: [process.env.PRIVATE_KEY]
}
}
Resources¶
Documentation¶
Tools¶
- Hardhat VSCode Extension
- Remix IDE - Browser-based Solidity IDE
- Tenderly - Contract monitoring and debugging
- OpenZeppelin Defender - Operations platform
Block Explorers¶
- Polygonscan - Polygon mainnet
- Amoy Polygonscan - Polygon Amoy testnet
- Arbiscan - Arbitrum One
- Arbiscan Sepolia - Arbitrum Sepolia
Faucets¶
Next Steps¶
- API Reference - Detailed API documentation
- Basic Usage - How to use the provenance system
- Examples - Real-world examples
Support¶
- GitHub Issues: Report bugs
- Discord: Join our community for help
- Documentation: Full docs at project site