Foundry: A fast Solidity smart contract development toolkit
Paradigm, the company involved in many successful projects in one way or another, has recently published Foundry—a toolkit for EVM-based application development.
See the announcement blog post: Introducing the Foundry Ethereum development toolbox.
Let’s have a quick look at Foundry.
What is Foundry?
The very first thing to know about Foundry is that it’s a reimplementation of dapptools.
Why is that important?
If you have a look at the current top accounts by ether balance, you will see that the very top two are:
- Eth2 Deposit Contract — created and tested with dapptools.
- Wrapped Ether — created and tested with dapptools.
While dapptools is written in Haskell, Foundry is in Rust.
And Foundry is fast. On some occasions, when using Foundry, you’d blink and miss it compiled your code.
Project | Foundry compilation time | dapptools compilation time |
guni-lev | 28.6s | 2m36s |
solmate | 6s | 46s |
geb | 11s | 40s |
vaults | 1.4s | 5.5s |
See also the Foundry benchmarks repository.
Apart from speed, one of no less important features that Foundry shares with dapptools is being able to write and test your Solidity smart contracts in Solidity and to warp the VM state in your tests.
Let’s have a simple walkthrough.
Installation
Components
Foundry consists of the two main components to work with your smart contracts:
- Forge — use
forge
to compile, test, and deploy your smart contracts. - Cast — use
cast
to interact with the network and the smart contracts.
Initialize your project
Create your project directory and initialize it with forge
:
forge init
Foundry Solidity smart contract tutorial
For simplicity, let’s use the simple storage contract.
In the initialized directory, create /root/foundry/src/simplestorage.sol
:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}
Compile the smart contract
It will take less than half a second:
forge build --contracts /root/foundry/src/simplestorage.sol
Create tests
Write a test in Solidity to get the initial contract value, set a value and check the set value.
Create /root/foundry/src/test/simplestorage.t.sol
:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
import "../../lib/ds-test/src/test.sol";
import "../simplestorage.sol";
contract SimpleStorageTest is DSTest {
SimpleStorage simplestorage;
function setUp() public {
simplestorage = new SimpleStorage();
}
function testGetInitialValue() public {
assertTrue(simplestorage.get() == 0);
}
function testSetValue() public {
uint x = 300;
simplestorage.set(x);
assertTrue(simplestorage.get() == 300);
}
}
Run the test:
forge test --contracts /root/foundry/src/test/simplestorage.t.sol
Now that your test passes, you can spice it up with changing the VM state through a cheat code.
Cheat codes are state changing methods called from the address: 0x7109709ECfa91a80626fF3989D68f67F5b1DD12D
Read more at Foundry: Cheat codes.
Through changing the VM state in tests, you can warp the state of the network—for example, change the timestamp of the block, instantly jump a period of time, and so on.
Let’s add to the test instantly warping our blockchain state to 100 seconds in the future.
Edit /root/foundry/src/test/simplestorage.t.sol
to include changing the initial 0 seconds block timestamp to 100 seconds and checking it:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
import "../../lib/ds-test/src/test.sol";
import "../simplestorage.sol";
interface Vm {
function warp(uint256) external;
}
contract SimpleStorageTest is DSTest {
SimpleStorage simplestorage;
Vm vm = Vm(0x7109709ECfa91a80626fF3989D68f67F5b1DD12D);
function setUp() public {
simplestorage = new SimpleStorage();
}
function testGetInitialValue() public {
assertTrue(simplestorage.get() == 0);
}
function testSetValue() public {
uint x = 300;
simplestorage.set(x);
assertTrue(simplestorage.get() == 300);
}
function testWarp() public {
assertEq(block.timestamp, 0);
vm.warp(100);
assertEq(block.timestamp, 100);
}
}
Run the test again:
forge test --contracts /root/foundry/src/test/simplestorage.t.sol
Deploy the smart contract
Now that you have a tested contract, go ahead and deploy it.
You need a node to deploy the contract to an EVM network—for example, Ropsten.
To get a node:
Make sure you have a private key to your Ethereum account funded with Ropsten ether to pay for gas.
Use forge create
to deploy the contract through your node:
forge create SimpleStorage --contracts /root/foundry/contracts/simplestorage.sol --private-key 9c4b7f4ad48f977dbcdb2323249fd738cc9ff283a7514f3350d344e22c5b923d --rpc-url https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d
Interact with the smart contract
Call the contract simulation at the deployed contract address through your node:
cast call 0x131FBABE213A3a98cD8C71d0F1Ed94861A3665CA "get()" --rpc-url https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d
Conclusion
Foundry is currently in active development with a very involved community.
It really is blazing fast and the Foundry “pedigree” makes it a very promising project for the Web3 stack.
I suggest you watch the project and see the pace at which they mature to become part of your daily stack.
Power-boost your project on Chainstack
- Discover how you can save thousands in infra costs every month with our unbeatable pricing on the most complete Web3 development platform.
- Input your workload and see how affordable Chainstack is compared to other RPC providers.
- Connect to Ethereum, Solana, BNB Smart Chain, Polygon, Arbitrum, Base, Optimism, Avalanche, TON, Ronin, zkSync Era, Starknet, Scroll, Aptos, Fantom, Cronos, Gnosis Chain, Klaytn, Moonbeam, Celo, Aurora, Oasis Sapphire, Polygon zkEVM, Bitcoin, Tezos and Harmony mainnet or testnets through an interface designed to help you get the job done.
- To learn more about Chainstack, visit our Developer Portal or join our Discord server and Telegram group.
- Are you in need of testnet tokens? Request some from our faucets. Multi-chain faucet, Sepolia faucet, Holesky faucet, BNB faucet, zkSync faucet, Scroll faucet.
Have you already explored what you can achieve with Chainstack? Get started for free today.