乐闻世界logo
搜索文章和话题

What is an Ethereum testnet? Please explain how to use testnets like Sepolia and Goerli

2月21日 14:18

Ethereum testnets are important environments for developers to test smart contracts and DApps, providing similar functionality to mainnet but using test coins. Here's a comprehensive analysis of testnets:

Basic Concepts of Testnets

Ethereum testnets are independent networks with the same functionality as mainnet (Mainnet), used for development and testing. Testnets use test coins that have no actual value and can be obtained for free.

Major Testnets

1. Sepolia

Currently recommended testnet.

Features:

  • PoS consensus
  • Stable network
  • Supports EIP-1559

Get Test Coins:

javascript
// Use faucet to get test coins async function getSepoliaETH(address) { const response = await fetch('https://faucet.sepolia.dev', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ address }) }); const data = await response.json(); console.log("Transaction hash:", data.txHash); }

2. Goerli

Deprecated testnet, but some projects still use it.

3. Holesky

Beacon chain testnet for PoS testing.

Local Testnets

1. Hardhat Network

Hardhat's built-in local testnet.

Configuration:

javascript
// hardhat.config.js module.exports = { networks: { hardhat: { chainId: 31337, accounts: { count: 20, accountsBalance: "10000000000000000000000" // 10000 ETH } } }, solidity: "0.8.19" };

Usage:

javascript
// Test on Hardhat network const { expect } = require("chai"); describe("MyContract", function () { it("Should work on Hardhat network", async function () { const [owner, addr1] = await ethers.getSigners(); const MyContract = await ethers.getContractFactory("MyContract"); const contract = await MyContract.deploy(); await contract.connect(addr1).someFunction(); expect(await contract.balanceOf(addr1.address)).to.equal(1); }); });

2. Ganache

Local blockchain simulator.

Start:

bash
# Start Ganache ganache-cli --deterministic --accounts 10 --defaultBalanceEther 1000

Connect:

javascript
// Connect to Ganache const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545"); const wallet = new ethers.Wallet(privateKey, provider);

3. Anvil

Foundry's local testnet.

Start:

bash
# Start Anvil anvil --fork-url https://eth-mainnet.alchemyapi.io/v2/YOUR_API_KEY

Testnet Configuration

1. Network Parameters

javascript
const networks = { sepolia: { chainId: 11155111, name: 'Sepolia', rpcUrl: 'https://sepolia.infura.io/v3/YOUR_PROJECT_ID', blockExplorer: 'https://sepolia.etherscan.io', faucet: 'https://faucet.sepolia.dev' }, goerli: { chainId: 5, name: 'Goerli', rpcUrl: 'https://goerli.infura.io/v3/YOUR_PROJECT_ID', blockExplorer: 'https://goerli.etherscan.io', faucet: 'https://goerlifaucet.com' } };

2. Switch Networks

javascript
// MetaMask switch network async function switchNetwork(chainId) { try { await window.ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: `0x${chainId.toString(16)}` }] }); } catch (switchError) { if (switchError.code === 4902) { await window.ethereum.request({ method: 'wallet_addEthereumChain', params: [{ chainId: `0x${chainId.toString(16)}`, chainName: 'Sepolia', nativeCurrency: { name: 'Sepolia ETH', symbol: 'ETH', decimals: 18 }, rpcUrls: ['https://sepolia.infura.io/v3/YOUR_PROJECT_ID'], blockExplorerUrls: ['https://sepolia.etherscan.io'] }] }); } } }

Deploy to Testnet

1. Deploy with Hardhat

javascript
// scripts/deploy.js async function main() { const [deployer] = await ethers.getSigners(); console.log("Deploying contracts with account:", deployer.address); const MyContract = await ethers.getContractFactory("MyContract"); const contract = await MyContract.deploy(); await contract.deployed(); console.log("MyContract deployed to:", contract.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); }); }

Run Deployment:

bash
# Deploy to Sepolia npx hardhat run scripts/deploy.js --network sepolia

2. Deploy with Foundry

solidity
// script/Deploy.s.sol // SPDX-License-Identifier: MIT pragma solidity ^0.8.19; import "forge-std/Script.sol"; import "../src/MyContract.sol"; contract DeployScript is Script { function run() external { uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); vm.startBroadcast(deployerPrivateKey); MyContract contract = new MyContract(); console.log("Contract deployed to:", address(contract)); vm.stopBroadcast(); } }

Run Deployment:

bash
# Deploy to Sepolia forge script script/Deploy.s.sol --rpc-url $SEPOLIA_RPC_URL --private-key $PRIVATE_KEY --broadcast

Testnet Faucets

1. Common Faucets

2. Faucet Usage

javascript
// Automatically request test coins async function requestTestETH(address) { const faucets = [ 'https://faucet.sepolia.dev', 'https://goerlifaucet.com' ]; for (const faucet of faucets) { try { const response = await fetch(faucet, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ address }) }); if (response.ok) { console.log(`Successfully requested ETH from ${faucet}`); break; } } catch (error) { console.log(`Failed to request from ${faucet}:`, error.message); } } }

Testnet Best Practices

1. Development Workflow

bash
# 1. Develop on local network npx hardhat test # 2. Deploy to testnet npx hardhat run scripts/deploy.js --network sepolia # 3. Verify contract npx hardhat verify --network sepolia CONTRACT_ADDRESS # 4. Interact with contract npx hardhat console --network sepolia

2. Environment Variable Management

bash
# .env file SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/YOUR_PROJECT_ID PRIVATE_KEY=your_private_key ETHERSCAN_API_KEY=your_etherscan_api_key
javascript
// Use environment variables require('dotenv').config(); const config = { sepolia: { url: process.env.SEPOLIA_RPC_URL, accounts: [process.env.PRIVATE_KEY] } };

Testnet vs Mainnet Differences

1. Main Differences

FeatureTestnetMainnet
ETH ValueNo actual valueHas actual value
Gas FeesLowHigh
Network StabilityMay be unstableHigh stability
Data PersistenceMay resetPermanently saved
Community SupportDeveloper communityAll users

2. Mainnet Deployment Checklist

  • Thoroughly test on testnet
  • Pass security audit
  • Prepare sufficient ETH for Gas fees
  • Verify contract code
  • Prepare emergency plan
  • Set up monitoring and alerts
  • Prepare documentation and user guide

Common Questions

Q: Will testnets reset?

A: Some testnets reset periodically, but Sepolia is relatively stable. Important data should be backed up.

Q: How to get more test coins?

A: Use faucets, but there are usually time limits. Try different faucets or wait for cooldown time.

Q: Can testnet contracts be migrated to mainnet?

A: Yes, but need to redeploy. Contract addresses will change, related configurations need to be updated.

Testnets are important tools for Ethereum development. Thorough testing can avoid issues during mainnet deployment.

标签:以太坊