An example project for reading API3 data feeds on Avalanche testnet
API3 serves three kinds of data feeds:
- Beacons: Single-source data feeds, addressed by the hash of their parameters
- Beacon sets: Aggregation of Beacons, addressed by the hash of the IDs of the aggregated Beacons
- dAPIs: A managed data feed that is pointed to a Beacon or a Beacon set, addressed by its human-readable name
In this repo, we inherit the DapiReader contract to implement an example data feed reader contract that reads dAPIs.
Anyone can read an API3 data feed with an off-chain, static call. However, only contracts allowed by an authorized account are allowed to read on-chain. For production use-cases on mainnet, you will need to pay for contract read access. On Avalanche testnet, there is a contract that you can call to allow your contract to do on-chain calls for free for testing purposes, which we use in this repo.
dAPIs are read using their names. Here is the list of dAPI names you can use on Avalanche testnet
Bitcoin Index
(EOD)Ethereum Index
(EOD)BTC/USD
SOL/USD
AVAX/USD
While using the scripts, you will need to specify the dAPI names as the environment variable DAPI_NAME
.
- Install dependencies
yarn
- Enter credentials
cp credentials.example.json credentials.json
# ..and populate credentials.json with your mnemonic
Use the script below to read the data feeds off-chain. You need to do the static call with a VoidSigner with address 0, see the script for details.
DAPI_NAME="AVAX/USD" yarn run:off-chain-read-with-name
-
Get testnet AVAX from the faucet
-
Deploy DataFeedReaderExample
yarn deploy
First send a transaction to allow the deployed DataFeedReaderExample contract to read the dAPI. Note that you only need to do this once, and you can only do this on Avalanche testnet.
DAPI_NAME="AVAX/USD" yarn run:allow-to-read-with-name
Then you can use the script below to have the DataFeedReaderExample contract read the dAPI.
DAPI_NAME="AVAX/USD" yarn run:read-with-name
You can also omit reading the timestamp and only get the value.
DAPI_NAME="AVAX/USD" yarn run:read-value-with-name
A MockDapiServer contract is provided for local development and testing. See the tests for its usage, and run the tests with
yarn test