2022-03-23 10:43:46 +00:00
|
|
|
import { expect, should } from "chai";
|
|
|
|
import hre, { ethers, waffle } from "hardhat";
|
|
|
|
import type { Signer, Contract } from "ethers";
|
|
|
|
import { BigNumber } from "bignumber.js";
|
|
|
|
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
|
|
|
import { addresses } from "../../../scripts/tests/mainnet/addresses";
|
2022-03-23 10:51:56 +00:00
|
|
|
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
2022-03-23 10:43:46 +00:00
|
|
|
import { abis } from "../../../scripts/constant/abis";
|
|
|
|
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
|
|
|
import { parseEther, parseUnits } from "ethers/lib/utils";
|
|
|
|
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
2022-03-23 19:41:18 +00:00
|
|
|
import encodeFlashcastData from "../../../scripts/tests/encodeFlashcastData";
|
|
|
|
import { ConnectV2CompoundImport__factory } from "../../../typechain";
|
2022-03-23 10:43:46 +00:00
|
|
|
const { provider } = waffle;
|
|
|
|
|
|
|
|
const cEthAddress = "0x4Ddc2D193948926D02f9B1fE9e1daa0718270ED5";
|
|
|
|
const cDaiAddress = "0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643";
|
|
|
|
const daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
|
|
|
|
const comptrollerAddress = "0x3d9819210A31b4961b30EF54bE2aeD79B9c9Cd3B";
|
|
|
|
|
|
|
|
describe("Import Compound", function () {
|
2022-03-23 19:41:18 +00:00
|
|
|
const connectorName = "COMPOUND-IMPORT-X";
|
2022-03-23 10:43:46 +00:00
|
|
|
|
|
|
|
const cEthAbi = [
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [],
|
|
|
|
name: "mint",
|
|
|
|
outputs: [],
|
|
|
|
payable: true,
|
|
|
|
stateMutability: "payable",
|
|
|
|
type: "function",
|
|
|
|
signature: "0x1249c58b"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
constant: true,
|
|
|
|
inputs: [{ internalType: "address", name: "owner", type: "address" }],
|
|
|
|
name: "balanceOf",
|
|
|
|
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "view",
|
|
|
|
type: "function"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [],
|
|
|
|
name: "exchangeRateCurrent",
|
|
|
|
outputs: [{ name: "", type: "uint256" }],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "nonpayable",
|
|
|
|
type: "function"
|
2022-03-23 19:41:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [
|
|
|
|
{ internalType: "address", name: "usr", type: "address" },
|
|
|
|
{ internalType: "uint256", name: "wad", type: "uint256" }
|
|
|
|
],
|
|
|
|
name: "approve",
|
|
|
|
outputs: [{ internalType: "bool", name: "", type: "bool" }],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "nonpayable",
|
|
|
|
type: "function"
|
2022-03-23 10:43:46 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const cDaiAbi = [
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
internalType: "uint256",
|
|
|
|
name: "borrowAmount",
|
|
|
|
type: "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
name: "borrow",
|
|
|
|
outputs: [
|
|
|
|
{
|
|
|
|
internalType: "uint256",
|
|
|
|
name: "",
|
|
|
|
type: "uint256"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "nonpayable",
|
|
|
|
type: "function",
|
|
|
|
signature: "0xc5ebeaec"
|
2022-03-23 19:41:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [{ internalType: "address", name: "account", type: "address" }],
|
|
|
|
name: "borrowBalanceCurrent",
|
|
|
|
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "nonpayable",
|
|
|
|
type: "function"
|
2022-03-23 10:43:46 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const comptrollerAbi = [
|
|
|
|
{
|
|
|
|
constant: false,
|
|
|
|
inputs: [
|
|
|
|
{
|
|
|
|
internalType: "address[]",
|
|
|
|
name: "cTokens",
|
|
|
|
type: "address[]"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
name: "enterMarkets",
|
|
|
|
outputs: [
|
|
|
|
{
|
|
|
|
internalType: "uint256[]",
|
|
|
|
name: "",
|
|
|
|
type: "uint256[]"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
payable: false,
|
|
|
|
stateMutability: "nonpayable",
|
|
|
|
type: "function",
|
|
|
|
signature: "0xc2998238"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
let cEth: Contract, cDai: Contract, comptroller, Dai: any;
|
|
|
|
|
|
|
|
let dsaWallet0: any;
|
|
|
|
let masterSigner: Signer;
|
|
|
|
let instaConnectorsV2: Contract;
|
|
|
|
let connector: any;
|
|
|
|
|
|
|
|
const wallets = provider.getWallets();
|
|
|
|
const [wallet0, wallet1, wallet2, wallet3] = wallets;
|
2022-03-23 19:43:10 +00:00
|
|
|
|
2022-03-23 10:43:46 +00:00
|
|
|
before(async () => {
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_reset",
|
|
|
|
params: [
|
|
|
|
{
|
|
|
|
forking: {
|
|
|
|
// @ts-ignore
|
|
|
|
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
2022-03-23 19:41:18 +00:00
|
|
|
blockNumber: 14441991
|
2022-03-23 10:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
masterSigner = await getMasterSigner();
|
|
|
|
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
|
|
|
|
|
|
|
|
connector = await deployAndEnableConnector({
|
|
|
|
connectorName,
|
|
|
|
contractArtifact: ConnectV2CompoundImport__factory,
|
|
|
|
signer: masterSigner,
|
|
|
|
connectors: instaConnectorsV2
|
|
|
|
});
|
|
|
|
console.log("Connector address", connector.address);
|
|
|
|
|
|
|
|
cEth = new ethers.Contract(cEthAddress, cEthAbi);
|
|
|
|
cDai = new ethers.Contract(cDaiAddress, cDaiAbi);
|
|
|
|
Dai = new ethers.Contract(daiAddress, abis.basic.erc20);
|
|
|
|
comptroller = new ethers.Contract(comptrollerAddress, comptrollerAbi);
|
|
|
|
|
|
|
|
// deposit ether to Compound: ETH-A
|
2022-03-23 19:41:18 +00:00
|
|
|
await cEth.connect(wallet0).mint({
|
2022-03-23 10:43:46 +00:00
|
|
|
value: parseEther("9")
|
|
|
|
});
|
|
|
|
|
|
|
|
// enter markets with deposits
|
|
|
|
const cTokens = [cEth.address];
|
2022-03-23 19:41:18 +00:00
|
|
|
await comptroller.connect(wallet0).enterMarkets(cTokens);
|
2022-03-23 10:43:46 +00:00
|
|
|
|
|
|
|
// borrow dai from Compound: DAI-A
|
2022-03-23 19:41:18 +00:00
|
|
|
await cDai.connect(wallet0).borrow(parseUnits("100"));
|
2022-03-23 10:43:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("Deployment", async () => {
|
|
|
|
it("Should set correct name", async () => {
|
|
|
|
expect(await connector.name()).to.eq("Compound-Import-v2");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("checks", async () => {
|
|
|
|
it("Should check user COMPOUND position", async () => {
|
2022-03-23 19:41:18 +00:00
|
|
|
const ethExchangeRate = (await cEth.connect(wallet0).callStatic.exchangeRateCurrent()) / 1e28;
|
|
|
|
expect(new BigNumber(await cEth.connect(wallet0).balanceOf(wallet0.address)).dividedBy(1e8).toFixed(0)).to.eq(
|
2022-03-23 10:43:46 +00:00
|
|
|
new BigNumber(9).dividedBy(ethExchangeRate).toFixed(0)
|
|
|
|
);
|
2022-03-23 19:41:18 +00:00
|
|
|
expect(await Dai.connect(wallet0).balanceOf(wallet0.address)).to.eq("100000000000000000000");
|
2022-03-23 10:43:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("DSA wallet setup", async () => {
|
|
|
|
it("Should build DSA v2", async () => {
|
2022-03-23 19:41:18 +00:00
|
|
|
dsaWallet0 = await buildDSAv2(wallet0.address);
|
2022-03-23 10:43:46 +00:00
|
|
|
expect(!!dsaWallet0.address).to.be.true;
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Deposit ETH into DSA wallet", async function () {
|
2022-03-23 19:41:18 +00:00
|
|
|
await wallet0.sendTransaction({
|
2022-03-23 10:43:46 +00:00
|
|
|
to: dsaWallet0.address,
|
|
|
|
value: ethers.utils.parseEther("10")
|
|
|
|
});
|
|
|
|
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Compound position migration", async () => {
|
|
|
|
it("Should migrate Compound position", async () => {
|
2022-03-23 19:41:18 +00:00
|
|
|
const tx0 = await cEth
|
|
|
|
.connect(wallet0)
|
|
|
|
.approve(dsaWallet0.address, await cEth.connect(wallet0).balanceOf(wallet0.address));
|
|
|
|
|
|
|
|
await tx0.wait();
|
|
|
|
|
|
|
|
// const amount0 = await cDai.connect(wallet0).callStatic.borrowBalanceCurrent(wallet0.address);
|
|
|
|
const amount0 = new BigNumber("100000007061117456728");
|
|
|
|
const amount = new BigNumber(amount0.toString()).multipliedBy(5).dividedBy(1e4);
|
|
|
|
|
|
|
|
const amountWithFee = amount0.plus(amount);
|
2022-03-23 10:43:46 +00:00
|
|
|
const flashSpells = [
|
|
|
|
{
|
2022-03-23 19:41:18 +00:00
|
|
|
connector: "COMPOUND-IMPORT-X",
|
2022-03-23 10:43:46 +00:00
|
|
|
method: "importCompound",
|
2022-03-23 19:41:18 +00:00
|
|
|
args: [wallet0.address, ["ETH-A"], ["DAI-A"], [amount.toFixed(0)]]
|
2022-03-23 10:43:46 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
connector: "INSTAPOOL-C",
|
|
|
|
method: "flashPayback",
|
2022-03-23 19:41:18 +00:00
|
|
|
args: [daiAddress, amountWithFee.toFixed(0), 0, 0]
|
2022-03-23 10:43:46 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: "INSTAPOOL-C",
|
|
|
|
method: "flashBorrowAndCast",
|
2022-03-23 19:41:18 +00:00
|
|
|
args: [daiAddress, amount0.toString(), 5, encodeFlashcastData(flashSpells), "0x"]
|
2022-03-23 10:43:46 +00:00
|
|
|
}
|
|
|
|
];
|
2022-03-23 19:41:18 +00:00
|
|
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet0.address);
|
2022-03-23 10:43:46 +00:00
|
|
|
const receipt = await tx.wait();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|