Skip to content

Commit

Permalink
feat: implement AggregatorRouter contract
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jan 10, 2024
1 parent ec3c334 commit e40e910
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
```
32 changes: 26 additions & 6 deletions contracts/DataFeedConsumer.sol
Original file line number Diff line number Diff line change
@@ -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_;
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions deploy/0-DataFeedConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Check failure on line 7 in deploy/0-DataFeedConsumer.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·deployer,·aggregatorProxy:·aggregatorProxyAddress,·aggregatorRouter:·aggregatorRouterAddress` with `⏎····deployer,⏎····aggregatorProxy:·aggregatorProxyAddress,⏎····aggregatorRouter:·aggregatorRouterAddress⏎·`

console.log('0-DataFeedConsumer.ts')

Expand All @@ -14,7 +14,7 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
}

await deploy('DataFeedConsumer', {
args: [aggregatorProxyAddress],
args: [aggregatorProxyAddress, aggregatorRouterAddress],
from: deployer,
log: true
})
Expand Down
4 changes: 4 additions & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ const config: HardhatUserConfig = {
// cypress: '0xc2734F4c3Bf4E7c7673b86CB579013ea96295Ed3', // GBP-USD
// cypress: '0x40Ffd98968403078664FD27D9CF9aA8Ca527d101', // JPY-USD
// cypress: '0x369eabfeFdF585D84A714E7989a361D623B523C0', // KRW-USD
},
aggregatorRouter: {
baobab: '0xAF821aaaEdeF65b3bC1668c0b910c5b763dF6354'
//cypress: '0x16937CFc59A8Cd126Dc70A75A4bd3b78f690C861'
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions scripts/read-data-through-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ethers } from 'hardhat'

const pair_name = "BTC-USDT"

Check failure on line 3 in scripts/read-data-through-router.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"BTC-USDT"` with `'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
})

0 comments on commit e40e910

Please sign in to comment.