Foundry: A fast Solidity 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.

Introduction

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:

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.

Foundry benchmarks:

ProjectFoundry compilation timedapptools compilation time
guni-lev28.6s2m36s
solmate6s46s
geb11s40s
vaults1.4s5.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.

Quick walkthrough

Installation

  1. Install Rust.
  2. Install Foundry.

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

Create a contract

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 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 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:

  1. Sign up with Chainstack.
  2. Deploy a Ropsten node.
  3. Get the deployed node’s HTTPS endpoint.

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 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

  • Connect to the Ethereum, Polygon, BNB Smart Chain, Avalanche, Fantom, Solana, Harmony, Tezos and StarkNet mainnet or testnets through the interface designed to help you get the job done.
  • Get access to the Ethereum, Polygon, BNB Smart Chain, Avalanche, Fantom, and Tezos archive nodes to query the entire history of the mainnet—starting at just $49 per month.
  • Choose where you want to deploy, and we will provide you with the dedicated managed infrastructure that can handle high-volume, high-velocity read/write access to the network.
  • To learn more about Chainstack, visit our Knowledge Center or join our Discord server and Telegram group. 

Have you already explored what you can achieve with Chainstack? Get started for free today.

Multi-chain: The money on the table

Blockchain and Web3 experience is getting more unified. Get the best multi-chain experience, build and launch projects on multiple networks with Chainstack

Evgeny Konstantinov
Jan 14
Chainstack uses cookies to provide you with a secure and
personalized experience on its website. Learn more.