Foundry 工程中的合约部署和验证教程

  • Taylor
  • 更新于 2024-07-13 00:27
  • 阅读 904

使用founrdy keystore保护你的钱包私钥

使用bash脚本来部署任意合约

目录

  1. 导入钱包私钥到 keystore
  2. 编写合约和部署脚本
  3. 配置环境变量
  4. 编写 Bash 部署脚本
  5. 运行部署脚本
  6. 验证合约
  7. 总结

1-导入钱包私钥到-keystore

使用cast wallet import把你的钱包私钥导入到keystore

$ cast wallet import Metamask -i
Enter private key:
Enter password: 
`Metamask` keystore was saved successfully. Address: 0xe844f618f2c47aa2bab373a0b46ce6a2ce427ed9

$ cast wallet list
Metamask (Local)

$ cat ~/.foundry/keystores/Metamask 
{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"5e0e2865628deaeb4536cb1baa841c95"},"ciphertext":"afaa88a316b2dcfeeee87c3a701729c2f5aa98ef259e6c72c0582644b8539ef4","kdf":"scrypt","kdfparams":{"dklen":32,"n":8192,"p":1,"r":8,"salt":"8370cd9bf48c7e1f3efd535abcf81b974235532bb8d652bada982e3c73a31bdd"},"mac":"77bda41cac19695c29c604c645882df20659a3df48b9dfee33488aa876553c70"},"id":"33f5ac31-1837-41f7-a15c-98d03436cc98","version":3}%

可以看到上面已经生成了一个wallet存到的keystore里, 内容都是被加密过的。

2-编写合约和部署脚本

foundry工程中编写合约和script

// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "forge-std/Script.sol";

contract MyToken is ERC20 {
    constructor(
        string memory name_,
        string memory symbol_
    ) ERC20(name_, symbol_) {
        _mint(msg.sender, 1e10 * 1e18);
    }
}

contract DeployMyToken is Script {
    function run() external {
        vm.startBroadcast();

        // deploy contract
        MyToken myToken = new MyToken("MyToken", "MTK");

        vm.stopBroadcast();
    }
}

3-配置环境变量

在foundry工程中准备.env来配置必要的部署参数,请自行填写对应的参数

MAINNET_FORK_URL=
OPT_FORK_URL=
SEPOLIA_RPC_URL=<YOUR_ETHERSCAN_API_KEY>
DEV_PRIVATE_KEY=0x123
ETHERSCAN_API_KEY=<YOUR_ETHERSCAN_API_KEY>
CHAIN_ID=11155111

4-编写-bash-部署脚本

在foundry工程中准备编写bash部署脚本

#!/bin/bash

# effect the env vars
source .env

# get bash arg
while [[ "$#" -gt 0 ]]; do
  case $1 in
    --file) SCRIPT_FILE="$2"; shift ;;
    --account) ACCOUNT="$2"; shift ;;
    *) echo "unknown arg: $1" ; exit 1 ;;
  esac
  shift
done

# make sure the script file was provided
if [[ -z "$SCRIPT_FILE" ]]; then
  echo "Please specify --file <your_script_path>"
  exit 1
fi

if [[ -z "$ACCOUNT" ]]; then
  echo "Please specify --account <your_cast_wallet_account>"
  exit 1
fi

# check if the env vars was defined preceedly
if [[ -z "$SEPOLIA_RPC_URL" || -z "$ETHERSCAN_API_KEY" || -z "$CHAIN_ID" ]]; then
  echo "Please ensure .env defines the vars:SEPOLIA_RPC_URL, ETHERSCAN_API_KEY, CHAIN_ID"
  exit 1
fi

if [[ -z "$PRIVATE_KEY" ]]; then
  echo "Can not load the private key from keystore..."
  exit 1
fi

# deploy
forge script "$SCRIPT_FILE" \
  --rpc-url "$SEPOLIA_RPC_URL" \
  --broadcast \
  --verify \
  --etherscan-api-key "$ETHERSCAN_API_KEY" \
  --account $ACCOUNT \
  -vvvv

5-运行部署脚本

使用编写好的bash脚本

  • --file 参数指定要部署的文件 例如 -file script/DeployMyToken.s.sol:DeployMyToken
  • --account 参数指定你用cast wallet import 的 account
  • 在部署的过程中会问询你输入创建account时的密码
