From e40e910bddc221fd9c6a58bf39931637284882b0 Mon Sep 17 00:00:00 2001 From: nick Date: Wed, 10 Jan 2024 13:37:53 +0700 Subject: [PATCH] feat: implement AggregatorRouter contract --- README.md | 13 ++++++++++++ contracts/DataFeedConsumer.sol | 32 +++++++++++++++++++++++------ deploy/0-DataFeedConsumer.ts | 4 ++-- hardhat.config.ts | 4 ++++ scripts/read-data-through-router.ts | 20 ++++++++++++++++++ 5 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 scripts/read-data-through-router.ts diff --git a/README.md b/README.md index b27888f..343aa1f 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,11 @@ Currently, you can access the following data feeds from Orakl Network. If you want to access different data feeds, you can change the aggregator proxy address (`aggregatorProxy`) inside of `hardhat.config.ts` under `namedAccounts` property. +## AggregatorRouter + +- Baobab: [0xAF821aaaEdeF65b3bC1668c0b910c5b763dF6354](https://baobab.klaytnfinder.io/account/0xAF821aaaEdeF65b3bC1668c0b910c5b763dF6354) +- Cypress: [0x16937CFc59A8Cd126Dc70A75A4bd3b78f690C861](https://www.klaytnfinder.io/account/0x16937CFc59A8Cd126Dc70A75A4bd3b78f690C861) + ## Prerequisites Create a copy of `.env.example` file and fill in values for `PROVIDER`, and `MNEMONIC` or `PRIV_KEY` (the difference is explained below) environment variables. @@ -156,3 +161,11 @@ To deploy `DataFeedConsumer`, run `npx hardhat deploy --network baobab`. ```shell npx hardhat run scripts/read-data.ts --network baobab ``` + +## Request the latest value from Data Feed through RouterContract + +Pair name's fixed with "BTC-USDT", try out getting latest value of different pairs by changing pair name from the script. + +```shell +npx hardhat run scripts/read-data-through-router.ts --network baobab +``` diff --git a/contracts/DataFeedConsumer.sol b/contracts/DataFeedConsumer.sol index 5dd4f73..da4f849 100644 --- a/contracts/DataFeedConsumer.sol +++ b/contracts/DataFeedConsumer.sol @@ -1,24 +1,27 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.16; -import { IAggregator } from "@bisonai/orakl-contracts/src/v0.1/interfaces/IAggregator.sol"; +import {IAggregator} from "@bisonai/orakl-contracts/src/v0.1/interfaces/IAggregator.sol"; +import {IAggregatorRouter} from "@bisonai/orakl-contracts/src/v0.1/interfaces/IAggregatorRouter.sol"; contract DataFeedConsumer { IAggregator internal dataFeed; + IAggregatorRouter internal router; int256 public answer; uint80 public roundId; - constructor(address aggregatorProxy) { + constructor(address aggregatorProxy, address aggregatorRouter) { dataFeed = IAggregator(aggregatorProxy); + router = IAggregatorRouter(aggregatorRouter); } function getLatestData() public { ( uint80 roundId_, - int256 answer_ - , /* uint startedAt */ - , /* uint updatedAt */ - , /* uint80 answeredInRound */ + int256 answer_ /* uint startedAt */ /* uint updatedAt */ /* uint80 answeredInRound */, + , + , + ) = dataFeed.latestRoundData(); answer = answer_; @@ -28,4 +31,21 @@ contract DataFeedConsumer { function decimals() public view returns (uint8) { return dataFeed.decimals(); } + + function getLatestDataThroughRouter(string pair) public { + ( + uint80 roundId_, + int256 answer_ /* uint startedAt */ /* uint updatedAt */ /* uint80 answeredInRound */, + , + , + + ) = router.latestRoundData(pair); + + answer = answer_; + roundId = roundId_; + } + + function decimalsThroughRouter(string pair) public view returns (uint8) { + return router.decimals(pair); + } } diff --git a/deploy/0-DataFeedConsumer.ts b/deploy/0-DataFeedConsumer.ts index c215512..f8b88c2 100644 --- a/deploy/0-DataFeedConsumer.ts +++ b/deploy/0-DataFeedConsumer.ts @@ -4,7 +4,7 @@ import { DeployFunction } from 'hardhat-deploy/types' const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { const { deployments, getNamedAccounts, network } = hre const { deploy } = deployments - const { deployer, aggregatorProxy: aggregatorProxyAddress } = await getNamedAccounts() + const { deployer, aggregatorProxy: aggregatorProxyAddress, aggregatorRouter: aggregatorRouterAddress } = await getNamedAccounts() console.log('0-DataFeedConsumer.ts') @@ -14,7 +14,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) { } await deploy('DataFeedConsumer', { - args: [aggregatorProxyAddress], + args: [aggregatorProxyAddress, aggregatorRouterAddress], from: deployer, log: true }) diff --git a/hardhat.config.ts b/hardhat.config.ts index ec083ac..6c5f8fa 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -118,6 +118,10 @@ const config: HardhatUserConfig = { // cypress: '0xc2734F4c3Bf4E7c7673b86CB579013ea96295Ed3', // GBP-USD // cypress: '0x40Ffd98968403078664FD27D9CF9aA8Ca527d101', // JPY-USD // cypress: '0x369eabfeFdF585D84A714E7989a361D623B523C0', // KRW-USD + }, + aggregatorRouter: { + baobab: '0xAF821aaaEdeF65b3bC1668c0b910c5b763dF6354' + //cypress: '0x16937CFc59A8Cd126Dc70A75A4bd3b78f690C861' } } } diff --git a/scripts/read-data-through-router.ts b/scripts/read-data-through-router.ts new file mode 100644 index 0000000..a838843 --- /dev/null +++ b/scripts/read-data-through-router.ts @@ -0,0 +1,20 @@ +import { ethers } from 'hardhat' + +const pair_name = "BTC-USDT" + +async function main() { + const userContract = await ethers.getContract('DataFeedConsumer') + console.log('DataFeedConsumer', userContract.address) + + await (await userContract.getLatestDataThroughRouter(pair_name)).wait() + + const answer = await userContract.answer() + const roundId = await userContract.roundId() + console.log(`answer ${answer.toString()}`) + console.log(`roundId ${roundId.toString()}`) +} + +main().catch((error) => { + console.error(error) + process.exitCode = 1 +})