visit
Foundry manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line and via Solidity scripts.
Foundry is created by Paradigm, an investment firm focused on crypto.
You can explore more about Foundry toolkit from the and the .
Also, a list of Foundry resources including tools, tutorials, libraries & projects using Foundry.
curl -L //foundry.paradigm.xyz | bash
This will install Foundryup, then simply follow the instructions on-screen, which will make the foundryup
command available in your CLI.
Now run the foundryup
command in the terminal to install Foundry toolchain:
foundryup
To start a new project with Foundry, use forge init
:
forge init foundry_project
This will generate a new project folder called foundry_project. The folder will contain the following:
Inside the src
folder, you'll find an included sample smart contract named Counter.sol
.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
function setNumber(uint256 newNumber) public {
number = newNumber;
}
function increment() public {
number++;
}
}
To compile the smart contract use forge build
:
forge build
This will compile the smart contracts in the src
folder and also create a new folder named out
which will contain the bytecode of the compiled contract.
Foundry generates a directory named tests
, which is the default folder for storing all the tests for smart contracts. The name of the test files has an extension of .t.sol
If you navigate into the test folder, you'll come across a specifically named test file called Counter.t.sol
.
// SPDX-License-Identifier: UNLICENSED
pragma 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);
}
}
To run this file to test the Couter.sol
smart contract use forge test
:
forge test
You can replicate this by make some changes in the Counter.sol
smart contract(I've changed the increment function so that it increments the number by 2 instead of 1).
function increment() public {
number += 2;
}
To deploy the smart contract tot a network use forge create
command:
forge create --rpc-url <your_rpc_endpoint> --private-key <wallet_private_key> src/Counter.sol:Counter
You can get the RPC for the network from Alchemy by following .
Now let's add some of our own functions and tests to the existing Counter.sol
smart contract.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
uint256 public myNumber = 42;
function setNumber(uint256 newNumber) public {
number = newNumber;
}
function increment() public {
number++;
}
function decrement() public {
myNumber--;
}
}
In the above smart contract we have added variable myNumber
which has a value of 42 and the function decrement()
which decrements the value of myNumber
by 1.
To test these functions we need to add some functions in the Counter.t.sol
test file.
// SPDX-License-Identifier: UNLICENSED
pragma 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);
}
function testMyNumber() public {
assertEq(counter.myNumber(), 42);
}
function testDecrement() public {
counter.decrement();
assertEq(counter.myNumber(), 41);
}
}
In the above test file we have added test functions testMyNumber()
and testDecrement()
. The testMyNumber()
function checks the assigned value of myNumber
and testDecrement()
function checks the working of the decrement function. Both of these functions use assertEq
to equate the value assigned to myNumber
.
Now let's run these new tests with the forge test
command:
forge test
As you can see all the test passed, and we also got the amount of gas used to run each test function.
The libraries that you install will go in the lib
folder , of your project by default, ds-test is installed, this one allows you to run tests, create asserts or logs. If you want to install other libraries(e.g. openzeppelin) you can run:
forge install openzeppelin/openzeppelin-contracts
This will add the openzeppelin repository in your project under the lib/openzeppelin-contracts
folder. Now you can import and use the openzeppelin smart contracts in your project.