使用Foundry部署和开源合约erc20
将给定的合约部署到 Sepolia 并提供部署后的链接地址,你可以按照以下步骤进行操作。
新建项目
forge init foundry_erc20
在文件夹src
中创建一个新的 Solidity 文件 MyToken.sol
,内容如下:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_) {
_mint(msg.sender, 1e10 * 1e18);
}
}
右键单击鼠标,选择图下所示,然后点击对应的编译器的版本
确保在项目根目录下有一个 foundry.toml
文件,内容如下:
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
solc_version = "0.8.25"
然后运行命令
forge fix
然后在项目根目录下安装 OpenZeppelin 合约库:
forge install OpenZeppelin/openzeppelin-contracts
如果克隆失败,可以采用浅克隆
git clone --depth 1 https://github.com/OpenZeppelin/openzeppelin-contracts.git foundry_erc20/lib/openzeppelin-contracts
下载后更改文件的import 路径
import "lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
在 script
目录下创建一个新的部署脚本 DeployMyToken.s.sol
,内容如下:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;
import "forge-std/Script.sol";
import "../src/MyToken.sol";
contract DeployMyToken is Script {
function run() external {
string memory name = "MyToken";
string memory symbol = "MTK";
vm.startBroadcast();
new MyToken(name, symbol);
vm.stopBroadcast();
}
}
确保你已配置好环境变量 ETHERSCAN_API_KEY
和 PRIVATE_KEY
。可以在新建 .env
文件中设置这些变量,然后使用 source
命令加载它们:
echo "ETHERSCAN_API_KEY=your_etherscan_api_key" >> .env
echo "PRIVATE_KEY=your_private_key" >> .env
source .env
一个完整的部署命令示例,包括密钥的使用:
forge script script/DeployMyToken.s.sol --rpc-url ${RPC_URL} --broadcast --verify -vvvv --private-key ${YOUR_PRIVATE_KEY}
这里解释一下各个参数的含义:
forge script script/DeployMyToken.s.sol
: 这是 Forge 工具的命令,用于执行 Solidity 合约的部署脚本。--rpc-url ${RPC_URL}
: 指定你要连接的以太坊网络的 RPC URL。--broadcast
: 在部署完成后,自动广播交易以提交到区块链。--verify
: 部署后验证合约是否成功,包括检查合约地址是否有效。-vvvv
: 输出详细的调试信息,有助于查看部署过程中的详细步骤和可能的错误。--private-key ${YOUR_PRIVATE_KEY}
: 指定用于签署交易的私钥。确保替换 ${YOUR_PRIVATE_KEY}
为你的实际私钥值。运行以下命令进行编译和部署:
forge script script/DeployMyToken.s.sol --rpc-url ${RPC_URL} --broadcast --verify -vvvv --private-key ${PRIVATE_KEY}
输出结果为:
yhb@yhbdeMacBook-Air foundry_erc20 % forge script script/DeployMyToken.s.sol --rpc-url ${RPC_URL} --broadcast --verify -vvvv --private-key ${PRIVATE_KEY}
[⠊] Compiling...
No files changed, compilation skipped
Traces:
[494734] DeployMyToken::run()
├─ [0] VM::startBroadcast()
│ └─ ← [Return]
├─ [458012] → new MyToken@0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A
│ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0x531247BbA4d32ED9D870bc3aBe71A2B9ce911e69, 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@0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A
├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0x531247BbA4d32ED9D870bc3aBe71A2B9ce911e69, value: 10000000000000000000000000000 [1e28])
└─ ← [Return] 1825 bytes of code
==========================
Chain 11155111
Estimated gas price: 1542.607338374 gwei
Estimated total gas used for script: 725597
Estimated amount required: 1.119311256902159278 ETH
==========================
##### sepolia
✅ [Success]Hash: 0x886bd1fd28d0ebe765e412070626302fcd1b23175f6cefb0f1ba33d44e8a895a
Contract Address: 0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A
Block: 6289822
Paid: 0.4439682875506593 ETH (558350 gas * 795.143346558 gwei)
✅ Sequence #1 on sepolia | Total Paid: 0.4439682875506593 ETH (558350 gas * avg 795.143346558 gwei)
==========================
ONCHAIN EXECUTION COMPLETE & SUCCESSFUL.
##
Start verification for (1) contracts
Start verifying contract `0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A` deployed on sepolia
Submitting verification for [src/MyToken.sol:MyToken] 0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A.
Submitting verification for [src/MyToken.sol:MyToken] 0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A.
Submitting verification for [src/MyToken.sol:MyToken] 0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A.
Submitted contract for verification:
Response: `OK`
GUID: `46vydse7tlzpapnamqrrrxgfniedqhapuvsmg4stmtfmqu8xrd`
URL: https://sepolia.etherscan.io/address/0x3d3e70de5c8165c91ce240a91fc49ded986eb09a
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/yhb/foundry_erc20/broadcast/DeployMyToken.s.sol/11155111/run-latest.json
Sensitive values saved to: /Users/yhb/foundry_erc20/cache/DeployMyToken.s.sol/11155111/run-latest.json
成功部署了名为 MyToken
的合约到 Sepolia 测试网络,并且已经成功地在 Etherscan 上验证了合约的代码。让我详细解释一下输出中的关键部分:
new MyToken@0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A
:这表示成功部署了一个名为 MyToken
的合约到地址 0x3d3e70De5c8165C91ce240A91fC49deD986Eb09A
。emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0x531247BbA4d32ED9D870bc3aBe71A2B9ce911e69, value: 10000000000000000000000000000 [1e28])
:这是一个事件,表示在部署过程中向地址 0x531247BbA4d32ED9D870bc3aBe71A2B9ce911e69
发送了 10000000000000000000000000000 个单位的 MyToken
。1542.607338374 gwei
,总计使用了 725597
gas。1.119311256902159278 ETH
。0.4439682875506593 ETH
,这是根据使用的 gas 和 gas 价格计算出来的。如果一切顺利,部署脚本将输出合约的部署地址。然后你可以在 Sepolia Etherscan 上查看合约详情。
将部署地址替换到以下链接中并分享:
https://sepolia.etherscan.io/address/0x3d3e70de5c8165c91ce240a91fc49ded986eb09a
通过这些步骤,你应该能够成功将合约部署到 Sepolia 并提供验证后的链接地址。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!