Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add dsa management #30

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const constants = {
DAI_UNISWAP: "0x2a1530C4C41db0B0b2bB646CB5Eb1A67b7158667",
CDAI: "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643",
CETH: "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5",
ETH : "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
// Conditions
ConditionBalance: "0xd69DB9852bAbf9e0bd73B8A090382573C7488154",
// InstaDapp DSA
Expand Down
File renamed without changes.
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
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";
13 changes: 13 additions & 0 deletions src/lib/getDAIBalance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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):Promise<BigNumber> {
//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)
}
13 changes: 13 additions & 0 deletions src/lib/sendDSADAI.ts
Original file line number Diff line number Diff line change
@@ -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));
}
4 changes: 2 additions & 2 deletions src/lib/sendDSA.ts → src/lib/sendDSAETH.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down
24 changes: 24 additions & 0 deletions src/lib/withdrawDSADAI.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
24 changes: 24 additions & 0 deletions src/lib/withdrawDSAETH.ts
Original file line number Diff line number Diff line change
@@ -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,
})
}
61 changes: 52 additions & 9 deletions src/test/3_lib-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ 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 { getDAIBalance } from "../lib/getDAIBalance";
import { createGelatoOptimizer } from "../lib/createGelatoOptimizer";
import { createGelatoAutoLiquidator } from "../lib/createGelatoAutoLiquidator";
import { createMakerVault } from "../lib/createMakerVault";
Expand Down Expand Up @@ -39,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("Test createDSA, sendDSA && 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");
Expand All @@ -66,11 +70,15 @@ 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 () {
Expand All @@ -82,12 +90,17 @@ describe("Test Lib functions", function () {
// Wallet to use for local testing
let dsaAddress: string;
let userWallet;
let userAddress:string;
let dai: Ierc20;

let initialUserDaiBalance:BigNumber;

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();
initialUserDaiBalance=await getDAIBalance(web3,userAddress)
});
it("creates a Maker vault with 10ETH", async () => {
const initialWalletBalance: BigNumber = await userWallet.getBalance();
Expand Down Expand Up @@ -118,6 +131,36 @@ 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(
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(
initialUserDaiBalance.add(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(
initialUserDaiBalance
);
expect(await getDAIBalance(web3,dsaAddress)).to.eq(
ethers.utils.parseUnits("150", 18)
);
expect(await getDAIBalance(web3,userAddress)).to.eq(
initialUserDaiBalance
);
})
});

describe("Test createGelatoOptimizer", function () {
Expand Down Expand Up @@ -168,7 +211,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
Expand Down Expand Up @@ -477,7 +520,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
Expand Down
4 changes: 2 additions & 2 deletions src/test/4_mv-Debt-Maker-Compound_WITH_LIBS.test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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 ==================
Expand Down
4 changes: 2 additions & 2 deletions src/test/5_test-InstaDapp-Resolvers.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 !')
});
Expand Down