From 04ee337caf84998fe78a85c8c914490b4793e547 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 31 Aug 2020 22:21:02 +0530 Subject: [PATCH] Updated compound resolver --- contracts/protocols/compound.sol | 56 ++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/contracts/protocols/compound.sol b/contracts/protocols/compound.sol index da3456d..a74badb 100644 --- a/contracts/protocols/compound.sol +++ b/contracts/protocols/compound.sol @@ -6,11 +6,13 @@ interface CTokenInterface { function borrowRatePerBlock() external view returns (uint); function supplyRatePerBlock() external view returns (uint); function borrowBalanceStored(address) external view returns (uint); - + + function underlying() external view returns (address); function balanceOf(address) external view returns (uint); } interface TokenInterface { + function decimals() external view returns (uint); function balanceOf(address) external view returns (uint); } @@ -41,9 +43,33 @@ interface CompReadInterface { ) external returns (CompBalanceMetadataExt memory); } +contract DSMath { + function add(uint x, uint y) internal pure returns (uint z) { + require((z = x + y) >= x, "math-not-safe"); + } -contract Helpers { + function mul(uint x, uint y) internal pure returns (uint z) { + require(y == 0 || (z = x * y) / y == x, "math-not-safe"); + } + + uint constant WAD = 10 ** 18; + + function wmul(uint x, uint y) internal pure returns (uint z) { + z = add(mul(x, y), WAD / 2) / WAD; + } + + function wdiv(uint x, uint y) internal pure returns (uint z) { + z = add(mul(x, WAD), y / 2) / y; + } + + function sub(uint x, uint y) internal pure returns (uint z) { + require((z = x - y) <= x, "ds-math-sub-underflow"); + } + +} + +contract Helpers is DSMath { /** * @dev get Compound Comptroller */ @@ -52,10 +78,10 @@ contract Helpers { } /** - * @dev get Compound Orcale Address + * @dev get Compound Open Feed Oracle Address */ function getOracleAddress() public pure returns (address) { - return 0xDDc46a3B076aec7ab3Fc37420A8eDd2959764Ec4; + return 0x9B8Eb8b3d6e2e0Db36F41455185FEF7049a35CaE; } /** @@ -65,6 +91,13 @@ contract Helpers { return 0xd513d22422a3062Bd342Ae374b4b9c20E0a9a074; } + /** + * @dev get ETH Address + */ + function getCETHAddress() public pure returns (address) { + return 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5; + } + /** * @dev get Comp Token Address */ @@ -74,7 +107,8 @@ contract Helpers { struct CompData { - uint tokenPrice; + uint tokenPriceInEth; + uint tokenPriceInUsd; uint exchangeRateStored; uint balanceOfUser; uint borrowBalanceStoredUser; @@ -86,12 +120,22 @@ contract Helpers { contract Resolver is Helpers { + function getPriceInEth(CTokenInterface cToken) public view returns (uint priceInETH, uint priceInUSD) { + uint decimals = getCETHAddress() == address(cToken) ? 18 : TokenInterface(cToken.underlying()).decimals(); + uint price = OrcaleComp(getOracleAddress()).getUnderlyingPrice(address(cToken)); + uint ethPrice = OrcaleComp(getOracleAddress()).getUnderlyingPrice(getCETHAddress()); + priceInUSD = price / 10 ** (18 - decimals); + priceInETH = wdiv(priceInUSD, ethPrice); + } + function getCompoundData(address owner, address[] memory cAddress) public view returns (CompData[] memory) { CompData[] memory tokensData = new CompData[](cAddress.length); for (uint i = 0; i < cAddress.length; i++) { CTokenInterface cToken = CTokenInterface(cAddress[i]); + (uint priceInETH, uint priceInUSD) = getPriceInEth(cToken); tokensData[i] = CompData( - OrcaleComp(getOracleAddress()).getUnderlyingPrice(cAddress[i]), + priceInETH, + priceInUSD, cToken.exchangeRateStored(), cToken.balanceOf(owner), cToken.borrowBalanceStored(owner),