Foundry
Overview of the Foundry Smart Contract Development Toolkit
Last updated
Was this helpful?
Overview of the Foundry Smart Contract Development Toolkit
Last updated
Was this helpful?
Foundry is an alternative to tools like Hardhat, Truffle, Brownie, etc.
There are two main flaws with tools like Hardhat, Truffle, and Brownie:
They require the use of another language like JavaScript or Python for writing scripts.
They are slow.
Foundry is the superior choice is because:
Scripts and tests can be written in Solidity, the same language we use to write Ethereum smart contracts.
There is a wide array of CLI commands that aid in smart contract development, testing, deployment, as well as EVM chain interaction.
Foundry is incredibly fast because it was written in .
📖
⚙️
Foundry relies on to install and detect Solidity compiler versions. Working on multiple projects may require using different solc
versions.
For a guide on how to manage solc
versions, please refer to the section of these notes.
The forge
command is used to build, test, and deploy smart contracts.
The following command will compile all contracts inside the /src
directory and save the output in the /out
directory.
The following command assumes that the unit test will be running on a forked network.
The -vvvvv
flag specifies the level of verbosity (i.e., how much detail to display in the terminal as the test runs). In this case, we are specifying the highest level (Level 5).
If we don't specify this flag, Level 1 is assumed, -vv
= Level 2, -vvv
= Level 3, etc.
The following command assumes that the unit test will be running on a forked network.
Also, the minimum amount of detail will be displayed in the terminal as the tests run, since the default verbosity is Level 1.
The cast
command is used to perform Ethereum RPC calls to deployed smart contracts.
Calling a contract function does not modify any on-chain data. This is simply a way to read data returned by the function that we call using the cast call
command.
Submitting a transaction to a contract function will modify on-chain data. In order to successfully do this, we need to sign the transaction with our private key and pay the corresponding gas fee.
The amount can be an integer, which defaults to Wei, or you can specify the units. For example 1ether
.
The anvil
command is used to create a local Mainnet fork for deploying and testing smart contracts.
The --chain-id
and --port
commands are optional.
If --chain-id
is not specified, the default value is set to it's Mainnet counterpart (i.e., chain ID would be 1 for Ethereum).
If --port
is not specified, the default value is set to 8545.
This allows you to submit transactions as if you were the owner of the account you are impersonating.
After impersonating an account with a significant balance of some ERC-20 token, the following command allows you to transfer that balance to some recipient account.
When installing git submodules using forge install
, the installation may fail with the error git submodule exited with code 128
. This is typically due to initiating the project's git repository without using forge init
.
To get around this, do the following:
The --etherscan-api-key <API_KEY> --verify
part is only required if you would like your contract to be automatically on the chain's block explorer, which is highly recommended. You can get an API key by creating an account on the chain's block explorer (e.g., Etherscan, Arbiscan, Polygonscan, etc.).