dsa-connectors/test/mainnet/aave/v2.test.ts

227 lines
6.8 KiB
TypeScript
Raw Normal View History

import { expect } from "chai";
import hre from "hardhat";
2021-12-10 19:44:04 +00:00
import { abis } from "../../../scripts/constant/abis";
import { addresses } from "../../../scripts/tests/mainnet/addresses";
2021-12-05 19:10:22 +00:00
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
import { ConnectV2AaveV2, ConnectV2AaveV2__factory } from "../../../typechain";
import { parseEther } from "@ethersproject/units";
2021-12-05 19:10:22 +00:00
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
2021-12-10 19:10:45 +00:00
import { tokens } from "../../../scripts/tests/mainnet/tokens";
import { constants } from "../../../scripts/constant/constant";
2021-12-05 19:10:22 +00:00
import { addLiquidity } from "../../../scripts/tests/addLiquidity";
const { ethers } = hre;
2021-12-05 22:01:27 +00:00
import type { Signer, Contract } from "ethers";
describe("Aave V2", function () {
2021-06-04 10:47:41 +00:00
const connectorName = "AAVEV2-TEST-A";
2021-12-05 22:01:27 +00:00
let connector: any;
2021-06-04 10:47:41 +00:00
2021-12-10 19:44:04 +00:00
let wallet0: Signer, wallet1:Signer;
let dsaWallet0: any;
2021-12-05 22:01:27 +00:00
let instaConnectorsV2: Contract;
let masterSigner: Signer;
2021-06-04 10:47:41 +00:00
before(async () => {
2021-12-05 22:01:27 +00:00
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
// @ts-ignore
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
blockNumber: 12796965,
},
},
],
});
2021-06-04 10:47:41 +00:00
[wallet0, wallet1] = await ethers.getSigners();
masterSigner = await getMasterSigner();
instaConnectorsV2 = await ethers.getContractAt(
abis.core.connectorsV2,
2021-12-10 19:10:45 +00:00
addresses.core.connectorsV2
2021-06-04 10:47:41 +00:00
);
connector = await deployAndEnableConnector({
connectorName,
2021-12-05 19:10:22 +00:00
contractArtifact: ConnectV2AaveV2__factory,
2021-06-04 10:47:41 +00:00
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;
2021-12-05 22:01:27 +00:00
expect(!!(await masterSigner.getAddress())).to.be.true;
2021-06-04 10:47:41 +00:00
});
describe("DSA wallet setup", function () {
it("Should build DSA v2", async function () {
2021-12-10 19:44:04 +00:00
dsaWallet0 = await buildDSAv2(wallet0.getAddress());
2021-06-04 10:47:41 +00:00
expect(!!dsaWallet0.address).to.be.true;
});
it("Deposit ETH into DSA wallet", async function () {
2021-06-04 10:47:41 +00:00
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 () {
2021-06-04 10:47:41 +00:00
const amt = parseEther("1");
const spells = [
{
connector: connectorName,
method: "deposit",
2021-12-10 19:10:45 +00:00
args: [tokens.eth.address, amt, 0, 0],
2021-06-04 10:47:41 +00:00
},
];
const tx = await dsaWallet0
.connect(wallet0)
2021-12-10 19:44:04 +00:00
.cast(...encodeSpells(spells), wallet1.getAddress());
2021-06-04 10:47:41 +00:00
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 () {
2021-06-04 10:47:41 +00:00
const amt = parseEther("100"); // 100 DAI
const setId = "83478237";
const spells = [
{
connector: connectorName,
method: "borrow",
2021-12-10 19:10:45 +00:00
args: [tokens.dai.address, amt, 2, 0, setId],
2021-06-04 10:47:41 +00:00
},
{
connector: connectorName,
method: "payback",
2021-12-10 19:10:45 +00:00
args: [tokens.dai.address, amt, 2, setId, 0],
2021-06-04 10:47:41 +00:00
},
];
const tx = await dsaWallet0
.connect(wallet0)
2021-12-10 19:44:04 +00:00
.cast(...encodeSpells(spells), wallet1.getAddress());
2021-06-04 10:47:41 +00:00
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 () {
2021-06-04 10:47:41 +00:00
const amt = parseEther("100"); // 100 DAI
// const setId = "83478237";
await addLiquidity("dai", dsaWallet0.address, parseEther("1"));
let spells = [
{
connector: connectorName,
method: "borrow",
2021-12-10 19:10:45 +00:00
args: [tokens.dai.address, amt, 2, 0, 0],
2021-06-04 10:47:41 +00:00
},
{
connector: connectorName,
method: "payback",
2021-12-10 19:10:45 +00:00
args: [tokens.dai.address, amt.div(2), 2, 0, 0],
2021-06-04 10:47:41 +00:00
},
];
let tx = await dsaWallet0
.connect(wallet0)
2021-12-10 19:44:04 +00:00
.cast(...encodeSpells(spells), wallet1.getAddress());
2021-06-04 10:47:41 +00:00
await tx.wait();
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(
ethers.utils.parseEther("9")
);
spells = [
{
connector: connectorName,
method: "payback",
2021-12-10 19:10:45 +00:00
args: [tokens.dai.address, constants.max_value, 2, 0, 0],
2021-06-04 10:47:41 +00:00
},
];
tx = await dsaWallet0
.connect(wallet0)
2021-12-10 19:44:04 +00:00
.cast(...encodeSpells(spells), wallet1.getAddress());
2021-06-04 10:47:41 +00:00
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 () {
2021-06-04 10:47:41 +00:00
const spells = [
{
connector: connectorName,
method: "deposit",
2021-12-10 19:10:45 +00:00
args: [tokens.eth.address, constants.max_value, 0, 0],
2021-06-04 10:47:41 +00:00
},
];
const tx = await dsaWallet0
.connect(wallet0)
2021-12-10 19:44:04 +00:00
.cast(...encodeSpells(spells), wallet1.getAddress());
2021-06-04 10:47:41 +00:00
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 () {
2021-06-04 10:47:41 +00:00
const spells = [
{
connector: connectorName,
method: "withdraw",
2021-12-10 19:10:45 +00:00
args: [tokens.eth.address, constants.max_value, 0, 0],
2021-06-04 10:47:41 +00:00
},
];
const tx = await dsaWallet0
.connect(wallet0)
2021-12-10 19:44:04 +00:00
.cast(...encodeSpells(spells), wallet1.getAddress());
2021-06-04 10:47:41 +00:00
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",
2021-12-10 19:10:45 +00:00
args: [tokens.eth.address, amt, 0, setId],
2021-06-04 10:47:41 +00:00
},
{
connector: connectorName,
method: "withdraw",
2021-12-10 19:10:45 +00:00
args: [tokens.eth.address, amt, setId, 0],
2021-06-04 10:47:41 +00:00
},
];
const tx = await dsaWallet0
.connect(wallet0)
2021-12-10 19:44:04 +00:00
.cast(...encodeSpells(spells), wallet1.getAddress());
2021-06-04 10:47:41 +00:00
await tx.wait();
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(
ethers.utils.parseEther("10")
);
});
});
});