Overview
The Dev3 Chainlink SDK module is a fully open source TypeScript SDK which enables any frontend developer to fetch the prices of various assets through Chainlink Data Feeds. The SDK works with vanilla npm projects and frontend frameworks like Angular and React. Users can fetch price pairs and other data.
Objective
You will install and use the Dev3 SDK to monitor a Chainlink price feed. Optionally, you can also deploy a Dev3 Chainlink SDK template from the Dev3 dashboard.
Before you begin
Before you start this tutorial, complete the following items:
- Install git
- Run
git --versionto check the installation. You should see an output similar togit version x.x.x.
- Run
- Install Nodejs 16.0.0 or higher
- Run
node --versionto check the installation. You should see an output similar tov16.x.x.
- Run
Install and initialize the SDK
Install the Dev3 SDK in your npm project.
-
Type the following command on your terminal while on your project's root folder:
npm install dev3-sdk -
In your JavaScript or TypeScript file:
-
Import the Chainlink module:
import { Chainlink } from "dev3-sdk" -
Get an RPC URL for the network where you want to monitor price feeds. For example, a public node for Ethereum is
https://ethereum.publicnode.com. -
Fill in your RPC URL and then initialize the SDK by calling:
const ethSDK = Chainlink.instance("https://ethereum.publicnode.com", Chainlink.PriceFeeds.ETH)This sets up reading data feeds for Ethereum mainnet.
-
Consuming data feeds
Once the SDK is initialized, you can start consuming different feeds as outlined in the examples below:
- Add the following code to your JavaScript or TypeScript file:
// AAVE/ETH price feed ethSDK.getFromOracle(ethSDK.feeds.AAVE_ETH).then((res) => { console.log(res.answer.toString()); }); - Run your file. For example:
node index.js - The output should be similar to:
40104190000000000. Compare this to the AAVE / ETH price feed, which returnsΞ0.04010419at the time of writing.
- Add the following code to your JavaScript or TypeScript file:
// HBTC PoR ethSDK.getFromOracle(ethSDK.feeds.HBTC_POR).then((res) => { console.log(res.answer.toString()); }); - Run your file. For example:
node index.js - The output should be similar to:
970807798330000000000. Compare this to the HBTC PoR price feed, which returns970.81at the time of writing.
Full list of feeds
You can find all available feeds by visiting data.chain.link.
Reading data
The data is returned in the form of a RoundDataModel object:
export interface RoundDataModel {
roundID: BigNumber
answer: BigNumber
formattedAnswer?: string
startedAt: BigNumber
updatedAt: BigNumber
answeredInRound: BigNumber
assetName?: string
dataFeedName?: string
}
Deployable templates
The Dev3 SDK includes Chainlink interfaces and contracts that you can deploy from the Dev3 dashboard. You can use these interfaces and contracts from within your frontends and/or wallets.
To deploy them, navigate to the Dev3 dashboard and choose the Deploy from template option in your workspace. Search for "chainlink" within your Dev3 workspace, or view the source code directly:
Additional information
- The source code for this extension is extracted to the GitHub repo so you can also use it as a standalone module.
- Dev3 Chainlink SDK extracts all the pair contracts addresses for all networks that are compatible into code generated classes, so all modern editors will support full code autocomplete.
