使用 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 端點:您可以從其中一個支援的 端點提供者 取得。
- 從 Faucet測試 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
檔案。 您應該已經在 Scaffold 之後的專案根目錄中有一個。
在檔案末尾加入以下幾行:
[rpc_endpoints]kairos = "${KAIROS_RPC_URL}"
這會為 Kaia Kairos Testnet 建立 RPC alias。
匯入帳戶
在本指南中,我們將匯入一個已存在於 MetaMask 上的開發者帳號,這樣就可以透過--帳號
選項在forge script
、cast send
或其他需要私密金鑰的方法中存取該帳號。
執行以下指令匯入現有的錢包:
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 建立帳號。
在本指南中,我們將使用 foundry 提供的兩種合約部署方法:
使用 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 密碼:<KEYSTORE_PASSWORD>
對於開發環境中超出基本測試網路使用範圍的任何部署,強烈建議使用 硬體錢包或密碼保護的 keystore 以增強安全性。
步驟 2:開啟 Kaiascan 檢查計數器合約是否部署成功。
Step 3: Copy and paste the transaction hash in the search field and press Enter. You should see the recently deployed contract.