bash ./script/deploy.sh --file script/DeployMyToken.s.sol:DeployMyToken --account Metamask

Please input cast wallet account Metamask keystore password:

[⠒] Compiling...
[⠒] Compiling 1 files with Solc 0.8.25
[⠢] Solc 0.8.25 finished in 1.97s
Compiler run successful with warnings:
Warning (2072): Unused local variable.
  --> script/DeployMyToken.s.sol:24:9:
   |
24 |         MyToken myToken = new MyToken("MyToken", "MTK");
   |         ^^^^^^^^^^^^^^^

Enter keystore password:
Traces:
  [494159] DeployMyToken::run()
    ├─ [0] VM::startBroadcast()
    │   └─ ← [Return] 
    ├─ [458012] → new MyToken@0x69C7b902d29bE3B516F42Fba4E919f4C17A310fD
    │   ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0xE844F618f2c47aA2BAb373a0b46ce6a2ce427eD9, value: 10000000000000000000000000000 [1e28])
    │   └─ ← [Return] 1825 bytes of code
    ├─ [0] VM::stopBroadcast()
    │   └─ ← [Return] 
    └─ ← [Stop] 

Script ran successfully.

## Setting up 1 EVM.
==========================
Simulated On-chain Traces:

  [458012] → new MyToken@0x69C7b902d29bE3B516F42Fba4E919f4C17A310fD
    ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0xE844F618f2c47aA2BAb373a0b46ce6a2ce427eD9, value: 10000000000000000000000000000 [1e28])
    └─ ← [Return] 1825 bytes of code

==========================

Chain 11155111

Estimated gas price: 13.158315272 gwei

Estimated total gas used for script: 725597

Estimated amount required: 0.009547634086417384 ETH

==========================
##
Sending transactions [0 - 0].
⠁ [00:00:00] [###############################################################################################] 1/1 txes (0.0s)##
Waiting for receipts.
⠉ [00:00:21] [###########################################################################################] 1/1 receipts (0.0s)
##### sepolia
✅  [Success]Hash: 0xb814b28778d277f3c8b81a4f491c7dfcd33a96eaaff4f35ec13665d684eee79e
Contract Address: 0x69C7b902d29bE3B516F42Fba4E919f4C17A310fD
Block: 6297189
Paid: 0.00390782089756305 ETH (558350 gas * 6.998873283 gwei)

==========================

ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
Total Paid: 0.00390782089756305 ETH (558350 gas * avg 6.998873283 gwei)
##
Start verification for (1) contracts
Start verifying contract `0x69C7b902d29bE3B516F42Fba4E919f4C17A310fD` deployed on sepolia

Submitting verification for [script/DeployMyToken.s.sol:MyToken] 0x69C7b902d29bE3B516F42Fba4E919f4C17A310fD.

Submitting verification for [script/DeployMyToken.s.sol:MyToken] 0x69C7b902d29bE3B516F42Fba4E919f4C17A310fD.

Submitting verification for [script/DeployMyToken.s.sol:MyToken] 0x69C7b902d29bE3B516F42Fba4E919f4C17A310fD.
Submitted contract for verification:
        Response: `OK`
        GUID: `f8vuzmbhgyek3dzixgv4uzj2675ajdzikyprg8swkkfxbcz7cr`
        URL: https://sepolia.etherscan.io/address/0x69c7b902d29be3b516f42fba4e919f4c17a310fd
Contract verification status:
Response: `NOTOK`
Details: `Pending in queue`
Contract verification status:
Response: `OK`
Details: `Pass - Verified`
Contract successfully verified
All (1) contracts were verified!

Transactions saved to: /Users/taylor/Documents/Developer/web3-src/hello_foundry/broadcast/DeployMyToken.s.sol/11155111/run-latest.json

Sensitive values saved to: /Users/taylor/Documents/Developer/web3-src/hello_foundry/cache/DeployMyToken.s.sol/11155111/run-latest.json

6-验证合约

111.png

本次教程成功部署并验证的合约地址: https://sepolia.etherscan.io/address/0x69c7b902d29be3b516f42fba4e919f4c17a310fd

7-总结

本次教程所有代码记录在我个人github的repo中:https://github.com/TaylorDurden/hello_foundry/pull/2

点赞 1
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
Taylor
Taylor
全干工程师,有工作机会请联系微信号:taylorlee1991