|
| 1 | +--- |
| 2 | +sidebar_label: Using Foundry |
| 3 | +hide_table_of_contents: false |
| 4 | +sidebar_position: 2 |
| 5 | +description: Deploy Contracts on Core using the Foundry |
| 6 | +--- |
| 7 | + |
| 8 | +# Using Foundry |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +Foundry is a fast, efficient, and extensible toolkit for EVM development written in Rust.This guide will walk you through the process of installing Foundry, setting up your project, writing and testing Solidity code, and deploying and verifying your contracts on Core Blockchain. |
| 13 | + |
| 14 | +## 1. Installation of Foundry |
| 15 | + |
| 16 | +Before you start using Foundry, you need to install it on your system. The process is relatively simple and can be done in a few steps. Foundry uses `forge` as the main tool to interact with Solidity contracts. |
| 17 | + |
| 18 | +### Step 1: Install Foundry |
| 19 | + |
| 20 | +You can install Foundry by running the following command on a Unix-based system (macOS, Linux): |
| 21 | + |
| 22 | +```bash |
| 23 | +curl -L https://foundry.paradigm.xyz | bash |
| 24 | +``` |
| 25 | + |
| 26 | +This will download and install Foundry. It includes the following tools: |
| 27 | + |
| 28 | +- `forge`: The command-line interface for compiling, testing, and deploying Solidity contracts. |
| 29 | +- `cast`: A tool for interacting with smart contracts and viewing blockchain data. |
| 30 | +- `anvil`: A local node used for testing. |
| 31 | + |
| 32 | +Once installed, make sure the binary is in your system’s PATH. To verify the installation, run: |
| 33 | + |
| 34 | +```bash |
| 35 | +forge --version |
| 36 | +``` |
| 37 | + |
| 38 | +You should see the version of Foundry that was installed. |
| 39 | + |
| 40 | +### Step 2: Update Foundry |
| 41 | + |
| 42 | +Foundry is actively developed, so it's important to keep your installation up to date. You can update Foundry with the following command: |
| 43 | + |
| 44 | +```bash |
| 45 | +foundryup |
| 46 | +``` |
| 47 | + |
| 48 | +This ensures that you have the latest version of Foundry installed. |
| 49 | + |
| 50 | +## 2. Setting Up Your Project |
| 51 | + |
| 52 | +### Step 1: Create a New Foundry Project |
| 53 | + |
| 54 | +To create a new project with Foundry, you can use the `forge init` command: |
| 55 | + |
| 56 | +```bash |
| 57 | +forge init my-blockchain-project |
| 58 | +``` |
| 59 | + |
| 60 | +This will generate a new directory structure with all the basic files you need to start writing Solidity contracts and tests. |
| 61 | + |
| 62 | +Here’s what the generated folder structure will look like: |
| 63 | + |
| 64 | +``` |
| 65 | +my-blockchain-project/ |
| 66 | +│ |
| 67 | +├── lib/ # External dependencies (if any) |
| 68 | +├── script/ |
| 69 | +│ └── Script.s.sol # Default deploy script file |
| 70 | +| |
| 71 | +├── src/ |
| 72 | +│ └── Counter.sol # Default contract file |
| 73 | +| |
| 74 | +├── test/ # Test files |
| 75 | +│ └── Counter.t.sol # Default test file |
| 76 | +│ |
| 77 | +├── foundry.toml # Configuration file for foundry |
| 78 | +└── README.md # Project documentation |
| 79 | +``` |
| 80 | + |
| 81 | +### Step 2: Understanding the Folder Structure |
| 82 | + |
| 83 | +- **src**: Contains your Solidity contracts. |
| 84 | +- **script**: Contains your deployment script. |
| 85 | +- **lib**: Holds any external libraries or dependencies. |
| 86 | +- **test**: Contains test files to validate your contracts. |
| 87 | +- **foundry.toml**: The configuration file for Foundry. It includes compiler settings, network configurations, and more. |
| 88 | +- **README.md**: A markdown file to document your project. |
| 89 | + |
| 90 | +### Step 3: Update Solidity and EVM version |
| 91 | + |
| 92 | +Update the `foundry.toml` file with the appropriate Solidity and EVM versions. |
| 93 | + |
| 94 | +Ensure you are using Solidity version `0.8.24`, and set the EVM version to `Shanghai`. If you're using an **older testnet**, set the **EVM version to Paris**. |
| 95 | + |
| 96 | +```bash |
| 97 | +[profile.default] |
| 98 | +src = "src" |
| 99 | +out = "out" |
| 100 | +libs = ["lib"] |
| 101 | +evm_version = "shanghai" |
| 102 | +solc_version = "0.8.24" |
| 103 | +``` |
| 104 | + |
| 105 | +## 3. Writing a Solidity Contract |
| 106 | + |
| 107 | +Now that your project is set up, let's write a simple Solidity contract. |
| 108 | + |
| 109 | +### Step 1: Create a Simple Contract |
| 110 | + |
| 111 | +Navigate to the `src` directory and create a new Solidity contract. Let’s use a simple counter contract to start. |
| 112 | + |
| 113 | +Create a file called `Counter.sol` inside the `src` folder: |
| 114 | + |
| 115 | +```javascript |
| 116 | +// SPDX-License-Identifier: UNLICENSED |
| 117 | +pragma solidity ^0.8.24; |
| 118 | + |
| 119 | +contract Counter { |
| 120 | + uint256 public count; |
| 121 | + |
| 122 | + constructor() { |
| 123 | + count = 0; |
| 124 | + } |
| 125 | + |
| 126 | + function increment() public { |
| 127 | + count += 1; |
| 128 | + } |
| 129 | + |
| 130 | + function decrement() public { |
| 131 | + count -= 1; |
| 132 | + } |
| 133 | + |
| 134 | + function getCount() public view returns (uint256) { |
| 135 | + return count; |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +In this contract: |
| 141 | + |
| 142 | +- The `count` state variable keeps track of the count value. |
| 143 | +- The `increment` and `decrement` functions increase or decrease the counter. |
| 144 | +- The `getCount` function returns the current count. |
| 145 | + |
| 146 | +## 4. Testing the Solidity Contract |
| 147 | + |
| 148 | +Foundry makes it easy to write tests for your contracts. The testing framework uses Solidity itself, making it easy to write tests alongside your contracts. |
| 149 | + |
| 150 | +### Step 1: Create a Test File |
| 151 | + |
| 152 | +Navigate to the `test` directory and create a test file called `Counter.t.sol`: |
| 153 | + |
| 154 | +```javascript |
| 155 | +// SPDX-License-Identifier: UNLICENSED |
| 156 | +pragma solidity ^0.8.24; |
| 157 | + |
| 158 | +import "forge-std/Test.sol"; |
| 159 | +import "../src/Counter.sol"; |
| 160 | + |
| 161 | +contract CounterTest is Test { |
| 162 | + Counter counter; |
| 163 | + |
| 164 | + function setUp() public { |
| 165 | + counter = new Counter(); |
| 166 | + } |
| 167 | + |
| 168 | + function testInitialCount() public { |
| 169 | + assertEq(counter.getCount(), 0); |
| 170 | + } |
| 171 | + |
| 172 | + function testIncrement() public { |
| 173 | + counter.increment(); |
| 174 | + assertEq(counter.getCount(), 1); |
| 175 | + } |
| 176 | + |
| 177 | + function testDecrement() public { |
| 178 | + counter.increment(); |
| 179 | + counter.decrement(); |
| 180 | + assertEq(counter.getCount(), 0); |
| 181 | + } |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +### Explanation: |
| 186 | + |
| 187 | +- `setUp()`: This function runs before every test and is used to initialize your contract. |
| 188 | +- Test functions: `testInitialCount`, `testIncrement`, and `testDecrement` are individual test cases to check the contract's functionality. |
| 189 | + |
| 190 | +### Step 2: Run the Tests |
| 191 | + |
| 192 | +To run the tests, execute the following command: |
| 193 | + |
| 194 | +```bash |
| 195 | +forge test |
| 196 | +``` |
| 197 | + |
| 198 | +Foundry will compile the contract, run the tests, and provide you with a summary of the results. |
| 199 | + |
| 200 | +You should see output similar to the following: |
| 201 | + |
| 202 | +```bash |
| 203 | +[⠊] Compiling... |
| 204 | +[⠔] Compiling 1 files with Solc 0.8.28 |
| 205 | +[⠒] Solc 0.8.28 finished in 491.38ms |
| 206 | +Compiler run successful! |
| 207 | + |
| 208 | +Ran 3 tests for test/Counter.t.sol:CounterTest |
| 209 | +[PASS] testDecrement() (gas: 22192) |
| 210 | +[PASS] testIncrement() (gas: 32003) |
| 211 | +[PASS] testInitialCount() (gas: 10943) |
| 212 | +Suite result: ok. 3 passed; 0 failed; 0 skipped; finished in 5.38ms (3.86ms CPU time) |
| 213 | + |
| 214 | +Ran 1 test suite in 148.37ms (5.38ms CPU time): 3 tests passed, 0 failed, 0 skipped (3 total tests) |
| 215 | +``` |
| 216 | + |
| 217 | +## 5. Deploying the Contract |
| 218 | + |
| 219 | +Once you’ve written and tested your Solidity contract, you can deploy it to Core Testnet/Mainnet. |
| 220 | + |
| 221 | +### Step 1: Configuring Deployment |
| 222 | + |
| 223 | +To deploy your contract, you’ll need to set up a deployment script. create a file `Counter.s.sol` under script folder and paste the following code. |
| 224 | + |
| 225 | +```javascript |
| 226 | +// SPDX-License-Identifier: UNLICENSED |
| 227 | +pragma solidity ^0.8.24; |
| 228 | + |
| 229 | +import {Script, console} from "forge-std/Script.sol"; |
| 230 | +import {Counter} from "../src/Counter.sol"; |
| 231 | + |
| 232 | +contract CounterScript is Script { |
| 233 | + Counter public counter; |
| 234 | + |
| 235 | + function setUp() public {} |
| 236 | + |
| 237 | + function run() public { |
| 238 | + vm.startBroadcast(); |
| 239 | + |
| 240 | + counter = new Counter(); |
| 241 | + |
| 242 | + vm.stopBroadcast(); |
| 243 | + } |
| 244 | +} |
| 245 | +``` |
| 246 | + |
| 247 | +#### Creating and Loading environment variables |
| 248 | + |
| 249 | +create an `.env file` and add the following details |
| 250 | + |
| 251 | +```text |
| 252 | +RPC_URL = " https://rpc.test2.btcs.network" |
| 253 | +PRIVATE_KEY = "YOUR_PRIVATE_KEY" |
| 254 | +CORESCAN_API_KEY="YOUR_API_KEY" |
| 255 | +API_URL="https://api.test2.btcs.network/api" |
| 256 | +``` |
| 257 | + |
| 258 | +Make sure you replace `YOUR_API_KEY` and `YOUR_PRIVATE_KEY` with the actual values. |
| 259 | + |
| 260 | +Now that you've created the above `.env` file, run the following command to load the environment variables in the current command line session: |
| 261 | + |
| 262 | +```bash |
| 263 | +source .env |
| 264 | +``` |
| 265 | + |
| 266 | +### Step 2: Deploy the Contract |
| 267 | + |
| 268 | +To deploy the contract to Core testnet, use the `forge create` |
| 269 | + |
| 270 | +```bash |
| 271 | +forge create --rpc-url $RPC_URL --private-key $PRIVATE_KEY src/Counter.sol:Counter --broadcast |
| 272 | +``` |
| 273 | + |
| 274 | +or use `forge script` command |
| 275 | + |
| 276 | +```bash |
| 277 | +forge script script/Counter.s.sol:CounterScript --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast |
| 278 | +``` |
| 279 | + |
| 280 | +After running the command, Foundry will compile and deploy your contract to the specified network. It will return the deployed contract’s address. |
| 281 | + |
| 282 | +```bash |
| 283 | +[⠊] Compiling... |
| 284 | +No files changed, compilation skipped |
| 285 | +Deployer: 0x1b984521b42D3B9aCFCf37565Ab865f318b1Cd92 |
| 286 | +Deployed to: 0xc1C2466cBBa0f8E1FDb7f5E68e232190c745A6Ae |
| 287 | +Transaction hash: 0x9ce3604ef36d526cd0cad75a23b6f4bfc9558cb8ee26caa825acf2ad914784b2 |
| 288 | +``` |
| 289 | + |
| 290 | +## 6. Verifying the Contract |
| 291 | + |
| 292 | +Foundry has a built-in feature to automatically verify contracts on Core. You can verify your contract using the `forge verify-contract` command: |
| 293 | + |
| 294 | +```bash |
| 295 | +forge verify-contract 0xContract_Address Counter --verifier-url $API_URL --api-key $CORESCAN_API_KEY --watch |
| 296 | +``` |
| 297 | + |
| 298 | +Foundry will handle the verification process,you can use[ Core Scan](https://scan.test2.btcs.network/) to search for the contract's address to verify that the contract was successfully deployed and verified. |
| 299 | + |
| 300 | +## Further Reading |
| 301 | + |
| 302 | +For detailed instructions on using Foundry, please visit[ Foundry's official website](https://book.getfoundry.sh/). |
0 commit comments