Develop and Deploy a Contract on Gate Layer Testnet with Foundry
Gate Layer is fully EVM-compatible. With Foundry, you can efficiently develop, test, and deploy smart contracts. This guide walks you through initialization, deployment, verification, and interaction on the Gate Layer Testnet.
Prerequisites
- OS: Linux / macOS / Windows (WSL)
- Wallet account: a locally controlled private key
- Testnet funds: a small amount of GT on Gate Layer Testnet for gas (faucet:
https://www.gate.com/web3/faucet) - RPC (Testnet):
https://gatelayer-testnet.gatenode.cc
Install Foundry
curl -L https://foundry.paradigm.xyz | bash
foundryup
forge --version
Initialize project
forge init hello-world
cd hello-world
# Remove template files if present
rm -f src/Counter.sol script/Counter.s.sol test/Counter.t.sol
Write the contract
Create 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;
}
}
The contract includes a message variable with a constructor and simple read/write functions (getMessage / setMessage).
Write and run tests
Create 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");
}
}
Run tests:
forge test -vvv
Deployment script
Create 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 {
// Use env var for private key to avoid hardcoding
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);
HelloWorld hello = new HelloWorld("Hello Gate Layer!");
vm.stopBroadcast();
}
}
Deploy to Gate Layer Testnet
export PRIVATE_KEY=0xYOUR_PRIVATE_KEY
forge script script/DeployHelloWorld.s.sol \
--rpc-url https://gatelayer-testnet.gatenode.cc \
--broadcast
This will deploy your contract to Gate Layer Testnet.
Verify and interact
Read contract state:
cast call <DEPLOYED_CONTRACT_ADDRESS> "getMessage()(string)" \
--rpc-url https://gatelayer-testnet.gatenode.cc
Expected output:
"Hello Gate Layer!"
Update state (sends a transaction, requires private key):
cast send <CONTRACT_ADDRESS> "setMessage(string)" "GM Ethereum" \
--rpc-url https://gatelayer-testnet.gatenode.cc \
--private-key $PRIVATE_KEY
Read again:
cast call <CONTRACT_ADDRESS> "getMessage()(string)" \
--rpc-url https://gatelayer-testnet.gatenode.cc
Expected output:
"GM Ethereum"
Wrap-up
You’ve developed, tested, deployed, and interacted with a contract on Gate Layer Testnet using Foundry. Continue expanding with more scripts and tests, or integrate a frontend to build a full DApp.