deploying-contracts-on-base

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Deploying Contracts on Base

在Base网络部署合约

Prerequisites

前置条件

  1. Configure RPC endpoint (testnet:
    sepolia.base.org
    , mainnet:
    mainnet.base.org
    )
  2. Store private keys in Foundry's encrypted keystore — never commit keys
  3. Obtain testnet ETH from CDP faucet (testnet only)
  4. Get a BaseScan API key for contract verification
  1. 配置RPC端点(测试网:
    sepolia.base.org
    ,主网:
    mainnet.base.org
  2. 将私钥存储在Foundry的加密密钥库中——绝对不要提交密钥
  3. 从CDP水龙头获取测试网ETH(仅测试网)
  4. 获取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
    foundry.toml
    with
    ${ENV_VAR}
    references
  • Never expose
    .env
    files
    — add
    .env
    to
    .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
    ^[a-zA-Z0-9_/.-]+\.sol:[a-zA-Z0-9_]+$
    . Reject paths with spaces, semicolons, pipes, or backticks.
  • rpc-url: Must be a valid HTTPS URL (
    ^https://[^\s;|&]+$
    ). Reject non-HTTPS or malformed URLs.
  • 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://[^\s;|&]+$
    )。拒绝非HTTPS或格式错误的URL。
  • 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.
  1. Sign in to CDP Portal (create an account at portal.cdp.coinbase.com/create-account if needed)
  2. Go to Faucets
  3. Select Base Sepolia network
  4. Select ETH token
  5. Enter the wallet address and click Claim
  6. Verify on sepolia.basescan.org that the funds arrived
Agent操作说明:如果有浏览器访问权限,直接导航至门户并申领。否则,请用户完成以下步骤并提供已充值的钱包地址。
  1. 登录CDP门户(如需账户,请前往portal.cdp.coinbase.com/create-account创建)
  2. 进入水龙头页面
  3. 选择Base Sepolia网络
  4. 选择ETH代币
  5. 输入钱包地址并点击申领
  6. 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 dotenv
typescript
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
.env
:
CDP_API_KEY_ID=your-api-key-id
CDP_API_KEY_SECRET=your-api-key-secret
CDP_WALLET_SECRET=your-wallet-secret
To fund an existing wallet instead of creating a new one, pass its address directly to
requestFaucet
.
bash
npm install @coinbase/cdp-sdk dotenv
typescript
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
如果要为现有钱包充值而非创建新钱包,直接将其地址传入
requestFaucet
即可。

Obtaining a BaseScan API Key

获取BaseScan API密钥

A BaseScan API key is required for the
--verify
flag to auto-verify contracts on BaseScan. BaseScan uses the same account system as Etherscan.
Agent 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.
  1. Go to basescan.org/myapikey (or etherscan.io/myapikey — same account works)
  2. Sign in or create a free account
  3. Click Add to create a new API key
  4. Copy the key and set it in your environment:
bash
export ETHERSCAN_API_KEY=your-basescan-api-key
Alternatively, pass it directly to forge:
bash
forge create ... --etherscan-api-key <your-key>
Or add it to
foundry.toml
:
toml
[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" }
使用
--verify
标志在BaseScan上自动验证合约需要BaseScan API密钥。BaseScan使用与Etherscan相同的账户系统。
Agent操作说明:如果有浏览器访问权限,直接导航至BaseScan网站创建密钥。否则,请用户完成以下步骤并提供API密钥。
  1. 前往basescan.org/myapikey(或etherscan.io/myapikey——同一账户通用)
  2. 登录或创建免费账户
  3. 点击Add创建新的API密钥
  4. 复制密钥并在环境中设置:
bash
export ETHERSCAN_API_KEY=your-basescan-api-key
或者直接将其传入forge命令:
bash
forge create ... --etherscan-api-key <your-key>
或者将其添加至
foundry.toml
toml
[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_KEY
bash
forge create src/MyContract.sol:MyContract \
  --rpc-url https://sepolia.base.org \
  --account <keystore-account> \
  --verify \
  --etherscan-api-key $ETHERSCAN_API_KEY

Mainnet

主网

bash
forge create src/MyContract.sol:MyContract \
  --rpc-url https://mainnet.base.org \
  --account <keystore-account> \
  --verify \
  --etherscan-api-key $ETHERSCAN_API_KEY
bash
forge create src/MyContract.sol:MyContract \
  --rpc-url https://mainnet.base.org \
  --account <keystore-account> \
  --verify \
  --etherscan-api-key $ETHERSCAN_API_KEY

Key Notes

关键说明

  • Contract format:
    <contract-path>:<contract-name>
  • --verify
    flag auto-verifies on BaseScan (requires API key)
  • Explorers: basescan.org (mainnet), sepolia.basescan.org (testnet)
  • CDP Faucet docs: docs.cdp.coinbase.com/faucets
  • 合约格式:
    <contract-path>:<contract-name>
  • --verify
    标志可在BaseScan上自动验证合约(需要API密钥)
  • 区块浏览器:basescan.org(主网)、sepolia.basescan.org(测试网)
  • CDP水龙头文档:docs.cdp.coinbase.com/faucets

Common Issues

常见问题

ErrorCause
nonce has already been used
Node sync incomplete
Transaction failsInsufficient ETH for gas — claim from faucet
Verification failsWrong RPC endpoint for target network
Verification 403/unauthorizedMissing or invalid BaseScan API key
错误原因
nonce has already been used
节点同步不完整
交易失败Gas费用不足——从水龙头申领ETH
验证失败目标网络的RPC端点错误
验证返回403/未授权缺少或无效的BaseScan API密钥