From 47ea4ffdff1d8a6cc143c01060c5b29d1ff31e97 Mon Sep 17 00:00:00 2001 From: Daksh Miglani Date: Fri, 4 Jun 2021 16:17:16 +0530 Subject: [PATCH 1/2] =?UTF-8?q?perf:=20=E2=9A=A1=EF=B8=8F=20add=20utils=20?= =?UTF-8?q?for=20adding=20liq,=20impersonating=20and=20master=20sign?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/addLiquidity.js | 78 ++++++++++++++++++++++++++++++++++++++ scripts/getMasterSigner.js | 34 +++++++++-------- scripts/impersonate.js | 15 ++++++++ 3 files changed, 111 insertions(+), 16 deletions(-) create mode 100644 scripts/addLiquidity.js create mode 100644 scripts/impersonate.js diff --git a/scripts/addLiquidity.js b/scripts/addLiquidity.js new file mode 100644 index 00000000..0fb51156 --- /dev/null +++ b/scripts/addLiquidity.js @@ -0,0 +1,78 @@ +const { ethers } = require("hardhat"); +const impersonateAccounts = require("./impersonate"); + +const mineTx = async (tx) => { + await (await tx).wait(); +}; + +const tokenMapping = { + usdc: { + impersonateSigner: "0xfcb19e6a322b27c06842a71e8c725399f049ae3a", + address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", + abi: [ + "function mint(address _to, uint256 _amount) external returns (bool);", + ], + process: async function(owner, to, amt) { + const contract = new ethers.Contract(this.address, this.abi, owner); + + await mineTx(contract.mint(to, amt)); + }, + }, + dai: { + impersonateSigner: "0x47ac0fb4f2d84898e4d9e7b4dab3c24507a6d503", + abi: ["function transfer(address to, uint value)"], + address: "0x6b175474e89094c44da98b954eedeac495271d0f", + process: async function(owner, to, amt) { + const contract = new ethers.Contract(this.address, this.abi, owner); + await mineTx(contract.transfer(to, amt)); + }, + }, + usdt: { + impersonateSigner: "0xc6cde7c39eb2f0f0095f41570af89efc2c1ea828", + address: "0xdac17f958d2ee523a2206206994597c13d831ec7", + abi: [ + "function issue(uint amount)", + "function transfer(address to, uint value)", + ], + process: async function(owner, address, amt) { + const contract = new ethers.Contract(this.address, this.abi, owner); + + await mineTx(contract.issue(amt)); + await mineTx(contract.transfer(address, amt)); + }, + }, + wbtc: { + impersonateSigner: "0xCA06411bd7a7296d7dbdd0050DFc846E95fEBEB7", + address: "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599", + abi: ["function mint(address _to, uint256 _amount) public returns (bool)"], + process: async function(owner, address, amt) { + const contract = new ethers.Contract(this.address, this.abi, owner); + await mineTx(contract.mint(address, amt)); + }, + }, +}; + +module.exports = async (tokenName, address, amt) => { + const [signer] = await ethers.getSigners(); + tokenName = tokenName.toLowerCase(); + if (!tokenMapping[tokenName]) { + throw new Error( + "Add liquidity doesn't support the following token: ", + tokenName + ); + } + + const token = tokenMapping[tokenName]; + + const [impersonatedSigner] = await impersonateAccounts([ + token.impersonateSigner, + ]); + + // send 1 eth to cover any tx costs. + await signer.sendTransaction({ + to: impersonatedSigner.address, + value: ethers.utils.parseEther("1"), + }); + + await token.process(impersonatedSigner, address, amt); +}; diff --git a/scripts/getMasterSigner.js b/scripts/getMasterSigner.js index 5388f041..d0c19d6a 100644 --- a/scripts/getMasterSigner.js +++ b/scripts/getMasterSigner.js @@ -1,23 +1,25 @@ const hre = require("hardhat"); -const { ethers, waffle } = hre; +const { ethers } = hre; const addresses = require("./constant/addresses"); const abis = require("./constant/abis"); -const { provider, deployContract } = waffle +module.exports = async function() { + const [_, __, ___, wallet3] = await ethers.getSigners(); + const instaIndex = new ethers.Contract( + addresses.core.instaIndex, + abis.core.instaIndex, + wallet3 + ); -module.exports = async function () { - const instaIndex = await ethers.getContractAt(abis.core.instaIndex, addresses.core.instaIndex) + const masterAddress = await instaIndex.master(); // TODO: make it constant? + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [masterAddress], + }); + await wallet3.sendTransaction({ + to: masterAddress, + value: ethers.utils.parseEther("10"), + }); - const masterAddress = await instaIndex.master(); // TODO: make it constant? - await hre.network.provider.request({ - method: "hardhat_impersonateAccount", - params: [ masterAddress] - }) - const [wallet0, wallet1, wallet2, wallet3] = await ethers.getSigners() - await wallet3.sendTransaction({ - to: masterAddress, - value: ethers.utils.parseEther("10") - }); - - return await ethers.getSigner(masterAddress); + return await ethers.getSigner(masterAddress); }; diff --git a/scripts/impersonate.js b/scripts/impersonate.js new file mode 100644 index 00000000..fb485cee --- /dev/null +++ b/scripts/impersonate.js @@ -0,0 +1,15 @@ +const { ethers, network } = require("hardhat"); + +module.exports = async (accounts) => { + const signers = []; + for (const account of accounts) { + await network.provider.request({ + method: "hardhat_impersonateAccount", + params: [account], + }); + + signers.push(await ethers.getSigner(account)); + } + + return signers; +}; From 678fd641fb59341cdb4c3568aecf6b77262c4760 Mon Sep 17 00:00:00 2001 From: Daksh Miglani Date: Fri, 4 Jun 2021 16:17:41 +0530 Subject: [PATCH 2/2] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20aave=20v1=20an?= =?UTF-8?q?d=20v2=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hardhat.config.js | 3 + test/aave/v1.test.js | 151 +++++++++++++++++++++++ test/aave/v2.test.js | 214 +++++++++++++++++++++++++++++++++ test/compound/compound.test.js | 2 +- 4 files changed, 369 insertions(+), 1 deletion(-) create mode 100644 test/aave/v1.test.js create mode 100644 test/aave/v2.test.js diff --git a/hardhat.config.js b/hardhat.config.js index d592c5f7..45b17f9d 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -69,5 +69,8 @@ module.exports = { tenderly: { project: process.env.TENDERLY_PROJECT, username: process.env.TENDERLY_USERNAME, + }, + mocha: { + timeout: 100 * 1000 } }; diff --git a/test/aave/v1.test.js b/test/aave/v1.test.js new file mode 100644 index 00000000..aea2a222 --- /dev/null +++ b/test/aave/v1.test.js @@ -0,0 +1,151 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const abis = require("../../scripts/constant/abis"); +const addresses = require("../../scripts/constant/addresses"); +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector"); +const getMasterSigner = require("../../scripts/getMasterSigner"); +const buildDSAv2 = require("../../scripts/buildDSAv2"); +const ConnectV2AaveV1 = require("../../artifacts/contracts/mainnet/connectors/aave/v1/main.sol/ConnectV2AaveV1.json"); +const { parseEther } = require("@ethersproject/units"); +const encodeSpells = require("../../scripts/encodeSpells"); +const tokens = require("../../scripts/constant/tokens"); +const constants = require("../../scripts/constant/constant"); +const addLiquidity = require("../../scripts/addLiquidity"); +const { ethers } = hre; + +describe("Aave V1", function() { + const connectorName = "AAVEV1-TEST-A"; + + let wallet0, wallet1; + let dsaWallet0; + let instaConnectorsV2; + let connector; + let masterSigner; + + before(async () => { + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2AaveV1, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + console.log("Connector address", connector.address); + }); + + it("should have contracts deployed", async () => { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!masterSigner.address).to.be.true; + }); + + describe("DSA wallet setup", function() { + it("Should build DSA v2", async function() { + dsaWallet0 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet0.address).to.be.true; + }); + + it("Deposit ETH into DSA wallet", async function() { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: parseEther("10"), + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + parseEther("10") + ); + }); + }); + + describe("Main", function() { + it("should deposit ETH in Aave V1", async function() { + const amt = parseEther("1"); + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, amt, 0, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + + await tx.wait(); + + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.eq( + parseEther("9") + ); + }); + + it("Should borrow and payback DAI from Aave V1", async function() { + const amt = parseEther("100"); // 100 DAI + + // add a little amount of dai to cover any shortfalls + await addLiquidity("dai", dsaWallet0.address, parseEther("1")); + + const spells = [ + { + connector: connectorName, + method: "borrow", + args: [tokens.dai.address, amt, 0, 0], + }, + { + connector: connectorName, + method: "payback", + // FIXME: we need to pass max_value because of roundoff/shortfall errors + args: [tokens.dai.address, constants.max_value, 0, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + + it("Should deposit all ETH in Aave V1", async function() { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, constants.max_value, 0, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("0") + ); + }); + + it("Should withdraw all ETH from Aave V1", async function() { + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [tokens.eth.address, constants.max_value, 0, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + }); +}); diff --git a/test/aave/v2.test.js b/test/aave/v2.test.js new file mode 100644 index 00000000..09b023e6 --- /dev/null +++ b/test/aave/v2.test.js @@ -0,0 +1,214 @@ +const { expect } = require("chai"); +const hre = require("hardhat"); +const abis = require("../../scripts/constant/abis"); +const addresses = require("../../scripts/constant/addresses"); +const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector"); +const getMasterSigner = require("../../scripts/getMasterSigner"); +const buildDSAv2 = require("../../scripts/buildDSAv2"); +const ConnectV2AaveV2 = require("../../artifacts/contracts/mainnet/connectors/aave/v2/main.sol/ConnectV2AaveV2.json"); +const { parseEther } = require("@ethersproject/units"); +const encodeSpells = require("../../scripts/encodeSpells"); +const tokens = require("../../scripts/constant/tokens"); +const constants = require("../../scripts/constant/constant"); +const addLiquidity = require("../../scripts/addLiquidity"); +const { ethers } = hre; + +describe("Aave V2", function() { + const connectorName = "AAVEV2-TEST-A"; + + let wallet0, wallet1; + let dsaWallet0; + let instaConnectorsV2; + let connector; + let masterSigner; + + before(async () => { + [wallet0, wallet1] = await ethers.getSigners(); + masterSigner = await getMasterSigner(); + instaConnectorsV2 = await ethers.getContractAt( + abis.core.connectorsV2, + addresses.core.connectorsV2 + ); + connector = await deployAndEnableConnector({ + connectorName, + contractArtifact: ConnectV2AaveV2, + signer: masterSigner, + connectors: instaConnectorsV2, + }); + console.log("Connector address", connector.address); + }); + + it("should have contracts deployed", async () => { + expect(!!instaConnectorsV2.address).to.be.true; + expect(!!connector.address).to.be.true; + expect(!!masterSigner.address).to.be.true; + }); + + describe("DSA wallet setup", function() { + it("Should build DSA v2", async function() { + dsaWallet0 = await buildDSAv2(wallet0.address); + expect(!!dsaWallet0.address).to.be.true; + }); + + it("Deposit ETH into DSA wallet", async function() { + await wallet0.sendTransaction({ + to: dsaWallet0.address, + value: parseEther("10"), + }); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + parseEther("10") + ); + }); + }); + + describe("Main", function() { + it("should deposit ETH in Aave V2", async function() { + const amt = parseEther("1"); + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, amt, 0, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + + await tx.wait(); + + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.eq( + parseEther("9") + ); + }); + + it("Should borrow and payback DAI from Aave V2", async function() { + const amt = parseEther("100"); // 100 DAI + const setId = "83478237"; + const spells = [ + { + connector: connectorName, + method: "borrow", + args: [tokens.dai.address, amt, 2, 0, setId], + }, + { + connector: connectorName, + method: "payback", + args: [tokens.dai.address, amt, 2, setId, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + + it("Should borrow and payback half DAI from Aave V2", async function() { + const amt = parseEther("100"); // 100 DAI + // const setId = "83478237"; + await addLiquidity("dai", dsaWallet0.address, parseEther("1")); + let spells = [ + { + connector: connectorName, + method: "borrow", + args: [tokens.dai.address, amt, 2, 0, 0], + }, + { + connector: connectorName, + method: "payback", + args: [tokens.dai.address, amt.div(2), 2, 0, 0], + }, + ]; + + let tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + + spells = [ + { + connector: connectorName, + method: "payback", + args: [tokens.dai.address, constants.max_value, 2, 0, 0], + }, + ]; + + tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("9") + ); + }); + + it("Should deposit all ETH in Aave V2", async function() { + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, constants.max_value, 0, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte( + ethers.utils.parseEther("0") + ); + }); + + it("Should withdraw all ETH from Aave V2", async function() { + const spells = [ + { + connector: connectorName, + method: "withdraw", + args: [tokens.eth.address, constants.max_value, 0, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + + it("should deposit and withdraw", async () => { + const amt = parseEther("1"); // 1 eth + const setId = "834782373"; + const spells = [ + { + connector: connectorName, + method: "deposit", + args: [tokens.eth.address, amt, 0, setId], + }, + { + connector: connectorName, + method: "withdraw", + args: [tokens.eth.address, amt, setId, 0], + }, + ]; + + const tx = await dsaWallet0 + .connect(wallet0) + .cast(...encodeSpells(spells), wallet1.address); + await tx.wait(); + expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte( + ethers.utils.parseEther("10") + ); + }); + }); +}); diff --git a/test/compound/compound.test.js b/test/compound/compound.test.js index a3b79965..d1c77475 100644 --- a/test/compound/compound.test.js +++ b/test/compound/compound.test.js @@ -124,4 +124,4 @@ describe("Compound", function () { expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); }); }) -}) \ No newline at end of file +})