Intgerated chainlink priceFeed in eth exchangeRate

This commit is contained in:
Thrilok Kumar 2020-09-13 04:12:33 +05:30
parent 052e05bc8d
commit d4f9ccfc7b

View File

@ -9,6 +9,8 @@ interface CTokenInterface {
function exchangeRateCurrent() external returns (uint256); function exchangeRateCurrent() external returns (uint256);
function balanceOf(address owner) external view returns (uint256); function balanceOf(address owner) external view returns (uint256);
function underlying() external view returns (address);
} }
interface CompTroller { interface CompTroller {
@ -28,7 +30,7 @@ interface CurveMapping {
} }
interface CurveRegistry { interface CurveRegistry {
function pool_list(uint) external view returns (address poolAddress, address poolToken, address ctokenPrice); function pool_list(uint) external view returns (address poolAddress, address poolToken);
function pool_count() external view returns (uint); function pool_count() external view returns (uint);
} }
@ -40,12 +42,20 @@ interface TokenInterface {
function balanceOf(address owner) external view returns (uint256); function balanceOf(address owner) external view returns (uint256);
} }
interface PriceFeedInterface {
function getPrices(address[] memory tokens) external view returns (uint256[] memory pricesInETH);
function getPrice(address token) external view returns (uint256 priceInETH);
function getEthPrice() external view returns (uint256 ethPriceUSD);
}
contract EthRateLogic is DSMath { contract EthRateLogic is DSMath {
address public immutable poolToken; address public immutable poolToken;
address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
address public constant ctokenMapping = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant ctokenMapping = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
address public constant curveRegistryAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant curveRegistryAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88);
@ -53,11 +63,11 @@ contract EthRateLogic is DSMath {
uint totalSupplyInETH; uint totalSupplyInETH;
uint totalBorrowInETH; uint totalBorrowInETH;
address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets();
OracleComp oracleContract = OracleComp(compOracleAddr); PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); // uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr);
for (uint i = 0; i < allMarkets.length; i++) { for (uint i = 0; i < allMarkets.length; i++) {
CTokenInterface ctoken = CTokenInterface(allMarkets[i]); CTokenInterface ctoken = CTokenInterface(allMarkets[i]);
uint tokenPriceInETH = wdiv(oracleContract.getUnderlyingPrice(address(ctoken)), ethPrice); uint tokenPriceInETH = priceFeedContract.getPrice(address(ctoken) == cethAddr ? ethAddr : ctoken.underlying());
uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent()); uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent());
uint supplyInETH = wmul(supply, tokenPriceInETH); uint supplyInETH = wmul(supply, tokenPriceInETH);
@ -70,18 +80,18 @@ contract EthRateLogic is DSMath {
_netBal = sub(totalSupplyInETH, totalBorrowInETH); _netBal = sub(totalSupplyInETH, totalBorrowInETH);
} }
function getCurveNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { function getCurveNetAssetsInEth(address _dsa) private view returns (uint256 _netBal) {
// NOTICE - only stable coins pool as of now
CurveRegistry curveRegistry = CurveRegistry(curveRegistryAddr); CurveRegistry curveRegistry = CurveRegistry(curveRegistryAddr);
uint poolLen = curveRegistry.pool_count(); uint poolLen = curveRegistry.pool_count();
OracleComp oracleContract = OracleComp(compOracleAddr); PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr);
uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); uint ethPriceUSD = priceFeedContract.getEthPrice();
for (uint i = 0; i < poolLen; i++) { for (uint i = 0; i < poolLen; i++) {
(address curvePoolAddr, address curveTokenAddr, address ctokenPriceAddr) = curveRegistry.pool_list(i); (address curvePoolAddr, address curveTokenAddr) = curveRegistry.pool_list(i);
uint tokenPriceInETH = wdiv(oracleContract.getUnderlyingPrice(address(ctokenPriceAddr)), ethPrice);
uint virtualPrice = ICurve(curvePoolAddr).get_virtual_price(); uint virtualPrice = ICurve(curvePoolAddr).get_virtual_price();
uint curveTokenBal = TokenInterface(curveTokenAddr).balanceOf(_dsa); uint curveTokenBal = TokenInterface(curveTokenAddr).balanceOf(_dsa);
uint amtInUSD = wmul(curveTokenBal, virtualPrice); uint amtInUSD = wmul(curveTokenBal, virtualPrice);
uint amtInETH = wmul(amtInUSD, tokenPriceInETH); uint amtInETH = wmul(amtInUSD, ethPriceUSD);
_netBal = add(_netBal, amtInETH); _netBal = add(_netBal, amtInETH);
} }
} }