My first taste of Foundry
December 10th, 2023

This article was originally published on my previous mirror.xyz site on 2 Dec 2022 and moved to my new space

If you have been building smart contract on Ethereum or any EVM compatible blockchain, you must have used Truffle or Hardhat as your solidity development toolkit. Recently I bumped into a blockchain dev in a meetup and he recommended to try Foundry.

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.- Description on Foundry Github Repository

Install and setup

My journey starts with curl -L https://foundry.paradigm.xyz | bash and foundryup. The journey started pretty well. I didn’t encounter any unexpected error during my installation and it didn’t take too long to install.

After installation and initialising the project, I started to write a contract. I came up an idea to create a ERC-721 contract which mint an NFT to an address by taking some ERC-20 tokens. The experience is similar to what I have with Hardhat. In Foundry, I can also install libraries such as OpenZeppelin as well. It is achieved by using the forge install openzeppelin/openzeppelin-contracts command.

From here, I do need to make some configuration changes such that the Openzeppelin contracts can be imported correctly. I had to create a remappings.txt which tell Foundry how to resolve the path to my library. This article came to rescue -

nft://undefined/undefined/undefined?showBuying=true&showMeta=true

nft://undefined/undefined/undefined?showBuying=true&showMeta=true

nft://undefined/undefined/undefined?showBuying=true&showMeta=true

nft://undefined/undefined/undefined?showBuying=true&showMeta=true

nft://undefined/undefined/undefined?showBuying=true&showMeta=true

Testing

After writing the contract, it’s time to test the contract. Unlike Hardhat, Foundry enabled me to write solidity tests in solidity. You might ask what’s the benefit of that. One of the major benefit that I notice is I don’t have to do convert the number to BigNumber when I try to call a contract function with uint256 as parameter. This is amazing. I also didn’t have to setup mocha and chai like what I normally did in Hardhat. All I need is to create a ContractName.t.sol and create a public function with test as the prefix of the function name. forge test would be able to pick it up and the test run as lightning fast.

There are some tips to test with Foundry. When I run forge test, I was able to configure the verbose level and with forge test -vvv, it shows the function that has been called in the solidity, which is very helpful in debugging.

Verdict

I haven’t tried the contract deploy and verify functionality in Foundry but I reckon that it is a better development toolkit for solidity development by far. If you are creating a new solidity project, I recommend you to have a try.

Subscribe to ericxstone
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from ericxstone

Skeleton

Skeleton

Skeleton