Warning
This package is currently in BETA.
Open issues to submit bugs.
This is a toolkit that makes spinning up, managing and testing a local Chainlink nodes easier.
This project uses Foundry tools to deploy and test smart contracts.
It can be easily integrated into an existing Foundry project.
The purpose of this project is to simplify the immersion in the development and testing of Smart Contracts using Chainlink oracles. This project is aimed primarily at those who use the Foundry toolchain.
-
Install Foundry toolchain. Reference the below commands or go to the Foundry documentation.
- MacOS/Linux
This will download foundryup. Restart your terminal session, then install Foundry by running:
curl -L https://foundry.paradigm.xyz | bash
foundryup
Note
You may see the following error on MacOS:
dyld: Library not loaded: /usr/local/opt/libusb/lib/libusb-1.0.0.dylib
In order to fix this, you should install libusb:
brew install libusb
Reference: https://github.com/foundry-rs/foundry/blob/master/README.md#troubleshooting-installation
- MacOS/Linux
-
Install GNU make. The functionality of the project is wrapped in the makefile. Reference the below commands based on your OS or go to Make documentation.
-
MacOS: install Homebrew first, then run
brew install make
-
Debian/Ubuntu
apt install make
-
Fedora/RHEL
yum install make
-
-
Install and run Docker; for convenience, the Chainlink nodes run in a container. Instructions: docs.docker.com/get-docker.
Note
Foundry-Chainlink toolkit was tested using:
- Forge 0.2.0 (e99cf83 2023-04-21T00:15:57.602861000Z)
- GNU Make 3.81
- Docker version 20.10.23, build 7155243
The Foundry-Chainlink toolkit has been designed so that it can be installed as a Forge dependency.
To integrate it into your project, you need to run the following command:
forge install smartcontractkit/foundry-chainlink-toolkit
In addition, the Forge Standard Library must be installed in your project:
forge install foundry-rs/forge-std
Note
In addition to being used as a plugin, this toolkit is ready to be used as a demo standalone application.
In this case, to install dependencies, run:git submodule update
Based on the env.template - create or update an .env
file in the root directory of your project.
In most cases, you will not need to modify the default values specified in this file.
Below are comments on some environment variables:
FCT_PLUGIN_PATH
- path to the Foundry-Chainlink toolkit rootETH_URL
- RPC node web socket used by the Chainlink nodeRPC_URL
- RPC node http endpoint used by ForgePRIVATE_KEY
- private key of an account used for deployment and interaction with smart contracts. Once Anvil is started, a set of private keys for local usage is provided. Use one of these for local developmentROOT
- root directory of the Chainlink nodeCHAINLINK_CONTAINER_NAME
- Chainlink node container name for the possibility of automating communication with itCOMPOSE_PROJECT_NAME
- Docker network project name for the possibility of automating communication with it, more on it: https://docs.docker.com/compose/environment-variables/envvars/#compose_project_name
Note
If environment variables related to a Chainlink node, including a Link Token contract address, were changed during your work you should run themake fct-run-nodes
command in order for them to be applied.
-
Give Forge permission to read the output directory of the toolkit by adding this setting to the foundry.toml:
fs_permissions = [{ access = "read", path = "lib/foundry-chainlink-toolkit/out"}]
Note
The default path to the root of the Foundry-Chainlink toolkit islib/foundry-chainlink-toolkit
.
Unfortunately at the momentfoundry.toml
cannot read all environment variables. Specify a different path if necessary. -
Incorporate the makefile-external into your project. To do this, create or update a makefile in the root of your project with:
-include ${FCT_PLUGIN_PATH}/makefile-external
In order for a Chainlink node to be able to interact with the blockchain, and to interact with the blockchain using the Forge, you have to know an RPC node http endpoint and web socket for a chosen network compatible with Chainlink. In addition to the networks listed in this list, Chainlink is compatible with any EVM-compatible networks.
For local testing, we recommend using Anvil, which is a part of the Foundry toolchain.
You can run it using the following command:
make fct-anvil
Note
In case the local Ethereum node has been restarted, you should also re-initialize the Chainlink cluster or perform a clean spin-up of the Chainlink nodes to avoid possible synchronization errors.
Scripts for automating the initialization of the test environment and setting up Chainlink jobs will be described below.
To display autogenerated help with a brief description of the most commonly used scripts, run:
make fct-help
For a more detailed description of the available scripts, you can refer to DOCUMENTATION.md.
make fct-init
This command automatically initializes the test environment, in particular, it makes clean spin-up of a Chainlink cluster of 5 Chainlink nodes.
Once Chainlink cluster is launched, a Chainlink nodes' Operator GUI will be available at:
- http://127.0.0.1:6711 - Chainlink node 1
- http://127.0.0.1:6722 - Chainlink node 2
- http://127.0.0.1:6733 - Chainlink node 3
- http://127.0.0.1:6744 - Chainlink node 4
- http://127.0.0.1:6755 - Chainlink node 5
For authorization, you must use the credentials specified in the chainlink_api_credentials.
You can also initialize the test environment manually by following these steps:
- Deploy Link Token contract
- Set
LINK_TOKEN_CONTRACT
in.env
- Spin up a Chainlink nodes cluster
- Fund Chainlink nodes with ETH
- Fund Chainlink nodes with Link tokens
Note
For ARM64 users. When starting a docker container, there will be warnings:
The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested
You can safely ignore these warnings, container will start normally.
make fct-setup-job
This command displays a list of available Chainlink jobs and sets up the selected one.
You can also set up a Chainlink job by calling the respective command.
make fct-setup-direct-request-job
This command automatically sets up a Direct Request job.
You can also set up a Direct Request job manually by following these steps:
- Deploy Oracle contract
- Deploy Consumer contract
- Fund Consumer contract with Link tokens
- Create Direct Request Job
- Request ETH price with Consumer contract, a corresponding job will be launched
- Get ETH price after completing a job
make fct-setup-cron-job
This command automatically sets up a Cron job.
You can also set up a Cron job manually by following these steps:
make fct-setup-webhook-job
This command automatically sets up a Webhook job.
You can also set up a Webhook job manually by following these steps:
make fct-setup-keeper-job
This command automatically sets up a Keeper job.
You can also set up a Keeper job manually by following these steps:
- Deploy Keeper Consumer contract
- Deploy Registry contract
- Create Keeper Jobs for Chainlink nodes in a cluster
- Register Chainlink nodes as keepers in a Registry contract
- Register Keeper Consumer as upkeep in a Registry contract
- Fund the latest upkeep in a Registry contract
- Get value of
counter
variable in a Keeper contract
make fct-setup-flux-job
This command automatically sets up a Flux job.
You can also set up a Flux job manually by following these steps:
- Deploy Flux Aggregator contract
- Fund Flux Aggregator contract with Link tokens
- Update Flux Aggregator available funds
- Set Flux Aggregator oracles
- Create Flux Jobs for the first 3 Chainlink nodes in a cluster
- Get the answer of the latest Flux round from the Flux Aggregator contract
make fct-setup-ocr-job
This command automatically sets up an OCR job.
You can also set up a OCR job manually by following these steps:
- Deploy Offchain Aggregator contract
- Set Offchain Aggregator payees
- Set Offchain Aggregator config
- Create OCR Job for a bootstrap Chainlink node (first in a cluster)
- Create OCR Jobs for Chainlink nodes in a cluster except the first one (bootstrap)
- Request new OCR round in the Offchain Aggregator contract (optional)
- Get the answer of the latest OCR round from the Offchain Aggregator contract
Note Manual set up of a Chainlink Job is recommended when utilizing a custom Consumer or Aggregator contract, or when a different job configuration is desired.
You can create a custom TOML file and use it to create a Chainlink Job instance through the Operator GUI or develop a custom script using the existing scripts provided by this toolkit.
Chainlink *
This directory contains configuration files, scripts and smart contracts source code.
Contracts *
- ChainlinkConsumer.sol - example consumer contract for Chainlink Direct Request Job
- ChainlinkCronConsumer.sol - example consumer contract for Chainlink Cron Job
- ChainlinkKeeperConsumer.sol - example consumer contract for Chainlink Keeper Job
- LinkToken.sol - flattened Link Token contract
Jobs *
- cron_job.toml - example configuration file for a Chainlink Cron job
- direct_request_job.toml - example configuration file for a Chainlink Direct Request job
- flux_job.toml - example configuration file for a Chainlink Flux job
- keeper_job.toml - example configuration file for a Chainlink Keeper job
- ocr_job.toml - example configuration file for a Chainlink OCR job
- ocr_job_bootstrap.toml - example configuration file for a Chainlink OCR (bootstrap) job
- webhook_job.toml - example configuration file for a Chainlink Webhook job
Note
More info on Chainlink v2 Jobs, their types and configuration can be found here: docs.chain.link/chainlink-nodes/oracle-jobs/jobs/.
You can change these configuration according to your requirements.
Setting *
- chainlink_api_credentials - Chainlink API credentials
- chainlink_password - Chainlink password
Note
More info on authentication can be found here github.com/smartcontractkit/chainlink/wiki/Authenticating-with-the-API.
You can specify any credentials there. Password provided must be 16 characters or more.
SQL *
- create_tables.sql - sql script to create tables related to Chainlink nodes in a Postgres DB
- drop_tables.sql - sql script to delete tables related to Chainlink nodes in a Postgres DB
Once Chainlink nodes are started, log directories will be created for each of them.
chainlink.env *
This file contains environment variables related to Chainlink node configuration. You can modify it according to your requirements.
More info on Chainlink environment variables can be found here: https://docs.chain.link/chainlink-nodes/v1/configuration.
Note
Subdirectories: jobs, settings and sql are used as shared folders for running Chainlink nodes and Postgres DB containers.
External *
This directory contains external libraries.
OCRHelper *
This Go library is based on https://github.com/smartcontractkit/chainlink-testing-framework integration tests and is used to prepare configuration parameters for Offchain Aggregator contract.
It has pre-built binaries for platforms: darwin/amd64(x86_64), darwin/arm64, linux/amd64(x86_64), linux/arm,linux/arm64.
Note
If you use another platform, in the root of the project please run:
make fct-build-ocr-helper
It will build the external library for your platform.
It requires Go (1.18 or higher) installed.
Script *
This directory contains Solidity Scripts to deploy and interact with Solidity smart contracts:
- Link Token
- Oracle
- Registry
- Flux and Offchain aggregators
- Chainlink Consumer contracts
- Helper Solidity Scripts
You can run these scripts with the command: forge script path/to/script [--args]
. Logs and artifacts dedicated to each script run, including a transaction hash and an address of a deployed smart contract, are stored in a corresponding subdirectory of the broadcast folder (created automatically).
More info on Foundry Solidity Scripting can be found here: https://book.getfoundry.sh/tutorials/solidity-scripting?highlight=script#solidity-scripting.
More info on forge script
can be found here: https://book.getfoundry.sh/reference/forge/forge-script.
Src *
Interfaces *
This directory contains interfaces to interact with Solidity contracts deployed using its pre-built artifacts. This is necessary in order to reduce dependence on a specific version of Solidity compiler.
Mocks *
This directory contains mock Solidity contracts used for testing purposes:
- MockAccessController.sol - mock contract used during deployment of Offchain Aggregator contract
- MockAggregatorValidator.sol - mock contract used during deployment of Flux Aggregator contract
- MockEthFeed.sol - mock contract used during deployment of Registry contract
- MockGasFeed.sol - mock contract used during deployment of Registry contract
Note
Foundry-Chainlink toolkit intended to support different compiler versions in range[>=0.6.2 <0.9.0]
.
It was tested e2e with these solc versions:
- 0.6.2
- 0.6.12
- 0.7.6
- 0.8.12
Therefore, you can specify any supported Solidity compiler version in your foundry.toml.
In case you find any problems you are welcome to open an issue.
This project based on https://github.com/protofire/hardhat-chainlink-plugin.