Skip to content

Commit

Permalink
fix: update based on feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jan 10, 2024
1 parent b789bf7 commit 1595227
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ yarn compile
## Deploy

```shell
npx hardhat deploy --network baobab
# aggregator example
npx hardhat deploy --network baobab --deploy-scripts deploy/DataFeedConsumer
```

```shell
# router example
npx hardhat deploy --network baobab --deploy-scripts deploy/DataFeedRouterConsumer
```

## Request the latest value from Data Feed
Expand Down
23 changes: 2 additions & 21 deletions contracts/DataFeedConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@
pragma solidity ^0.8.16;

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, address aggregatorRouter) {
constructor(address aggregatorProxy) {
dataFeed = IAggregator(aggregatorProxy);
router = IAggregatorRouter(aggregatorRouter);
}

function getLatestData() public {
Expand All @@ -31,21 +29,4 @@ contract DataFeedConsumer {
function decimals() public view returns (uint8) {
return dataFeed.decimals();
}

function getLatestDataThroughRouter(string calldata pair) public {
(
uint80 roundId_,
int256 answer_
, /* uint startedAt */
, /* uint updatedAt */
, /* uint80 answeredInRound */
) = router.latestRoundData(pair);

answer = answer_;
roundId = roundId_;
}

function decimalsThroughRouter(string calldata pair) public view returns (uint8) {
return router.decimals(pair);
}
}
32 changes: 32 additions & 0 deletions contracts/DataFeedRouterConsumer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

import {IAggregatorRouter} from "@bisonai/orakl-contracts/src/v0.1/interfaces/IAggregatorRouter.sol";

contract DataFeedRouterConsumer {
IAggregatorRouter internal router;
int256 public answer;
uint80 public roundId;

constructor(address aggregatorRouter) {
router = IAggregatorRouter(aggregatorRouter);
}


function getLatestData(string calldata pair) public {
(
uint80 roundId_,
int256 answer_
, /* uint startedAt */
, /* uint updatedAt */
, /* uint80 answeredInRound */
) = router.latestRoundData(pair);

answer = answer_;
roundId = roundId_;
}

function decimals(string calldata pair) public view returns (uint8) {
return router.decimals(pair);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +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,
aggregatorRouter: aggregatorRouterAddress
} = await getNamedAccounts()
const { deployer, aggregatorProxy: aggregatorProxyAddress } = await getNamedAccounts()

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

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

await deploy('DataFeedConsumer', {
args: [aggregatorProxyAddress, aggregatorRouterAddress],
args: [aggregatorProxyAddress],
from: deployer,
log: true
})
Expand Down
25 changes: 25 additions & 0 deletions deploy/DataFeedRouterConsumer/DataFeedRouterConsumer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { DeployFunction } from 'hardhat-deploy/types'

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const { deployments, getNamedAccounts, network } = hre
const { deploy } = deployments
const { deployer, aggregatorRouter: aggregatorRouterAddress } = await getNamedAccounts()

console.log('1-DataFeedRouterConsumer.ts')

if (network.name != 'baobab') {
console.log('Skipping')
return
}

await deploy('DataFeedRouterConsumer', {
args: [aggregatorRouterAddress],
from: deployer,
log: true
})
}

export default func
func.id = 'deploy-data-feed-router-consumer'
func.tags = ['data-feed-router-consumer']
6 changes: 3 additions & 3 deletions scripts/read-data-through-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ethers } from 'hardhat'
const pair_name = 'BTC-USDT'

async function main() {
const userContract = await ethers.getContract('DataFeedConsumer')
console.log('DataFeedConsumer', userContract.address)
const userContract = await ethers.getContract('DataFeedRouterConsumer')
console.log('DataFeedRouterConsumer', userContract.address)

await (await userContract.getLatestDataThroughRouter(pair_name)).wait()
await (await userContract.getLatestData(pair_name)).wait()

const answer = await userContract.answer()
const roundId = await userContract.roundId()
Expand Down

0 comments on commit 1595227

Please sign in to comment.