|
1 | | -# Using Foundry |
| 1 | +# Using Foundry to Deploy Smart Contracts |
| 2 | + |
| 3 | +This tutorial will guide you through the process of deploying a smart contract on Status Network testnet using Foundry. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before you begin, ensure you have the following: |
| 8 | + |
| 9 | +- **Foundry**: Install from the [official Foundry book](https://book.getfoundry.sh/getting-started/installation) |
| 10 | +- **Ethereum Wallet**: A private key for Status Network testnet |
| 11 | +- **Testnet ETH**: You'll need Status Network testnet ETH |
| 12 | + - Get Status Network testnet ETH from our [Faucet](/tools/testnet-faucets) |
| 13 | +- **Basic Knowledge**: Familiarity with Solidity and command line |
| 14 | + |
| 15 | +## What You'll Accomplish |
| 16 | + |
| 17 | +- Initialize a Foundry project |
| 18 | +- Write a basic Ethereum smart contract |
| 19 | +- Configure Foundry for Status Network testnet deployment |
| 20 | +- Deploy your smart contract |
| 21 | + |
| 22 | +## Steps |
| 23 | + |
| 24 | +### 1. Initialize a Foundry Project |
| 25 | + |
| 26 | +First, create a new Foundry project: |
| 27 | + |
| 28 | +```bash |
| 29 | +# Create a new project |
| 30 | +forge init hello_status |
| 31 | +cd hello_status |
| 32 | + |
| 33 | +# Create .env file for private key |
| 34 | +touch .env |
| 35 | +echo "PRIVATE_KEY=your_private_key_here" >> .env |
| 36 | +``` |
| 37 | + |
| 38 | +### 2. Writing the Smart Contract |
| 39 | + |
| 40 | +Replace `src/Counter.sol` with our `HelloWorld.sol`: |
| 41 | + |
| 42 | +```solidity |
| 43 | +// SPDX-License-Identifier: MIT |
| 44 | +pragma solidity ^0.8.24; |
| 45 | +
|
| 46 | +contract HelloWorld { |
| 47 | + string public greet = "Hello, Status Network!"; |
| 48 | +
|
| 49 | + function setGreet(string memory _greet) public { |
| 50 | + greet = _greet; |
| 51 | + } |
| 52 | +
|
| 53 | + function getGreet() public view returns (string memory) { |
| 54 | + return greet; |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### 3. Configure Foundry for Status Network |
| 60 | + |
| 61 | +Create or update `foundry.toml`: |
| 62 | + |
| 63 | +```toml |
| 64 | +[profile.default] |
| 65 | +src = "src" |
| 66 | +out = "out" |
| 67 | +libs = ["lib"] |
| 68 | +solc = "0.8.24" |
| 69 | + |
| 70 | +[rpc_endpoints] |
| 71 | +status_testnet = "https://public.sepolia.rpc.status.network" |
| 72 | +``` |
| 73 | + |
| 74 | +### 4. Deploy the Contract |
| 75 | + |
| 76 | +Create a deployment script `script/Deploy.s.sol`: |
| 77 | + |
| 78 | +```solidity |
| 79 | +// SPDX-License-Identifier: MIT |
| 80 | +pragma solidity ^0.8.24; |
| 81 | +
|
| 82 | +import "forge-std/Script.sol"; |
| 83 | +import "../src/HelloWorld.sol"; |
| 84 | +
|
| 85 | +contract DeployScript is Script { |
| 86 | + function run() external { |
| 87 | + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); |
| 88 | + |
| 89 | + vm.startBroadcast(deployerPrivateKey); |
| 90 | + |
| 91 | + HelloWorld hello = new HelloWorld(); |
| 92 | + console.log("HelloWorld deployed to:", address(hello)); |
| 93 | + |
| 94 | + vm.stopBroadcast(); |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Deploy using forge: |
| 100 | + |
| 101 | +```bash |
| 102 | +# Load environment variables |
| 103 | +source .env |
| 104 | + |
| 105 | +# Deploy to Status Network testnet |
| 106 | +forge script script/Deploy.s.sol:DeployScript \ |
| 107 | + --rpc-url https://public.sepolia.rpc.status.network \ |
| 108 | + --broadcast \ |
| 109 | +``` |
| 110 | + |
| 111 | +### 5. Interact with the Contract |
| 112 | + |
| 113 | +Create a script to interact with your contract `script/Interact.s.sol`: |
| 114 | + |
| 115 | +```solidity |
| 116 | +// SPDX-License-Identifier: MIT |
| 117 | +pragma solidity ^0.8.24; |
| 118 | +
|
| 119 | +import "forge-std/Script.sol"; |
| 120 | +import "../src/HelloWorld.sol"; |
| 121 | +
|
| 122 | +contract InteractScript is Script { |
| 123 | + function run() external { |
| 124 | + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); |
| 125 | + address contractAddress = address(0x...); // Replace with your contract address |
| 126 | + |
| 127 | + vm.startBroadcast(deployerPrivateKey); |
| 128 | + |
| 129 | + HelloWorld hello = HelloWorld(contractAddress); |
| 130 | + |
| 131 | + // Read current greeting |
| 132 | + string memory currentGreeting = hello.getGreet(); |
| 133 | + console.log("Current greeting:", currentGreeting); |
| 134 | + |
| 135 | + // Update greeting |
| 136 | + hello.setGreet("Hello from Foundry!"); |
| 137 | + |
| 138 | + vm.stopBroadcast(); |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +Run the interaction script: |
| 144 | + |
| 145 | +```bash |
| 146 | +forge script script/Interact.s.sol:InteractScript \ |
| 147 | + --rpc-url https://public.sepolia.rpc.status.network \ |
| 148 | + --broadcast |
| 149 | +``` |
| 150 | + |
| 151 | +### 6. Cast Commands for Quick Interactions |
| 152 | + |
| 153 | +You can also use `cast` to interact with your contract: |
| 154 | + |
| 155 | +```bash |
| 156 | +# Read the greeting |
| 157 | +cast call <CONTRACT_ADDRESS> "getGreet()" \ |
| 158 | + --rpc-url https://public.sepolia.rpc.status.network |
| 159 | + |
| 160 | +# Set a new greeting |
| 161 | +cast send <CONTRACT_ADDRESS> "setGreet(string)" "New greeting!" \ |
| 162 | + --private-key $PRIVATE_KEY \ |
| 163 | + --rpc-url https://public.sepolia.rpc.status.network |
| 164 | +``` |
| 165 | + |
| 166 | +### 7. Testing |
| 167 | + |
| 168 | +Create a test file `test/HelloWorld.t.sol`: |
| 169 | + |
| 170 | +```solidity |
| 171 | +// SPDX-License-Identifier: MIT |
| 172 | +pragma solidity ^0.8.24; |
| 173 | +
|
| 174 | +import "forge-std/Test.sol"; |
| 175 | +import "../src/HelloWorld.sol"; |
| 176 | +
|
| 177 | +contract HelloWorldTest is Test { |
| 178 | + HelloWorld hello; |
| 179 | +
|
| 180 | + function setUp() public { |
| 181 | + hello = new HelloWorld(); |
| 182 | + } |
| 183 | +
|
| 184 | + function testGreeting() public { |
| 185 | + assertEq(hello.getGreet(), "Hello, Status Network!"); |
| 186 | + |
| 187 | + hello.setGreet("New greeting"); |
| 188 | + assertEq(hello.getGreet(), "New greeting"); |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +Run the tests: |
| 194 | + |
| 195 | +```bash |
| 196 | +forge test |
| 197 | +``` |
| 198 | + |
| 199 | +## Support |
| 200 | + |
| 201 | +If you encounter any issues: |
| 202 | +- Join our [Telegram Community](https://t.me/+k04A_OZbhIs1Mzc9) |
| 203 | +- Check [Network Status](https://health.status.network) |
| 204 | +- View our [Network Details](/general-info/network-details) |
| 205 | + |
| 206 | +## Additional Resources |
| 207 | + |
| 208 | +- [Foundry Book](https://book.getfoundry.sh/) |
| 209 | +- [Status Network Explorer](https://sepoliascan.status.network) |
0 commit comments