Web3.js libraries are being sunset on March 4th, 2025. For migration guides and more details please refer to Chainsafe blog
This is a web3.js 4.x plugin for interacting with Chainlink Ethereum contracts.
yarn add @chainsafe/web3.js-chainlink-pluginWhen adding the web3 package to your project, make sure to use version 4.x:
- npm i -S [email protected]
- yarn add [email protected]
NOTE
If 4.x was already released, you are good to just useweb3without appending anything to it.
To verify you have the correct web3 version installed, after adding the package to your project (the above commands), look at the versions listed in your project's package.json under the dependencies section, it should contain version 4.x similar to:
"dependencies": {
	"web3": "4.0.3"
}After importing ChainlinkPlugin from @chainsafe/web3-plugin-chainlink and Web3 from web3, register an instance of ChainlinkPlugin with an instance of Web3 like so:
import { ChainlinkPlugin } from '@chainsafe/web3-plugin-chainlink';
import { Web3 } from 'web3';
const web3 = new Web3('YOUR_PROVIDER_URL');
const chainlinkPlugin = new ChainlinkPlugin();
web3.registerPlugin(chainlinkPlugin);More information about registering web3.js plugins can be found here.
Included in this plugin are two enums that contain the Ethereum contract addresses for specific token pairs: MainnetPriceFeeds and GoerliPriceFeeds. If you cannot find your desired price feed within these enums, please check here to make sure it's supported, and if it is, please open an issue or a pull request for the missing price feed so that it can be added to the appropriate enum.
async getPrice(
		priceFeedAddress: MainnetPriceFeeds | GoerliPriceFeeds | Address,
		aggregatorInterfaceAbi: ContractAbi = defaultAggregatorInterfaceAbi,
	): {
        roundId: bigint,
        answer: bigint,
        startedAt: bigint,
        updatedAt: bigint,
        answeredInRound: bigint
    }defaultAggregatorInterfaceAbi can be found here.
The getPrice method, accepts MainnetPriceFeeds | GoerliPriceFeeds | Address for it's first parameter, and an optional second parameter for specifying the Chainlink Aggregator Interface ABI of the Ethereum smart contract you'd like to interact with (the parameter is defaulted to defaultAggregatorInterfaceAbi).
Under the hood, this method is calling the latestRoundData for the specified price feed, more information about it can be found here.
import { ChainlinkPlugin, MainnetPriceFeeds } from '@chainsafe/web3-plugin-chainlink';
import { Web3 } from 'web3';
const web3 = new Web3('YOUR_PROVIDER_URL');
const chainlinkPlugin = new ChainlinkPlugin();
web3.registerPlugin(chainlinkPlugin);
web3.chainlink.getPrice(MainnetPriceFeeds.LinkEth).then(console.log);
// {
//     roundId: 73786976294838212867n,
//     answer: 4185000000000000n,
//     startedAt: 1674178043n,
//     updatedAt: 1674178043n,
//     answeredInRound: 73786976294838212867n
// }- ✍️ If you found an issue or have a question or suggestion submit an issue or join us on Discord
- Clone the repo
- Run yarnto install dependencies- If you receive the following warning, please remove the file package-lock.jsonand make sure to runyarnto install dependencies instead ofnpm i:
 
- If you receive the following warning, please remove the file 
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.- Run the tests:
- yarn test:unit: Runs the mocked tests that do not make a network request using the Jest framework
- End-to-end tests: Runs Webpack bundled tests that make a network request to the RPC provider https://rpc.ankr.com/ethand returns an actual response fromMainnetPriceFeeds.LinkEthsmart contract using the Cypress framework- yarn test:e2e:chrome: Runs the tests using Chrome
- yarn test:e2e:electron: Runs the tests using Electron
- yarn test:e2e:firefox: Runs the tests using Firefox
 
- Black box tests: Uses a published version of the plugin from Verdaccio to run tests that make a network request to the RPC provider https://rpc.ankr.com/ethand returns an actual response fromMainnetPriceFeeds.LinkEthsmart contract using the Jest framework- NOTE The black box tests are setup to run within Github actions environment, but can be ran locally. The black_box_test_helpers.sh script can be used to:
- start: Start Verdaccio using a Docker container
- stop: Kill the Docker container
- startBackgroundAndPublish: Starts a headless Docker container and publishes the plugin package
- runTests:- cds into the- test/black_boxdirectory, installs the black box package dependencies, and runs- yarn testwhich will use Jest to run the tests
 
- In addition to the black_box_test_helpers.shscript, the black box tests can be ran using the followingpackage.jsonscripts:- yarn pre-black-box: Calls- startBackgroundAndPublishfrom the- black_box_test_helpers.shscript
- yarn test:black-box: Calls- yarn pre-black-boxand- runTestsfrom the from the- black_box_test_helpers.shscript
- yarn post-black-box: Calls- stopfrom the- black_box_test_helpers.shscript
 
 
- NOTE The black box tests are setup to run within Github actions environment, but can be ran locally. The black_box_test_helpers.sh script can be used to:
 
| Script | Description | 
|---|---|
| build | Uses tscto build package and dependent packages | 
| build:web | Uses webpackto build a browser ready build of the plugin indistdirectory | 
| clean | Uses rimrafto removelib/anddist/ | 
| format | Uses prettierto format the code | 
| lint | Uses eslintto lint package | 
| lint:fix | Uses eslintto check and fix any warnings | 
| post-black-box | Uses stopfromblack_box_test_helpers.shto kill running Verdaccio Docker container | 
| pre-black-box | Uses startBackgroundAndPublishfromblack_box_test_helpers.shto start a Verdaccio Docker container and publish the plugin package to it | 
| prebuild | Calls yarn clean | 
| prepare | Installs husky | 
| test | Uses jestto run unit tests | 
| test:black-box | Calls yarn pre-black-boxandrunTestsfromblack_box_test_helpers.shto run black box tests | 
| test:coverage | Uses jestto report test coverage | 
| test:e2e:chrome | Users cypressto rune2etest in a Chrome environment | 
| test:e2e:firefox | Users cypressto rune2etest in a Firefox environment | 
| test:e2e:electron | Users cypressto rune2etest in a Electron environment | 
| test:unit | Uses jestto run tests under/test/unit |