deploying-contracts-on-base
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDeploying Contracts on Base
在Base网络部署合约
Prerequisites
前置条件
- Configure RPC endpoint (testnet: , mainnet:
sepolia.base.org)mainnet.base.org - Store private keys in Foundry's encrypted keystore — never commit keys
- Obtain testnet ETH from CDP faucet (testnet only)
- Get a BaseScan API key for contract verification
- 配置RPC端点(测试网:,主网:
sepolia.base.org)mainnet.base.org - 将私钥存储在Foundry的加密密钥库中——绝对不要提交密钥
- 从CDP水龙头获取测试网ETH(仅测试网)
- 获取BaseScan API密钥用于合约验证
Security
安全注意事项
- Never commit private keys to version control — use Foundry's encrypted keystore ()
cast wallet import - Never hardcode API keys in source files — use environment variables or with
foundry.tomlreferences${ENV_VAR} - Never expose files — add
.envto.env.gitignore - Use production RPC providers (not public endpoints) for mainnet deployments to avoid rate limits and data leaks
- Verify contracts on BaseScan to enable public audit of deployed code
- 绝对不要将私钥提交至版本控制系统——使用Foundry的加密密钥库()
cast wallet import - 绝对不要在源文件中硬编码API密钥——使用环境变量或带有引用的
${ENV_VAR}foundry.toml - 绝对不要暴露文件——将
.env添加至.env.gitignore - 主网部署时使用生产级RPC提供商(而非公共端点),以避免速率限制和数据泄露
- 在BaseScan上验证合约,以便公开审计已部署的代码
Input Validation
输入验证
Before constructing shell commands, validate all user-provided values:
- contract-path: Must match . Reject paths with spaces, semicolons, pipes, or backticks.
^[a-zA-Z0-9_/.-]+\.sol:[a-zA-Z0-9_]+$ - rpc-url: Must be a valid HTTPS URL (). Reject non-HTTPS or malformed URLs.
^https://[^\s;|&]+$ - keystore-account: Must be alphanumeric with hyphens/underscores ().
^[a-zA-Z0-9_-]+$ - etherscan-api-key: Must be alphanumeric ().
^[a-zA-Z0-9]+$
Do not pass unvalidated user input into shell commands.
在构造Shell命令前,验证所有用户提供的值:
- contract-path:必须匹配。拒绝包含空格、分号、管道符或反引号的路径。
^[a-zA-Z0-9_/.-]+\.sol:[a-zA-Z0-9_]+$ - rpc-url:必须是有效的HTTPS URL()。拒绝非HTTPS或格式错误的URL。
^https://[^\s;|&]+$ - keystore-account:必须是包含连字符/下划线的字母数字字符串()。
^[a-zA-Z0-9_-]+$ - etherscan-api-key:必须是字母数字字符串()。
^[a-zA-Z0-9]+$
不要将未验证的用户输入传入Shell命令。
Obtaining Testnet ETH via CDP Faucet
通过CDP水龙头获取测试网ETH
Testnet ETH is required to pay gas on Base Sepolia. Use the CDP Faucet to claim it. Supported tokens: ETH, USDC, EURC, cbBTC. ETH claims are capped at 0.0001 ETH per claim, 1000 claims per 24 hours.
在Base Sepolia测试网部署需要测试网ETH来支付Gas费用。使用CDP水龙头申领。支持的代币:ETH、USDC、EURC、cbBTC。ETH申领上限为每次0.0001 ETH,24小时内最多1000次。
Option A: CDP Portal UI (recommended for quick setup)
选项A:CDP门户UI(推荐用于快速设置)
Agent behavior: If you have browser access, navigate to the portal and claim directly. Otherwise, ask the user to complete these steps and provide the funded wallet address.
- Sign in to CDP Portal (create an account at portal.cdp.coinbase.com/create-account if needed)
- Go to Faucets
- Select Base Sepolia network
- Select ETH token
- Enter the wallet address and click Claim
- Verify on sepolia.basescan.org that the funds arrived
Agent操作说明:如果有浏览器访问权限,直接导航至门户并申领。否则,请用户完成以下步骤并提供已充值的钱包地址。
- 登录CDP门户(如需账户,请前往portal.cdp.coinbase.com/create-account创建)
- 进入水龙头页面
- 选择Base Sepolia网络
- 选择ETH代币
- 输入钱包地址并点击申领
- 在sepolia.basescan.org上验证资金是否到账
Option B: Programmatic via CDP SDK
选项B:通过CDP SDK以编程方式获取
Requires a CDP API key and Wallet Secret.
bash
npm install @coinbase/cdp-sdk dotenvtypescript
import { CdpClient } from "@coinbase/cdp-sdk";
import dotenv from "dotenv";
dotenv.config();
const cdp = new CdpClient();
const account = await cdp.evm.createAccount();
const faucetResponse = await cdp.evm.requestFaucet({
address: account.address,
network: "base-sepolia",
token: "eth",
});
console.log(`Funded: https://sepolia.basescan.org/tx/${faucetResponse.transactionHash}`);Environment variables needed in :
.envCDP_API_KEY_ID=your-api-key-id
CDP_API_KEY_SECRET=your-api-key-secret
CDP_WALLET_SECRET=your-wallet-secretTo fund an existing wallet instead of creating a new one, pass its address directly to .
requestFaucetbash
npm install @coinbase/cdp-sdk dotenvtypescript
import { CdpClient } from "@coinbase/cdp-sdk";
import dotenv from "dotenv";
dotenv.config();
const cdp = new CdpClient();
const account = await cdp.evm.createAccount();
const faucetResponse = await cdp.evm.requestFaucet({
address: account.address,
network: "base-sepolia",
token: "eth",
});
console.log(`充值完成:https://sepolia.basescan.org/tx/${faucetResponse.transactionHash}`);.env文件中需要配置的环境变量:
CDP_API_KEY_ID=your-api-key-id
CDP_API_KEY_SECRET=your-api-key-secret
CDP_WALLET_SECRET=your-wallet-secret如果要为现有钱包充值而非创建新钱包,直接将其地址传入即可。
requestFaucetObtaining a BaseScan API Key
获取BaseScan API密钥
A BaseScan API key is required for the flag to auto-verify contracts on BaseScan. BaseScan uses the same account system as Etherscan.
--verifyAgent behavior: If you have browser access, navigate to the BaseScan site and create the key. Otherwise, ask the user to complete these steps and provide the API key.
- Go to basescan.org/myapikey (or etherscan.io/myapikey — same account works)
- Sign in or create a free account
- Click Add to create a new API key
- Copy the key and set it in your environment:
bash
export ETHERSCAN_API_KEY=your-basescan-api-keyAlternatively, pass it directly to forge:
bash
forge create ... --etherscan-api-key <your-key>Or add it to :
foundry.tomltoml
[etherscan]
base-sepolia = { key = "${ETHERSCAN_API_KEY}", url = "https://api-sepolia.basescan.org/api" }
base = { key = "${ETHERSCAN_API_KEY}", url = "https://api.basescan.org/api" }使用标志在BaseScan上自动验证合约需要BaseScan API密钥。BaseScan使用与Etherscan相同的账户系统。
--verifyAgent操作说明:如果有浏览器访问权限,直接导航至BaseScan网站创建密钥。否则,请用户完成以下步骤并提供API密钥。
- 前往basescan.org/myapikey(或etherscan.io/myapikey——同一账户通用)
- 登录或创建免费账户
- 点击Add创建新的API密钥
- 复制密钥并在环境中设置:
bash
export ETHERSCAN_API_KEY=your-basescan-api-key或者直接将其传入forge命令:
bash
forge create ... --etherscan-api-key <your-key>或者将其添加至:
foundry.tomltoml
[etherscan]
base-sepolia = { key = "${ETHERSCAN_API_KEY}", url = "https://api-sepolia.basescan.org/api" }
base = { key = "${ETHERSCAN_API_KEY}", url = "https://api.basescan.org/api" }Deployment Commands
部署命令
Testnet
测试网
bash
forge create src/MyContract.sol:MyContract \
--rpc-url https://sepolia.base.org \
--account <keystore-account> \
--verify \
--etherscan-api-key $ETHERSCAN_API_KEYbash
forge create src/MyContract.sol:MyContract \
--rpc-url https://sepolia.base.org \
--account <keystore-account> \
--verify \
--etherscan-api-key $ETHERSCAN_API_KEYMainnet
主网
bash
forge create src/MyContract.sol:MyContract \
--rpc-url https://mainnet.base.org \
--account <keystore-account> \
--verify \
--etherscan-api-key $ETHERSCAN_API_KEYbash
forge create src/MyContract.sol:MyContract \
--rpc-url https://mainnet.base.org \
--account <keystore-account> \
--verify \
--etherscan-api-key $ETHERSCAN_API_KEYKey Notes
关键说明
- Contract format:
<contract-path>:<contract-name> - flag auto-verifies on BaseScan (requires API key)
--verify - Explorers: basescan.org (mainnet), sepolia.basescan.org (testnet)
- CDP Faucet docs: docs.cdp.coinbase.com/faucets
- 合约格式:
<contract-path>:<contract-name> - 标志可在BaseScan上自动验证合约(需要API密钥)
--verify - 区块浏览器:basescan.org(主网)、sepolia.basescan.org(测试网)
- CDP水龙头文档:docs.cdp.coinbase.com/faucets
Common Issues
常见问题
| Error | Cause |
|---|---|
| Node sync incomplete |
| Transaction fails | Insufficient ETH for gas — claim from faucet |
| Verification fails | Wrong RPC endpoint for target network |
| Verification 403/unauthorized | Missing or invalid BaseScan API key |
| 错误 | 原因 |
|---|---|
| 节点同步不完整 |
| 交易失败 | Gas费用不足——从水龙头申领ETH |
| 验证失败 | 目标网络的RPC端点错误 |
| 验证返回403/未授权 | 缺少或无效的BaseScan API密钥 |