2021-12-29 10:37:54 +00:00
|
|
|
import { expect } from "chai";
|
|
|
|
import hre from "hardhat";
|
2021-12-30 10:43:37 +00:00
|
|
|
const { waffle, ethers } = hre;
|
|
|
|
const { provider } = waffle;
|
2021-12-29 10:37:54 +00:00
|
|
|
|
|
|
|
import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
|
|
|
|
import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2";
|
|
|
|
import { encodeSpells } from "../../../scripts/tests/encodeSpells";
|
|
|
|
import { getMasterSigner } from "../../../scripts/tests/getMasterSigner";
|
|
|
|
|
|
|
|
import { addresses } from "../../../scripts/tests/polygon/addresses";
|
|
|
|
import { abis } from "../../../scripts/constant/abis";
|
2021-12-30 10:43:37 +00:00
|
|
|
import type { Signer, Contract } from "ethers";
|
2021-12-29 10:37:54 +00:00
|
|
|
|
2021-12-30 10:43:37 +00:00
|
|
|
import { ConnectV2PmStable__factory, IERC20Minimal__factory, IERC20Minimal } from "../../../typechain";
|
2021-12-29 10:37:54 +00:00
|
|
|
|
2021-12-30 09:21:54 +00:00
|
|
|
import { executeAndAssertDeposit, executeAndAssertSwap, executeAndAssertWithdraw } from "./mstable.utils";
|
|
|
|
|
2021-12-30 05:05:46 +00:00
|
|
|
import {
|
|
|
|
fundWallet,
|
|
|
|
getToken,
|
|
|
|
simpleToExactAmount,
|
|
|
|
DEAD_ADDRESS,
|
|
|
|
calcMinOut,
|
|
|
|
ONE_DAY,
|
2021-12-30 09:21:54 +00:00
|
|
|
increaseTime,
|
|
|
|
connectorName,
|
|
|
|
toEther
|
2021-12-30 05:05:46 +00:00
|
|
|
} from "./mstable.helpers";
|
2021-12-29 10:37:54 +00:00
|
|
|
|
|
|
|
describe("MStable", async () => {
|
|
|
|
let dsaWallet0: Contract;
|
|
|
|
let masterSigner: Signer;
|
|
|
|
let instaConnectorsV2: Contract;
|
|
|
|
let connector: Contract;
|
|
|
|
|
2021-12-29 13:06:43 +00:00
|
|
|
let mtaToken: IERC20Minimal = IERC20Minimal__factory.connect(getToken("MTA").tokenAddress, provider);
|
2021-12-29 10:37:54 +00:00
|
|
|
let mUsdToken: IERC20Minimal = IERC20Minimal__factory.connect(getToken("mUSD").tokenAddress, provider);
|
|
|
|
let imUsdToken: IERC20Minimal = IERC20Minimal__factory.connect(getToken("imUSD").tokenAddress, provider);
|
|
|
|
let imUsdVault: IERC20Minimal = IERC20Minimal__factory.connect(getToken("imUSDVault").tokenAddress, provider);
|
|
|
|
|
2021-12-30 09:21:54 +00:00
|
|
|
let daiToken: IERC20Minimal = IERC20Minimal__factory.connect(getToken("DAI").tokenAddress, provider);
|
|
|
|
let usdcToken: IERC20Minimal = IERC20Minimal__factory.connect(getToken("USDC").tokenAddress, provider);
|
|
|
|
let fraxToken: IERC20Minimal = IERC20Minimal__factory.connect(getToken("FRAX").tokenAddress, provider);
|
|
|
|
|
2021-12-29 10:37:54 +00:00
|
|
|
const wallets = provider.getWallets();
|
|
|
|
const [wallet0, wallet1, wallet2, wallet3] = wallets;
|
|
|
|
|
2021-12-30 09:21:54 +00:00
|
|
|
describe("DSA wallet", async () => {
|
|
|
|
const fundAmount = simpleToExactAmount(10000);
|
|
|
|
|
|
|
|
const setup = async () => {
|
|
|
|
await hre.network.provider.request({
|
|
|
|
method: "hardhat_reset",
|
|
|
|
params: [
|
|
|
|
{
|
|
|
|
forking: {
|
|
|
|
// @ts-ignore
|
|
|
|
jsonRpcUrl: hre.config.networks.hardhat.forking.url,
|
|
|
|
blockNumber: 23059414
|
|
|
|
}
|
2021-12-29 10:37:54 +00:00
|
|
|
}
|
2021-12-30 09:21:54 +00:00
|
|
|
]
|
|
|
|
});
|
2021-12-29 10:37:54 +00:00
|
|
|
|
2021-12-30 09:21:54 +00:00
|
|
|
masterSigner = await getMasterSigner();
|
|
|
|
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
|
|
|
|
connector = await deployAndEnableConnector({
|
|
|
|
connectorName,
|
2021-12-30 10:43:37 +00:00
|
|
|
contractArtifact: ConnectV2PmStable__factory,
|
2021-12-30 09:21:54 +00:00
|
|
|
signer: masterSigner,
|
|
|
|
connectors: instaConnectorsV2
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log("Connector address", connector.address);
|
2021-12-29 10:37:54 +00:00
|
|
|
|
|
|
|
dsaWallet0 = await buildDSAv2(wallet0.address);
|
2021-12-30 09:21:54 +00:00
|
|
|
|
2021-12-29 10:37:54 +00:00
|
|
|
await wallet0.sendTransaction({
|
|
|
|
to: dsaWallet0.address,
|
|
|
|
value: simpleToExactAmount(10)
|
|
|
|
});
|
|
|
|
|
|
|
|
await fundWallet("mUSD", fundAmount, dsaWallet0.address);
|
|
|
|
await fundWallet("DAI", fundAmount, dsaWallet0.address);
|
|
|
|
await fundWallet("FRAX", fundAmount, dsaWallet0.address);
|
2021-12-30 09:21:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
describe("Deploy", async () => {
|
|
|
|
before(async () => {
|
|
|
|
await setup();
|
|
|
|
});
|
2021-12-29 10:37:54 +00:00
|
|
|
|
2021-12-30 09:21:54 +00:00
|
|
|
it("Should deploy properly", async () => {
|
|
|
|
expect(instaConnectorsV2.address).to.be.properAddress;
|
|
|
|
expect(connector.address).to.be.properAddress;
|
|
|
|
expect(await masterSigner.getAddress()).to.be.properAddress;
|
2021-12-29 10:37:54 +00:00
|
|
|
|
2021-12-30 09:21:54 +00:00
|
|
|
expect(dsaWallet0.address).to.be.properAddress;
|
|
|
|
});
|
|
|
|
it("Should fund the wallet", async () => {
|
|
|
|
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
|
|
|
|
|
|
|
|
expect(await mUsdToken.balanceOf(dsaWallet0.address)).to.be.gte(fundAmount);
|
|
|
|
expect(await daiToken.balanceOf(dsaWallet0.address)).to.be.gte(fundAmount);
|
|
|
|
expect(await fraxToken.balanceOf(dsaWallet0.address)).to.be.gte(fundAmount);
|
|
|
|
});
|
|
|
|
it("Should not have vault tokens prior", async () => {
|
|
|
|
// No deposits prior
|
|
|
|
expect(await imUsdToken.balanceOf(dsaWallet0.address)).to.be.eq(0);
|
|
|
|
expect(await imUsdVault.balanceOf(dsaWallet0.address)).to.be.eq(0);
|
|
|
|
});
|
2021-12-29 10:37:54 +00:00
|
|
|
});
|
|
|
|
|
2021-12-30 09:21:54 +00:00
|
|
|
describe("Main SAVE", async () => {
|
|
|
|
before(async () => {
|
|
|
|
await setup();
|
|
|
|
});
|
2021-12-29 10:37:54 +00:00
|
|
|
it("Should deposit mUSD to Vault successfully", async () => {
|
|
|
|
const depositAmount = simpleToExactAmount(100);
|
2022-01-02 08:28:40 +00:00
|
|
|
const minOut = depositAmount;
|
2021-12-29 10:37:54 +00:00
|
|
|
|
2022-01-27 12:34:47 +00:00
|
|
|
await executeAndAssertDeposit("deposit", mUsdToken, depositAmount, dsaWallet0, wallet0, [minOut, true]);
|
2021-12-29 10:37:54 +00:00
|
|
|
});
|
|
|
|
it("Should deposit DAI to Vault successfully (mUSD bAsset)", async () => {
|
|
|
|
const depositAmount = simpleToExactAmount(100);
|
|
|
|
const minOut = calcMinOut(depositAmount, 0.02);
|
|
|
|
|
2022-01-27 12:34:47 +00:00
|
|
|
await executeAndAssertDeposit("deposit", daiToken, depositAmount, dsaWallet0, wallet0, [minOut, true]);
|
2021-12-29 10:37:54 +00:00
|
|
|
});
|
2021-12-29 13:06:43 +00:00
|
|
|
it("Should deposit FRAX to Vault successfully (via Feeder Pool)", async () => {
|
2021-12-29 10:37:54 +00:00
|
|
|
const depositAmount = simpleToExactAmount(100);
|
|
|
|
const minOut = calcMinOut(depositAmount, 0.02);
|
2021-12-30 09:21:54 +00:00
|
|
|
const path = getToken("FRAX").feederPool;
|
2021-12-29 10:37:54 +00:00
|
|
|
|
2022-01-27 12:34:47 +00:00
|
|
|
await executeAndAssertDeposit("depositViaSwap", fraxToken, depositAmount, dsaWallet0, wallet0, [
|
|
|
|
minOut,
|
|
|
|
path,
|
|
|
|
true
|
|
|
|
]);
|
2021-12-29 10:37:54 +00:00
|
|
|
});
|
2021-12-29 13:06:43 +00:00
|
|
|
it("Should withdraw from Vault to mUSD", async () => {
|
|
|
|
const withdrawAmount = simpleToExactAmount(100);
|
2022-01-02 08:28:40 +00:00
|
|
|
const minOut = simpleToExactAmount(1);
|
2021-12-29 13:06:43 +00:00
|
|
|
|
2022-01-27 12:34:47 +00:00
|
|
|
await executeAndAssertWithdraw("withdraw", mUsdToken, withdrawAmount, dsaWallet0, wallet0, [minOut, true]);
|
2021-12-29 13:06:43 +00:00
|
|
|
});
|
|
|
|
it("Should withdraw from Vault to DAI (mUSD bAsset)", async () => {
|
|
|
|
const withdrawAmount = simpleToExactAmount(100);
|
|
|
|
const minOut = simpleToExactAmount(1);
|
|
|
|
|
2022-01-27 12:34:47 +00:00
|
|
|
await executeAndAssertWithdraw("withdraw", mUsdToken, withdrawAmount, dsaWallet0, wallet0, [minOut, true]);
|
2021-12-29 13:06:43 +00:00
|
|
|
});
|
|
|
|
it("Should withdraw from Vault to FRAX (via Feeder Pool)", async () => {
|
|
|
|
const withdrawAmount = simpleToExactAmount(100);
|
|
|
|
const minOut = simpleToExactAmount(1);
|
2022-01-02 08:28:40 +00:00
|
|
|
const path = getToken("FRAX").feederPool;
|
2021-12-29 13:06:43 +00:00
|
|
|
|
2022-01-02 08:28:40 +00:00
|
|
|
await executeAndAssertWithdraw("withdrawViaSwap", fraxToken, withdrawAmount, dsaWallet0, wallet0, [
|
|
|
|
minOut,
|
2022-01-27 12:34:47 +00:00
|
|
|
path,
|
|
|
|
true
|
2022-01-02 08:28:40 +00:00
|
|
|
]);
|
2021-12-29 13:06:43 +00:00
|
|
|
});
|
|
|
|
it("Should claim Rewards", async () => {
|
|
|
|
const mtaBalanceBefore = await mtaToken.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("MTA balance before: ", toEther(mtaBalanceBefore));
|
|
|
|
|
2021-12-30 05:05:46 +00:00
|
|
|
// Wait a bit and let the rewards accumulate
|
|
|
|
await increaseTime(ONE_DAY);
|
2021-12-29 13:06:43 +00:00
|
|
|
|
|
|
|
const spells = [
|
|
|
|
{
|
|
|
|
connector: connectorName,
|
2021-12-30 05:05:46 +00:00
|
|
|
method: "claimRewards",
|
2022-01-27 12:34:47 +00:00
|
|
|
args: [0, 0]
|
2021-12-29 13:06:43 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const tx = await dsaWallet0.connect(wallet0).cast(...encodeSpells(spells), DEAD_ADDRESS);
|
|
|
|
|
|
|
|
const mtaBalanceAfter = await mtaToken.balanceOf(dsaWallet0.address);
|
|
|
|
console.log("MTA balance after: ", toEther(mtaBalanceAfter));
|
|
|
|
|
|
|
|
expect(mtaBalanceAfter).to.be.gt(mtaBalanceBefore);
|
|
|
|
});
|
2021-12-29 10:37:54 +00:00
|
|
|
});
|
2021-12-30 09:21:54 +00:00
|
|
|
describe("Main SWAP", async () => {
|
|
|
|
before(async () => {
|
|
|
|
await setup();
|
|
|
|
});
|
|
|
|
it("Should swap mUSD to bAsset (redeem)", async () => {
|
|
|
|
const swapAmount = simpleToExactAmount(100);
|
|
|
|
await executeAndAssertSwap("swap", mUsdToken, 18, daiToken, 18, swapAmount, dsaWallet0, wallet0);
|
|
|
|
});
|
|
|
|
it("Should swap mUSD to fAsset (via feeder pool)", async () => {
|
|
|
|
const swapAmount = simpleToExactAmount(100);
|
|
|
|
const path = getToken("FRAX").feederPool;
|
|
|
|
await executeAndAssertSwap("swapViaFeeder", mUsdToken, 18, fraxToken, 18, swapAmount, dsaWallet0, wallet0, [
|
|
|
|
path
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it("Should swap bAsset to mUSD (mint)", async () => {
|
|
|
|
const swapAmount = simpleToExactAmount(100);
|
|
|
|
await executeAndAssertSwap("swap", daiToken, 18, mUsdToken, 18, swapAmount, dsaWallet0, wallet0);
|
|
|
|
});
|
|
|
|
it("Should swap bAsset to bAsset (swap)", async () => {
|
|
|
|
const swapAmount = simpleToExactAmount(100);
|
|
|
|
await executeAndAssertSwap("swap", daiToken, 18, usdcToken, 6, swapAmount, dsaWallet0, wallet0);
|
|
|
|
});
|
|
|
|
it("Should swap bAsset to fAsset (via feeder)", async () => {
|
|
|
|
const swapAmount = simpleToExactAmount(100);
|
|
|
|
const path = getToken("FRAX").feederPool;
|
|
|
|
await executeAndAssertSwap("swapViaFeeder", daiToken, 18, fraxToken, 18, swapAmount, dsaWallet0, wallet0, [
|
|
|
|
path
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it("Should swap fAsset to bAsset (via feeder)", async () => {
|
|
|
|
const swapAmount = simpleToExactAmount(100);
|
|
|
|
const path = getToken("FRAX").feederPool;
|
|
|
|
await executeAndAssertSwap("swapViaFeeder", fraxToken, 18, daiToken, 18, swapAmount, dsaWallet0, wallet0, [
|
|
|
|
path
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
it("Should swap fAsset to mUSD (via feeder)", async () => {
|
|
|
|
const swapAmount = simpleToExactAmount(100);
|
|
|
|
const path = getToken("FRAX").feederPool;
|
|
|
|
await executeAndAssertSwap("swapViaFeeder", fraxToken, 18, mUsdToken, 18, swapAmount, dsaWallet0, wallet0, [
|
|
|
|
path
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2021-12-29 10:37:54 +00:00
|
|
|
});
|
|
|
|
});
|