From e6bcc576e8a2b392c4dda5b8a5afcc91891ef1e0 Mon Sep 17 00:00:00 2001 From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com> Date: Fri, 9 Apr 2021 05:14:37 +0530 Subject: [PATCH] added receiver settle and fixes --- contracts/receivers/aave-v2-receiver/main.sol | 36 ++++++++++++- .../receivers/aave-v2-receiver/variables.sol | 8 +++ contracts/senders/aave-v2-migrator/main.sol | 53 +++++++++---------- .../senders/aave-v2-migrator/variables.sol | 2 +- 4 files changed, 70 insertions(+), 29 deletions(-) diff --git a/contracts/receivers/aave-v2-receiver/main.sol b/contracts/receivers/aave-v2-receiver/main.sol index 1ed3bd9..ae64a2e 100644 --- a/contracts/receivers/aave-v2-receiver/main.sol +++ b/contracts/receivers/aave-v2-receiver/main.sol @@ -113,7 +113,41 @@ contract MigrateResolver is Helpers, Events { // Keep flashAmt tokens as ideal // Object is the decrease the ratio and pay as less interest function settle() external { - + AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); + for (uint i = 0; i < supportedTokens.length; i++) { + for (uint i = 0; i < supportedTokens.length; i++) { + address _token = supportedTokens[i]; + if (_token == maticAddr) { + _token = wmaticAddr; + if (address(this).balance > 0) { + TokenInterface(wmaticAddr).deposit{value: address(this).balance}(); + } + } + IERC20 _tokenContract = IERC20(_token); + uint _tokenBal = _tokenContract.balanceOf(address(this)); + if (_tokenBal > 0) { + _tokenContract.approve(address(this), _tokenBal); + aave.deposit(_token, _tokenBal, address(this), 3288); + } + ( + uint supplyBal,, + uint borrowBal, + ,,,,, + ) = aaveData.getUserReserveData(_token, address(this)); + if (supplyBal != 0 && borrowBal != 0) { + uint _withdrawAmt; + if (supplyBal > borrowBal) { + aave.withdraw(_token, borrowBal, address(this)); // TODO: fail because of not enough withdrawing capacity? + IERC20(_token).approve(address(aave), borrowBal); + aave.repay(_token, borrowBal, 2, address(this)); + } else { + aave.withdraw(_token, supplyBal, address(this)); // TODO: fail because of not enough withdrawing capacity? + IERC20(_token).approve(address(aave), supplyBal); + aave.repay(_token, supplyBal, 2, address(this)); + } + } + } + } } } diff --git a/contracts/receivers/aave-v2-receiver/variables.sol b/contracts/receivers/aave-v2-receiver/variables.sol index aa11486..3f7d382 100644 --- a/contracts/receivers/aave-v2-receiver/variables.sol +++ b/contracts/receivers/aave-v2-receiver/variables.sol @@ -3,6 +3,7 @@ pragma solidity ^0.7.0; import { TokenMappingInterface, AaveLendingPoolProviderInterface, + AaveDataProviderInterface, IndexInterface } from "./interfaces.sol"; @@ -21,6 +22,13 @@ contract Variables { AaveLendingPoolProviderInterface constant internal aaveProvider = AaveLendingPoolProviderInterface(0xd05e3E715d945B59290df0ae8eF85c1BdB684744); + /** + * @dev Aave Data Provider + */ + // TODO: add L2 Data provider address + AaveDataProviderInterface constant internal aaveData = AaveDataProviderInterface(address(0)); + + // dsa => position mapping(uint => bytes) public positions; mapping(address => mapping(address => uint)) public deposits; diff --git a/contracts/senders/aave-v2-migrator/main.sol b/contracts/senders/aave-v2-migrator/main.sol index 170fd7c..0a664dd 100644 --- a/contracts/senders/aave-v2-migrator/main.sol +++ b/contracts/senders/aave-v2-migrator/main.sol @@ -128,14 +128,14 @@ contract LiquidityResolver is Helpers, Events { * @param _tokens - array of tokens to transfer to L2 receiver's contract * @param _amts - array of token amounts to transfer to L2 receiver's contract */ - function settle(address[] calldata _tokens, uint[] _amts) external { + function settle(address[] calldata _tokens, uint[] calldata _amts) external { AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); for (uint i = 0; i < supportedTokens.length; i++) { address _token = supportedTokens[i]; if (_token == ethAddr) { _token = wethAddr; if (address(this).balance > 0) { - TokenInterface(wethAddr).deposit(address(this).balance); + TokenInterface(wethAddr).deposit{value: address(this).balance}(); } } IERC20 _tokenContract = IERC20(_token); @@ -189,18 +189,18 @@ contract MigrateResolver is LiquidityResolver { (,,,,,uint healthFactor) = aave.getUserAccountData(sourceDsa); require(healthFactor > 1e18, "position-not-safe"); - (uint[] stableBorrows, uint[] variableBorrows, uint[] totalBorrows) = _PaybackCalculate(aave, _data, sourceDsa); + (uint[] memory stableBorrows, uint[] memory variableBorrows, uint[] memory totalBorrows) = _PaybackCalculate(aave, _data, sourceDsa); _PaybackStable(_data.borrowTokens.length, aave, _data.borrowTokens, stableBorrows, sourceDsa); _PaybackVariable(_data.borrowTokens.length, aave, _data.borrowTokens, variableBorrows, sourceDsa); - (uint[] totalSupplies) = _getAtokens(sourceDsa, aave, _data.supplyTokens, _data.supplyAmts, fee); + (uint[] memory totalSupplies) = _getAtokens(sourceDsa, aave, _data.supplyTokens, _data.supplyAmts); // Aave on Polygon doesn't have stable borrowing so we'll borrow all the debt in variable AaveData memory data; data.borrowTokens = _data.borrowTokens; - data.stableBorrowAmts = _data.stableBorrowAmts; + data.borrowAmts = _data.stableBorrowAmts; data.supplyAmts = totalSupplies; data.supplyTokens = _data.supplyTokens; data.targetDsa = _data.targetDsa; @@ -212,17 +212,16 @@ contract MigrateResolver is LiquidityResolver { isPositionSafe(); - bytes memory positionData = data; // TODO: @everyone Can we do anything else to make the data more secure? (It's secure already) - stateSender.syncState(polygonReceiver, positionData); + stateSender.syncState(polygonReceiver, abi.encode(data)); emit LogAaveV2Migrate( sourceDsa, data.targetDsa, data.supplyTokens, data.borrowTokens, - data.supplyAmts, - data.variableBorrowAmts, - data.stableBorrowAmts + totalSupplies, + variableBorrows, + stableBorrows ); } @@ -231,7 +230,7 @@ contract MigrateResolver is LiquidityResolver { } function migrateWithFlash(AaveDataRaw calldata _data, uint ethAmt) external { - bytes data = abi.encode(_data, msg.sender, ethAmt); + bytes memory data = abi.encode(_data, msg.sender, ethAmt); // TODO: @everyone integrate dydx flashloan and borrow "ethAmt" and transfer ETH to this address // Should we create a new contract for flashloan or interact directly from this contract? Mainly to resolve 2 WEI bug of dydx flashloan @@ -239,21 +238,21 @@ contract MigrateResolver is LiquidityResolver { } // TODO: @everyone - function callFunction( - address sender, - Account.Info memory account, - bytes memory data - ) public override { - require(sender == address(this), "wrong-sender"); - // require(msg.sender == dydxFlash, "wrong-sender"); // TODO: msg.sender should be flashloan contract - (address l2DSA, AaveDataRaw memory _data, address sourceDsa, uint ethAmt) = abi.decode( - data, - (address, AaveDataRaw, address, uint) - ); - // TODO: deposit WETH "ethAmt" in Aave - _migrate(l2DSA, _data, sourceDsa); - // TODO: withdraw WETH "ethAmt" from Aave - // TODO: approve WETH "ethAmt + 2" to dydx - } + // function callFunction( + // address sender, + // Account.Info memory account, + // bytes memory data + // ) public override { + // require(sender == address(this), "wrong-sender"); + // // require(msg.sender == dydxFlash, "wrong-sender"); // TODO: msg.sender should be flashloan contract + // (address l2DSA, AaveDataRaw memory _data, address sourceDsa, uint ethAmt) = abi.decode( + // data, + // (address, AaveDataRaw, address, uint) + // ); + // // TODO: deposit WETH "ethAmt" in Aave + // _migrate(l2DSA, _data, sourceDsa); + // // TODO: withdraw WETH "ethAmt" from Aave + // // TODO: approve WETH "ethAmt + 2" to dydx + // } } \ No newline at end of file diff --git a/contracts/senders/aave-v2-migrator/variables.sol b/contracts/senders/aave-v2-migrator/variables.sol index 0780286..d9d5fb6 100644 --- a/contracts/senders/aave-v2-migrator/variables.sol +++ b/contracts/senders/aave-v2-migrator/variables.sol @@ -31,7 +31,7 @@ contract Variables { */ uint16 constant internal referralCode = 3228; - address constant internal polygonReceiver = address(0); // Replace this + address constant internal polygonReceiver = address(0); // TODO: Replace this // This will be used to have debt/collateral ratio always 20% less than liquidation // TODO: Is this number correct for it?