From 4cfe052bd4efc0f20c12a98488aee4a98bd7e102 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Tue, 8 Sep 2020 23:19:20 +1000 Subject: [PATCH 01/33] eth pool basic logic setup --- contracts/tests/ethLogic.sol | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/contracts/tests/ethLogic.sol b/contracts/tests/ethLogic.sol index 1972c17..c0b4c5b 100644 --- a/contracts/tests/ethLogic.sol +++ b/contracts/tests/ethLogic.sol @@ -3,10 +3,27 @@ pragma solidity ^0.6.8; contract EthRateLogic { address poolToken; + + function getCompoundNetAssetsInEth(address _dsa) private returns (uint _netBal) { + // Logics + // Take price from Compound's Oracle? + } + + function getCurveNetAssetsInEth(address _dsa) private returns (uint _netBal) { + // Logics + // Take price from ChainLink's Oracle? + } + + function getNetDsaAssets(address _dsa) private returns (uint _netBal) { + _netBal = _dsa.balance; + _netBal += getCompoundNetAssetsInEth(_dsa); + _netBal += getCurveNetAssetsInEth(_dsa); + } function getTotalToken() public returns (uint) { - uint bal = (address(this).balance); - bal += (address(poolToken).balance); + address _dsa = 0x0000000000000000000000000000000000000000; + uint bal = poolToken.balance; + bal += getNetDsaAssets(_dsa); return bal; } From c901d97fa8e8bfe83a35bc66408e59939434d26d Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Tue, 8 Sep 2020 23:23:30 +1000 Subject: [PATCH 02/33] shifted eth logic to new folder --- contracts/logics/ethLogic.sol | 39 +++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 contracts/logics/ethLogic.sol diff --git a/contracts/logics/ethLogic.sol b/contracts/logics/ethLogic.sol new file mode 100644 index 0000000..c0b4c5b --- /dev/null +++ b/contracts/logics/ethLogic.sol @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +contract EthRateLogic { + address poolToken; + + function getCompoundNetAssetsInEth(address _dsa) private returns (uint _netBal) { + // Logics + // Take price from Compound's Oracle? + } + + function getCurveNetAssetsInEth(address _dsa) private returns (uint _netBal) { + // Logics + // Take price from ChainLink's Oracle? + } + + function getNetDsaAssets(address _dsa) private returns (uint _netBal) { + _netBal = _dsa.balance; + _netBal += getCompoundNetAssetsInEth(_dsa); + _netBal += getCurveNetAssetsInEth(_dsa); + } + + function getTotalToken() public returns (uint) { + address _dsa = 0x0000000000000000000000000000000000000000; + uint bal = poolToken.balance; + bal += getNetDsaAssets(_dsa); + return bal; + } + + function reduceETH(uint amt) public { + payable(address(0)).transfer(amt); + } + + constructor (address ethPool) public { + poolToken = address(ethPool); + } + + receive() external payable {} +} \ No newline at end of file From 960c9748b41e18f003eb2ed5706bc20cb94cbddf Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Tue, 8 Sep 2020 23:24:06 +1000 Subject: [PATCH 03/33] text logic proxy as it is --- contracts/tests/ethLogic.sol | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/contracts/tests/ethLogic.sol b/contracts/tests/ethLogic.sol index c0b4c5b..1972c17 100644 --- a/contracts/tests/ethLogic.sol +++ b/contracts/tests/ethLogic.sol @@ -3,27 +3,10 @@ pragma solidity ^0.6.8; contract EthRateLogic { address poolToken; - - function getCompoundNetAssetsInEth(address _dsa) private returns (uint _netBal) { - // Logics - // Take price from Compound's Oracle? - } - - function getCurveNetAssetsInEth(address _dsa) private returns (uint _netBal) { - // Logics - // Take price from ChainLink's Oracle? - } - - function getNetDsaAssets(address _dsa) private returns (uint _netBal) { - _netBal = _dsa.balance; - _netBal += getCompoundNetAssetsInEth(_dsa); - _netBal += getCurveNetAssetsInEth(_dsa); - } function getTotalToken() public returns (uint) { - address _dsa = 0x0000000000000000000000000000000000000000; - uint bal = poolToken.balance; - bal += getNetDsaAssets(_dsa); + uint bal = (address(this).balance); + bal += (address(poolToken).balance); return bal; } From 1d4844e219ec237c42075d2e91e2e73cbff654ad Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 10 Sep 2020 17:14:06 +1000 Subject: [PATCH 04/33] logics basic structure --- .../logics/{ => exchangeRate}/ethLogic.sol | 4 ++- contracts/logics/settle/eth/logicOne.sol | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) rename contracts/logics/{ => exchangeRate}/ethLogic.sol (92%) create mode 100644 contracts/logics/settle/eth/logicOne.sol diff --git a/contracts/logics/ethLogic.sol b/contracts/logics/exchangeRate/ethLogic.sol similarity index 92% rename from contracts/logics/ethLogic.sol rename to contracts/logics/exchangeRate/ethLogic.sol index c0b4c5b..0b4e273 100644 --- a/contracts/logics/ethLogic.sol +++ b/contracts/logics/exchangeRate/ethLogic.sol @@ -1,6 +1,8 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.6.8; +import { DSMath } from "../../libs/safeMath.sol"; + contract EthRateLogic { address poolToken; @@ -35,5 +37,5 @@ contract EthRateLogic { poolToken = address(ethPool); } - receive() external payable {} + receive() external payable {} } \ No newline at end of file diff --git a/contracts/logics/settle/eth/logicOne.sol b/contracts/logics/settle/eth/logicOne.sol new file mode 100644 index 0000000..4524c2c --- /dev/null +++ b/contracts/logics/settle/eth/logicOne.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +import { DSMath } from "../../../libs/safeMath.sol"; + +contract LogicOne { + + address poolToken; + + function maxComp(uint amt) public { + // open cast function with flashloan and Compound connectors access + // check if status is safe and only have assets in the specific tokens + } + + function unwindCompToSafe(uint amt) public { + // Will only uniwnd till safe limit + // open cast function with flashloan and Compound connectors access + // check if status is safe and only have assets in the specific tokens + } + + function unwindMaxComp(uint amt) public { + // open cast function with flashloan and Compound connectors access + // check if status is safe and only have assets in the specific tokens + } + + constructor (address ethPool) public { + poolToken = address(ethPool); + } + + receive() external payable {} + +} From bb3a20595a4932706f2f2ef04ea9977da823d380 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 10 Sep 2020 17:16:15 +1000 Subject: [PATCH 05/33] basic logic structure --- contracts/logics/settle/eth/basic.sol | 25 +++++++++++++++++++ .../settle/eth/{logicOne.sol => maxComp.sol} | 0 2 files changed, 25 insertions(+) create mode 100644 contracts/logics/settle/eth/basic.sol rename contracts/logics/settle/eth/{logicOne.sol => maxComp.sol} (100%) diff --git a/contracts/logics/settle/eth/basic.sol b/contracts/logics/settle/eth/basic.sol new file mode 100644 index 0000000..e7987b8 --- /dev/null +++ b/contracts/logics/settle/eth/basic.sol @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +import { DSMath } from "../../../libs/safeMath.sol"; + +contract LogicOne { + + address poolToken; + + function deploy(address _dsa, uint amt) public { + // check if DSA is authorised + // transfer assets to DSA + } + + function redeem(address _dsa, uint amt) public { + // withdraw assets from DSA + } + + constructor (address ethPool) public { + poolToken = address(ethPool); + } + + receive() external payable {} + +} diff --git a/contracts/logics/settle/eth/logicOne.sol b/contracts/logics/settle/eth/maxComp.sol similarity index 100% rename from contracts/logics/settle/eth/logicOne.sol rename to contracts/logics/settle/eth/maxComp.sol From 49f98c31e7818913c12b000b0ecd8be13da9ecbb Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 10 Sep 2020 17:26:30 +1000 Subject: [PATCH 06/33] curve mining basic structure --- contracts/logics/settle/eth/crvMining.sol | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 contracts/logics/settle/eth/crvMining.sol diff --git a/contracts/logics/settle/eth/crvMining.sol b/contracts/logics/settle/eth/crvMining.sol new file mode 100644 index 0000000..5f1eb0f --- /dev/null +++ b/contracts/logics/settle/eth/crvMining.sol @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +import { DSMath } from "../../../libs/safeMath.sol"; + +contract LogicOne { + + address poolToken; + + function compCrvMine(uint amt) public { + // borrow from Compound & deposit in Curve (static logic for DAI) + // check if status is safe and only have assets in the specific tokens + } + + function compCrvRedeem(uint amt) public { + // Withdraw from Curve and payback on Compound + } + + constructor (address ethPool) public { + poolToken = address(ethPool); + } + + receive() external payable {} + +} From 36673344e55cc990f91ae74dc6e40791da97197d Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 10 Sep 2020 17:30:33 +1000 Subject: [PATCH 07/33] setExchangeRate basic logic structure --- contracts/logics/settle/eth/exchangeRate.sol | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 contracts/logics/settle/eth/exchangeRate.sol diff --git a/contracts/logics/settle/eth/exchangeRate.sol b/contracts/logics/settle/eth/exchangeRate.sol new file mode 100644 index 0000000..eccd21e --- /dev/null +++ b/contracts/logics/settle/eth/exchangeRate.sol @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +import { DSMath } from "../../../libs/safeMath.sol"; + +contract LogicOne { + + address poolToken; + + function setExchangeRate() public { + // run setExchangeRate in address(this) + } + + constructor (address ethPool) public { + poolToken = address(ethPool); + } + + receive() external payable {} + +} From 51348dc0ab94b0986a7f39d40361953584061fb3 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 10 Sep 2020 23:02:47 +1000 Subject: [PATCH 08/33] Max COMP connector side done --- contracts/logics/settle/eth/maxComp.sol | 31 +++++++++++++------------ 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/contracts/logics/settle/eth/maxComp.sol b/contracts/logics/settle/eth/maxComp.sol index 4524c2c..8bff49e 100644 --- a/contracts/logics/settle/eth/maxComp.sol +++ b/contracts/logics/settle/eth/maxComp.sol @@ -1,30 +1,31 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.6.8; +pragma experimental ABIEncoderV2; import { DSMath } from "../../../libs/safeMath.sol"; +interface DSAInterface { + function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable; +} + contract LogicOne { address poolToken; + DSAInterface dsa; - function maxComp(uint amt) public { - // open cast function with flashloan and Compound connectors access + function maxComp(address[] calldata _targets, bytes[] calldata _data) public { + address compoundConnector = address(0); + address instaPoolConnector = address(0); + for (uint i = 0; i < _targets.length; i++) { + require(_targets[i] == compoundConnector || _targets[i] == instaPoolConnector, "connector-not-authorised"); + } + dsa.cast(_targets, _data, address(0)); // check if status is safe and only have assets in the specific tokens } - function unwindCompToSafe(uint amt) public { - // Will only uniwnd till safe limit - // open cast function with flashloan and Compound connectors access - // check if status is safe and only have assets in the specific tokens - } - - function unwindMaxComp(uint amt) public { - // open cast function with flashloan and Compound connectors access - // check if status is safe and only have assets in the specific tokens - } - - constructor (address ethPool) public { - poolToken = address(ethPool); + constructor (address ethPool, address _dsa) public { + poolToken = ethPool; + dsa = DSAInterface(_dsa); } receive() external payable {} From d5eaf43bcb7aa076e6ac149b9aa5b982bf4cf964 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Fri, 11 Sep 2020 02:34:56 +0530 Subject: [PATCH 09/33] Almost done with ethLogic exchangeRate --- contracts/logics/exchangeRate/ethLogic.sol | 96 ++++++++++++++++++---- 1 file changed, 81 insertions(+), 15 deletions(-) diff --git a/contracts/logics/exchangeRate/ethLogic.sol b/contracts/logics/exchangeRate/ethLogic.sol index 0b4e273..a68df45 100644 --- a/contracts/logics/exchangeRate/ethLogic.sol +++ b/contracts/logics/exchangeRate/ethLogic.sol @@ -3,36 +3,102 @@ pragma solidity ^0.6.8; import { DSMath } from "../../libs/safeMath.sol"; -contract EthRateLogic { - address poolToken; +// TODO - have to check y pool virtual price +interface CTokenInterface { + function borrowBalanceCurrent(address account) external returns (uint256); + function exchangeRateCurrent() external returns (uint256); - function getCompoundNetAssetsInEth(address _dsa) private returns (uint _netBal) { - // Logics - // Take price from Compound's Oracle? + function balanceOf(address owner) external view returns (uint256); +} + +interface CompTroller { + function getAllMarkets() external view returns (address[] memory); +} + +interface OracleComp { + function getUnderlyingPrice(address) external view returns (uint); +} + +interface InstaMapping { + function cTokenMapping(address) external view returns (address); +} + +interface CurveMapping { + function curvePoolMapping(address poolAddr) external view returns (address); +} + +interface CurveRegistry { + function pool_list(uint) external view returns (address poolAddress, address poolToken, address ctokenPrice); + function pool_count() external view returns (uint); +} + +interface ICurve { + function get_virtual_price() external view returns (uint256 out); +} + +interface TokenInterface { + function balanceOf(address owner) external view returns (uint256); +} + + +contract EthRateLogic is DSMath { + address public immutable poolToken; + address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant ctokenMapping = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant curveRegistryAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { + uint totalSupplyInETH; + uint totalBorrowInETH; + address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); + OracleComp oracleContract = OracleComp(compOracleAddr); + uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); + for (uint i = 0; i < allMarkets.length; i++) { + CTokenInterface ctoken = CTokenInterface(allMarkets[i]); + uint tokenPriceInETH = wdiv(oracleContract.getUnderlyingPrice(address(ctoken)), ethPrice); + uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent()); + uint supplyInETH = wmul(supply, tokenPriceInETH); + + uint borrow = ctoken.borrowBalanceCurrent(_dsa); + uint borrowInETH = wmul(borrow, tokenPriceInETH); + + totalSupplyInETH += add(totalSupplyInETH, supplyInETH); + totalBorrowInETH = add(totalBorrowInETH, borrowInETH); + } + _netBal = sub(totalSupplyInETH, totalBorrowInETH); } - function getCurveNetAssetsInEth(address _dsa) private returns (uint _netBal) { - // Logics - // Take price from ChainLink's Oracle? + function getCurveNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { + CurveRegistry curveRegistry = CurveRegistry(curveRegistryAddr); + uint poolLen = curveRegistry.pool_count(); + OracleComp oracleContract = OracleComp(compOracleAddr); + uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); + for (uint i = 0; i < poolLen; i++) { + (address curvePoolAddr, address curveTokenAddr, address ctokenPriceAddr) = curveRegistry.pool_list(i); + uint tokenPriceInETH = wdiv(oracleContract.getUnderlyingPrice(address(ctokenPriceAddr)), ethPrice); + uint virtualPrice = ICurve(curvePoolAddr).get_virtual_price(); + uint curveTokenBal = TokenInterface(curveTokenAddr).balanceOf(_dsa); + uint amtInUSD = wmul(curveTokenBal, virtualPrice); + uint amtInETH = wmul(amtInUSD, tokenPriceInETH); + _netBal = add(_netBal, amtInETH); + } } - function getNetDsaAssets(address _dsa) private returns (uint _netBal) { + function getNetDsaAssets(address _dsa) private returns (uint256 _netBal) { _netBal = _dsa.balance; _netBal += getCompoundNetAssetsInEth(_dsa); _netBal += getCurveNetAssetsInEth(_dsa); } - function getTotalToken() public returns (uint) { + function getTotalToken() public returns (uint256) { address _dsa = 0x0000000000000000000000000000000000000000; - uint bal = poolToken.balance; + uint256 bal = poolToken.balance; bal += getNetDsaAssets(_dsa); return bal; } - function reduceETH(uint amt) public { - payable(address(0)).transfer(amt); - } - constructor (address ethPool) public { poolToken = address(ethPool); } From a3da8614d6b2717830899a225bee0b9d2d4a4dcd Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sat, 12 Sep 2020 01:52:48 +1000 Subject: [PATCH 10/33] set exchange rate logic done --- contracts/logics/settle/eth/exchangeRate.sol | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/contracts/logics/settle/eth/exchangeRate.sol b/contracts/logics/settle/eth/exchangeRate.sol index eccd21e..37493cc 100644 --- a/contracts/logics/settle/eth/exchangeRate.sol +++ b/contracts/logics/settle/eth/exchangeRate.sol @@ -3,16 +3,14 @@ pragma solidity ^0.6.8; import { DSMath } from "../../../libs/safeMath.sol"; +interface PoolInterface { + function setExchangeRate() external; +} + contract LogicOne { - address poolToken; - - function setExchangeRate() public { - // run setExchangeRate in address(this) - } - - constructor (address ethPool) public { - poolToken = address(ethPool); + function setExchangeRate() public payable { + PoolInterface(address(this)).setExchangeRate(); } receive() external payable {} From 56d37868f61b63f537d57558a059aab796fb4e49 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sat, 12 Sep 2020 04:32:04 +0530 Subject: [PATCH 11/33] Created chainlink price feed contract --- contracts/logics/exchangeRate/priceFeed.sol | 147 ++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 contracts/logics/exchangeRate/priceFeed.sol diff --git a/contracts/logics/exchangeRate/priceFeed.sol b/contracts/logics/exchangeRate/priceFeed.sol new file mode 100644 index 0000000..861e491 --- /dev/null +++ b/contracts/logics/exchangeRate/priceFeed.sol @@ -0,0 +1,147 @@ +pragma solidity ^0.6.0; +pragma experimental ABIEncoderV2; + +interface ChainLinkInterface { + function latestAnswer() external view returns (int256); + function decimals() external view returns (uint256); +} + +interface IndexInterface { + function master() external view returns (address); +} + +interface TokenInterface { + function decimals() external view returns (uint); +} + +import { DSMath } from "../../libs/safeMath.sol"; + + +contract Basic is DSMath { + address public constant instaIndex = 0x2971AdFa57b20E5a416aE5a708A8655A9c74f723; + address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + + modifier isChief { + require( + IndexInterface(instaIndex).master() == msg.sender, "not-Chief"); + _; + } + + event LogAddChainLinkMapping( + address tokenSymbol, + address chainlinkFeed + ); + + event LogRemoveChainLinkMapping( + address tokenSymbol, + address chainlinkFeed + ); + + struct FeedData { + address feedAddress; + uint multiplier; + } + + mapping (address => FeedData) public chainLinkMapping; + + + function convertPrice(uint price, uint multiplier) internal pure returns (uint) { + return price * (10 ** multiplier); + } + + function _addChainLinkMapping( + address token, + address chainlinkFeed + ) internal { + require(token != address(0), "token-not-vaild"); + require(chainlinkFeed != address(0), "chainlinkFeed-not-vaild"); + require(chainLinkMapping[token].feedAddress == address(0), "chainlinkFeed-already-added"); + uint tokenDec = token == ethAddr ? 18 : TokenInterface(token).decimals(); + uint feedDec = ChainLinkInterface(chainlinkFeed).decimals(); + + chainLinkMapping[token].feedAddress = chainlinkFeed; + chainLinkMapping[token].multiplier = sub(36, add(tokenDec, feedDec)); + emit LogAddChainLinkMapping(token, chainlinkFeed); + } + + function _removeChainLinkMapping(address token) internal { + require(token != address(0), "token-not-vaild"); + require(chainLinkMapping[token].feedAddress != address(0), "chainlinkFeed-not-added-yet"); + + emit LogRemoveChainLinkMapping(token, chainLinkMapping[token].feedAddress); + delete chainLinkMapping[token]; + } + + function addChainLinkMapping( + address[] memory tokens, + address[] memory chainlinkFeeds + ) public isChief { + require(tokens.length == chainlinkFeeds.length, "lenght-not-same"); + for (uint i = 0; i < tokens.length; i++) { + _addChainLinkMapping(tokens[i], chainlinkFeeds[i]); + } + } + + function removeChainLinkMapping(address[] memory tokens) public isChief { + for (uint i = 0; i < tokens.length; i++) { + _removeChainLinkMapping(tokens[i]); + } + } +} + +contract Resolver is Basic { + function getPrices(address[] memory tokens) + public + view + returns ( + uint ethPriceInUsd, + uint[] memory tokensPriceInETH + ) { + tokensPriceInETH = new uint[](tokens.length); + for (uint i = 0; i < tokens.length; i++) { + if (tokens[i] != ethAddr) { + FeedData memory feedData = chainLinkMapping[tokens[i]]; + ChainLinkInterface feedContract = ChainLinkInterface(feedData.feedAddress); + require(address(feedContract) != address(0), "price-not-found"); + tokensPriceInETH[i] = convertPrice(uint(feedContract.latestAnswer()), feedData.multiplier); + } else { + tokensPriceInETH[i] = 10 ** 18; + } + } + + FeedData memory ethFeedData = chainLinkMapping[ethAddr]; + ChainLinkInterface ethFeed = ChainLinkInterface(ethFeedData.feedAddress); + ethPriceInUsd = convertPrice(uint(ethFeed.latestAnswer()), ethFeedData.multiplier); + } + + function getPrice(address token) + public + view + returns ( + uint ethPriceInUsd, + uint tokenPriceInETH + ) { + if (token != ethAddr) { + FeedData memory tokenFeedData = chainLinkMapping[token]; + ChainLinkInterface tokenFeed = ChainLinkInterface(tokenFeedData.feedAddress); + require(address(tokenFeed) != address(0), "price-not-found"); + tokenPriceInETH = convertPrice(uint(tokenFeed.latestAnswer()), tokenFeedData.multiplier); + } else { + tokenPriceInETH = 10 ** 18; + } + + FeedData memory ethFeedData = chainLinkMapping[ethAddr]; + ChainLinkInterface ethFeed = ChainLinkInterface(ethFeedData.feedAddress); + ethPriceInUsd = convertPrice(uint(ethFeed.latestAnswer()), ethFeedData.multiplier); + + } +} + +contract ChainLinkPriceFeed is Resolver { + constructor (address[] memory tokens, address[] memory chainlinkFeeds) public { + require(tokens.length == chainlinkFeeds.length, "Lenght-not-same"); + for (uint i = 0; i < tokens.length; i++) { + _addChainLinkMapping(tokens[i], chainlinkFeeds[i]); + } + } +} \ No newline at end of file From d45ed96efb9148a2ae3c00e3411475ef2100f15a Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sat, 12 Sep 2020 04:33:29 +0530 Subject: [PATCH 12/33] Minor changes --- contracts/logics/exchangeRate/priceFeed.sol | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/contracts/logics/exchangeRate/priceFeed.sol b/contracts/logics/exchangeRate/priceFeed.sol index 861e491..8b132b3 100644 --- a/contracts/logics/exchangeRate/priceFeed.sol +++ b/contracts/logics/exchangeRate/priceFeed.sol @@ -94,7 +94,6 @@ contract Resolver is Basic { public view returns ( - uint ethPriceInUsd, uint[] memory tokensPriceInETH ) { tokensPriceInETH = new uint[](tokens.length); @@ -108,17 +107,12 @@ contract Resolver is Basic { tokensPriceInETH[i] = 10 ** 18; } } - - FeedData memory ethFeedData = chainLinkMapping[ethAddr]; - ChainLinkInterface ethFeed = ChainLinkInterface(ethFeedData.feedAddress); - ethPriceInUsd = convertPrice(uint(ethFeed.latestAnswer()), ethFeedData.multiplier); } function getPrice(address token) public view returns ( - uint ethPriceInUsd, uint tokenPriceInETH ) { if (token != ethAddr) { @@ -129,11 +123,17 @@ contract Resolver is Basic { } else { tokenPriceInETH = 10 ** 18; } + } + function getEthPrice() + public + view + returns ( + uint ethPriceInUsd + ) { FeedData memory ethFeedData = chainLinkMapping[ethAddr]; ChainLinkInterface ethFeed = ChainLinkInterface(ethFeedData.feedAddress); ethPriceInUsd = convertPrice(uint(ethFeed.latestAnswer()), ethFeedData.multiplier); - } } From 66565bb1d4c9528eb04d3fe74c7dd78ae2854e5f Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sat, 12 Sep 2020 23:22:28 +1000 Subject: [PATCH 13/33] removed storage variables --- contracts/logics/settle/eth/maxComp.sol | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/contracts/logics/settle/eth/maxComp.sol b/contracts/logics/settle/eth/maxComp.sol index 8bff49e..116331b 100644 --- a/contracts/logics/settle/eth/maxComp.sol +++ b/contracts/logics/settle/eth/maxComp.sol @@ -10,24 +10,17 @@ interface DSAInterface { contract LogicOne { - address poolToken; - DSAInterface dsa; - - function maxComp(address[] calldata _targets, bytes[] calldata _data) public { + function maxComp(address _dsa, address[] calldata _targets, bytes[] calldata _data) public { + // check if DSA is authorised for interaction address compoundConnector = address(0); address instaPoolConnector = address(0); for (uint i = 0; i < _targets.length; i++) { require(_targets[i] == compoundConnector || _targets[i] == instaPoolConnector, "connector-not-authorised"); } - dsa.cast(_targets, _data, address(0)); + DSAInterface(_dsa).cast(_targets, _data, address(0)); // check if status is safe and only have assets in the specific tokens } - constructor (address ethPool, address _dsa) public { - poolToken = ethPool; - dsa = DSAInterface(_dsa); - } - receive() external payable {} } From 205a60f9d712d5259b109b239c16f0adedf4ece9 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sat, 12 Sep 2020 23:29:12 +1000 Subject: [PATCH 14/33] basic logic done --- contracts/logics/settle/eth/basic.sol | 45 ++++++++++++++++++++----- contracts/logics/settle/eth/maxComp.sol | 7 ++-- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/contracts/logics/settle/eth/basic.sol b/contracts/logics/settle/eth/basic.sol index e7987b8..bac5305 100644 --- a/contracts/logics/settle/eth/basic.sol +++ b/contracts/logics/settle/eth/basic.sol @@ -1,24 +1,53 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.6.8; +pragma experimental ABIEncoderV2; + +import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol"; +import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { DSMath } from "../../../libs/safeMath.sol"; +interface DSAInterface { + function cast(address[] calldata _targets, bytes[] calldata _data, address _origin) external payable; +} + contract LogicOne { - address poolToken; + using SafeERC20 for IERC20; - function deploy(address _dsa, uint amt) public { + /** + * @dev Return ethereum address + */ + function getEthAddr() internal pure returns (address) { + return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; // ETH Address + } + + function deploy(address _dsa, address _token, uint amt) public { // check if DSA is authorised - // transfer assets to DSA + if (_token == getEthAddr()) { + uint _bal = address(this).balance; + amt = amt > _bal ? _bal : amt; + payable(_dsa).transfer(amt); + } else { + IERC20 token = IERC20(_token); + uint _bal = token.balanceOf(address(this)); + amt = amt > _bal ? _bal : amt; + token.safeTransfer(_dsa, amt); + } } - function redeem(address _dsa, uint amt) public { - // withdraw assets from DSA + // withdraw assets from DSA + function redeem(address _dsa, address _token, uint amt) public { + uint _bal = IERC20(_token).balanceOf(_dsa); + amt = amt > _bal ? _bal : amt; + address[] memory _targets = new address[](1); + _targets[0] = address(0); // Check9898 - address of basic connector + bytes[] memory _data = new bytes[](1); + _data[0] = abi.encodeWithSignature("withdraw(address,uint256,address,uint256,uint256)", _token, amt, address(this), uint(0), uint(0)); + DSAInterface(_dsa).cast(_targets, _data, address(0)); // Check9898 - address of origin } - constructor (address ethPool) public { - poolToken = address(ethPool); - } + constructor () public {} receive() external payable {} diff --git a/contracts/logics/settle/eth/maxComp.sol b/contracts/logics/settle/eth/maxComp.sol index 116331b..b858ce4 100644 --- a/contracts/logics/settle/eth/maxComp.sol +++ b/contracts/logics/settle/eth/maxComp.sol @@ -12,12 +12,13 @@ contract LogicOne { function maxComp(address _dsa, address[] calldata _targets, bytes[] calldata _data) public { // check if DSA is authorised for interaction - address compoundConnector = address(0); - address instaPoolConnector = address(0); + // Also think on dydx flash loan connector + address compoundConnector = address(0); // Check9898 - address of compound connector + address instaPoolConnector = address(0); // Check9898 - address of instaPool connector for (uint i = 0; i < _targets.length; i++) { require(_targets[i] == compoundConnector || _targets[i] == instaPoolConnector, "connector-not-authorised"); } - DSAInterface(_dsa).cast(_targets, _data, address(0)); + DSAInterface(_dsa).cast(_targets, _data, address(0)); // Check9898 - address of basic connector // check if status is safe and only have assets in the specific tokens } From 052e05bc8d5abe96c5a8bfe2aa6434de578303ee Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sun, 13 Sep 2020 03:34:36 +1000 Subject: [PATCH 15/33] removed storage variable --- contracts/logics/settle/eth/crvMining.sol | 6 ------ 1 file changed, 6 deletions(-) diff --git a/contracts/logics/settle/eth/crvMining.sol b/contracts/logics/settle/eth/crvMining.sol index 5f1eb0f..c38b481 100644 --- a/contracts/logics/settle/eth/crvMining.sol +++ b/contracts/logics/settle/eth/crvMining.sol @@ -5,8 +5,6 @@ import { DSMath } from "../../../libs/safeMath.sol"; contract LogicOne { - address poolToken; - function compCrvMine(uint amt) public { // borrow from Compound & deposit in Curve (static logic for DAI) // check if status is safe and only have assets in the specific tokens @@ -16,10 +14,6 @@ contract LogicOne { // Withdraw from Curve and payback on Compound } - constructor (address ethPool) public { - poolToken = address(ethPool); - } - receive() external payable {} } From d4f9ccfc7b0e2eaafffda2345cb6dca7519c513b Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sun, 13 Sep 2020 04:12:33 +0530 Subject: [PATCH 16/33] Intgerated chainlink priceFeed in eth exchangeRate --- contracts/logics/exchangeRate/ethLogic.sol | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/contracts/logics/exchangeRate/ethLogic.sol b/contracts/logics/exchangeRate/ethLogic.sol index a68df45..c2486a4 100644 --- a/contracts/logics/exchangeRate/ethLogic.sol +++ b/contracts/logics/exchangeRate/ethLogic.sol @@ -9,6 +9,8 @@ interface CTokenInterface { function exchangeRateCurrent() external returns (uint256); function balanceOf(address owner) external view returns (uint256); + + function underlying() external view returns (address); } interface CompTroller { @@ -28,7 +30,7 @@ interface CurveMapping { } 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); } @@ -40,12 +42,20 @@ interface TokenInterface { 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 { address public immutable poolToken; address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant ctokenMapping = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant curveRegistryAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); @@ -53,11 +63,11 @@ contract EthRateLogic is DSMath { uint totalSupplyInETH; uint totalBorrowInETH; address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); - OracleComp oracleContract = OracleComp(compOracleAddr); - uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + // uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); for (uint i = 0; i < allMarkets.length; 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 supplyInETH = wmul(supply, tokenPriceInETH); @@ -70,18 +80,18 @@ contract EthRateLogic is DSMath { _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); uint poolLen = curveRegistry.pool_count(); - OracleComp oracleContract = OracleComp(compOracleAddr); - uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + uint ethPriceUSD = priceFeedContract.getEthPrice(); for (uint i = 0; i < poolLen; i++) { - (address curvePoolAddr, address curveTokenAddr, address ctokenPriceAddr) = curveRegistry.pool_list(i); - uint tokenPriceInETH = wdiv(oracleContract.getUnderlyingPrice(address(ctokenPriceAddr)), ethPrice); + (address curvePoolAddr, address curveTokenAddr) = curveRegistry.pool_list(i); uint virtualPrice = ICurve(curvePoolAddr).get_virtual_price(); uint curveTokenBal = TokenInterface(curveTokenAddr).balanceOf(_dsa); uint amtInUSD = wmul(curveTokenBal, virtualPrice); - uint amtInETH = wmul(amtInUSD, tokenPriceInETH); + uint amtInETH = wmul(amtInUSD, ethPriceUSD); _netBal = add(_netBal, amtInETH); } } From 79d1c80d61fc11ab7ecd2784b99fe7c254ccf6bc Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Mon, 14 Sep 2020 02:02:25 +1000 Subject: [PATCH 17/33] CRV mining logics --- contracts/logics/settle/eth/crvMining.sol | 33 ++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/contracts/logics/settle/eth/crvMining.sol b/contracts/logics/settle/eth/crvMining.sol index c38b481..2430c60 100644 --- a/contracts/logics/settle/eth/crvMining.sol +++ b/contracts/logics/settle/eth/crvMining.sol @@ -5,13 +5,40 @@ import { DSMath } from "../../../libs/safeMath.sol"; contract LogicOne { - function compCrvMine(uint amt) public { - // borrow from Compound & deposit in Curve (static logic for DAI) + // borrow from Compound & deposit in Curve (static logic for DAI) + function compCrvMine(address token, uint amt, uint unitAmt, string calldata guage) public { + address[] memory _targets = new address[](3); + bytes[] memory _data = new bytes[](3); + _targets[0] = address(0); // Check9898 - address of compound connector + _data[0] = abi.encodeWithSignature("borrow(address,uint256,uint256,uint256)", token, amt, uint(0), uint(0)); + _targets[1] = address(0); // Check9898 - address of curve connector + _data[1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0)); + _targets[2] = address(0); // Check9898 - address of curve guage connector + _data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", guage, uint(-1), uint(0), uint(0)); // check if status is safe and only have assets in the specific tokens } - function compCrvRedeem(uint amt) public { + function compCrvRedeem(address token, uint amt, uint unitAmt, string calldata guage) public { // Withdraw from Curve and payback on Compound + address[] memory _targets; + bytes[] memory _data; + if (amt == uint(-1)) { + _targets = new address[](3); + _data = new bytes[](3); + } else { + _targets = new address[](4); + _data = new bytes[](4); + } + _targets[0] = address(0); // Check9898 - address of curve guage connector + _data[0] = abi.encodeWithSignature("withdraw(string,uint256,uint256,uint256,uint256,uint256)", guage, uint(-1), uint(0), uint(0), uint(0), uint(0)); + _targets[1] = address(0); // Check9898 - address of curve connector + _data[1] = abi.encodeWithSignature("withdraw(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0)); + _targets[2] = address(0); // Check9898 - address of compound connector + _data[2] = abi.encodeWithSignature("payback(address,uint256,uint256,uint256)", token, amt, uint(0), uint(0)); + if (amt != uint(-1)) { + _targets[3] = address(0); // Check9898 - address of curve guage connector + _data[3] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", guage, uint(-1), uint(0), uint(0)); + } } receive() external payable {} From 778bc3af631b689d40bad37814311be7245c4abe Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Wed, 16 Sep 2020 22:46:37 +1000 Subject: [PATCH 18/33] curve bug fix --- contracts/logics/exchangeRate/ethLogic.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/logics/exchangeRate/ethLogic.sol b/contracts/logics/exchangeRate/ethLogic.sol index c2486a4..cc7596c 100644 --- a/contracts/logics/exchangeRate/ethLogic.sol +++ b/contracts/logics/exchangeRate/ethLogic.sol @@ -91,7 +91,7 @@ contract EthRateLogic is DSMath { uint virtualPrice = ICurve(curvePoolAddr).get_virtual_price(); uint curveTokenBal = TokenInterface(curveTokenAddr).balanceOf(_dsa); uint amtInUSD = wmul(curveTokenBal, virtualPrice); - uint amtInETH = wmul(amtInUSD, ethPriceUSD); + uint amtInETH = wdiv(amtInUSD, ethPriceUSD); _netBal = add(_netBal, amtInETH); } } From ff537d0dd19307008aba7062fce4ab7da8f2eea6 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Thu, 24 Sep 2020 22:08:51 +1000 Subject: [PATCH 19/33] maxComp in wip --- contracts/logics/settle/eth/maxComp.sol | 58 ++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/contracts/logics/settle/eth/maxComp.sol b/contracts/logics/settle/eth/maxComp.sol index b858ce4..588f82b 100644 --- a/contracts/logics/settle/eth/maxComp.sol +++ b/contracts/logics/settle/eth/maxComp.sol @@ -2,23 +2,79 @@ pragma solidity ^0.6.8; pragma experimental ABIEncoderV2; -import { DSMath } from "../../../libs/safeMath.sol"; +import { DSMath } from "../../libs/safeMath.sol"; + +interface CTokenInterface { + function borrowBalanceCurrent(address account) external returns (uint256); + function exchangeRateCurrent() external returns (uint256); + + function balanceOf(address owner) external view returns (uint256); + + function underlying() external view returns (address); +} interface DSAInterface { function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable; } +interface CompTroller { + function getAllMarkets() external view returns (address[] memory); +} + +interface OracleComp { + function getUnderlyingPrice(address) external view returns (uint); +} + +interface InstaMapping { + function cTokenMapping(address) external view returns (address); +} + contract LogicOne { + address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant cdaiAddr = address(0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643); + address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { + uint totalSupplyInETH; + uint totalBorrowInETH; + address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); + OracleComp priceFeedContract = OracleComp(compOracleAddr); + // uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); + for (uint i = 0; i < allMarkets.length; i++) { + CTokenInterface ctoken = CTokenInterface(allMarkets[i]); + uint tokenPriceInETH = priceFeedContract.getPrice(address(ctoken) == cethAddr ? ethAddr : ctoken.underlying()); + uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent()); + uint supplyInETH = wmul(supply, tokenPriceInETH); + + uint borrow = ctoken.borrowBalanceCurrent(_dsa); + uint borrowInETH = wmul(borrow, tokenPriceInETH); + + totalSupplyInETH += add(totalSupplyInETH, supplyInETH); + totalBorrowInETH = add(totalBorrowInETH, borrowInETH); + + if (allMarkets[i] != cdaiAddr && allMarkets[i] != cethAddr) { + require(supply == 0 && borrow == 0, "assets"); + } + // require() + } + _netBal = sub(totalSupplyInETH, totalBorrowInETH); + } + function maxComp(address _dsa, address[] calldata _targets, bytes[] calldata _data) public { // check if DSA is authorised for interaction // Also think on dydx flash loan connector + // initial Compound position borrow and supply address compoundConnector = address(0); // Check9898 - address of compound connector address instaPoolConnector = address(0); // Check9898 - address of instaPool connector for (uint i = 0; i < _targets.length; i++) { require(_targets[i] == compoundConnector || _targets[i] == instaPoolConnector, "connector-not-authorised"); } DSAInterface(_dsa).cast(_targets, _data, address(0)); // Check9898 - address of basic connector + // final Compound position borrow and supply + // check the chnages should only be in eth supply & dai // check if status is safe and only have assets in the specific tokens } From 3f712600cc092b9cb7e97b42045a3d60af90635a Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 24 Sep 2020 17:48:41 +0530 Subject: [PATCH 20/33] checking only curve3pool in ethLogic exchange rate --- contracts/logics/exchangeRate/ethLogic.sol | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/contracts/logics/exchangeRate/ethLogic.sol b/contracts/logics/exchangeRate/ethLogic.sol index cc7596c..1924193 100644 --- a/contracts/logics/exchangeRate/ethLogic.sol +++ b/contracts/logics/exchangeRate/ethLogic.sol @@ -57,7 +57,8 @@ contract EthRateLogic is DSMath { address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant ctokenMapping = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); - address public constant curveRegistryAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant curve3poolAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant curve3poolTokenAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { uint totalSupplyInETH; @@ -81,19 +82,13 @@ contract EthRateLogic is DSMath { } function getCurveNetAssetsInEth(address _dsa) private view returns (uint256 _netBal) { - // NOTICE - only stable coins pool as of now - CurveRegistry curveRegistry = CurveRegistry(curveRegistryAddr); - uint poolLen = curveRegistry.pool_count(); PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); uint ethPriceUSD = priceFeedContract.getEthPrice(); - for (uint i = 0; i < poolLen; i++) { - (address curvePoolAddr, address curveTokenAddr) = curveRegistry.pool_list(i); - uint virtualPrice = ICurve(curvePoolAddr).get_virtual_price(); - uint curveTokenBal = TokenInterface(curveTokenAddr).balanceOf(_dsa); - uint amtInUSD = wmul(curveTokenBal, virtualPrice); - uint amtInETH = wdiv(amtInUSD, ethPriceUSD); - _netBal = add(_netBal, amtInETH); - } + uint virtualPrice = ICurve(curve3poolAddr).get_virtual_price(); + uint curveTokenBal = TokenInterface(curve3poolTokenAddr).balanceOf(_dsa); + uint amtInUSD = wmul(curveTokenBal, virtualPrice); + uint amtInETH = wdiv(amtInUSD, ethPriceUSD); + _netBal = add(_netBal, amtInETH); } function getNetDsaAssets(address _dsa) private returns (uint256 _netBal) { From ebb634397761933cc215ab047cdd5c40d17a00f6 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 24 Sep 2020 17:49:11 +0530 Subject: [PATCH 21/33] removed TODO --- contracts/logics/exchangeRate/ethLogic.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/contracts/logics/exchangeRate/ethLogic.sol b/contracts/logics/exchangeRate/ethLogic.sol index 1924193..34e3b3a 100644 --- a/contracts/logics/exchangeRate/ethLogic.sol +++ b/contracts/logics/exchangeRate/ethLogic.sol @@ -3,7 +3,6 @@ pragma solidity ^0.6.8; import { DSMath } from "../../libs/safeMath.sol"; -// TODO - have to check y pool virtual price interface CTokenInterface { function borrowBalanceCurrent(address account) external returns (uint256); function exchangeRateCurrent() external returns (uint256); From f3e2e5d8fd9c0ae95757f0c4b3baa28960072026 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sat, 26 Sep 2020 02:59:22 +1000 Subject: [PATCH 22/33] ETH pool logics done --- contracts/logics/settle/eth/crvMining.sol | 100 ++++++++++++---- contracts/logics/settle/eth/maker.sol | 91 ++++++++++++++ contracts/logics/settle/eth/maxComp.sol | 140 +++++++++++++++------- 3 files changed, 264 insertions(+), 67 deletions(-) create mode 100644 contracts/logics/settle/eth/maker.sol diff --git a/contracts/logics/settle/eth/crvMining.sol b/contracts/logics/settle/eth/crvMining.sol index 2430c60..ccac2b5 100644 --- a/contracts/logics/settle/eth/crvMining.sol +++ b/contracts/logics/settle/eth/crvMining.sol @@ -1,44 +1,98 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.6.8; +pragma experimental ABIEncoderV2; import { DSMath } from "../../../libs/safeMath.sol"; +interface DSAInterface { + function cast(address[] calldata _targets, bytes[] calldata _data, address _origin) external payable; +} + contract LogicOne { - // borrow from Compound & deposit in Curve (static logic for DAI) - function compCrvMine(address token, uint amt, uint unitAmt, string calldata guage) public { - address[] memory _targets = new address[](3); - bytes[] memory _data = new bytes[](3); - _targets[0] = address(0); // Check9898 - address of compound connector - _data[0] = abi.encodeWithSignature("borrow(address,uint256,uint256,uint256)", token, amt, uint(0), uint(0)); - _targets[1] = address(0); // Check9898 - address of curve connector - _data[1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0)); - _targets[2] = address(0); // Check9898 - address of curve guage connector - _data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", guage, uint(-1), uint(0), uint(0)); - // check if status is safe and only have assets in the specific tokens + function getEthAddress() private pure returns(address) { + return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; } - function compCrvRedeem(address token, uint amt, uint unitAmt, string calldata guage) public { - // Withdraw from Curve and payback on Compound + function getCrvAddress() private pure returns(address) { + return 0xD533a949740bb3306d119CC777fa900bA034cd52; + } + + function getOriginAddress() private pure returns(address) { + return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // Origin address + } + + function getDsaAddress() private pure returns(address) { + return address(0); // DSA address + } + + function getGuageAddress() private pure returns(address) { + return 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d; // DSA address + } + + function getGuageName() private pure returns(string memory) { + return "guage-3"; // Curve Guage name + } + + function getCurveConnectAddress() private pure returns(address) { + return 0x1568a9D336A7aC051DCC4bdcc4A0B09299DE5Daf; + } + + function getCurveGuageConnectAddress() private pure returns(address) { + return 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d; + } + + function getUniswapConnectAddress() private pure returns(address) { + return 0x62EbfF47B2Ba3e47796efaE7C51676762dC961c0; + } + + function mineCrv(address token, uint amt, uint unitAmt) external { + address[] memory _targets = new address[](2); + bytes[] memory _data = new bytes[](2); + _targets[1] = address(0); // Check9898 - address of curve 3pool connector + _data[1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0)); + _targets[2] = address(0); // Check9898 - address of curve 3pool guage connector + _data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0)); + DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress()); + } + + function redeemCrv(address token, uint amt, uint unitAmt) external { address[] memory _targets; bytes[] memory _data; if (amt == uint(-1)) { + _targets = new address[](2); + _data = new bytes[](2); + } else { _targets = new address[](3); _data = new bytes[](3); - } else { - _targets = new address[](4); - _data = new bytes[](4); } - _targets[0] = address(0); // Check9898 - address of curve guage connector - _data[0] = abi.encodeWithSignature("withdraw(string,uint256,uint256,uint256,uint256,uint256)", guage, uint(-1), uint(0), uint(0), uint(0), uint(0)); - _targets[1] = address(0); // Check9898 - address of curve connector + _targets[0] = address(0); // Check9898 - address of curve 3pool guage connector + _data[0] = abi.encodeWithSignature("withdraw(string,uint256,uint256,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0), uint(0), uint(0)); + _targets[1] = address(0); // Check9898 - address of curve 3pool connector _data[1] = abi.encodeWithSignature("withdraw(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0)); - _targets[2] = address(0); // Check9898 - address of compound connector - _data[2] = abi.encodeWithSignature("payback(address,uint256,uint256,uint256)", token, amt, uint(0), uint(0)); if (amt != uint(-1)) { - _targets[3] = address(0); // Check9898 - address of curve guage connector - _data[3] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", guage, uint(-1), uint(0), uint(0)); + _targets[2] = address(0); // Check9898 - address of curve 3pool guage connector + _data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0)); } + DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress()); + } + + function claimCrv() external { + address[] memory _target = new address[](1); + bytes[] memory _data = new bytes[](1); + _target[0] = 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d; // Curve guage connector + _data[0] = abi.encodeWithSignature("claimReward(string,uint256,uint256)", getGuageName(), 0, 0); + DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); + } + + function claimCrvAndSwap(uint amt, uint unitAmt) external { + address crv = getCrvAddress(); + address eth = getEthAddress(); + address[] memory _target = new address[](1); + bytes[] memory _data = new bytes[](1); + _target[0] = getUniswapConnectAddress(); // Uniswap Connector + _data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", eth, crv, amt, unitAmt, 0, 0); + DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } receive() external payable {} diff --git a/contracts/logics/settle/eth/maker.sol b/contracts/logics/settle/eth/maker.sol new file mode 100644 index 0000000..0862d0d --- /dev/null +++ b/contracts/logics/settle/eth/maker.sol @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; +pragma experimental ABIEncoderV2; + +import { DSMath } from "../../../libs/safeMath.sol"; + +interface VaultDataInterface { + + struct VaultData { + uint id; + address owner; + string colType; + uint collateral; + uint art; + uint debt; + uint liquidatedCol; + uint borrowRate; + uint colPrice; + uint liquidationRatio; + address vaultAddress; + } + + function getVaultById(uint id) external view returns (VaultData memory); + +} + +interface DSAInterface { + function cast(address[] calldata _targets, bytes[] calldata _datas, address _origin) external payable; +} + +contract LogicOne is DSMath { + + function getOriginAddress() private pure returns(address) { + return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // DSA address + } + + function getMcdAddresses() public pure returns (address) { + return 0xF23196DF1C440345DE07feFbe556a5eF0dcD29F0; + } + + function getInstaMakerResolver() public pure returns (address) { + return 0x0A7008B38E7015F8C36A49eEbc32513ECA8801E5; + } + + function getMakerConnectAddress() public pure returns (address) { + return 0x6c4E4D4aB22cAB08b8498a3A232D92609e8b2d62; + } + + function getDsaAddress() private pure returns(address) { + return address(0); // DSA address + } + + function vaultId() private pure returns(uint) { + return 0; // vault ID + } + + function checkMakerVault() private view { + VaultDataInterface.VaultData memory vaultData = VaultDataInterface(getInstaMakerResolver()).getVaultById(vaultId()); + uint col = vaultData.collateral; + uint debt = vaultData.debt; + uint price = vaultData.colPrice / 10 ** 9; // making 18 decimal + // uint liquidation = vaultData.liqInk / 10 ** 9; // making 18 decimal + uint currentRatio = wdiv(wmul(col, price), debt); + require(200 * 10 ** 18 < currentRatio, "position-risky"); // ratio should be less than 50% (should we keep it 60%?) + } + + function depositAndBorrow(uint depositAmt, uint borrowAmt) public { + address[] memory _targets = new address[](2); + bytes[] memory _data = new bytes[](2); + _targets[0] = getMakerConnectAddress(); + _data[0] = abi.encodeWithSignature("deposit(uint256,uint256,uint256,uint256)", vaultId(), depositAmt, uint(0), uint(0)); + _targets[1] = getMakerConnectAddress(); + _data[1] = abi.encodeWithSignature("borrow(uint256,uint256,uint256,uint256)", vaultId(), borrowAmt, uint(0), uint(0)); + DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress()); + checkMakerVault(); + } + + function paybackAndWithdraw(uint withdrawAmt, uint paybackAmt) public { + address[] memory _targets = new address[](2); + bytes[] memory _data = new bytes[](2); + _targets[0] = getMakerConnectAddress(); + _data[0] = abi.encodeWithSignature("payback(uint256,uint256,uint256,uint256)", vaultId(), paybackAmt, uint(0), uint(0)); + _targets[1] = getMakerConnectAddress(); + _data[1] = abi.encodeWithSignature("withdraw(uint256,uint256,uint256,uint256)", vaultId(), withdrawAmt, uint(0), uint(0)); + DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress()); + checkMakerVault(); + } + + receive() external payable {} + +} diff --git a/contracts/logics/settle/eth/maxComp.sol b/contracts/logics/settle/eth/maxComp.sol index 588f82b..094de68 100644 --- a/contracts/logics/settle/eth/maxComp.sol +++ b/contracts/logics/settle/eth/maxComp.sol @@ -2,7 +2,7 @@ pragma solidity ^0.6.8; pragma experimental ABIEncoderV2; -import { DSMath } from "../../libs/safeMath.sol"; +import { DSMath } from "../../../libs/safeMath.sol"; interface CTokenInterface { function borrowBalanceCurrent(address account) external returns (uint256); @@ -29,53 +29,105 @@ interface InstaMapping { function cTokenMapping(address) external view returns (address); } -contract LogicOne { +contract LogicOne is DSMath { - address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); - address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); - address public constant cdaiAddr = address(0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643); - address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; - address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); - - function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { - uint totalSupplyInETH; - uint totalBorrowInETH; - address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); - OracleComp priceFeedContract = OracleComp(compOracleAddr); - // uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); - for (uint i = 0; i < allMarkets.length; i++) { - CTokenInterface ctoken = CTokenInterface(allMarkets[i]); - uint tokenPriceInETH = priceFeedContract.getPrice(address(ctoken) == cethAddr ? ethAddr : ctoken.underlying()); - uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent()); - uint supplyInETH = wmul(supply, tokenPriceInETH); - - uint borrow = ctoken.borrowBalanceCurrent(_dsa); - uint borrowInETH = wmul(borrow, tokenPriceInETH); - - totalSupplyInETH += add(totalSupplyInETH, supplyInETH); - totalBorrowInETH = add(totalBorrowInETH, borrowInETH); - - if (allMarkets[i] != cdaiAddr && allMarkets[i] != cethAddr) { - require(supply == 0 && borrow == 0, "assets"); - } - // require() - } - _netBal = sub(totalSupplyInETH, totalBorrowInETH); + struct CastData { + address[] dsaTargets; + bytes[] dsaData; } - function maxComp(address _dsa, address[] calldata _targets, bytes[] calldata _data) public { - // check if DSA is authorised for interaction - // Also think on dydx flash loan connector - // initial Compound position borrow and supply - address compoundConnector = address(0); // Check9898 - address of compound connector - address instaPoolConnector = address(0); // Check9898 - address of instaPool connector - for (uint i = 0; i < _targets.length; i++) { - require(_targets[i] == compoundConnector || _targets[i] == instaPoolConnector, "connector-not-authorised"); + function getOriginAddress() private pure returns(address) { + return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // DSA address + } + + function getEthAddress() private pure returns(address) { + return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; // DSA address + } + + function getCompAddress() private pure returns(address) { + return 0xc00e94Cb662C3520282E6f5717214004A7f26888; // DSA address + } + + function getComptrollerAddress() private pure returns (address) { + return 0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B; + } + + function getDaiAddress() private pure returns(address) { + return 0x6B175474E89094C44Da98b954EedeAC495271d0F; // DAI address + } + + function getCdaiAddress() private pure returns(address) { + return 0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643; // CDAI address + } + + function getDsaAddress() private pure returns(address) { + return address(0); // DSA address + } + + function getCompoundConnectAddress() private pure returns(address) { + return 0x07F81230d73a78f63F0c2A3403AD281b067d28F8; + } + + function getFlashloanConnectAddress() private pure returns(address) { + return 0xaA3EA0b22802d68DA73D5f4D3f9F1C7C238fE03A; + } + + function getCompConnectAddress() private pure returns(address) { + return 0xB4a04F1C194bEed64FCE27843B5b3079339cdaD4; + } + + function getUniswapConnectAddress() private pure returns(address) { + return 0x62EbfF47B2Ba3e47796efaE7C51676762dC961c0; + } + + function checkCompoundAssets() private { + address[] memory allMarkets = CompTroller(getComptrollerAddress()).getAllMarkets(); + uint supply; + uint borrow; + for (uint i = 0; i < allMarkets.length; i++) { + CTokenInterface ctoken = CTokenInterface(allMarkets[i]); + if (allMarkets[i] == getCdaiAddress()) { + supply = wmul(ctoken.balanceOf(getDsaAddress()), ctoken.exchangeRateCurrent()); + } + borrow = ctoken.borrowBalanceCurrent(getDsaAddress()); + + if (allMarkets[i] != getCdaiAddress()) { + require(borrow == 0, "assets"); + } else { + require(wdiv(borrow, supply) < 745 * 10 ** 15, "position-risky"); // DAI ratio - should be less than 74.5% + } } - DSAInterface(_dsa).cast(_targets, _data, address(0)); // Check9898 - address of basic connector - // final Compound position borrow and supply - // check the chnages should only be in eth supply & dai - // check if status is safe and only have assets in the specific tokens + } + + function maxComp(uint flashAmt, uint route, address[] calldata _targets, bytes[] calldata _data) external { + address compoundConnect = getCompoundConnectAddress(); + address flashloanConnect = getFlashloanConnectAddress(); + for (uint i = 0; i < _targets.length; i++) { + require(_targets[i] == compoundConnect || _targets[i] == flashloanConnect, "not-authorised-connector"); + } + bytes memory _dataEncode = abi.encode(_targets, _data); + address[] memory _targetFlash = new address[](1); + bytes[] memory _dataFlash = new bytes[](1); + _targetFlash[0] = flashloanConnect; + _dataFlash[0] = abi.encodeWithSignature("flashBorrowAndCast(address,uint256,uint256,bytes)", getDaiAddress(), flashAmt, route, _dataEncode); + DSAInterface(getDsaAddress()).cast(_targetFlash, _dataFlash, getOriginAddress()); + checkCompoundAssets(); + } + + function claimComp(address[] calldata tokens) external { + address[] memory _target = new address[](1); + bytes[] memory _data = new bytes[](1); + _target[0] = getCompConnectAddress(); // Comp connector + _data[0] = abi.encodeWithSignature("ClaimCompTwo(address[],uint256)", tokens, 0); + DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); + } + + function swapComp(uint amt, uint unitAmt) external { + address[] memory _target = new address[](1); + bytes[] memory _data = new bytes[](1); + _target[0] = getUniswapConnectAddress(); // Uniswap Connector + _data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", getEthAddress(), getCompAddress(), amt, unitAmt, 0, 0); + DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } receive() external payable {} From 7a5b1272253e47e85b2cf941ed2070d62a6e1131 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Sat, 26 Sep 2020 03:27:10 +1000 Subject: [PATCH 23/33] minor bug fixes --- contracts/logics/settle/eth/basic.sol | 7 ++++++- contracts/logics/settle/eth/crvMining.sol | 20 ++++++++++---------- contracts/logics/settle/eth/maxComp.sol | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/contracts/logics/settle/eth/basic.sol b/contracts/logics/settle/eth/basic.sol index bac5305..751745e 100644 --- a/contracts/logics/settle/eth/basic.sol +++ b/contracts/logics/settle/eth/basic.sol @@ -22,6 +22,10 @@ contract LogicOne { return 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; // ETH Address } + function getOriginAddress() private pure returns(address) { + return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // DSA address + } + function deploy(address _dsa, address _token, uint amt) public { // check if DSA is authorised if (_token == getEthAddr()) { @@ -34,6 +38,7 @@ contract LogicOne { amt = amt > _bal ? _bal : amt; token.safeTransfer(_dsa, amt); } + // emit event? } // withdraw assets from DSA @@ -44,7 +49,7 @@ contract LogicOne { _targets[0] = address(0); // Check9898 - address of basic connector bytes[] memory _data = new bytes[](1); _data[0] = abi.encodeWithSignature("withdraw(address,uint256,address,uint256,uint256)", _token, amt, address(this), uint(0), uint(0)); - DSAInterface(_dsa).cast(_targets, _data, address(0)); // Check9898 - address of origin + DSAInterface(_dsa).cast(_targets, _data, getOriginAddress()); } constructor () public {} diff --git a/contracts/logics/settle/eth/crvMining.sol b/contracts/logics/settle/eth/crvMining.sol index ccac2b5..558f928 100644 --- a/contracts/logics/settle/eth/crvMining.sol +++ b/contracts/logics/settle/eth/crvMining.sol @@ -19,7 +19,7 @@ contract LogicOne { } function getOriginAddress() private pure returns(address) { - return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; // Origin address + return 0xB7fA44c2E964B6EB24893f7082Ecc08c8d0c0F87; } function getDsaAddress() private pure returns(address) { @@ -27,11 +27,11 @@ contract LogicOne { } function getGuageAddress() private pure returns(address) { - return 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d; // DSA address + return 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d; } function getGuageName() private pure returns(string memory) { - return "guage-3"; // Curve Guage name + return "guage-3"; } function getCurveConnectAddress() private pure returns(address) { @@ -49,9 +49,9 @@ contract LogicOne { function mineCrv(address token, uint amt, uint unitAmt) external { address[] memory _targets = new address[](2); bytes[] memory _data = new bytes[](2); - _targets[1] = address(0); // Check9898 - address of curve 3pool connector + _targets[1] = getCurveConnectAddress(); _data[1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0)); - _targets[2] = address(0); // Check9898 - address of curve 3pool guage connector + _targets[2] = getCurveGuageConnectAddress(); _data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0)); DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress()); } @@ -66,12 +66,12 @@ contract LogicOne { _targets = new address[](3); _data = new bytes[](3); } - _targets[0] = address(0); // Check9898 - address of curve 3pool guage connector + _targets[0] = getCurveGuageConnectAddress(); _data[0] = abi.encodeWithSignature("withdraw(string,uint256,uint256,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0), uint(0), uint(0)); - _targets[1] = address(0); // Check9898 - address of curve 3pool connector + _targets[1] = getCurveConnectAddress(); _data[1] = abi.encodeWithSignature("withdraw(address,uint256,uint256,uint256,uint256)", token, amt, unitAmt, uint(0), uint(0)); if (amt != uint(-1)) { - _targets[2] = address(0); // Check9898 - address of curve 3pool guage connector + _targets[2] = getCurveGuageConnectAddress(); _data[2] = abi.encodeWithSignature("deposit(string,uint256,uint256,uint256)", getGuageName(), uint(-1), uint(0), uint(0)); } DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress()); @@ -80,7 +80,7 @@ contract LogicOne { function claimCrv() external { address[] memory _target = new address[](1); bytes[] memory _data = new bytes[](1); - _target[0] = 0xAf615b36Db171fD5A369A0060b9bCB88fFf0190d; // Curve guage connector + _target[0] = getCurveGuageConnectAddress(); _data[0] = abi.encodeWithSignature("claimReward(string,uint256,uint256)", getGuageName(), 0, 0); DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } @@ -90,7 +90,7 @@ contract LogicOne { address eth = getEthAddress(); address[] memory _target = new address[](1); bytes[] memory _data = new bytes[](1); - _target[0] = getUniswapConnectAddress(); // Uniswap Connector + _target[0] = getUniswapConnectAddress(); _data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", eth, crv, amt, unitAmt, 0, 0); DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } diff --git a/contracts/logics/settle/eth/maxComp.sol b/contracts/logics/settle/eth/maxComp.sol index 094de68..ddd2e61 100644 --- a/contracts/logics/settle/eth/maxComp.sol +++ b/contracts/logics/settle/eth/maxComp.sol @@ -117,7 +117,7 @@ contract LogicOne is DSMath { function claimComp(address[] calldata tokens) external { address[] memory _target = new address[](1); bytes[] memory _data = new bytes[](1); - _target[0] = getCompConnectAddress(); // Comp connector + _target[0] = getCompConnectAddress(); _data[0] = abi.encodeWithSignature("ClaimCompTwo(address[],uint256)", tokens, 0); DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } @@ -125,7 +125,7 @@ contract LogicOne is DSMath { function swapComp(uint amt, uint unitAmt) external { address[] memory _target = new address[](1); bytes[] memory _data = new bytes[](1); - _target[0] = getUniswapConnectAddress(); // Uniswap Connector + _target[0] = getUniswapConnectAddress(); _data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", getEthAddress(), getCompAddress(), amt, unitAmt, 0, 0); DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } From 17be61f608e46d4fbe0ca851126a5ce33666ca27 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sun, 27 Sep 2020 22:35:14 +0530 Subject: [PATCH 24/33] done with eth pool exchange rate --- contracts/logics/exchangeRate/ethLogic.sol | 76 ++++++++++++++-------- 1 file changed, 50 insertions(+), 26 deletions(-) diff --git a/contracts/logics/exchangeRate/ethLogic.sol b/contracts/logics/exchangeRate/ethLogic.sol index 34e3b3a..2750401 100644 --- a/contracts/logics/exchangeRate/ethLogic.sol +++ b/contracts/logics/exchangeRate/ethLogic.sol @@ -16,23 +16,6 @@ interface CompTroller { function getAllMarkets() external view returns (address[] memory); } -interface OracleComp { - function getUnderlyingPrice(address) external view returns (uint); -} - -interface InstaMapping { - function cTokenMapping(address) external view returns (address); -} - -interface CurveMapping { - function curvePoolMapping(address poolAddr) external view returns (address); -} - -interface CurveRegistry { - function pool_list(uint) external view returns (address poolAddress, address poolToken); - function pool_count() external view returns (uint); -} - interface ICurve { function get_virtual_price() external view returns (uint256 out); } @@ -47,27 +30,65 @@ interface PriceFeedInterface { function getEthPrice() external view returns (uint256 ethPriceUSD); } +interface ManagerLike { + function ilks(uint) external view returns (bytes32); + function owns(uint) external view returns (address); + function urns(uint) external view returns (address); + function vat() external view returns (address); +} + +interface VatLike { + function ilks(bytes32) external view returns (uint, uint, uint, uint, uint); + function dai(address) external view returns (uint); + function urns(bytes32, address) external view returns (uint, uint); + function gem(bytes32, address) external view returns (uint); +} + contract EthRateLogic is DSMath { address public immutable poolToken; + address public immutable dsa; + + uint public immutable vaultId; + address public immutable vaultUrn; + + address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + address public constant daiAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + + address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); - address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; - address public constant compOracleAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); - address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); - address public constant ctokenMapping = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant curve3poolAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); address public constant curve3poolTokenAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant managerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + bytes32 public constant ethIlk = bytes32(0); + + + function getMakerNetAssetsInEth() private view returns (uint256 _netBal) { + ManagerLike managerContract = ManagerLike(managerAddr); + VatLike vatContract = VatLike(managerContract.vat()); + uint daiPriceInETH = PriceFeedInterface(PriceFeedAddr).getPrice(daiAddr); + + (uint coll, uint art) = vatContract.urns(ethIlk, vaultUrn); + (,uint rate,,,) = vatContract.ilks(ethIlk); + uint debt = rmul(art,rate); + uint debtInEth = wmul(debt, daiPriceInETH); + return sub(coll, debtInEth); + + } + function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { uint totalSupplyInETH; uint totalBorrowInETH; address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); - // uint ethPrice = oracleContract.getUnderlyingPrice(cethAddr); + for (uint i = 0; i < allMarkets.length; i++) { CTokenInterface ctoken = CTokenInterface(allMarkets[i]); - uint tokenPriceInETH = priceFeedContract.getPrice(address(ctoken) == cethAddr ? ethAddr : ctoken.underlying()); + uint tokenPriceInETH = address(ctoken) == cethAddr ? 10 ** 18 : priceFeedContract.getPrice(ctoken.underlying()); uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent()); uint supplyInETH = wmul(supply, tokenPriceInETH); @@ -93,6 +114,7 @@ contract EthRateLogic is DSMath { function getNetDsaAssets(address _dsa) private returns (uint256 _netBal) { _netBal = _dsa.balance; _netBal += getCompoundNetAssetsInEth(_dsa); + _netBal += getMakerNetAssetsInEth(); _netBal += getCurveNetAssetsInEth(_dsa); } @@ -103,9 +125,11 @@ contract EthRateLogic is DSMath { return bal; } - constructor (address ethPool) public { + constructor (address ethPool, address _dsa, uint _vaultId) public { poolToken = address(ethPool); - } + vaultId = _vaultId; + dsa = _dsa; + vaultUrn = ManagerLike(managerAddr).urns(_vaultId); - receive() external payable {} + } } \ No newline at end of file From 80164b1683d5f620b88fa04f68bb63f454078424 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sun, 27 Sep 2020 22:35:21 +0530 Subject: [PATCH 25/33] done with dai pool exchange rate --- contracts/logics/exchangeRate/daiLogic.sol | 101 +++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 contracts/logics/exchangeRate/daiLogic.sol diff --git a/contracts/logics/exchangeRate/daiLogic.sol b/contracts/logics/exchangeRate/daiLogic.sol new file mode 100644 index 0000000..a8b6a48 --- /dev/null +++ b/contracts/logics/exchangeRate/daiLogic.sol @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +import { DSMath } from "../../libs/safeMath.sol"; + +interface CTokenInterface { + function borrowBalanceCurrent(address account) external returns (uint256); + function exchangeRateCurrent() external returns (uint256); + + function balanceOf(address owner) external view returns (uint256); + + function underlying() external view returns (address); +} + +interface CompTroller { + function getAllMarkets() external view returns (address[] memory); +} + +interface ICurve { + function get_virtual_price() external view returns (uint256 out); +} + +interface TokenInterface { + 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 DaiRateLogic is DSMath { + address public immutable poolToken; + address public immutable dsa; + + address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + address public constant daiAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + + address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + address public constant curve3poolAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant curve3poolTokenAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBal) { + uint totalSupplyInETH; + uint totalBorrowInETH; + address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + + for (uint i = 0; i < allMarkets.length; i++) { + CTokenInterface ctoken = CTokenInterface(allMarkets[i]); + uint tokenPriceInETH = address(ctoken) == cethAddr ? 10 ** 18 : priceFeedContract.getPrice(ctoken.underlying()); + uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent()); + uint supplyInETH = wmul(supply, tokenPriceInETH); + + uint borrow = ctoken.borrowBalanceCurrent(_dsa); + uint borrowInETH = wmul(borrow, tokenPriceInETH); + + totalSupplyInETH += add(totalSupplyInETH, supplyInETH); + totalBorrowInETH = add(totalBorrowInETH, borrowInETH); + } + _netBal = sub(totalSupplyInETH, totalBorrowInETH); + } + + function getCurveNetAssetsInEth(address _dsa) private view returns (uint256 _netBal) { + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + uint ethPriceUSD = priceFeedContract.getEthPrice(); + uint virtualPrice = ICurve(curve3poolAddr).get_virtual_price(); + uint curveTokenBal = TokenInterface(curve3poolTokenAddr).balanceOf(_dsa); + uint amtInUSD = wmul(curveTokenBal, virtualPrice); + uint amtInETH = wdiv(amtInUSD, ethPriceUSD); + _netBal = add(_netBal, amtInETH); + } + + function getNetDsaAssets(address _dsa) private returns (uint256 _netBal) { + _netBal = _dsa.balance; + _netBal += getCompoundNetAssetsInEth(_dsa); + _netBal += getCurveNetAssetsInEth(_dsa); + } + + function getTotalToken() public returns (uint256) { + address _dsa = 0x0000000000000000000000000000000000000000; + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + uint daiPriceInETH = priceFeedContract.getPrice(daiAddr); + + uint256 balInEth = poolToken.balance; + balInEth += getNetDsaAssets(_dsa); + uint balInDai = wdiv(balInEth, daiPriceInETH); + return balInDai; + } + + constructor (address daiPool, address _dsa) public { + poolToken = address(daiPool); + dsa = _dsa; + + } +} \ No newline at end of file From 001fc9175cd9555265f07975ab7e4b9a33a03f56 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Sun, 27 Sep 2020 22:35:26 +0530 Subject: [PATCH 26/33] done with usdc pool exchange rate --- contracts/logics/exchangeRate/usdcLogic.sol | 144 ++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 contracts/logics/exchangeRate/usdcLogic.sol diff --git a/contracts/logics/exchangeRate/usdcLogic.sol b/contracts/logics/exchangeRate/usdcLogic.sol new file mode 100644 index 0000000..07b02f4 --- /dev/null +++ b/contracts/logics/exchangeRate/usdcLogic.sol @@ -0,0 +1,144 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.6.8; + +import { DSMath } from "../../libs/safeMath.sol"; + +interface CTokenInterface { + function borrowBalanceCurrent(address account) external returns (uint256); + function exchangeRateCurrent() external returns (uint256); + + function balanceOf(address owner) external view returns (uint256); + + function underlying() external view returns (address); +} + +interface CompTroller { + function getAllMarkets() external view returns (address[] memory); +} + +interface ICurve { + function get_virtual_price() external view returns (uint256 out); +} + +interface TokenInterface { + 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); +} + +interface ManagerLike { + function ilks(uint) external view returns (bytes32); + function owns(uint) external view returns (address); + function urns(uint) external view returns (address); + function vat() external view returns (address); +} + +interface VatLike { + function ilks(bytes32) external view returns (uint, uint, uint, uint, uint); + function dai(address) external view returns (uint); + function urns(bytes32, address) external view returns (uint, uint); + function gem(bytes32, address) external view returns (uint); +} + + +contract UsdcRateLogic is DSMath { + address public immutable poolToken; + address public immutable dsa; + + uint public immutable vaultId; + address public immutable vaultUrn; + + address public constant ethAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + address public constant daiAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + address public constant usdcAddr = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + + address public constant PriceFeedAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + address public constant compTrollerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant cethAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + address public constant curve3poolAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + address public constant curve3poolTokenAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + + address public constant managerAddr = address(0xe81F70Cc7C0D46e12d70efc60607F16bbD617E88); + bytes32 public constant usdcAIlk = bytes32(0); + + + function getMakerNetAssetsInEth() private view returns (uint256 _netBal) { + ManagerLike managerContract = ManagerLike(managerAddr); + VatLike vatContract = VatLike(managerContract.vat()); + uint daiPriceInETH = PriceFeedInterface(PriceFeedAddr).getPrice(daiAddr); + uint usdcPriceInETH = PriceFeedInterface(PriceFeedAddr).getPrice(usdcAddr); + + (uint coll, uint art) = vatContract.urns(usdcAIlk, vaultUrn); + (,uint rate,,,) = vatContract.ilks(usdcAIlk); + uint debt = rmul(art, rate); + + uint debtInEth = wmul(debt, daiPriceInETH); + uint collInEth = wmul(coll, usdcPriceInETH); + return sub(collInEth, debtInEth); + + } + + function getCompoundNetAssetsInEth(address _dsa) private returns (uint256 _netBalInEth) { + uint totalSupplyInETH; + uint totalBorrowInETH; + address[] memory allMarkets = CompTroller(compTrollerAddr).getAllMarkets(); + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + + for (uint i = 0; i < allMarkets.length; i++) { + CTokenInterface ctoken = CTokenInterface(allMarkets[i]); + uint tokenPriceInETH = address(ctoken) == cethAddr ? 10 ** 18 : priceFeedContract.getPrice(ctoken.underlying()); + uint supply = wmul(ctoken.balanceOf(_dsa), ctoken.exchangeRateCurrent()); + uint supplyInETH = wmul(supply, tokenPriceInETH); + + uint borrow = ctoken.borrowBalanceCurrent(_dsa); + uint borrowInETH = wmul(borrow, tokenPriceInETH); + + totalSupplyInETH += add(totalSupplyInETH, supplyInETH); + totalBorrowInETH = add(totalBorrowInETH, borrowInETH); + } + _netBalInEth = sub(totalSupplyInETH, totalBorrowInETH); + } + + function getCurveNetAssetsInEth(address _dsa) private view returns (uint256 _netBalInEth) { + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + uint ethPriceUSD = priceFeedContract.getEthPrice(); + uint virtualPrice = ICurve(curve3poolAddr).get_virtual_price(); + uint curveTokenBal = TokenInterface(curve3poolTokenAddr).balanceOf(_dsa); + uint amtInUSD = wmul(curveTokenBal, virtualPrice); + uint amtInETH = wdiv(amtInUSD, ethPriceUSD); + _netBalInEth = add(_netBalInEth, amtInETH); + } + + function getNetDsaAssets(address _dsa) private returns (uint256 _netBalInEth) { + _netBalInEth = _dsa.balance; + _netBalInEth += getCompoundNetAssetsInEth(_dsa); + _netBalInEth += getMakerNetAssetsInEth(); + _netBalInEth += getCurveNetAssetsInEth(_dsa); + } + + function getTotalToken() public returns (uint256) { + address _dsa = 0x0000000000000000000000000000000000000000; + PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); + uint usdcPriceInETH = priceFeedContract.getPrice(usdcAddr); + + uint256 balInEth = poolToken.balance; + balInEth += getNetDsaAssets(_dsa); + uint balInUsdc = wdiv(balInEth, usdcPriceInETH); + return balInUsdc; + } + + + constructor (address usdcPool, address _dsa, uint _vaultId) public { + poolToken = address(usdcPool); + vaultId = _vaultId; + dsa = _dsa; + vaultUrn = ManagerLike(managerAddr).urns(_vaultId); + + } +} \ No newline at end of file From 349f3496db8b09a109f5702b940a0aa40492adef Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 28 Sep 2020 13:35:54 +0530 Subject: [PATCH 27/33] removed receive fallback function crvMining.sol --- contracts/logics/settle/eth/crvMining.sol | 3 --- 1 file changed, 3 deletions(-) diff --git a/contracts/logics/settle/eth/crvMining.sol b/contracts/logics/settle/eth/crvMining.sol index 558f928..632a188 100644 --- a/contracts/logics/settle/eth/crvMining.sol +++ b/contracts/logics/settle/eth/crvMining.sol @@ -94,7 +94,4 @@ contract LogicOne { _data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", eth, crv, amt, unitAmt, 0, 0); DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } - - receive() external payable {} - } From ebfb2b7a03f508d8ddb8863eac5ec34d095ec8b4 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 28 Sep 2020 13:36:12 +0530 Subject: [PATCH 28/33] Minor changes to basic.sol settle logic --- contracts/logics/settle/eth/basic.sol | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/contracts/logics/settle/eth/basic.sol b/contracts/logics/settle/eth/basic.sol index 751745e..cd02417 100644 --- a/contracts/logics/settle/eth/basic.sol +++ b/contracts/logics/settle/eth/basic.sol @@ -9,6 +9,7 @@ import { DSMath } from "../../../libs/safeMath.sol"; interface DSAInterface { function cast(address[] calldata _targets, bytes[] calldata _data, address _origin) external payable; + function isAuth(address) external returns(bool); } contract LogicOne { @@ -27,7 +28,7 @@ contract LogicOne { } function deploy(address _dsa, address _token, uint amt) public { - // check if DSA is authorised + require(DSAInterface(_dsa).isAuth(address(this)), "pool-not-auth-in-dsa"); if (_token == getEthAddr()) { uint _bal = address(this).balance; amt = amt > _bal ? _bal : amt; @@ -51,9 +52,4 @@ contract LogicOne { _data[0] = abi.encodeWithSignature("withdraw(address,uint256,address,uint256,uint256)", _token, amt, address(this), uint(0), uint(0)); DSAInterface(_dsa).cast(_targets, _data, getOriginAddress()); } - - constructor () public {} - - receive() external payable {} - } From 1bebad05cd7b1416b67f34f9336918e538cd4ea4 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 28 Sep 2020 13:43:59 +0530 Subject: [PATCH 29/33] removed receive() function from exchangeRate.sol --- contracts/logics/settle/eth/exchangeRate.sol | 2 -- 1 file changed, 2 deletions(-) diff --git a/contracts/logics/settle/eth/exchangeRate.sol b/contracts/logics/settle/eth/exchangeRate.sol index 37493cc..e0ace7e 100644 --- a/contracts/logics/settle/eth/exchangeRate.sol +++ b/contracts/logics/settle/eth/exchangeRate.sol @@ -13,6 +13,4 @@ contract LogicOne { PoolInterface(address(this)).setExchangeRate(); } - receive() external payable {} - } From 314e64dfd06a83d6b0dfb8e33f255aed2983120a Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 28 Sep 2020 13:44:18 +0530 Subject: [PATCH 30/33] removed `receive()` fallback function from maker.sol settle logic --- contracts/logics/settle/eth/maker.sol | 3 --- 1 file changed, 3 deletions(-) diff --git a/contracts/logics/settle/eth/maker.sol b/contracts/logics/settle/eth/maker.sol index 0862d0d..504f82c 100644 --- a/contracts/logics/settle/eth/maker.sol +++ b/contracts/logics/settle/eth/maker.sol @@ -85,7 +85,4 @@ contract LogicOne is DSMath { DSAInterface(getDsaAddress()).cast(_targets, _data, getOriginAddress()); checkMakerVault(); } - - receive() external payable {} - } From cc548cfb693b1cc6b02076b747f7c5a8c7285c10 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 28 Sep 2020 13:44:45 +0530 Subject: [PATCH 31/33] removed `receive()` fallback function --- contracts/logics/settle/eth/maxComp.sol | 3 --- 1 file changed, 3 deletions(-) diff --git a/contracts/logics/settle/eth/maxComp.sol b/contracts/logics/settle/eth/maxComp.sol index ddd2e61..44dfe30 100644 --- a/contracts/logics/settle/eth/maxComp.sol +++ b/contracts/logics/settle/eth/maxComp.sol @@ -129,7 +129,4 @@ contract LogicOne is DSMath { _data[0] = abi.encodeWithSignature("sell(address,address,unit256,unit256,unit256,unit256)", getEthAddress(), getCompAddress(), amt, unitAmt, 0, 0); DSAInterface(getDsaAddress()).cast(_target, _data, getOriginAddress()); } - - receive() external payable {} - } From 9eab74bc40514de853f6c3e6642ab182837c46a9 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 28 Sep 2020 13:51:48 +0530 Subject: [PATCH 32/33] fixed Usdc exchange rate logic --- contracts/logics/exchangeRate/usdcLogic.sol | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/contracts/logics/exchangeRate/usdcLogic.sol b/contracts/logics/exchangeRate/usdcLogic.sol index 07b02f4..6c69792 100644 --- a/contracts/logics/exchangeRate/usdcLogic.sol +++ b/contracts/logics/exchangeRate/usdcLogic.sol @@ -115,22 +115,23 @@ contract UsdcRateLogic is DSMath { _netBalInEth = add(_netBalInEth, amtInETH); } - function getNetDsaAssets(address _dsa) private returns (uint256 _netBalInEth) { - _netBalInEth = _dsa.balance; + function getNetDsaAssetsInEth(address _dsa) private returns (uint256 _netBalInEth) { _netBalInEth += getCompoundNetAssetsInEth(_dsa); _netBalInEth += getMakerNetAssetsInEth(); _netBalInEth += getCurveNetAssetsInEth(_dsa); } - function getTotalToken() public returns (uint256) { + function getTotalToken() public returns (uint256 usdcBal) { address _dsa = 0x0000000000000000000000000000000000000000; PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); uint usdcPriceInETH = priceFeedContract.getPrice(usdcAddr); - uint256 balInEth = poolToken.balance; - balInEth += getNetDsaAssets(_dsa); - uint balInUsdc = wdiv(balInEth, usdcPriceInETH); - return balInUsdc; + TokenInterface usdcToken = TokenInterface(usdcAddr); + usdcBal = usdcToken.balanceOf(_dsa); + usdcBal += TokenInterface(usdcAddr).balanceOf(poolToken); + + uint balInEth = getNetDsaAssetsInEth(_dsa); + usdcBal += wdiv(balInEth, usdcPriceInETH); } @@ -139,6 +140,5 @@ contract UsdcRateLogic is DSMath { vaultId = _vaultId; dsa = _dsa; vaultUrn = ManagerLike(managerAddr).urns(_vaultId); - } } \ No newline at end of file From f935e7e3f23cd195828ca384b75cc0c0febc8bcd Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Mon, 28 Sep 2020 13:52:26 +0530 Subject: [PATCH 33/33] fixed dai exchangeRate logic --- contracts/logics/exchangeRate/daiLogic.sol | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/contracts/logics/exchangeRate/daiLogic.sol b/contracts/logics/exchangeRate/daiLogic.sol index a8b6a48..cdb4fd4 100644 --- a/contracts/logics/exchangeRate/daiLogic.sol +++ b/contracts/logics/exchangeRate/daiLogic.sol @@ -12,6 +12,10 @@ interface CTokenInterface { function underlying() external view returns (address); } +interface TokenInterface { + function balanceOf(address owner) external view returns (uint256); +} + interface CompTroller { function getAllMarkets() external view returns (address[] memory); } @@ -20,9 +24,6 @@ interface ICurve { function get_virtual_price() external view returns (uint256 out); } -interface TokenInterface { - function balanceOf(address owner) external view returns (uint256); -} interface PriceFeedInterface { function getPrices(address[] memory tokens) external view returns (uint256[] memory pricesInETH); @@ -76,26 +77,26 @@ contract DaiRateLogic is DSMath { _netBal = add(_netBal, amtInETH); } - function getNetDsaAssets(address _dsa) private returns (uint256 _netBal) { - _netBal = _dsa.balance; + function getNetDsaAssetsInEth(address _dsa) private returns (uint256 _netBal) { _netBal += getCompoundNetAssetsInEth(_dsa); _netBal += getCurveNetAssetsInEth(_dsa); } - function getTotalToken() public returns (uint256) { + function getTotalToken() public returns (uint256 _daiBal) { address _dsa = 0x0000000000000000000000000000000000000000; PriceFeedInterface priceFeedContract = PriceFeedInterface(PriceFeedAddr); uint daiPriceInETH = priceFeedContract.getPrice(daiAddr); - - uint256 balInEth = poolToken.balance; - balInEth += getNetDsaAssets(_dsa); - uint balInDai = wdiv(balInEth, daiPriceInETH); - return balInDai; + + TokenInterface daiToken = TokenInterface(daiAddr); + _daiBal = daiToken.balanceOf(poolToken); + _daiBal += daiToken.balanceOf(_dsa); + + uint balInEth = getNetDsaAssetsInEth(_dsa); + _daiBal = wdiv(balInEth, daiPriceInETH); } constructor (address daiPool, address _dsa) public { poolToken = address(daiPool); dsa = _dsa; - } } \ No newline at end of file