diff --git a/contracts/receivers/aave-v2-receiver/helpers.sol b/contracts/receivers/aave-v2-receiver/helpers.sol index de463aa..2ed3c27 100644 --- a/contracts/receivers/aave-v2-receiver/helpers.sol +++ b/contracts/receivers/aave-v2-receiver/helpers.sol @@ -23,9 +23,7 @@ abstract contract Helpers is Stores, DSMath, Variables { address atoken; uint atokenBal; uint supplyAmt; - uint flashAmt; uint tokenLiq; - bool isFlash; } struct SpellHelperData { @@ -33,8 +31,6 @@ abstract contract Helpers is Stores, DSMath, Variables { address atoken; uint tokenLiq; uint borrowAmt; - uint flashAmt; - bool isFlash; } function remapTokens(AaveData memory data) internal view returns (AaveData memory) { @@ -76,16 +72,6 @@ abstract contract Helpers is Stores, DSMath, Variables { if (data.atokenBal < data.supplyAmt) { uint _reqAmt = data.supplyAmt - data.atokenBal; - if (data.tokenLiq < _reqAmt) { - data.flashAmt = flashAmts[data.token]; - if (data.flashAmt > 0) { - _tokenContract.approve(address(aave), data.flashAmt); - aave.deposit(data.token, data.flashAmt, address(this), 3288); // TODO: what is our ID on Polygon? - data.tokenLiq += data.flashAmt; - data.isFlash = true; - } - } - uint num = _reqAmt/data.tokenLiq + 1; // TODO: Is this right uint splitAmt = _reqAmt/num; // TODO: Check decimal uint finalSplit = _reqAmt - (splitAmt * (num - 1)); // TODO: to resolve upper decimal error @@ -103,10 +89,6 @@ abstract contract Helpers is Stores, DSMath, Variables { } } - if (data.isFlash) { - aave.withdraw(data.token, data.flashAmt, address(this)); - } - _atokenContract.safeTransfer(dsa, data.supplyAmt); } } @@ -119,12 +101,6 @@ abstract contract Helpers is Stores, DSMath, Variables { (data.tokenLiq,,,,,,,,,) = aaveData.getReserveData(data.token); data.borrowAmt = borrowAmts[i]; - if (data.tokenLiq < data.borrowAmt) { - data.flashAmt = flashAmts[data.token]; - aave.deposit(data.token, data.flashAmt, address(this), 3288); // TODO: what is our ID on Polygon? - data.isFlash = true; - data.tokenLiq += data.flashAmt; - } // TODO: Check number of loops needed. Borrow and supply on user's account. uint num = data.borrowAmt/data.tokenLiq + 1; // TODO: Is this right uint splitAmt = data.borrowAmt/num; // TODO: Check decimal @@ -156,12 +132,8 @@ abstract contract Helpers is Stores, DSMath, Variables { } } - if (data.isFlash) { - aave.withdraw(data.token, data.flashAmt, address(this)); - } - - targets[spellsAmt - 1] = "BASIC-A"; // TODO: right spell? - castData[spellsAmt - 1] = abi.encodeWithSignature("withdraw(address,uint256,address,uint256,uint256)", data.atoken, data.borrowAmt, address(this), 0, 0); // encode the data of atoken withdrawal + targets[spellsAmt] = "BASIC-A"; // TODO: right spell? + castData[spellsAmt] = abi.encodeWithSignature("withdraw(address,uint256,address,uint256,uint256)", data.atoken, data.borrowAmt, address(this), 0, 0); // encode the data of atoken withdrawal AccountInterface(dsa).castMigrate(targets, castData, address(this)); } diff --git a/contracts/receivers/aave-v2-receiver/main.sol b/contracts/receivers/aave-v2-receiver/main.sol index 2146d96..1c9ef06 100644 --- a/contracts/receivers/aave-v2-receiver/main.sol +++ b/contracts/receivers/aave-v2-receiver/main.sol @@ -19,6 +19,10 @@ contract MigrateResolver is Helpers, Events { function addTokenSupport(address[] memory _tokens) public { require(msg.sender == instaIndex.master(), "not-master"); + for (uint i = 0; i < supportedTokens.length; i++) { + delete isSupportedToken[supportedTokens[i]]; + } + delete supportedTokens; for (uint i = 0; i < _tokens.length; i++) { require(!isSupportedToken[_tokens[i]], "already-added"); isSupportedToken[_tokens[i]] = true; @@ -66,11 +70,11 @@ contract MigrateResolver is Helpers, Events { ) = aaveData.getUserReserveData(_token, address(this)); if (supplyBal != 0 && borrowBal != 0) { if (supplyBal > borrowBal) { - aave.withdraw(_token, (borrowBal + flashAmts[_token]), address(this)); // TODO: fail because of not enough withdrawing capacity? + 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 + flashAmts[_token]), address(this)); // TODO: fail because of not enough withdrawing capacity? + 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)); } @@ -108,6 +112,8 @@ contract AaveV2Migrator is MigrateResolver { } function onStateReceive(uint256 stateId, bytes calldata receivedData) external { + // Add some more require statements. Any kind of hashing for better privacy? + require(msg.sender == maticReceiver, "not-receiver-address"); require(stateId > lastStateId, "wrong-data"); lastStateId = stateId; diff --git a/contracts/receivers/aave-v2-receiver/variables.sol b/contracts/receivers/aave-v2-receiver/variables.sol index f624035..3248973 100644 --- a/contracts/receivers/aave-v2-receiver/variables.sol +++ b/contracts/receivers/aave-v2-receiver/variables.sol @@ -13,10 +13,6 @@ contract Variables { // TODO: Is this number correct for it? uint public safeRatioGap = 800000000000000000; // 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) - // TODO: Replace this TokenMappingInterface tokenMapping = TokenMappingInterface(address(0xa31442F2607947a88807b2bcD5D4951eEdd4A885)); // TODO: FAKE ADDR, CHANGE THIS @@ -36,6 +32,9 @@ contract Variables { // TODO: Set by construtor? mapping(address => bool) public isSupportedToken; - address[] public supportedTokens; + address[] public supportedTokens; // don't add maticAddr. Only add wmaticAddr? + + // Address which will receive L1 data and post it on L2 + address public maticReceiver = address(0); // TODO: Change address } \ No newline at end of file diff --git a/contracts/senders/aave-v2-migrator/main.sol b/contracts/senders/aave-v2-migrator/main.sol index 74bac2b..c463ee3 100644 --- a/contracts/senders/aave-v2-migrator/main.sol +++ b/contracts/senders/aave-v2-migrator/main.sol @@ -20,6 +20,10 @@ contract LiquidityResolver is Helpers, Events { function addTokenSupport(address[] memory _tokens) public { require(msg.sender == instaIndex.master(), "not-master"); + for (uint i = 0; i < supportedTokens.length; i++) { + delete isSupportedToken[supportedTokens[i]]; + } + delete supportedTokens; for (uint i = 0; i < _tokens.length; i++) { require(!isSupportedToken[_tokens[i]], "already-added"); isSupportedToken[_tokens[i]] = true; @@ -53,8 +57,7 @@ contract LiquidityResolver is Helpers, Events { AaveInterface aave = AaveInterface(aaveProvider.getLendingPool()); for (uint i = 0; i < supportedTokens.length; i++) { address _token = supportedTokens[i]; - if (_token == ethAddr) { - _token = wethAddr; + if (_token == wethAddr) { if (address(this).balance > 0) { TokenInterface(wethAddr).deposit{value: address(this).balance}(); } diff --git a/contracts/senders/aave-v2-migrator/variables.sol b/contracts/senders/aave-v2-migrator/variables.sol index 64bb15d..121c7e1 100644 --- a/contracts/senders/aave-v2-migrator/variables.sol +++ b/contracts/senders/aave-v2-migrator/variables.sol @@ -49,7 +49,7 @@ contract Variables { uint public fee = 998000000000000000; // 0.2% (99.8%) on collateral? TODO: Is this right? // TODO: Set by construtor? mapping(address => bool) public isSupportedToken; - address[] public supportedTokens; + address[] public supportedTokens; // don't add ethAddr. Only add wethAddr /** * @dev Aave Provider