From 5f7449fd27b80bc37ec2dc0beace442564046621 Mon Sep 17 00:00:00 2001
From: Richa-iitr <richa@cs.iitr.ac.in>
Date: Sun, 3 Apr 2022 17:31:16 +0530
Subject: [PATCH] Modified tests, amount approved

---
 .../aave/v3-import-permit/helpers.sol         |   7 +-
 .../connectors/aave/v3-import-permit/main.sol |   1 -
 test/polygon/aave/aaveV3-import-test.ts       | 258 +++++++++++++++++-
 3 files changed, 248 insertions(+), 18 deletions(-)

diff --git a/contracts/polygon/connectors/aave/v3-import-permit/helpers.sol b/contracts/polygon/connectors/aave/v3-import-permit/helpers.sol
index 7350486f..ce1ba6a9 100644
--- a/contracts/polygon/connectors/aave/v3-import-permit/helpers.sol
+++ b/contracts/polygon/connectors/aave/v3-import-permit/helpers.sol
@@ -7,6 +7,7 @@ import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol
 import { AaveInterface, AavePoolProviderInterface, AaveDataProviderInterface } from "./interface.sol";
 import "./events.sol";
 import "./interface.sol";
+import "hardhat/console.sol";
 
 abstract contract Helper is DSMath, Basic {
 	/**
@@ -234,14 +235,16 @@ contract AaveHelpers is Helper {
 		address userAccount,
 		ATokenInterface[] memory aTokenContracts,
 		address[] memory tokens,
-		uint256[] memory amts,
 		uint8[] memory v,
 		bytes32[] memory r,
 		bytes32[] memory s,
 		uint256[] memory expiry
 	) internal {
+		console.log("this");
+		console.log(address(this));
 		for(uint256 i = 0; i < tokens.length; i++) {
-			aTokenContracts[i].permit(userAccount, address(this), amts[i], expiry[i], v[i], r[i], s[i]);
+			ATokenInterface aTokenContracts = ATokenInterface(tokens[i]);
+			aTokenContracts.permit(userAccount, address(this), uint(-1), expiry[i], v[i], r[i], s[i]);
 		}
 	}
 
diff --git a/contracts/polygon/connectors/aave/v3-import-permit/main.sol b/contracts/polygon/connectors/aave/v3-import-permit/main.sol
index bbd3af95..4a449cd9 100644
--- a/contracts/polygon/connectors/aave/v3-import-permit/main.sol
+++ b/contracts/polygon/connectors/aave/v3-import-permit/main.sol
@@ -52,7 +52,6 @@ contract AaveV3ImportPermitResolver is AaveHelpers {
 			userAccount,
 			data.aTokens,
 			data._supplyTokens,
-			data.supplyAmts,
 			permitData.v,
 			permitData.r,
 			permitData.s,
diff --git a/test/polygon/aave/aaveV3-import-test.ts b/test/polygon/aave/aaveV3-import-test.ts
index 50d13ac4..4c762685 100644
--- a/test/polygon/aave/aaveV3-import-test.ts
+++ b/test/polygon/aave/aaveV3-import-test.ts
@@ -25,13 +25,237 @@ const ABI = [
 const aDaiAddress = "0x82E64f49Ed5EC1bC6e43DAD4FC8Af9bb3A2312EE";
 const aaveAddress = "0x794a61358D6845594F94dc1DB02A252b5b4814aD";
 const account = "0xf04adbf75cdfc5ed26eea4bbbb991db002036bdd";
+// const account = "0x44df7e55c643c3cB048465E176A443Ad5670A6fa";
 const DAI = "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063";
 const USDC = "0x2791bca1f2de4661ed88a30c99a7a9449aa84174";
 const mnemonic = "test test test test test test test test test test test junk";
 
-const token = new ethers.Contract(DAI, IERC20__factory.abi);
+const erc20Abi = [
+  
+  {
+    "constant": true,
+    "inputs": [],
+    "name": "name",
+    "outputs": [
+      {
+        "name": "",
+        "type": "string"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "constant": false,
+    "inputs": [
+      {
+        "name": "_spender",
+        "type": "address"
+      },
+      {
+        "name": "_value",
+        "type": "uint256"
+      }
+    ],
+    "name": "approve",
+    "outputs": [
+      {
+        "name": "",
+        "type": "bool"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "nonpayable",
+    "type": "function"
+  },
+  {
+    "constant": true,
+    "inputs": [],
+    "name": "totalSupply",
+    "outputs": [
+      {
+        "name": "",
+        "type": "uint256"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "constant": false,
+    "inputs": [
+      {
+        "name": "_from",
+        "type": "address"
+      },
+      {
+        "name": "_to",
+        "type": "address"
+      },
+      {
+        "name": "_value",
+        "type": "uint256"
+      }
+    ],
+    "name": "transferFrom",
+    "outputs": [
+      {
+        "name": "",
+        "type": "bool"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "nonpayable",
+    "type": "function"
+  },
+  {
+    "constant": true,
+    "inputs": [],
+    "name": "decimals",
+    "outputs": [
+      {
+        "name": "",
+        "type": "uint8"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "constant": true,
+    "inputs": [
+      {
+        "name": "_owner",
+        "type": "address"
+      }
+    ],
+    "name": "balanceOf",
+    "outputs": [
+      {
+        "name": "balance",
+        "type": "uint256"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "constant": true,
+    "inputs": [],
+    "name": "symbol",
+    "outputs": [
+      {
+        "name": "",
+        "type": "string"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "constant": false,
+    "inputs": [
+      {
+        "name": "_to",
+        "type": "address"
+      },
+      {
+        "name": "_value",
+        "type": "uint256"
+      }
+    ],
+    "name": "transfer",
+    "outputs": [
+      {
+        "name": "",
+        "type": "bool"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "nonpayable",
+    "type": "function"
+  },
+  {
+    "constant": true,
+    "inputs": [
+      {
+        "name": "_owner",
+        "type": "address"
+      },
+      {
+        "name": "_spender",
+        "type": "address"
+      }
+    ],
+    "name": "allowance",
+    "outputs": [
+      {
+        "name": "",
+        "type": "uint256"
+      }
+    ],
+    "payable": false,
+    "stateMutability": "view",
+    "type": "function"
+  },
+  {
+    "payable": true,
+    "stateMutability": "payable",
+    "type": "fallback"
+  },
+  {
+    "anonymous": false,
+    "inputs": [
+      {
+        "indexed": true,
+        "name": "owner",
+        "type": "address"
+      },
+      {
+        "indexed": true,
+        "name": "spender",
+        "type": "address"
+      },
+      {
+        "indexed": false,
+        "name": "value",
+        "type": "uint256"
+      }
+    ],
+    "name": "Approval",
+    "type": "event"
+  },
+  {
+    "anonymous": false,
+    "inputs": [
+      {
+        "indexed": true,
+        "name": "from",
+        "type": "address"
+      },
+      {
+        "indexed": true,
+        "name": "to",
+        "type": "address"
+      },
+      {
+        "indexed": false,
+        "name": "value",
+        "type": "uint256"
+      }
+    ],
+    "name": "Transfer",
+    "type": "event"
+  }
+];
+const token = new ethers.Contract(DAI, erc20Abi);
 const aDai = new ethers.Contract(aDaiAddress, ABI);
-const usdcToken = new ethers.Contract(USDC, IERC20__factory.abi);
+const usdcToken = new ethers.Contract(USDC, erc20Abi);
 
 describe("Import Aave", function () {
   const connectorName = "AAVE-V3-IMPORT-PERMIT-X";
@@ -160,7 +384,8 @@ describe("Import Aave", function () {
 
     signer = await ethers.getSigner(account);
 
-    await token.connect(signer).transfer(wallet0.address, ethers.utils.parseEther("100"));
+    console.log(await token.connect(signer).balanceOf(signer.address));
+    await token.connect(signer).transfer(wallet0.address, ethers.utils.parseEther("8"));
 
     instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
     connector = await deployAndEnableConnector({
@@ -175,14 +400,14 @@ describe("Import Aave", function () {
     const aave = new ethers.Contract(aaveAddress, aaveAbi);
 
     // approve DAI to aavePool
-    await token.connect(wallet0).approve(aaveAddress, parseEther("100"));
+    await token.connect(wallet0).approve(aaveAddress, parseEther("8"));
 
     //deposit DAI in aave
-    await aave.connect(wallet0).supply(DAI, parseEther("100"), wallet.address, 3228);
+    await aave.connect(wallet0).supply(DAI, parseEther("8"), wallet.address, 3228);
     console.log("Supplied DAI on aave");
 
     //borrow USDC from aave
-    await aave.connect(wallet0).borrow(USDC, parseUnits("10", 6), 2, 3228, wallet.address);
+    await aave.connect(wallet0).borrow(USDC, parseUnits("5", 6), 2, 3228, wallet.address);
     console.log("Borrowed USDC from aave");
   });
 
@@ -201,21 +426,21 @@ describe("Import Aave", function () {
     it("Deposit ETH into DSA wallet", async function () {
       await wallet0.sendTransaction({
         to: dsaWallet0.address,
-        value: ethers.utils.parseEther("10")
+        value: ethers.utils.parseEther("5")
       });
 
-      expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
+      expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("5"));
     });
   });
 
   describe("check user AAVE position", async () => {
     it("Should check position of user", async () => {
       expect(await aDai.connect(wallet0).balanceOf(wallet.address)).to.be.gte(
-        new BigNumber(100).multipliedBy(1e18).toString()
+        new BigNumber(8).multipliedBy(1e18).toString()
       );
 
       expect(await usdcToken.connect(wallet0).balanceOf(wallet.address)).to.be.gte(
-        new BigNumber(10).multipliedBy(1e6).toString()
+        new BigNumber(5).multipliedBy(1e6).toString()
       );
     });
   });
@@ -224,8 +449,8 @@ describe("Import Aave", function () {
     it("Should migrate Aave position", async () => {
       const DOMAIN_SEPARATOR = await aDai.connect(wallet0).DOMAIN_SEPARATOR();
       const PERMIT_TYPEHASH = "0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9";
-
-      let nonce = await aDai.connect(wallet0).nonces(wallet.address);
+    
+      let nonce = (await aDai.connect(wallet0).nonces(wallet.address)).toNumber();
       const amount = await aDai.connect(wallet0).balanceOf(wallet.address);
       const expiry = Date.now() + 20 * 60;
 
@@ -246,7 +471,10 @@ describe("Import Aave", function () {
         )
       );
       const { v, r, s } = ecsign(Buffer.from(digest.slice(2), "hex"), Buffer.from(wallet.privateKey.slice(2), "hex"));
-
+          // console.log(v);
+          // console.log(pubToAddress(ecrecover(Buffer.from(digest.slice(2),'hex'),v,r,s)));
+          // console.log(dsaWallet0.address);
+          // console.log(wallet.address);
       const amount0 = new BigNumber(await usdcToken.connect(wallet0).balanceOf(wallet.address));
       const amountB = new BigNumber(amount0.toString()).multipliedBy(5).dividedBy(1e4);
       const amountWithFee = amount0.plus(amountB);
@@ -255,7 +483,7 @@ describe("Import Aave", function () {
         {
           connector: "AAVE-V3-IMPORT-PERMIT-X",
           method: "importAave",
-          args: [wallet.address, [[DAI], [USDC], false, [amount]], [[v], [r], [s], [expiry]]]
+          args: [wallet.address, [[DAI], [USDC], false, [amountB]], [[v], [r], [s], [expiry]]]
         },
         {
           connector: "INSTAPOOL-C",
@@ -277,7 +505,7 @@ describe("Import Aave", function () {
 
     it("Should check DSA AAVE position", async () => {
       expect(await aDai.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte(
-        new BigNumber(100).multipliedBy(1e18).toString()
+        new BigNumber(8).multipliedBy(1e18).toString()
       );
     });
   });