Skip to content
This repository was archived by the owner on Aug 26, 2024. It is now read-only.

Commit 5a72159

Browse files
authored
Merge pull request #45 from ionicprotocol/feat/redstone-oracle
Redstone oracle
2 parents 1b5701f + 333f67d commit 5a72159

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.0;
3+
4+
interface IRedstoneOracle {
5+
function priceOf(address asset) external view returns (uint256);
6+
7+
function priceOfETH() external view returns (uint256);
8+
9+
function getDataFeedIdForAsset(address asset) external view returns (bytes32);
10+
11+
function getDataFeedIds() external view returns (bytes32[] memory dataFeedIds);
12+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
import "openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol";
5+
6+
import "../../external/redstone/IRedstoneOracle.sol";
7+
import "../BasePriceOracle.sol";
8+
9+
/**
10+
* @title RedstoneAdapterPriceOracle
11+
* @notice Returns prices from Redstone.
12+
* @dev Implements `BasePriceOracle`.
13+
* @author Veliko Minkov <[email protected]> (https://github.com/vminkov)
14+
*/
15+
contract RedstoneAdapterPriceOracle is BasePriceOracle {
16+
/**
17+
* @notice The Redstone oracle contract
18+
*/
19+
IRedstoneOracle public REDSTONE_ORACLE;
20+
21+
/**
22+
* @dev Constructor to set admin, wtoken address and native token USD price feed address
23+
* @param redstoneOracle The Redstone oracle contract address
24+
*/
25+
constructor(address redstoneOracle) {
26+
REDSTONE_ORACLE = IRedstoneOracle(redstoneOracle);
27+
}
28+
29+
/**
30+
* @notice Internal function returning the price in of `underlying`.
31+
* @dev will return a price denominated in the native token
32+
*/
33+
function _price(address underlying) internal view returns (uint256) {
34+
uint256 priceInUsd = REDSTONE_ORACLE.priceOf(underlying);
35+
uint256 priceOfNativeInUsd = REDSTONE_ORACLE.priceOfETH();
36+
return (priceInUsd * 1e18) / priceOfNativeInUsd;
37+
}
38+
39+
/**
40+
* @notice Returns the price in of `underlying` either in the
41+
* native token (implements `BasePriceOracle`).
42+
*/
43+
function price(address underlying) external view override returns (uint256) {
44+
return _price(underlying);
45+
}
46+
47+
/**
48+
* @notice Returns the price in WNATIVE of the token underlying `cToken`.
49+
* @dev Implements the `BasePriceOracle` interface for Ionic pools (and Compound v2).
50+
* @return Price in WNATIVE of the token underlying `cToken`, scaled by `10 ** (36 - underlyingDecimals)`.
51+
*/
52+
function getUnderlyingPrice(ICErc20 cToken) external view override returns (uint256) {
53+
// Get underlying token address
54+
address underlying = cToken.underlying();
55+
56+
uint256 oraclePrice = _price(underlying);
57+
58+
uint256 underlyingDecimals = uint256(ERC20Upgradeable(underlying).decimals());
59+
return
60+
underlyingDecimals <= 18
61+
? uint256(oraclePrice) * (10**(18 - underlyingDecimals))
62+
: uint256(oraclePrice) / (10**(underlyingDecimals - 18));
63+
}
64+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
import { TransparentUpgradeableProxy } from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
5+
6+
import { RedstoneAdapterPriceOracle } from "../../oracles/default/RedstoneAdapterPriceOracle.sol";
7+
8+
import { BaseTest } from "../config/BaseTest.t.sol";
9+
10+
contract RedstoneAdapterOracleTest is BaseTest {
11+
RedstoneAdapterPriceOracle public oracle;
12+
address public redstoneOracleAddress;
13+
address MODE_USDC = 0xd988097fb8612cc24eeC14542bC03424c656005f;
14+
address MODE_EZETH = 0x2416092f143378750bb29b79eD961ab195CcEea5;
15+
address MODE_WBTC = 0xcDd475325D6F564d27247D1DddBb0DAc6fA0a5CF;
16+
17+
function afterForkSetUp() internal override {
18+
if (block.chainid == MODE_MAINNET) {
19+
redstoneOracleAddress = 0x7C1DAAE7BB0688C9bfE3A918A4224041c7177256;
20+
}
21+
22+
oracle = new RedstoneAdapterPriceOracle(redstoneOracleAddress);
23+
}
24+
25+
function testPrintPricesMode() public fork(MODE_MAINNET) {
26+
emit log_named_uint("ezETH price (18 dec)", oracle.price(MODE_EZETH));
27+
emit log_named_uint("WBTC price (8 dec)", oracle.price(MODE_WBTC));
28+
}
29+
}

0 commit comments

Comments
 (0)