From 9779cd424114c8fdcf8b7fdd66ff3c7fea6a6f95 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Wed, 7 Apr 2021 20:58:21 +0530 Subject: [PATCH] updates, fixes & refactoring --- .../receivers2/aave-v2-receiver/helpers.sol | 9 +++++++++ .../receivers2/aave-v2-receiver/main.sol | 19 +++++++++++++------ contracts/senders2/aave-v2-migrator/main.sol | 11 +++++++---- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/contracts/receivers2/aave-v2-receiver/helpers.sol b/contracts/receivers2/aave-v2-receiver/helpers.sol index 1547730..e3b494a 100644 --- a/contracts/receivers2/aave-v2-receiver/helpers.sol +++ b/contracts/receivers2/aave-v2-receiver/helpers.sol @@ -15,6 +15,10 @@ import { abstract contract Helpers is DSMath { using SafeERC20 for IERC20; + // This will be used to have debt/collateral ratio always 20% less than liquidation + // TODO: Is this number correct for it? + uint public safeRatioGap = 200000000000000000; // 20%? 2e17 + // TODO: Add function for flash deposits and withdraw mapping(address => mapping(address => uint)) flashDeposits; // Flash deposits of particular token mapping(address => uint) flashAmts; // token amount for flashloan usage (these token will always stay raw in this contract) @@ -36,6 +40,11 @@ abstract contract Helpers is DSMath { return data; } + function isPositionSafe() internal returns (bool isOk) { + // TODO: Check the final position health + require(isOk, "position-at-risk"); + } + function transferAtokens(address dsa, address[] memory supplyTokens, uint[] memory supplyAmts) internal { AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); for (uint i = 0; i < supplyTokens.length; i++) { diff --git a/contracts/receivers2/aave-v2-receiver/main.sol b/contracts/receivers2/aave-v2-receiver/main.sol index 45f16a4..e24923e 100644 --- a/contracts/receivers2/aave-v2-receiver/main.sol +++ b/contracts/receivers2/aave-v2-receiver/main.sol @@ -11,13 +11,9 @@ import { Helpers } from "./helpers.sol"; contract MigrateResolver is Helpers, Events { using SafeERC20 for IERC20; - // This will be used to have debt/collateral ratio always 20% less than liquidation - // TODO: Is this number correct for it? - uint public safeRatioGap = 200000000000000000; // 20%? 2e17 - // dsa => position mapping(uint => bytes) public positions; - mapping(address => mapping(address => uint)) deposits; + mapping(address => mapping(address => uint)) public deposits; // InstaIndex Address. IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); @@ -82,8 +78,19 @@ contract MigrateResolver is Helpers, Events { _amts[i] = _amt; } + isPositionSafe(); + emit LogWithdraw(msg.sender, tokens, _amts); } + + // TODO: Things to factor + // If there is same token supply and borrow, then close the smaller one + // If there is ideal token then payback or deposit according to the position + // Object is the decrease the ratio and pay as less interest + function settle() external { + + } + } contract AaveV2Migrator is MigrateResolver { @@ -105,7 +112,7 @@ contract AaveV2Migrator is MigrateResolver { // Have to borrow from user's account borrowAndTransferSpells(dsa, borrowTokens, borrowAmts); - // TODO: Final position should be 20% less than liquidation (use 'safeRatioGap', Also should we check this at start?) + isPositionSafe(); } // function getPosition(address owner) public view returns (AaveData memory data) { diff --git a/contracts/senders2/aave-v2-migrator/main.sol b/contracts/senders2/aave-v2-migrator/main.sol index 2b1a432..bb90c58 100644 --- a/contracts/senders2/aave-v2-migrator/main.sol +++ b/contracts/senders2/aave-v2-migrator/main.sol @@ -106,11 +106,14 @@ contract LiquidityResolver is Helpers, Events { emit LogWithdraw(msg.sender, tokens, _amts); } - // TODO: payback if debt else deposit - // TODO: if ratio is safe then transfer excess collateral to L2 migration contract + // TODO: Things to factor + // If there is same token supply and borrow, then close the smaller one + // If there is ideal token then payback or deposit according to the position + // Object is the decrease the ratio and pay as less interest + // If ratio is safe then transfer excess collateral to L2 migration contract // Always, keep 1000 wei WETH ideal for flashloan - function settle(address[] calldata tokens) external { - + function settle() external { + } }