From ad0b76d955203172336d42daac681a1e47a79b2b Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sat, 2 May 2020 01:21:37 +0530 Subject: [PATCH] Done with dydx resolver --- protocols/dydx.sol | 141 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 protocols/dydx.sol diff --git a/protocols/dydx.sol b/protocols/dydx.sol new file mode 100644 index 0000000..f511939 --- /dev/null +++ b/protocols/dydx.sol @@ -0,0 +1,141 @@ +pragma solidity ^0.6.0; +pragma experimental ABIEncoderV2; + +interface ERC20Interface { + function allowance(address, address) external view returns (uint); + function balanceOf(address) external view returns (uint); + function approve(address, uint) external; + function transfer(address, uint) external returns (bool); + function transferFrom(address, address, uint) external returns (bool); + function deposit() external payable; + function withdraw(uint) external; +} + + +interface SoloMarginContract { + + struct Info { + address owner; // The address that owns the account + uint256 number; // A nonce that allows a single address to control many accounts + } + + + struct Wei { + bool sign; // true if positive + uint256 value; + } + + struct Price { + uint256 value; + } + + struct TotalPar { + uint128 borrow; + uint128 supply; + } + + struct Index { + uint96 borrow; + uint96 supply; + uint32 lastUpdate; + } + + function getMarketPrice(uint256 marketId) external view returns (Price memory); + function getAccountWei(Info calldata account, uint256 marketId) external view returns (Wei memory); + function getMarketTotalPar(uint256 marketId) external view returns (TotalPar memory); + function getMarketCurrentIndex(uint256 marketId) external view returns (Index memory); +} + +interface RegistryInterface { + function proxies(address owner) external view returns (address); +} + + +contract DSMath { + + function add(uint x, uint y) internal pure returns (uint z) { + require((z = x + y) >= x, "math-not-safe"); + } + + function sub(uint x, uint y) internal pure returns (uint z) { + z = x - y <= x ? x - y : 0; + } + + 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; + uint constant RAY = 10 ** 27; + + function rmul(uint x, uint y) internal pure returns (uint z) { + z = add(mul(x, y), RAY / 2) / RAY; + } + + function wmul(uint x, uint y) internal pure returns (uint z) { + z = add(mul(x, y), WAD / 2) / WAD; + } + + function rdiv(uint x, uint y) internal pure returns (uint z) { + z = add(mul(x, RAY), y / 2) / y; + } + + function wdiv(uint x, uint y) internal pure returns (uint z) { + z = add(mul(x, WAD), y / 2) / y; + } + +} + +contract Helpers is DSMath{ + + /** + * @dev get Dydx Solo Address + */ + function getSoloAddress() public pure returns (address addr) { + addr = 0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e; + } + + /** + * @dev Get Dydx Account Arg + */ + function getAccountArgs(address owner) internal pure returns (SoloMarginContract.Info memory) { + SoloMarginContract.Info[] memory accounts = new SoloMarginContract.Info[](1); + accounts[0] = (SoloMarginContract.Info(owner, 0)); + return accounts[0]; + } + + struct DydxData { + uint tokenPrice; + uint supplyBalance; + uint borrowBalance; + uint supplyUtil; + uint borrowUtil; + } +} + + +contract Resolver is Helpers { + function getPosition(address user, uint[] memory marketId) public view returns(DydxData[] memory) { + SoloMarginContract solo = SoloMarginContract(getSoloAddress()); + DydxData[] memory tokensData = new DydxData[](marketId.length); + for (uint i = 0; i < marketId.length; i++) { + uint id = marketId[i]; + SoloMarginContract.Wei memory tokenBal = solo.getAccountWei(getAccountArgs(user), id); + SoloMarginContract.TotalPar memory totalPar = solo.getMarketTotalPar(id); + SoloMarginContract.Index memory rateIndex = solo.getMarketCurrentIndex(id); + + tokensData[i] = DydxData( + solo.getMarketPrice(id).value, + tokenBal.sign ? tokenBal.value : 0, + !tokenBal.sign ? tokenBal.value : 0, + wmul(totalPar.supply, rateIndex.supply), + wmul(totalPar.borrow, rateIndex.borrow) + ); + } + return tokensData; + } +} + +contract InstaDydxResolver is Resolver { + string public constant name = "Dydx-Resolver-v1"; +} \ No newline at end of file