2021-12-02 17:31:51 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
import hre from "hardhat";
|
|
|
|
const { web3, deployments, waffle, ethers } = hre; //check
|
2021-08-23 10:06:49 +00:00
|
|
|
const { provider, deployContract } = waffle
|
|
|
|
|
2021-12-05 19:10:22 +00:00
|
|
|
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector.js"
|
|
|
|
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"
|
|
|
|
import { encodeSpells } from "../../../scripts/tests/encodeSpells.js"
|
|
|
|
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"
|
2021-12-02 17:31:51 +00:00
|
|
|
import { addresses } from "../../../scripts/constant/addresses";
|
|
|
|
import { abis } from "../../../scripts/constant/abis";
|
2021-12-05 22:18:23 +00:00
|
|
|
import { ConnectV2BLiquity__factory } from "../../../typechain";
|
|
|
|
import type { Signer, Contract } from "ethers";
|
2021-08-23 10:06:49 +00:00
|
|
|
|
|
|
|
const LUSD_WHALE = "0x66017D22b0f8556afDd19FC67041899Eb65a21bb" // stability pool
|
|
|
|
const BAMM_ADDRESS = "0x0d3AbAA7E088C2c82f54B2f47613DA438ea8C598"
|
|
|
|
|
|
|
|
describe("B.Liquity", function () {
|
2021-09-29 05:19:30 +00:00
|
|
|
const connectorName = "B.LIQUITY-TEST-A"
|
|
|
|
|
2021-12-02 17:31:51 +00:00
|
|
|
let dsaWallet0: any;
|
|
|
|
let dsaWallet1: any
|
2021-12-05 22:18:23 +00:00
|
|
|
let masterSigner: Signer;
|
|
|
|
let instaConnectorsV2: Contract;
|
2021-12-02 17:31:51 +00:00
|
|
|
let connector: any;
|
|
|
|
let manager: any;
|
|
|
|
let vat: any;
|
|
|
|
let lusd: any;
|
|
|
|
let bammToken: any;
|
|
|
|
let stabilityPool: any;
|
2021-09-29 05:19:30 +00:00
|
|
|
|
|
|
|
const wallets = provider.getWallets()
|
|
|
|
const [wallet0, wallet1, wallet2, wallet3] = wallets
|
|
|
|
before(async () => {
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_reset",
|
|
|
|
params: [
|
|
|
|
{
|
|
|
|
forking: {
|
2021-12-05 22:18:23 +00:00
|
|
|
// @ts-ignore
|
2021-09-29 05:19:30 +00:00
|
|
|
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
|
|
|
blockNumber: 12996875,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
masterSigner = await getMasterSigner(wallet3)
|
|
|
|
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
|
|
|
|
connector = await deployAndEnableConnector({
|
|
|
|
connectorName,
|
|
|
|
contractArtifact: connectorLiquityArtifacts,
|
|
|
|
signer: masterSigner,
|
|
|
|
connectors: instaConnectorsV2
|
|
|
|
})
|
|
|
|
|
|
|
|
lusd = await ethers.getContractAt("../artifacts/contracts/mainnet/common/interfaces.sol:TokenInterface", "0x5f98805A4E8be255a32880FDeC7F6728C6568bA0")
|
|
|
|
bammToken = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:BAMMLike", BAMM_ADDRESS)
|
|
|
|
stabilityPool = await ethers.getContractAt("../artifacts/contracts/mainnet/connectors/b.protocol/liquity/interface.sol:StabilityPoolLike", "0x66017D22b0f8556afDd19FC67041899Eb65a21bb")
|
|
|
|
|
|
|
|
console.log("Connector address", connector.address)
|
2021-08-23 10:06:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it("test veryClose.", async function () {
|
|
|
|
expect(veryClose(1000001, 1000000)).to.be.true
|
2021-09-29 05:19:30 +00:00
|
|
|
expect(veryClose(1000000, 1000001)).to.be.true
|
2021-08-23 10:06:49 +00:00
|
|
|
expect(veryClose(1003000, 1000001)).to.be.false
|
2021-09-29 05:19:30 +00:00
|
|
|
expect(veryClose(1000001, 1000300)).to.be.false
|
2021-08-23 10:06:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should have contracts deployed.", async function () {
|
|
|
|
expect(!!instaConnectorsV2.address).to.be.true;
|
|
|
|
expect(!!connector.address).to.be.true;
|
|
|
|
expect(!!masterSigner.address).to.be.true;
|
|
|
|
expect(await connector.name()).to.be.equal("B.Liquity-v1");
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("DSA wallet setup", function () {
|
|
|
|
it("Should build DSA v2", async function () {
|
2021-09-29 05:19:30 +00:00
|
|
|
dsaWallet0 = await buildDSAv2(wallet0.address)
|
|
|
|
expect(!!dsaWallet0.address).to.be.true;
|
2021-08-23 10:06:49 +00:00
|
|
|
|
2021-09-29 05:19:30 +00:00
|
|
|
dsaWallet1 = await buildDSAv2(wallet1.address)
|
|
|
|
expect(!!dsaWallet1.address).to.be.true;
|
2021-08-23 10:06:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Deposit LUSD into DSA wallet", async function () {
|
2021-09-29 05:19:30 +00:00
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_impersonateAccount",
|
|
|
|
params: [LUSD_WHALE],
|
|
|
|
});
|
2021-08-23 10:06:49 +00:00
|
|
|
|
2021-09-29 05:19:30 +00:00
|
|
|
const signer = await hre.ethers.provider.getSigner(LUSD_WHALE);
|
|
|
|
await lusd.connect(signer).transfer(dsaWallet0.address, ethers.utils.parseEther("100000"))
|
2021-08-23 10:06:49 +00:00
|
|
|
|
2021-09-29 05:19:30 +00:00
|
|
|
expect(await lusd.balanceOf(dsaWallet0.address)).to.equal(ethers.utils.parseEther("100000"));
|
2021-08-23 10:06:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Main", function () {
|
|
|
|
it("should deposit 10k LUSD", async function () {
|
2021-09-29 05:19:30 +00:00
|
|
|
const totalSupplyBefore = await bammToken.totalSupply();
|
|
|
|
const lusdBalanceBefore = await stabilityPool.getCompoundedLUSDDeposit(BAMM_ADDRESS);
|
|
|
|
const amount = ethers.utils.parseEther("10000");
|
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "deposit",
|
|
|
|
args: [amount, 0, 0, 0]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
|
|
|
|
const receipt = await tx.wait()
|
|
|
|
|
|
|
|
const expectedBalance = totalSupplyBefore.mul(amount).div(lusdBalanceBefore)
|
|
|
|
expect(veryClose(expectedBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true
|
2021-08-23 10:06:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should deposit all LUSD", async function () {
|
|
|
|
const totalSupplyBefore = await bammToken.totalSupply();
|
|
|
|
const lusdBalanceBefore = await stabilityPool.getCompoundedLUSDDeposit(BAMM_ADDRESS);
|
|
|
|
const amount = web3.utils.toBN("2").pow(web3.utils.toBN("256")).sub(web3.utils.toBN("1"));
|
|
|
|
const balanceBefore = await bammToken.balanceOf(dsaWallet0.address)
|
|
|
|
|
|
|
|
const spells = [
|
2021-09-29 05:19:30 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "deposit",
|
|
|
|
args: [amount, 0, 0, 0]
|
|
|
|
}
|
2021-08-23 10:06:49 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
|
|
|
|
const receipt = await tx.wait()
|
|
|
|
|
|
|
|
const expectedBalance = (totalSupplyBefore.mul(ethers.utils.parseEther("90000")).div(lusdBalanceBefore)).add(balanceBefore)
|
|
|
|
expect(veryClose(expectedBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true
|
|
|
|
});
|
2021-09-29 05:19:30 +00:00
|
|
|
|
2021-08-23 10:06:49 +00:00
|
|
|
it("should withdraw half of the shares", async function () {
|
|
|
|
const balanceBefore = await bammToken.balanceOf(dsaWallet0.address)
|
|
|
|
const halfBalance = balanceBefore.div("2")
|
|
|
|
|
|
|
|
const spells = [
|
2021-09-29 05:19:30 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "withdraw",
|
|
|
|
args: [halfBalance, 0, 0, 0]
|
|
|
|
}
|
2021-08-23 10:06:49 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
|
|
|
|
const receipt = await tx.wait()
|
|
|
|
|
|
|
|
expect(veryClose(halfBalance, await bammToken.balanceOf(dsaWallet0.address))).to.be.true
|
2021-09-29 05:19:30 +00:00
|
|
|
expect(veryClose(ethers.utils.parseEther("50000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true
|
2021-08-23 10:06:49 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should withdraw all the shares", async function () {
|
|
|
|
const amount = web3.utils.toBN("2").pow(web3.utils.toBN("256")).sub(web3.utils.toBN("1"));
|
|
|
|
|
|
|
|
const spells = [
|
2021-09-29 05:19:30 +00:00
|
|
|
{
|
|
|
|
connector: connectorName,
|
|
|
|
method: "withdraw",
|
|
|
|
args: [amount, 0, 0, 0]
|
|
|
|
}
|
2021-08-23 10:06:49 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), wallet1.address)
|
|
|
|
const receipt = await tx.wait()
|
|
|
|
|
2021-09-29 05:19:30 +00:00
|
|
|
expect(veryClose(ethers.utils.parseEther("100000"), await lusd.balanceOf(dsaWallet0.address))).to.be.true
|
|
|
|
});
|
2021-08-23 10:06:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
function veryClose(n1, n2) {
|
2021-09-29 05:19:30 +00:00
|
|
|
n1 = web3.utils.toBN(n1)
|
|
|
|
n2 = web3.utils.toBN(n2)
|
2021-08-23 10:06:49 +00:00
|
|
|
|
2021-09-29 05:19:30 +00:00
|
|
|
_10000 = web3.utils.toBN(10000)
|
|
|
|
_9999 = web3.utils.toBN(9999)
|
2021-08-23 10:06:49 +00:00
|
|
|
|
2021-09-29 05:19:30 +00:00
|
|
|
if (n1.mul(_10000).lt(n2.mul(_9999))) return false
|
|
|
|
if (n2.mul(_10000).lt(n1.mul(_9999))) return false
|
2021-08-23 10:06:49 +00:00
|
|
|
|
2021-09-29 05:19:30 +00:00
|
|
|
return true
|
2021-08-23 10:06:49 +00:00
|
|
|
}
|