From eb14905ffee6f7b57d08f31591fe741a2fb86f84 Mon Sep 17 00:00:00 2001 From: joelamouche Date: Fri, 23 Oct 2020 15:35:18 +0200 Subject: [PATCH 1/4] withdraw and send eth --- src/lib/{sendDSA.ts => sendDSAETH.ts} | 4 ++-- src/lib/withdrawDSAETH.ts | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) rename src/lib/{sendDSA.ts => sendDSAETH.ts} (80%) create mode 100644 src/lib/withdrawDSAETH.ts diff --git a/src/lib/sendDSA.ts b/src/lib/sendDSAETH.ts similarity index 80% rename from src/lib/sendDSA.ts rename to src/lib/sendDSAETH.ts index 2d9cc72..dd867b7 100644 --- a/src/lib/sendDSA.ts +++ b/src/lib/sendDSAETH.ts @@ -1,12 +1,12 @@ import { ethers, BigNumber, utils } from "ethers"; import Web3 from "web3"; -export async function sendDSA(web3: Web3, dsaAddress:string, eth_amount:number) { +export async function sendDSAETH(web3: Web3, dsaAddress:string, eth_amount:number) { //setup ethers let provider = new ethers.providers.Web3Provider(web3.currentProvider as any); let userWallet: ethers.providers.JsonRpcSigner = await provider.getSigner(); - // And send ETH to the DSA, just in case + // And send ETH to the DSA const gasLimit = BigNumber.from(1000000); const gasPrice = utils.parseUnits("20", "gwei"); return await userWallet.sendTransaction({ diff --git a/src/lib/withdrawDSAETH.ts b/src/lib/withdrawDSAETH.ts new file mode 100644 index 0000000..f90e85d --- /dev/null +++ b/src/lib/withdrawDSAETH.ts @@ -0,0 +1,24 @@ +import { ethers, BigNumber, utils, Contract } from "ethers"; +import Web3 from "web3"; +import { constants } from "../constants/constants"; +import { abiEncodeWithSelector } from "./utils/abiEncodeWithSelector"; +const ConnectBasic = require("../../pre-compiles/ConnectBasic.json"); +const InstaAccount = require("../../pre-compiles/InstaAccount.json"); + +export async function withdrawDSAETH(web3: Web3, dsaAddress:string, eth_amount:number) { + //setup ethers + let provider = new ethers.providers.Web3Provider(web3.currentProvider as any); + let userWallet: ethers.providers.JsonRpcSigner = await provider.getSigner(); + let userAddress = await userWallet.getAddress(); + let dsa = new Contract(dsaAddress, InstaAccount.abi, userWallet); + + // And withdraw fron the DSA + const gasLimit = BigNumber.from(1000000); + const gasPrice = utils.parseUnits("20", "gwei"); + const withdrawData = abiEncodeWithSelector(ConnectBasic.abi, "withdraw", [constants.ETH, ethers.utils.parseEther(eth_amount.toString()), userAddress, 0, 0]); + + return await dsa.cast([constants.ConnectBasic], [withdrawData], userAddress, { + gasLimit, + gasPrice, + }) +} From 0e8a9bebbcc2a1b7bffe2ab7fc12e4bf92da45b0 Mon Sep 17 00:00:00 2001 From: joelamouche Date: Fri, 23 Oct 2020 15:53:43 +0200 Subject: [PATCH 2/4] send and withdraw dai from dsa --- src/constants/constants.ts | 1 + src/lib/sendDSADAI.ts | 13 +++++ src/lib/withdrawDSADAI.ts | 24 ++++++++++ src/test/3_lib-tests.ts | 47 +++++++++++++++---- ...4_mv-Debt-Maker-Compound_WITH_LIBS.test.ts | 4 +- src/test/5_test-InstaDapp-Resolvers.ts | 4 +- 6 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 src/lib/sendDSADAI.ts create mode 100644 src/lib/withdrawDSADAI.ts diff --git a/src/constants/constants.ts b/src/constants/constants.ts index 81a05e9..59782f1 100644 --- a/src/constants/constants.ts +++ b/src/constants/constants.ts @@ -6,6 +6,7 @@ export const constants = { DAI_UNISWAP: "0x2a1530C4C41db0B0b2bB646CB5Eb1A67b7158667", CDAI: "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643", CETH: "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5", + ETH : "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // Conditions ConditionBalance: "0xd69DB9852bAbf9e0bd73B8A090382573C7488154", // InstaDapp DSA diff --git a/src/lib/sendDSADAI.ts b/src/lib/sendDSADAI.ts new file mode 100644 index 0000000..e160de8 --- /dev/null +++ b/src/lib/sendDSADAI.ts @@ -0,0 +1,13 @@ +import { ethers, BigNumber, utils, Contract } from "ethers"; +import Web3 from "web3"; +import { constants } from "../constants/constants"; +const IERC20 = require("../../pre-compiles/IERC20.json"); + +export async function sendDSADAI(web3: Web3, dsaAddress:string, dai_amount:number) { + //setup ethers + let provider = new ethers.providers.Web3Provider(web3.currentProvider as any); + let userWallet: ethers.providers.JsonRpcSigner = await provider.getSigner(); + let dai = new Contract(constants.DAI, IERC20.abi, userWallet); + + return await dai.transfer(dsaAddress, ethers.utils.parseUnits(dai_amount.toString(), 18)); +} diff --git a/src/lib/withdrawDSADAI.ts b/src/lib/withdrawDSADAI.ts new file mode 100644 index 0000000..24e7177 --- /dev/null +++ b/src/lib/withdrawDSADAI.ts @@ -0,0 +1,24 @@ +import { ethers, BigNumber, utils, Contract } from "ethers"; +import Web3 from "web3"; +import { constants } from "../constants/constants"; +import { abiEncodeWithSelector } from "./utils/abiEncodeWithSelector"; +const ConnectBasic = require("../../pre-compiles/ConnectBasic.json"); +const InstaAccount = require("../../pre-compiles/InstaAccount.json"); + +export async function withdrawDSADAI(web3: Web3, dsaAddress:string, dai_amount:number) { + //setup ethers + let provider = new ethers.providers.Web3Provider(web3.currentProvider as any); + let userWallet: ethers.providers.JsonRpcSigner = await provider.getSigner(); + let userAddress = await userWallet.getAddress(); + let dsa = new Contract(dsaAddress, InstaAccount.abi, userWallet); + + // And withdraw fron the DSA + const gasLimit = BigNumber.from(1000000); + const gasPrice = utils.parseUnits("20", "gwei"); + const withdrawData = abiEncodeWithSelector(ConnectBasic.abi, "withdraw", [constants.DAI, ethers.utils.parseUnits(dai_amount.toString(), 18), userAddress, 0, 0]); + + return await dsa.cast([constants.ConnectBasic], [withdrawData], userAddress, { + gasLimit, + gasPrice, + }) +} diff --git a/src/test/3_lib-tests.ts b/src/test/3_lib-tests.ts index e3200b9..f64ec42 100644 --- a/src/test/3_lib-tests.ts +++ b/src/test/3_lib-tests.ts @@ -4,7 +4,10 @@ import { BigNumber, Contract } from "ethers"; import { createDSA } from "../lib/createDSA"; import { getDSAAccounts } from "../lib/getDSAAccounts"; import { getVaults } from "../lib/getVaults"; -import { sendDSA } from "../lib/sendDSA"; +import { sendDSAETH } from "../lib/sendDSAETH"; +import { withdrawDSAETH } from "../lib/withdrawDSAETH"; +import { sendDSADAI } from "../lib/sendDSADAI"; +import { withdrawDSADAI } from "../lib/withdrawDSADAI"; import { createGelatoOptimizer } from "../lib/createGelatoOptimizer"; import { createGelatoAutoLiquidator } from "../lib/createGelatoAutoLiquidator"; import { createMakerVault } from "../lib/createMakerVault"; @@ -39,7 +42,7 @@ const DAI_150 = ethers.utils.parseUnits("150", 18); const APY_2_PERCENT_IN_SECONDS = BigNumber.from("1000000000627937192491029810"); describe("Test Lib functions", function () { - describe("Test createDSA, sendDSA && getDSAAccounts", function () { + describe.only("Test createDSA, sendDSA, withdrawDSA && getDSAAccounts", function () { this.timeout(0); if (bre.network.name !== "ganache") { console.error("Test Suite is meant to be run on ganache only"); @@ -66,14 +69,18 @@ describe("Test Lib functions", function () { dsaAddress ); expect(await dsa.version()).to.be.equal(1); - - // send 30 eth to DSA - await sendDSA(web3, dsaAddress,30) - expect(await web3.eth.getBalance(dsaAddress)).to.eq(ethers.utils.parseEther("30")) }); + it("sends 40 eth to dsa",async()=>{ + await sendDSAETH(web3, dsaAddress,40) + expect(await web3.eth.getBalance(dsaAddress)).to.eq(ethers.utils.parseEther("40")) + }) + it("withdraws 10 eth from dsa",async()=>{ + await withdrawDSAETH(web3, dsaAddress,10) + expect(await web3.eth.getBalance(dsaAddress)).to.eq(ethers.utils.parseEther("30")) + }) }); - describe("Test createMakerVault && getVaults", function () { + describe.only("Test createMakerVault && getVaults", function () { this.timeout(0); if (bre.network.name !== "ganache") { console.error("Test Suite is meant to be run on ganache only"); @@ -82,12 +89,14 @@ describe("Test Lib functions", function () { // Wallet to use for local testing let dsaAddress: string; let userWallet; + let userAddress:string; let dai: Ierc20; before(async function () { dsaAddress = await createDSA(web3); - await sendDSA(web3, dsaAddress,30); + await sendDSAETH(web3, dsaAddress,30); [userWallet] = await ethers.getSigners(); + userAddress = await userWallet.getAddress(); }); it("creates a Maker vault with 10ETH", async () => { const initialWalletBalance: BigNumber = await userWallet.getBalance(); @@ -118,6 +127,24 @@ describe("Test Lib functions", function () { ethers.utils.parseUnits("150", 18) ); }); + it("withdraws 10 dai from dsa",async()=>{ + await withdrawDSADAI(web3, dsaAddress,10) + expect(await dai.balanceOf(dsaAddress)).to.eq( + ethers.utils.parseUnits("140", 18) + ); + expect(await dai.balanceOf(userAddress)).to.eq( + ethers.utils.parseUnits("10", 18) + ); + }) + it("sends 10 dai to dsa",async()=>{ + await sendDSADAI(web3, dsaAddress,10) + expect(await dai.balanceOf(dsaAddress)).to.eq( + ethers.utils.parseUnits("150", 18) + ); + expect(await dai.balanceOf(userAddress)).to.eq( + ethers.utils.parseUnits("0", 18) + ); + }) }); describe("Test createGelatoOptimizer", function () { @@ -168,7 +195,7 @@ describe("Test Lib functions", function () { // Create DSA for user dsaAddress = await createDSA(web3); - await sendDSA(web3, dsaAddress,30); + await sendDSAETH(web3, dsaAddress,30); [userWallet] = await ethers.getSigners(); userAddress = await userWallet.getAddress(); // deploy Maker vault for user @@ -477,7 +504,7 @@ describe("Test Lib functions", function () { // Create DSA for user dsaAddress = await createDSA(web3); - await sendDSA(web3, dsaAddress,30); + await sendDSAETH(web3, dsaAddress,30); [userWallet] = await ethers.getSigners(); userAddress = await userWallet.getAddress(); // deploy Maker vault for user diff --git a/src/test/4_mv-Debt-Maker-Compound_WITH_LIBS.test.ts b/src/test/4_mv-Debt-Maker-Compound_WITH_LIBS.test.ts index ee6d019..b8f8627 100644 --- a/src/test/4_mv-Debt-Maker-Compound_WITH_LIBS.test.ts +++ b/src/test/4_mv-Debt-Maker-Compound_WITH_LIBS.test.ts @@ -1,7 +1,7 @@ // running `npx buidler test` automatically makes use of buidler-waffle plugin import { createDSA } from "../lib/createDSA"; -import { sendDSA } from "../lib/sendDSA"; +import { sendDSAETH } from "../lib/sendDSAETH"; import { createGelatoOptimizer } from "../lib/createGelatoOptimizer"; import { createMakerVault } from "../lib/createMakerVault"; @@ -67,7 +67,7 @@ describe("Move DAI Debt from Maker to Compound WITH LIBS", function () { // ===== DSA SETUP ================== // Instantiate the InstaDapp DSA dsaAddress = await createDSA(web3); - await sendDSA(web3, dsaAddress,30) + await sendDSAETH(web3, dsaAddress,30) dsa = await ethers.getContractAt(InstaAccount.abi, dsaAddress); // ===== GELATO SETUP ================== diff --git a/src/test/5_test-InstaDapp-Resolvers.ts b/src/test/5_test-InstaDapp-Resolvers.ts index 5eaad90..2573146 100644 --- a/src/test/5_test-InstaDapp-Resolvers.ts +++ b/src/test/5_test-InstaDapp-Resolvers.ts @@ -1,5 +1,5 @@ import { createDSA } from "../lib/createDSA"; -import { sendDSA } from "../lib/sendDSA"; +import { sendDSAETH } from "../lib/sendDSAETH"; import { createMakerVault } from "../lib/createMakerVault"; // => only dependency we need is "chai" @@ -65,7 +65,7 @@ describe("Test our condition source contracts", function () { before(async function () { dsaAddress = await createDSA(web3); - await sendDSA(web3, dsaAddress,30) + await sendDSAETH(web3, dsaAddress,30) addresses = await bre.run("deployContractsMainNet") console.log('Contracts Deployed !') }); From 278d7c1f5c6afdec4bafbc8f954798b2f7592712 Mon Sep 17 00:00:00 2001 From: joelamouche Date: Fri, 23 Oct 2020 16:07:08 +0200 Subject: [PATCH 3/4] get dai balance --- src/index.ts | 9 ++++++++- src/lib/getDAIBalance.ts | 13 +++++++++++++ src/test/3_lib-tests.ts | 17 +++++++++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/lib/getDAIBalance.ts diff --git a/src/index.ts b/src/index.ts index 16078bf..480556e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,4 +2,11 @@ export { createMakerVault } from "./lib/createMakerVault"; export { createDSA } from "./lib/createDSA"; export { createGelatoOptimizer } from "./lib/createGelatoOptimizer"; export { getMainnetAddresses } from "./lib/getMainnetAddresses"; -export { getDSAAccounts } from "./lib/getDSAAccounts"; \ No newline at end of file +export { getDSAAccounts } from "./lib/getDSAAccounts"; +export { getDAIBalance } from "./lib/getDAIBalance"; +export { sendDSAETH } from "./lib/sendDSAETH"; +export { sendDSADAI } from "./lib/sendDSADAI"; +export { withdrawDSAETH } from "./lib/withdrawDSAETH"; +export { withdrawDSADAI } from "./lib/withdrawDSADAI"; +export { getVaults } from "./lib/getVaults"; +export { getUserAddress } from "./lib/getUserAddress"; \ No newline at end of file diff --git a/src/lib/getDAIBalance.ts b/src/lib/getDAIBalance.ts new file mode 100644 index 0000000..e2fd819 --- /dev/null +++ b/src/lib/getDAIBalance.ts @@ -0,0 +1,13 @@ +import { ethers, Contract } from "ethers"; +import Web3 from "web3"; +import { constants } from "../constants/constants"; +const IERC20 = require("../../pre-compiles/IERC20.json"); + +export async function getDAIBalance(web3: Web3, address:string) { + //setup dai + let provider = new ethers.providers.Web3Provider(web3.currentProvider as any); + let userWallet: ethers.providers.JsonRpcSigner = await provider.getSigner(); + let dai = new Contract(constants.DAI, IERC20.abi, userWallet); + + return await dai.balanceOf(address) +} diff --git a/src/test/3_lib-tests.ts b/src/test/3_lib-tests.ts index f64ec42..bebfc5d 100644 --- a/src/test/3_lib-tests.ts +++ b/src/test/3_lib-tests.ts @@ -8,6 +8,7 @@ import { sendDSAETH } from "../lib/sendDSAETH"; import { withdrawDSAETH } from "../lib/withdrawDSAETH"; import { sendDSADAI } from "../lib/sendDSADAI"; import { withdrawDSADAI } from "../lib/withdrawDSADAI"; +import { getDAIBalance } from "../lib/getDAIBalance"; import { createGelatoOptimizer } from "../lib/createGelatoOptimizer"; import { createGelatoAutoLiquidator } from "../lib/createGelatoAutoLiquidator"; import { createMakerVault } from "../lib/createMakerVault"; @@ -42,7 +43,7 @@ const DAI_150 = ethers.utils.parseUnits("150", 18); const APY_2_PERCENT_IN_SECONDS = BigNumber.from("1000000000627937192491029810"); describe("Test Lib functions", function () { - describe.only("Test createDSA, sendDSA, withdrawDSA && getDSAAccounts", function () { + describe("Test createDSA, sendDSA, withdrawDSA && getDSAAccounts", function () { this.timeout(0); if (bre.network.name !== "ganache") { console.error("Test Suite is meant to be run on ganache only"); @@ -80,7 +81,7 @@ describe("Test Lib functions", function () { }) }); - describe.only("Test createMakerVault && getVaults", function () { + describe("Test createMakerVault && getVaults", function () { this.timeout(0); if (bre.network.name !== "ganache") { console.error("Test Suite is meant to be run on ganache only"); @@ -135,6 +136,12 @@ describe("Test Lib functions", function () { expect(await dai.balanceOf(userAddress)).to.eq( ethers.utils.parseUnits("10", 18) ); + expect(await getDAIBalance(web3,dsaAddress)).to.eq( + ethers.utils.parseUnits("140", 18) + ); + expect(await getDAIBalance(web3,userAddress)).to.eq( + ethers.utils.parseUnits("10", 18) + ); }) it("sends 10 dai to dsa",async()=>{ await sendDSADAI(web3, dsaAddress,10) @@ -144,6 +151,12 @@ describe("Test Lib functions", function () { expect(await dai.balanceOf(userAddress)).to.eq( ethers.utils.parseUnits("0", 18) ); + expect(await getDAIBalance(web3,dsaAddress)).to.eq( + ethers.utils.parseUnits("150", 18) + ); + expect(await getDAIBalance(web3,userAddress)).to.eq( + ethers.utils.parseUnits("0", 18) + ); }) }); From f3b7604f5e9c47aebe303242a5e6967edfcb7094 Mon Sep 17 00:00:00 2001 From: joelamouche Date: Fri, 23 Oct 2020 16:36:36 +0200 Subject: [PATCH 4/4] add getdaibalance --- src/customTypes/{customTypes.d.ts => customTypes.ts} | 0 src/lib/getDAIBalance.ts | 4 ++-- src/test/3_lib-tests.ts | 11 +++++++---- 3 files changed, 9 insertions(+), 6 deletions(-) rename src/customTypes/{customTypes.d.ts => customTypes.ts} (100%) diff --git a/src/customTypes/customTypes.d.ts b/src/customTypes/customTypes.ts similarity index 100% rename from src/customTypes/customTypes.d.ts rename to src/customTypes/customTypes.ts diff --git a/src/lib/getDAIBalance.ts b/src/lib/getDAIBalance.ts index e2fd819..018d7b5 100644 --- a/src/lib/getDAIBalance.ts +++ b/src/lib/getDAIBalance.ts @@ -1,9 +1,9 @@ -import { ethers, Contract } from "ethers"; +import { ethers, Contract, BigNumber } from "ethers"; import Web3 from "web3"; import { constants } from "../constants/constants"; const IERC20 = require("../../pre-compiles/IERC20.json"); -export async function getDAIBalance(web3: Web3, address:string) { +export async function getDAIBalance(web3: Web3, address:string):Promise { //setup dai let provider = new ethers.providers.Web3Provider(web3.currentProvider as any); let userWallet: ethers.providers.JsonRpcSigner = await provider.getSigner(); diff --git a/src/test/3_lib-tests.ts b/src/test/3_lib-tests.ts index bebfc5d..3a94d7c 100644 --- a/src/test/3_lib-tests.ts +++ b/src/test/3_lib-tests.ts @@ -93,11 +93,14 @@ describe("Test Lib functions", function () { let userAddress:string; let dai: Ierc20; + let initialUserDaiBalance:BigNumber; + before(async function () { dsaAddress = await createDSA(web3); await sendDSAETH(web3, dsaAddress,30); [userWallet] = await ethers.getSigners(); userAddress = await userWallet.getAddress(); + initialUserDaiBalance=await getDAIBalance(web3,userAddress) }); it("creates a Maker vault with 10ETH", async () => { const initialWalletBalance: BigNumber = await userWallet.getBalance(); @@ -134,13 +137,13 @@ describe("Test Lib functions", function () { ethers.utils.parseUnits("140", 18) ); expect(await dai.balanceOf(userAddress)).to.eq( - ethers.utils.parseUnits("10", 18) + initialUserDaiBalance.add(ethers.utils.parseUnits("10", 18)) ); expect(await getDAIBalance(web3,dsaAddress)).to.eq( ethers.utils.parseUnits("140", 18) ); expect(await getDAIBalance(web3,userAddress)).to.eq( - ethers.utils.parseUnits("10", 18) + initialUserDaiBalance.add(ethers.utils.parseUnits("10", 18)) ); }) it("sends 10 dai to dsa",async()=>{ @@ -149,13 +152,13 @@ describe("Test Lib functions", function () { ethers.utils.parseUnits("150", 18) ); expect(await dai.balanceOf(userAddress)).to.eq( - ethers.utils.parseUnits("0", 18) + initialUserDaiBalance ); expect(await getDAIBalance(web3,dsaAddress)).to.eq( ethers.utils.parseUnits("150", 18) ); expect(await getDAIBalance(web3,userAddress)).to.eq( - ethers.utils.parseUnits("0", 18) + initialUserDaiBalance ); }) });