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] 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 {}