|
| 1 | +pragma solidity ^0.5.16; |
| 2 | +pragma experimental ABIEncoderV2; |
| 3 | + |
| 4 | +import "../CErc20.sol"; |
| 5 | +import "../CToken.sol"; |
| 6 | +import "../Comptroller.sol"; |
| 7 | +import "../EIP20Interface.sol"; |
| 8 | + |
| 9 | +contract CompoundLens { |
| 10 | + struct CTokenMetadata { |
| 11 | + address cToken; |
| 12 | + uint exchangeRateCurrent; |
| 13 | + uint supplyRatePerBlock; |
| 14 | + uint borrowRatePerBlock; |
| 15 | + uint reserveFactorMantissa; |
| 16 | + uint totalBorrows; |
| 17 | + uint totalReserves; |
| 18 | + uint totalSupply; |
| 19 | + uint totalCash; |
| 20 | + bool isListed; |
| 21 | + uint collateralFactorMantissa; |
| 22 | + address underlyingAssetAddress; |
| 23 | + uint cTokenDecimals; |
| 24 | + uint underlyingDecimals; |
| 25 | + } |
| 26 | + |
| 27 | + function cTokenMetadata(CToken cToken) public returns (CTokenMetadata memory) { |
| 28 | + uint exchangeRateCurrent = cToken.exchangeRateCurrent(); |
| 29 | + Comptroller comptroller = Comptroller(address(cToken.comptroller())); |
| 30 | + (bool isListed, uint collateralFactorMantissa) = comptroller.markets(address(cToken)); |
| 31 | + address underlyingAssetAddress; |
| 32 | + uint underlyingDecimals; |
| 33 | + |
| 34 | + if (compareStrings(cToken.symbol(), "cETH")) { |
| 35 | + underlyingAssetAddress = address(0); |
| 36 | + underlyingDecimals = 18; |
| 37 | + } else { |
| 38 | + CErc20 cErc20 = CErc20(address(cToken)); |
| 39 | + underlyingAssetAddress = cErc20.underlying(); |
| 40 | + underlyingDecimals = EIP20Interface(cErc20.underlying()).decimals(); |
| 41 | + } |
| 42 | + |
| 43 | + return CTokenMetadata({ |
| 44 | + cToken: address(cToken), |
| 45 | + exchangeRateCurrent: exchangeRateCurrent, |
| 46 | + supplyRatePerBlock: cToken.supplyRatePerBlock(), |
| 47 | + borrowRatePerBlock: cToken.borrowRatePerBlock(), |
| 48 | + reserveFactorMantissa: cToken.reserveFactorMantissa(), |
| 49 | + totalBorrows: cToken.totalBorrows(), |
| 50 | + totalReserves: cToken.totalReserves(), |
| 51 | + totalSupply: cToken.totalSupply(), |
| 52 | + totalCash: cToken.getCash(), |
| 53 | + isListed: isListed, |
| 54 | + collateralFactorMantissa: collateralFactorMantissa, |
| 55 | + underlyingAssetAddress: underlyingAssetAddress, |
| 56 | + cTokenDecimals: cToken.decimals(), |
| 57 | + underlyingDecimals: underlyingDecimals |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + function cTokenMetadataAll(CToken[] calldata cTokens) external returns (CTokenMetadata[] memory) { |
| 62 | + uint cTokenCount = cTokens.length; |
| 63 | + CTokenMetadata[] memory res = new CTokenMetadata[](cTokenCount); |
| 64 | + for (uint i = 0; i < cTokenCount; i++) { |
| 65 | + res[i] = cTokenMetadata(cTokens[i]); |
| 66 | + } |
| 67 | + return res; |
| 68 | + } |
| 69 | + |
| 70 | + struct CTokenBalances { |
| 71 | + address cToken; |
| 72 | + uint balanceOf; |
| 73 | + uint borrowBalanceCurrent; |
| 74 | + uint balanceOfUnderlying; |
| 75 | + uint tokenBalance; |
| 76 | + uint tokenAllowance; |
| 77 | + } |
| 78 | + |
| 79 | + function cTokenBalances(CToken cToken, address payable account) public returns (CTokenBalances memory) { |
| 80 | + uint balanceOf = cToken.balanceOf(account); |
| 81 | + uint borrowBalanceCurrent = cToken.borrowBalanceCurrent(account); |
| 82 | + uint balanceOfUnderlying = cToken.balanceOfUnderlying(account); |
| 83 | + uint tokenBalance; |
| 84 | + uint tokenAllowance; |
| 85 | + |
| 86 | + if (compareStrings(cToken.symbol(), "cETH")) { |
| 87 | + tokenBalance = account.balance; |
| 88 | + tokenAllowance = account.balance; |
| 89 | + } else { |
| 90 | + CErc20 cErc20 = CErc20(address(cToken)); |
| 91 | + EIP20Interface underlying = EIP20Interface(cErc20.underlying()); |
| 92 | + tokenBalance = underlying.balanceOf(account); |
| 93 | + tokenAllowance = underlying.allowance(account, address(cToken)); |
| 94 | + } |
| 95 | + |
| 96 | + return CTokenBalances({ |
| 97 | + cToken: address(cToken), |
| 98 | + balanceOf: balanceOf, |
| 99 | + borrowBalanceCurrent: borrowBalanceCurrent, |
| 100 | + balanceOfUnderlying: balanceOfUnderlying, |
| 101 | + tokenBalance: tokenBalance, |
| 102 | + tokenAllowance: tokenAllowance |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + function cTokenBalancesAll(CToken[] calldata cTokens, address payable account) external returns (CTokenBalances[] memory) { |
| 107 | + uint cTokenCount = cTokens.length; |
| 108 | + CTokenBalances[] memory res = new CTokenBalances[](cTokenCount); |
| 109 | + for (uint i = 0; i < cTokenCount; i++) { |
| 110 | + res[i] = cTokenBalances(cTokens[i], account); |
| 111 | + } |
| 112 | + return res; |
| 113 | + } |
| 114 | + |
| 115 | + struct CTokenUnderlyingPrice { |
| 116 | + address cToken; |
| 117 | + uint underlyingPrice; |
| 118 | + } |
| 119 | + |
| 120 | + function cTokenUnderlyingPrice(CToken cToken) public returns (CTokenUnderlyingPrice memory) { |
| 121 | + Comptroller comptroller = Comptroller(address(cToken.comptroller())); |
| 122 | + PriceOracle priceOracle = comptroller.oracle(); |
| 123 | + |
| 124 | + return CTokenUnderlyingPrice({ |
| 125 | + cToken: address(cToken), |
| 126 | + underlyingPrice: priceOracle.getUnderlyingPrice(cToken) |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + function cTokenUnderlyingPriceAll(CToken[] calldata cTokens) external returns (CTokenUnderlyingPrice[] memory) { |
| 131 | + uint cTokenCount = cTokens.length; |
| 132 | + CTokenUnderlyingPrice[] memory res = new CTokenUnderlyingPrice[](cTokenCount); |
| 133 | + for (uint i = 0; i < cTokenCount; i++) { |
| 134 | + res[i] = cTokenUnderlyingPrice(cTokens[i]); |
| 135 | + } |
| 136 | + return res; |
| 137 | + } |
| 138 | + |
| 139 | + struct AccountLimits { |
| 140 | + CToken[] markets; |
| 141 | + uint liquidity; |
| 142 | + uint shortfall; |
| 143 | + } |
| 144 | + |
| 145 | + function getAccountLimits(Comptroller comptroller, address account) public returns (AccountLimits memory) { |
| 146 | + (uint errorCode, uint liquidity, uint shortfall) = comptroller.getAccountLiquidity(account); |
| 147 | + require(errorCode == 0); |
| 148 | + |
| 149 | + return AccountLimits({ |
| 150 | + markets: comptroller.getAssetsIn(account), |
| 151 | + liquidity: liquidity, |
| 152 | + shortfall: shortfall |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + function compareStrings(string memory a, string memory b) internal pure returns (bool) { |
| 157 | + return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b)))); |
| 158 | + } |
| 159 | +} |
0 commit comments