Skip to main content

使用 Foundry 在 Gate Layer 测试网开发与部署合约

Gate Layer 完全兼容 EVM,您可以使用 Foundry 高效进行开发、测试与部署。本教程将带您在 Gate Layer 测试网完成从初始化到部署、验证与交互的完整流程。

准备工作

  • 操作系统:Linux / macOS / Windows(WSL)
  • 钱包账户:本地持有私钥的钱包
  • 测试网代币:确保钱包在 Gate Layer 测试网有少量 GT 以支付 Gas(可从 Gate Web3 水龙头领取:https://www.gate.com/zh/web3/faucet
  • RPC(测试网):https://gatelayer-testnet.gatenode.cc

安装 Foundry

curl -L https://foundry.paradigm.xyz | bash
foundryup
forge --version

初始化项目

forge init hello-world
cd hello-world

# 如存在模板示例,可按需删除
rm -f src/Counter.sol script/Counter.s.sol test/Counter.t.sol

编写合约

创建 src/HelloWorld.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract HelloWorld {
string public message;

constructor(string memory _message) {
message = _message;
}

function setMessage(string memory _newMessage) public {
message = _newMessage;
}

function getMessage() public view returns (string memory) {
return message;
}
}

该合约包含 message 变量、构造器与读写方法(getMessage / setMessage)。

编写并运行测试

创建 test/HelloWorld.t.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "forge-std/Test.sol";
import "../src/HelloWorld.sol";

contract HelloWorldTest is Test {
HelloWorld hello;

function setUp() public {
hello = new HelloWorld("Hello Foundry!");
}

function testInitialMessage() public {
assertEq(hello.getMessage(), "Hello Foundry!");
}

function testSetMessage() public {
hello.setMessage("GM Ethereum");
assertEq(hello.getMessage(), "GM Ethereum");
}
}

运行测试(应全部通过):

forge test -vvv

部署脚本

创建 script/DeployHelloWorld.s.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "forge-std/Script.sol";
import "../src/HelloWorld.sol";

contract DeployHelloWorld is Script {
function run() external {
// 直接写死私钥(用于部署到具体链)
// 请注意这是一个 demo,不要泄露您任何的真实使用的私钥
uint256 deployerPrivateKey = 0xYOUR_PRIVATE_KEY;

vm.startBroadcast(deployerPrivateKey);

HelloWorld hello = new HelloWorld("Hello Gate Layer!");
console.log("HelloWorld deployed at:", address(hello));

vm.stopBroadcast();
}
}

请注意更换其中私钥部分。

部署合约

forge script script/DeployHelloWorld.s.sol \
--rpc-url https://gatelayer-testnet.gatenode.cc\
--broadcast

该指令会将合约部署到 Gate Layer 的测试网 输出示例:

HelloWorld deployed at: 0xAbC1234... (实际合约地址)

验证合约

您可以使用 cast 来验证您部署的合约

cast call <部署后的合约地址> "getMessage()(string)" --rpc-url https://gatelayer-testnet.gatenode.cc

应输出:

"Hello Gate Layer!"

恭喜您,您的合约已经成功部署!
我们可以调用方法来修改合约的数据,需要注意的是,这一步需要真实发送交易上链,因此需要您填入私钥
```shell
cast send <合约地址> "setMessage(string)" "GM Ethereum" --rpc-url https://gatelayer-testnet.gatenode.cc --private-key 0xYOUR_PRIVATE_KEY

此命令会真实发送交易上链 输出类似以下的结果(数据均为mock)

blockHash            0x6d971b896bc3207663a413e7237c933609fb341592abb836dc231b7be00c0011
blockNumber 5970581
contractAddress
cumulativeGasUsed 74282
effectiveGasPrice 120000001
from <您的钱包地址>
gasUsed 28126
logs []
logsBloom 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root
status 1 (success)
transactionHash 0xa60ebe127a32e78ff49adf56d4dce02a31d77eddeded8f0ac8fa498a08cc7ea5
transactionIndex 1
type 2
blobGasPrice
blobGasUsed
to <合约地址>
l1BaseFeeScalar 113016
l1BlobBaseFee 1
l1BlobBaseFeeScalar 801949
l1Fee 1808256000080
l1GasPrice 10000000000
l1GasUsed 1600

这个时候我们在用 cast 验证合约

cast call <部署后的合约地址> "getMessage()(string)" --rpc-url https://gatelayer-testnet.gatenode.cc

应输出:

"GM Ethereum"

结语

至此,您已使用 Foundry 在 Gate Layer 测试网完成合约的开发、测试、部署与交互。更多示例可继续拓展脚本与测试用例,或集成前端进行 DApp 开发。

最后更新于2025/11/19