dsa-connectors/test/mainnet/compound/compound.test.ts

139 lines
5.2 KiB
TypeScript
Raw Normal View History

import { expect } from "chai";
import hre from "hardhat";
2021-12-05 22:01:27 +00:00
const { waffle, ethers } = hre;
2021-12-05 21:24:36 +00:00
const { provider, deployContract} = waffle
import type { Signer, Contract } from "ethers";
2021-05-14 19:16:54 +00:00
2021-12-05 22:01:27 +00:00
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
2021-12-05 19:10:22 +00:00
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"
2021-12-05 22:01:27 +00:00
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
2021-12-05 19:10:22 +00:00
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"
2021-12-09 19:32:08 +00:00
import addresses from "../../../scripts/constant/addresses";
2021-12-10 17:29:55 +00:00
import abis from "../../../scripts/constant/abis";
import { constants } from "../../../scripts/constant/constant";
2021-12-05 22:01:27 +00:00
import { ConnectV2Compound__factory } from "../../../typechain";
2021-05-14 19:16:54 +00:00
describe("Compound", function () {
const connectorName = "COMPOUND-TEST-A"
2021-09-29 05:19:30 +00:00
2021-12-05 22:01:27 +00:00
let dsaWallet0: any;
2021-12-05 21:24:36 +00:00
let masterSigner: Signer;
let instaConnectorsV2: Contract;
let connector: any;
2021-09-29 05:19:30 +00:00
2021-05-14 19:16:54 +00:00
const wallets = provider.getWallets()
const [wallet0, wallet1, wallet2, wallet3] = wallets
before(async () => {
2021-09-29 05:19:30 +00:00
await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
2021-12-05 21:24:36 +00:00
//@ts-ignore
2021-09-29 05:19:30 +00:00
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
blockNumber: 13300000,
},
},
],
});
2021-12-05 22:01:27 +00:00
masterSigner = await getMasterSigner()
2021-12-08 07:44:19 +00:00
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.mainnet.core.connectorsV2);
2021-05-14 19:16:54 +00:00
connector = await deployAndEnableConnector({
connectorName,
2021-12-05 22:01:27 +00:00
contractArtifact: ConnectV2Compound__factory,
2021-05-14 19:16:54 +00:00
signer: masterSigner,
connectors: instaConnectorsV2
})
console.log("Connector address", connector.address)
2021-09-29 05:19:30 +00:00
})
it("Should have contracts deployed.", async function () {
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-05-14 19:16:54 +00:00
});
2021-09-29 05:19:30 +00:00
describe("DSA wallet setup", function () {
it("Should build DSA v2", async function () {
dsaWallet0 = await buildDSAv2(wallet0.address)
expect(!!dsaWallet0.address).to.be.true;
2021-05-14 19:16:54 +00:00
});
2021-09-29 05:19:30 +00:00
it("Deposit ETH into DSA wallet", async function () {
await wallet0.sendTransaction({
to: dsaWallet0.address,
value: ethers.utils.parseEther("10")
});
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
});
2021-05-14 19:16:54 +00:00
});
2021-09-29 05:19:30 +00:00
describe("Main", function () {
it("Should deposit ETH in Compound", async function () {
const amount = ethers.utils.parseEther("1") // 1 ETH
const spells = [
{
connector: connectorName,
method: "deposit",
args: ["ETH-A", amount, 0, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("9"));
});
2021-05-14 19:16:54 +00:00
2021-09-29 05:19:30 +00:00
it("Should borrow and payback DAI from Compound", async function () {
const amount = ethers.utils.parseEther("100") // 100 DAI
const setId = "83478237"
const spells = [
{
connector: connectorName,
method: "borrow",
args: ["DAI-A", amount, 0, setId]
},
{
connector: connectorName,
method: "payback",
args: ["DAI-A", 0, setId, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("9"));
});
it("Should deposit all ETH in Compound", async function () {
const spells = [
{
connector: connectorName,
method: "deposit",
args: ["ETH-A", constants.max_value, 0, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.lte(ethers.utils.parseEther("0"));
});
it("Should withdraw all ETH from Compound", async function () {
const spells = [
{
connector: connectorName,
method: "withdraw",
args: ["ETH-A", constants.max_value, 0, 0]
}
]
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
const receipt = await tx.wait()
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
});
})
2021-06-04 10:47:41 +00:00
})