smartcontractkit / foundry-starter-kit

MIT License
332 stars 94 forks source link

Note: This repo has been recently updated for Sepolia

Foundry Starter Kit


Chainlink Foundry logo


Open in Gitpod

Foundry Starter Kit is a repo that shows developers how to quickly build, test, and deploy smart contracts with one of the fastest frameworks out there, foundry!

Getting Started

Requirements

Please install the following:

Quickstart

git clone https://github.com/smartcontractkit/foundry-starter-kit
cd foundry-starter-kit

Install dependencies as follows:

Run forge install to install dependencies. Foundry uses git submodules as its dependency management system.

⚠️ when running forge install, you may see an error message if you have uncomitted changes in your repo. Read the message carefully - it may inform you that you can add the --no-commit flag to each of these install commands if your workspace has uncommitted changes.

You can update dependencies by running forge update

Testing

To check that everything is compiling and working as intended after cloning and installing dependencies, run

forge test

All tests should pass.

Chainlink Foundry Starter Kit

Implementation of the following 4 Chainlink services using the [Foundry] (https://book.getfoundry.sh/) smart contract development tooling:

For Chainlink Functions please go to these starter kits: Hardhat | Foundry (coming soon)

For Chainlink CCIP (Cross Chain Interoperability Prototocol) please go to these starter kits: Hardhat | Foundry

Deploying to a network

Deploying to a network uses the foundry scripting system, where you write your deploy scripts in solidity!

Setup

We'll demo using the Sepolia testnet. (Go here for testnet sepolia ETH.)

You'll need to add the following variables to a .env file:

When you've added your environment variables to the .env file, run source .env in your terminal (and for each new terminal session) to load the environment variables into your terminal.

Deploying

Deploy scripts are in ./script. The relevant Chainlink Service can be determined from the name of the Contract script. HelperConfig is not meant to be deployed.

To deploy one of the Chainlink Service consumer contracts run the script as follows:

forge script script/${CONTRACT_NAME}.s.sol:Deploy${CONTRACT_NAME} --rpc-url $SEPOLIA_RPC_URL  --private-key PRIVATE_KEY --broadcast --verify --etherscan-api-key $ETHERSCAN_API_KEY  -vvvv
make deploy-sepolia contract=<CONTRACT_NAME>

For example, to deploy the PriceFeedConsumer contract:

forge script script/PriceFeedConsumer.s.sol:DeployPriceFeedConsumer  --rpc-url $SEPOLIA_RPC_URL --private-key $PRIVATE_KEY --broadcast --verify --etherscan-api-key $ETHERSCAN_API_KEY -vvvv

If you don't have an ETHERSCAN_API_KEY, you can omit --verify --etherscan-api-key $ETHERSCAN_API_KEY

Working with Anvil local development network

Foundry comes with local network anvil baked in, and allows us to deploy to our local network for quick testing locally.

To start a local network run the following in a new terminal window or tab:

anvil

This will spin up a local blockchain on http://localhost:8545 : (see console output for the mnemonic used, and 10 private keys and their associated wallet address), so you can use the same private key each time.

Then, you can deploy to it with one of those private keys; in this example we use the first one:

forge script script/${contract}.s.sol:Deploy${contract} --rpc-url http://localhost:8545  --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 --broadcast 

Working with other chains

To add a chain, you'd just need to pass in the RPC URL for the relevant chain to the --rpc-url flag.

forge script script/${contract}.s.sol:Deploy${contract} --rpc-url ${<OTHER_CHAIN>_RPC_URL}  --private-key ${PRIVATE_KEY} --broadcast -vvvv

Security

This framework comes with slither parameters, a popular security framework from Trail of Bits. To use slither, you'll first need to install python and install slither.

Then, you can run:

make slither

And get your slither output.

Contributing

Contributions are always welcome! Open a PR or an issue! If you do contribute please add solidity.formatter": "forge to your VSCode Settings, or run forge fmt before you commit and push.

Thank You!

Resources

TODO

[ ] Add bash scripts to interact with contracts using cast

[ ] Make deploying contracts to anvil simpler