2020-03-22 16:38:35 +00:00
|
|
|
pragma solidity ^0.6.0;
|
|
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
|
|
|
|
interface CTokenInterface {
|
|
|
|
function exchangeRateStored() external view returns (uint);
|
|
|
|
function borrowRatePerBlock() external view returns (uint);
|
|
|
|
function supplyRatePerBlock() external view returns (uint);
|
|
|
|
function borrowBalanceStored(address) external view returns (uint);
|
2021-01-16 06:52:36 +00:00
|
|
|
function totalBorrows() external view returns (uint);
|
2020-08-31 16:51:02 +00:00
|
|
|
|
|
|
|
function underlying() external view returns (address);
|
2020-03-22 16:38:35 +00:00
|
|
|
function balanceOf(address) external view returns (uint);
|
2021-04-27 10:57:06 +00:00
|
|
|
function getCash() external view returns (uint);
|
2020-03-22 16:38:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:03:12 +00:00
|
|
|
interface TokenInterface {
|
2020-08-31 16:51:02 +00:00
|
|
|
function decimals() external view returns (uint);
|
2020-06-17 11:03:12 +00:00
|
|
|
function balanceOf(address) external view returns (uint);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-22 16:38:35 +00:00
|
|
|
interface OrcaleComp {
|
|
|
|
function getUnderlyingPrice(address) external view returns (uint);
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:03:12 +00:00
|
|
|
interface ComptrollerLensInterface {
|
2021-01-04 01:22:29 +00:00
|
|
|
function markets(address) external view returns (bool, uint, bool);
|
2020-06-17 11:03:12 +00:00
|
|
|
function getAccountLiquidity(address) external view returns (uint, uint, uint);
|
|
|
|
function claimComp(address) external;
|
|
|
|
function compAccrued(address) external view returns (uint);
|
2021-01-16 06:52:36 +00:00
|
|
|
function borrowCaps(address) external view returns (uint);
|
|
|
|
function borrowGuardianPaused(address) external view returns (bool);
|
2021-01-16 08:13:28 +00:00
|
|
|
function oracle() external view returns (address);
|
2021-04-27 10:57:06 +00:00
|
|
|
function compSpeeds(address) external view returns (uint);
|
2020-06-17 11:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CompReadInterface {
|
|
|
|
struct CompBalanceMetadataExt {
|
|
|
|
uint balance;
|
|
|
|
uint votes;
|
|
|
|
address delegate;
|
|
|
|
uint allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCompBalanceMetadataExt(
|
|
|
|
TokenInterface comp,
|
|
|
|
ComptrollerLensInterface comptroller,
|
|
|
|
address account
|
|
|
|
) external returns (CompBalanceMetadataExt memory);
|
|
|
|
}
|
|
|
|
|
2020-08-31 16:51:02 +00:00
|
|
|
contract DSMath {
|
|
|
|
|
|
|
|
function add(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
require((z = x + y) >= x, "math-not-safe");
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2020-06-17 11:03:12 +00:00
|
|
|
|
2020-08-31 16:51:02 +00:00
|
|
|
function wmul(uint x, uint y) internal pure returns (uint z) {
|
|
|
|
z = add(mul(x, y), WAD / 2) / WAD;
|
|
|
|
}
|
2020-03-22 16:38:35 +00:00
|
|
|
|
2020-08-31 16:51:02 +00:00
|
|
|
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 {
|
2020-03-22 16:38:35 +00:00
|
|
|
/**
|
2020-06-17 11:03:12 +00:00
|
|
|
* @dev get Compound Comptroller
|
2020-03-22 16:38:35 +00:00
|
|
|
*/
|
2020-06-17 11:03:12 +00:00
|
|
|
function getComptroller() public pure returns (ComptrollerLensInterface) {
|
|
|
|
return ComptrollerLensInterface(0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B);
|
2020-03-22 16:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-31 16:51:02 +00:00
|
|
|
* @dev get Compound Open Feed Oracle Address
|
2020-03-22 16:38:35 +00:00
|
|
|
*/
|
2021-01-16 08:13:28 +00:00
|
|
|
function getOracleAddress() public view returns (address) {
|
|
|
|
return getComptroller().oracle();
|
2020-03-22 16:38:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:03:12 +00:00
|
|
|
/**
|
|
|
|
* @dev get Comp Read Address
|
|
|
|
*/
|
|
|
|
function getCompReadAddress() public pure returns (address) {
|
|
|
|
return 0xd513d22422a3062Bd342Ae374b4b9c20E0a9a074;
|
|
|
|
}
|
|
|
|
|
2020-08-31 16:51:02 +00:00
|
|
|
/**
|
|
|
|
* @dev get ETH Address
|
|
|
|
*/
|
|
|
|
function getCETHAddress() public pure returns (address) {
|
|
|
|
return 0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5;
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:03:12 +00:00
|
|
|
/**
|
|
|
|
* @dev get Comp Token Address
|
|
|
|
*/
|
|
|
|
function getCompToken() public pure returns (TokenInterface) {
|
|
|
|
return TokenInterface(0xc00e94Cb662C3520282E6f5717214004A7f26888);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-22 16:38:35 +00:00
|
|
|
struct CompData {
|
2020-08-31 16:51:02 +00:00
|
|
|
uint tokenPriceInEth;
|
|
|
|
uint tokenPriceInUsd;
|
2020-03-22 16:38:35 +00:00
|
|
|
uint exchangeRateStored;
|
|
|
|
uint balanceOfUser;
|
|
|
|
uint borrowBalanceStoredUser;
|
2021-01-16 06:52:36 +00:00
|
|
|
uint totalBorrows;
|
2021-04-27 10:57:06 +00:00
|
|
|
uint totalSupplied;
|
2021-01-16 06:52:36 +00:00
|
|
|
uint borrowCap;
|
2020-03-22 16:38:35 +00:00
|
|
|
uint supplyRatePerBlock;
|
|
|
|
uint borrowRatePerBlock;
|
2021-01-04 01:22:29 +00:00
|
|
|
uint collateralFactor;
|
2021-04-27 10:57:06 +00:00
|
|
|
uint compSpeed;
|
2021-01-04 01:22:29 +00:00
|
|
|
bool isComped;
|
2021-01-16 06:52:36 +00:00
|
|
|
bool isBorrowPaused;
|
2020-03-22 16:38:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract Resolver is Helpers {
|
2020-04-10 18:08:56 +00:00
|
|
|
|
2020-08-31 16:51:02 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:03:12 +00:00
|
|
|
function getCompoundData(address owner, address[] memory cAddress) public view returns (CompData[] memory) {
|
2020-03-22 16:38:35 +00:00
|
|
|
CompData[] memory tokensData = new CompData[](cAddress.length);
|
2021-01-16 06:52:36 +00:00
|
|
|
ComptrollerLensInterface troller = getComptroller();
|
2020-03-22 16:38:35 +00:00
|
|
|
for (uint i = 0; i < cAddress.length; i++) {
|
|
|
|
CTokenInterface cToken = CTokenInterface(cAddress[i]);
|
2020-08-31 16:51:02 +00:00
|
|
|
(uint priceInETH, uint priceInUSD) = getPriceInEth(cToken);
|
2021-01-16 06:52:36 +00:00
|
|
|
(,uint collateralFactor, bool isComped) = troller.markets(address(cToken));
|
2021-04-27 10:57:06 +00:00
|
|
|
uint _totalBorrowed = cToken.totalBorrows();
|
2020-03-22 16:38:35 +00:00
|
|
|
tokensData[i] = CompData(
|
2020-08-31 16:51:02 +00:00
|
|
|
priceInETH,
|
|
|
|
priceInUSD,
|
2020-03-22 16:38:35 +00:00
|
|
|
cToken.exchangeRateStored(),
|
|
|
|
cToken.balanceOf(owner),
|
|
|
|
cToken.borrowBalanceStored(owner),
|
2021-04-27 10:57:06 +00:00
|
|
|
_totalBorrowed,
|
|
|
|
add(_totalBorrowed, cToken.getCash()),
|
2021-01-16 06:52:36 +00:00
|
|
|
troller.borrowCaps(cAddress[i]),
|
2020-03-22 16:38:35 +00:00
|
|
|
cToken.supplyRatePerBlock(),
|
2021-01-04 01:22:29 +00:00
|
|
|
cToken.borrowRatePerBlock(),
|
|
|
|
collateralFactor,
|
2021-04-27 10:57:06 +00:00
|
|
|
troller.compSpeeds(cAddress[i]),
|
2021-01-16 06:52:36 +00:00
|
|
|
isComped,
|
|
|
|
troller.borrowGuardianPaused(cAddress[i])
|
2020-03-22 16:38:35 +00:00
|
|
|
);
|
|
|
|
}
|
2020-06-17 11:03:12 +00:00
|
|
|
|
2020-03-22 16:38:35 +00:00
|
|
|
return tokensData;
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:03:12 +00:00
|
|
|
function getPosition(
|
|
|
|
address owner,
|
|
|
|
address[] memory cAddress
|
|
|
|
)
|
|
|
|
public
|
|
|
|
returns (CompData[] memory, CompReadInterface.CompBalanceMetadataExt memory)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
getCompoundData(owner, cAddress),
|
|
|
|
CompReadInterface(getCompReadAddress()).getCompBalanceMetadataExt(
|
|
|
|
getCompToken(),
|
|
|
|
getComptroller(),
|
|
|
|
owner
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-22 16:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract InstaCompoundResolver is Resolver {
|
2021-04-29 09:09:01 +00:00
|
|
|
string public constant name = "Compound-Resolver-v1.4";
|
2021-01-16 08:13:28 +00:00
|
|
|
}
|