From 58a01e00aa2231463b53614d941e3975b6f80e0d Mon Sep 17 00:00:00 2001
From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com>
Date: Fri, 16 Apr 2021 02:36:12 +0530
Subject: [PATCH 1/3] supported token update

---
 contracts/receivers/aave-v2-receiver/main.sol      | 4 ++++
 contracts/receivers/aave-v2-receiver/variables.sol | 2 +-
 contracts/senders/aave-v2-migrator/main.sol        | 7 +++++--
 contracts/senders/aave-v2-migrator/variables.sol   | 2 +-
 4 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/contracts/receivers/aave-v2-receiver/main.sol b/contracts/receivers/aave-v2-receiver/main.sol
index 94b7cc5..58a659b 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;
diff --git a/contracts/receivers/aave-v2-receiver/variables.sol b/contracts/receivers/aave-v2-receiver/variables.sol
index 043624b..7741a24 100644
--- a/contracts/receivers/aave-v2-receiver/variables.sol
+++ b/contracts/receivers/aave-v2-receiver/variables.sol
@@ -36,6 +36,6 @@ contract Variables {
 
     // TODO: Set by construtor?
     mapping(address => bool) public isSupportedToken;
-    address[] public supportedTokens;
+    address[] public supportedTokens; // don't add ethAddr. Only add wethAddr
 
 }
\ 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 144ec42..f6668b1 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 9ce8e72..df0f59c 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

From e31032a3d3a2428f74f79543872f79ceb236dc79 Mon Sep 17 00:00:00 2001
From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com>
Date: Fri, 16 Apr 2021 03:06:40 +0530
Subject: [PATCH 2/3] matic receiver condition

---
 contracts/receivers/aave-v2-receiver/main.sol      | 2 ++
 contracts/receivers/aave-v2-receiver/variables.sol | 5 ++++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/contracts/receivers/aave-v2-receiver/main.sol b/contracts/receivers/aave-v2-receiver/main.sol
index 58a659b..c549860 100644
--- a/contracts/receivers/aave-v2-receiver/main.sol
+++ b/contracts/receivers/aave-v2-receiver/main.sol
@@ -112,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 7741a24..bbe89a7 100644
--- a/contracts/receivers/aave-v2-receiver/variables.sol
+++ b/contracts/receivers/aave-v2-receiver/variables.sol
@@ -36,6 +36,9 @@ contract Variables {
 
     // TODO: Set by construtor?
     mapping(address => bool) public isSupportedToken;
-    address[] public supportedTokens; // don't add ethAddr. Only add wethAddr
+    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

From 777a98ef56b16503cccbc821dd682c7433704b7e Mon Sep 17 00:00:00 2001
From: Samyak Jain <34437877+KaymasJain@users.noreply.github.com>
Date: Fri, 16 Apr 2021 03:19:48 +0530
Subject: [PATCH 3/3] removed flash loan on L2

---
 .../receivers/aave-v2-receiver/helpers.sol    | 28 -------------------
 contracts/receivers/aave-v2-receiver/main.sol |  4 +--
 .../receivers/aave-v2-receiver/variables.sol  |  4 ---
 3 files changed, 2 insertions(+), 34 deletions(-)

diff --git a/contracts/receivers/aave-v2-receiver/helpers.sol b/contracts/receivers/aave-v2-receiver/helpers.sol
index 891ee99..2de9e93 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) {
@@ -71,16 +67,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
@@ -97,10 +83,6 @@ abstract contract Helpers is Stores, DSMath, Variables {
                 }
             }
 
-            if (data.isFlash) {
-                aave.withdraw(data.token, data.flashAmt, address(this));
-            }
-
             _atokenContract.safeTransfer(dsa, data.supplyAmt);
         }
     }
@@ -113,12 +95,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
@@ -142,10 +118,6 @@ abstract contract Helpers is Stores, DSMath, Variables {
                 }
             }
 
-            if (data.isFlash) {
-                aave.withdraw(data.token, data.flashAmt, address(this));
-            }
-
             targets[spellsAmt] = "BASIC-A"; // TODO: right spell?
             castData[spellsAmt] = abi.encode("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 c549860..c1f5dc3 100644
--- a/contracts/receivers/aave-v2-receiver/main.sol
+++ b/contracts/receivers/aave-v2-receiver/main.sol
@@ -70,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));
                 }
diff --git a/contracts/receivers/aave-v2-receiver/variables.sol b/contracts/receivers/aave-v2-receiver/variables.sol
index bbe89a7..c61145e 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(2));