使用 Foundry 部署智能合约
导言
Foundry 是一个用 Rust 编写的智能合约开发框架,开发人员可以通过 solidity 脚本从命令行管理和编译合约、运行测试、部署合约并与网络交互。
Foundry 由四个主要 CLI 工具组成,可实现快速、模块化的智能合约开发,它们是
- Forge: 您可以使用 Forge 部署、测试和编译智能合约。
- Cast:Cast 使与 EVM 智能合约的交互变得简单。 这包括获取链数据、发送交易等。
- Anvil:您需要启动本地节点吗? Anvil 是 Foundry 提供的本地节点环境。
- Chisel:快速、实用、冗长的 solidity REPL。
在本指南中,您将
- 创建一个简单的铸造项目。
- 使用 Foundry 编译和测试示例智能合约。
- 使用 Foundry 向 Kaia Kairos 网络部署智能合约。
- 探索使用铸铁和铁砧分叉主网。
先决条件
学习本教程的前提条件如下 :
- 代码编辑器:源代码编辑器,如 VS Code。
- MetaMask:用于部署合同、签署交易和与合同交互。
- RPC 端点:可从受支持的 端点提供程序 中获取。
- 从 水龙头测试 KAIA:为账户注入足够的 KAIA。
- 安装 Rust 和 Foundry。
设置开发环境
要检查 foundry 安装是否成功,请运行下面的命令:
forge -V
输出
成功安装 Foundry 后,您现在可以使用 Foundry 中的 CLI 工具(锻造、铸造、铁砧、凿子)。 让我们按以下步骤建立一个代工厂项目:
步骤 1:要启动一个新项目,请运行以下命令:
forge init foundry_example
第 2 步:进入项目文件夹。
cd foundry_example
初始化 foundry 项目后,当前目录应包括
- src:智能合约的默认目录。
- 测试:测试的默认目录。
- foundry.toml:默认项目配置文件。
- lib:项目依赖项的默认目录。
- script:solidity 脚本文件的默认目录。
配置 foundry.toml
现在我们的项目已经建立,我们必须创建一个 .env
文件并添加变量。 Foundry 会自动加载项目目录中的 .env 文件。
.env 文件应遵循以下格式:
kairos_rpc_url=paste_rpc_url
接下来是编辑 foundry.toml
文件。 在脚手架之后的项目根目录中应该已经有一个了。
在文件末尾添加以下几行:
[rpc_endpoints]kairos = "${KAIROS_RPC_URL}"
这将为 Kaia Kairos Testnet 创建一个 RPC 别名。
导入账户
在本指南中,我们将导入一个已存在于 MetaMask 上的开发者账户,这样就可以在 "锻造脚本"、"广播发送 "或其他需要私钥的方法中通过"--账户 "选项访问该账户。
运行以下命令导入现有钱包:
cast wallet import --interactive oxpampam-dev-i
输入私人密钥:输入密码
Sample smart contract
In this section, we will be using the sample counter contract in the initialized foundry project. The counter.sol
file in the src/
folder should look like this:
// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.13;contract Counter { uint256 public number; function setNumber(uint256 newNumber) public { number = newNumber; } function increment() public { number++; }}
Code Walkthrough
This is your smart contract. Line 1 shows it uses the Solidity version 0.8.13 or greater. From lines 4-12, a smart contract Counter
is created. This contract simply stores a new number using the setNumber function and increments it by calling the increment function.
Testing smart contract
Foundry allows us to write tests in solidity as opposed to writing tests in javascript in other smart contract development frameworks. In our initialized foundry project, the test/Counter.t.sol
is an example of a test written in solidity. The code looks like this:
// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.13;import "forge-std/Test.sol";import "../src/Counter.sol";contract CounterTest is Test { Counter public counter; function setUp() public { counter = new Counter(); counter.setNumber(0); } function testIncrement() public { counter.increment(); assertEq(counter.number(), 1); } function testSetNumber(uint256 x) public { counter.setNumber(x); assertEq(counter.number(), x); }}
The code above shows you imported forge standard library and Counter.sol.
The tests above check the following:
- Is the number increasing?
- Is the number equal to the set number?
To check if your test works fine, run the following command:
forge test
Output
To learn more about writing tests, advanced testing, and other features, refer to Foundry's documentation.
Compiling your contracts
Compile your contract with this command:
forge build
Deploying your contracts
To deploy a contract using foundry, you must provide an RPC URL and a private key of the account that will deploy the contract. 请查看 Kaia 上的 rpc-providers 列表,找到您的 rpc-url,并使用 MetaMask 创建一个账户。
在本指南中,我们将使用代工厂提供的两种合同部署方法:
使用 Forge Create
步骤 1:要使用 forge create 将合约部署到 Kaia Kairos 网络,请运行以下命令:
# 在 .env 文件中加载变量source .env# 部署我们的合约forge create --rpc-url $KAIROS_RPC_URL src/Counter.sol:Counter --broadcast --account oxpampam-dev-i
输入密钥库密码:<KEYSTORE_PASSWORD>
对于任何超出开发环境中基本测试网络使用范围的部署,强烈建议使用硬件钱包或受密码保护的密钥库 以增强安全性。
第 2 步:打开 Kaiascan 查看计数器合约是否部署成功。
Step 3: Copy and paste the transaction hash in the search field and press Enter. You should see the recently deployed contract.