From 8a6c936edbc5b0f2ee45baa3c08e9fdafdff2be3 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Thu, 2 Jun 2022 00:50:11 +0530 Subject: [PATCH] code refactor --- .../connectors/aave/v3-import/helpers.sol | 101 ++++++++++++++---- .../connectors/aave/v3-import/main.sol | 6 +- test/polygon/aave/v3-import-test.ts | 16 ++- 3 files changed, 94 insertions(+), 29 deletions(-) diff --git a/contracts/polygon/connectors/aave/v3-import/helpers.sol b/contracts/polygon/connectors/aave/v3-import/helpers.sol index 40831baf..902c6364 100644 --- a/contracts/polygon/connectors/aave/v3-import/helpers.sol +++ b/contracts/polygon/connectors/aave/v3-import/helpers.sol @@ -256,32 +256,44 @@ contract AaveHelpers is Helper { } } } else { - string[] memory _targets = new string[](_length); - bytes[] memory _data = new bytes[](_length); + uint256 _len = 0; + uint256 _cntr = 0; + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _len++; + } + } + string[] memory _targets = new string[](_len); + bytes[] memory _data = new bytes[](_len); + address[] memory _tokens = new address[](_len); bytes4 basicWithdraw = bytes4( keccak256("withdraw(address,uint256,address,uint256,uint256)") ); for (uint256 i = 0; i < _length; i++) { - _targets[i] = "BASIC-A"; if (amts[i] > 0) { uint256 _amt = amts[i]; address _token = tokens[i]; - _data[i] = abi.encodeWithSelector( + _targets[_cntr] = "BASIC-A"; + _data[_cntr] = abi.encodeWithSelector( basicWithdraw, - _token, + tokens[i], _amt, address(this), 0, 0 ); - if (!getIsColl(_token, address(this))) { - aave.setUserUseReserveAsCollateral(_token, true); - } + _tokens[_cntr] = _token; + _cntr++; } } AccountInterface(userAccount).cast(_targets, _data, address(0)); + for (uint256 i = 0; i < _len; i++) { + if (!getIsColl(_tokens[i], address(this))) { + aave.setUserUseReserveAsCollateral(_tokens[i], true); + } + } } } @@ -294,20 +306,67 @@ contract AaveHelpers is Helper { bool[] memory colEnable, address userAccount ) internal { - for (uint256 i = 0; i < _length; i++) { - if (amts[i] > 0) { - uint256 _amt = amts[i]; - require( - atokenContracts[i].transferFrom( - userAccount, - address(this), - _amt - ), - "allowance?" - ); + if (instaList.accountID(userAccount) == 0) { + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + uint256 _amt = amts[i]; + require( + atokenContracts[i].transferFrom( + userAccount, + address(this), + _amt + ), + "allowance?" + ); - if (!getIsColl(tokens[i], address(this))) { - aave.setUserUseReserveAsCollateral(tokens[i], colEnable[i]); + if (!getIsColl(tokens[i], address(this))) { + aave.setUserUseReserveAsCollateral( + tokens[i], + colEnable[i] + ); + } + } + } + } else { + uint256 _len = 0; + uint256 _cntr = 0; + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + _len++; + } + } + string[] memory _targets = new string[](_len); + bytes[] memory _data = new bytes[](_len); + address[] memory _tokens = new address[](_len); + bytes4 basicWithdraw = bytes4( + keccak256("withdraw(address,uint256,address,uint256,uint256)") + ); + + for (uint256 i = 0; i < _length; i++) { + if (amts[i] > 0) { + uint256 _amt = amts[i]; + address _token = tokens[i]; + _targets[_cntr] = "BASIC-A"; + _data[_cntr] = abi.encodeWithSelector( + basicWithdraw, + tokens[i], + _amt, + address(this), + 0, + 0 + ); + _tokens[_cntr] = _token; + _cntr++; + } + } + + AccountInterface(userAccount).cast(_targets, _data, address(0)); + for (uint256 i = 0; i < _len; i++) { + if (!getIsColl(_tokens[i], address(this))) { + aave.setUserUseReserveAsCollateral( + _tokens[i], + colEnable[i] + ); } } } diff --git a/contracts/polygon/connectors/aave/v3-import/main.sol b/contracts/polygon/connectors/aave/v3-import/main.sol index 37c46f2a..9229b8e3 100644 --- a/contracts/polygon/connectors/aave/v3-import/main.sol +++ b/contracts/polygon/connectors/aave/v3-import/main.sol @@ -16,11 +16,7 @@ contract AaveV3ImportResolver is AaveHelpers { internal returns (string memory _eventName, bytes memory _eventParam) { - if ( - ListInterface(0x839c2D3aDe63DF5b0b8F3E57D5e145057Ab41556).accountID( - userAccount - ) == 0 - ) { + if (instaList.accountID(userAccount) == 0) { require( AccountInterface(address(this)).isAuth(userAccount), "user-account-not-auth" diff --git a/test/polygon/aave/v3-import-test.ts b/test/polygon/aave/v3-import-test.ts index a5751275..b5201906 100644 --- a/test/polygon/aave/v3-import-test.ts +++ b/test/polygon/aave/v3-import-test.ts @@ -221,6 +221,7 @@ describe("Import Aave v3 Position", function () { let dsaWallet1: any; let dsaWallet2: any; let walletB: any; + let walletBsigner: any; let masterSigner: Signer; let instaConnectorsV2: Contract; let connector: any; @@ -325,6 +326,13 @@ describe("Import Aave v3 Position", function () { }); it("Should create DSA Aave v3 position of DAI(collateral) and USDC(debt)", async () => { + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [walletB.address] + }); + + walletBsigner = await ethers.getSigner(walletB.address); + await token.connect(signer).transfer(dsaWallet2.address, ethers.utils.parseEther("10")); console.log(await token.connect(signer).balanceOf(dsaWallet2.address)); console.log(dsaWallet1.address); @@ -343,7 +351,7 @@ describe("Import Aave v3 Position", function () { args: [USDC, parseUnits("3", 6), 2, 0, 0] } ]; - const tx = await dsaWallet2.connect(walletB).cast(...encodeSpells(spells), walletB.address); + const tx = await dsaWallet2.connect(walletBsigner).cast(...encodeSpells(spells), walletB.address); const receipt = await tx.wait(); }); @@ -351,6 +359,7 @@ describe("Import Aave v3 Position", function () { expect(await aDai.connect(wallet0).balanceOf(dsaWallet2.address)).to.be.gte( new BigNumber(10).multipliedBy(1e18).toString() ); + console.log((await aDai.connect(wallet0).balanceOf(dsaWallet2.address)).toString()); expect(await usdcToken.connect(wallet0).balanceOf(dsaWallet2.address)).to.be.gte( new BigNumber(3).multipliedBy(1e6).toString() @@ -395,7 +404,7 @@ describe("Import Aave v3 Position", function () { it("Should check DSA-1 AAVE position", async () => { expect(await aDai.connect(wallet0).balanceOf(dsaWallet0.address)).to.be.gte( - new BigNumber(8).multipliedBy(1e18).toString() + new BigNumber(10).multipliedBy(1e18).toString() ); }); @@ -413,7 +422,7 @@ describe("Import Aave v3 Position", function () { { connector: "AAVE-V3-IMPORT-X", method: "importAave", - args: [dsaWallet2.address, [[DAI], [USDC], false, [amountB.toFixed(0)]]] + args: [dsaWallet2.address, [[DAI], [USDC], false, [amountB.toFixed(0)]]] //dsaWallet2 --> DSA_A DSA with aave position }, { connector: "INSTAPOOL-C", @@ -429,6 +438,7 @@ describe("Import Aave v3 Position", function () { args: [USDC, amount0.toString(), 5, encodeFlashcastData(flashSpells), "0x"] } ]; + //merge to dsaWallet1 const tx = await dsaWallet1.connect(wallet0).cast(...encodeSpells(spells), wallet.address); const receipt = await tx.wait(); });