Skip to content

Commit be12ca5

Browse files
Merge pull request #299 from Midas-Protocol/feat/add-ankr-bnb-oracle
ankr bnbc oracle added.
2 parents e139e77 + 642f9dc commit be12ca5

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
import { ERC20Upgradeable } from "openzeppelin-contracts-upgradeable/contracts/token/ERC20/ERC20Upgradeable.sol";
5+
6+
import { ICToken } from "../../external/compound/ICToken.sol";
7+
import { MasterPriceOracle } from "../MasterPriceOracle.sol";
8+
import { ICErc20 } from "../../external/compound/ICErc20.sol";
9+
import "../../external/compound/IPriceOracle.sol";
10+
import "../BasePriceOracle.sol";
11+
12+
interface AnkrOracle {
13+
function peek() external view returns (bytes32, bool);
14+
}
15+
16+
contract AnkrBNBcPriceOracle is IPriceOracle, BasePriceOracle {
17+
MasterPriceOracle public immutable MASTER_PRICE_ORACLE;
18+
AnkrOracle private immutable ANKR_ORACLE;
19+
address public immutable BASE_TOKEN;
20+
address public immutable USD_TOKEN;
21+
uint256 private lastPrice = 0;
22+
23+
constructor(
24+
AnkrOracle _ankrOracle,
25+
MasterPriceOracle masterPriceOracle,
26+
address _baseToken,
27+
address usdToken
28+
) {
29+
MASTER_PRICE_ORACLE = masterPriceOracle;
30+
ANKR_ORACLE = _ankrOracle;
31+
USD_TOKEN = usdToken;
32+
BASE_TOKEN = _baseToken;
33+
}
34+
35+
function getUnderlyingPrice(ICToken cToken) external view override returns (uint256) {
36+
// Return 1e18 for ETH
37+
if (cToken.isCEther()) return 1e18;
38+
39+
// Get underlying token address
40+
address underlying = ICErc20(address(cToken)).underlying();
41+
42+
require(underlying == BASE_TOKEN, "Invalid underlying");
43+
// Get price
44+
uint256 oraclePrice = _price();
45+
46+
// Format and return price
47+
uint256 underlyingDecimals = uint256(ERC20Upgradeable(underlying).decimals());
48+
return
49+
underlyingDecimals <= 18
50+
? uint256(oraclePrice) * (10**(18 - underlyingDecimals))
51+
: uint256(oraclePrice) / (10**(underlyingDecimals - 18));
52+
}
53+
54+
function price(address underlying) external view override returns (uint256) {
55+
require(underlying == BASE_TOKEN, "Invalid underlying.");
56+
return _price();
57+
}
58+
59+
function _price() internal view returns (uint256) {
60+
// aBNBc/BUSD price
61+
(bytes32 price, bool success) = ANKR_ORACLE.peek();
62+
63+
if (success) {
64+
uint256 BusdBnbPrice = MASTER_PRICE_ORACLE.price(USD_TOKEN);
65+
66+
return (uint256(price) / 10**18) * BusdBnbPrice;
67+
}
68+
69+
return 0;
70+
}
71+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.0;
3+
4+
import "../../config/BaseTest.t.sol";
5+
import { AnkrBNBcPriceOracle, AnkrOracle } from "../../../oracles/default/AnkrBNBcPriceOracle.sol";
6+
import { MasterPriceOracle } from "../../../oracles/MasterPriceOracle.sol";
7+
8+
contract AnkrBNBcPriceOracleTest is BaseTest {
9+
AnkrBNBcPriceOracle private oracle;
10+
11+
function setUp() public shouldRun(forChains(BSC_MAINNET)) {
12+
oracle = new AnkrBNBcPriceOracle(
13+
AnkrOracle(0xB1aD00B8BB49FB3534120b43f1FEACeAf584AE06),
14+
MasterPriceOracle(0xB641c21124546e1c979b4C1EbF13aB00D43Ee8eA),
15+
0xE85aFCcDaFBE7F2B096f268e31ccE3da8dA2990A,
16+
ap.getAddress("bUSD")
17+
);
18+
}
19+
20+
function testPrice() public shouldRun(forChains(BSC_MAINNET)) {
21+
uint256 price = oracle.price(0xE85aFCcDaFBE7F2B096f268e31ccE3da8dA2990A);
22+
assertEq(price, 1019848384416702590);
23+
}
24+
}

0 commit comments

Comments
 (0)