Migrating tests to typescript

This commit is contained in:
Tianjie Wei 2022-02-02 10:22:30 -08:00
parent f5ac85692e
commit ffbdafbbc2
3 changed files with 144 additions and 80 deletions

View File

@ -98,7 +98,7 @@ const ERC20_TOKEN_ABI = [
"function approve(address spender, uint256 amount) external returns (bool)", "function approve(address spender, uint256 amount) external returns (bool)",
]; ];
module.exports = { export default {
NOTIONAL_CONTRACT_ADDRESS, NOTIONAL_CONTRACT_ADDRESS,
NOTIONAL_CONTRACT_ABI, NOTIONAL_CONTRACT_ABI,
WETH_TOKEN_ADDRESS, WETH_TOKEN_ADDRESS,

View File

@ -1,6 +1,14 @@
const encodeSpells = require("../../scripts/encodeSpells.js") import { BigNumber } from "ethers";
import { encodeSpells } from "../../../scripts/tests/encodeSpells"
const depositCollteral = async (dsa, authority, referrer, currencyId, amount, underlying) => { const depositCollteral = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
amount: BigNumber,
underlying: boolean
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
@ -13,7 +21,14 @@ const depositCollteral = async (dsa, authority, referrer, currencyId, amount, un
await tx.wait() await tx.wait()
}; };
const depositAndMintNToken = async (dsa, authority, referrer, currencyId, amount, underlying) => { const depositAndMintNToken = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
amount: BigNumber,
underlying: boolean
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
@ -26,12 +41,21 @@ const depositAndMintNToken = async (dsa, authority, referrer, currencyId, amount
await tx.wait() await tx.wait()
} }
const depositAndLend = async (dsa, authority, referrer, currencyId, underlying, amount, market, fcash, minRate) => { const depositAndLend = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
underlying: boolean,
amount: BigNumber,
market: number,
fcash: BigNumber
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
method: "depositAndLend", method: "depositAndLend",
args: [currencyId, amount, underlying, market, fcash, minRate, 0] args: [currencyId, amount, underlying, market, fcash, 0, 0]
} }
]; ];
@ -39,7 +63,14 @@ const depositAndLend = async (dsa, authority, referrer, currencyId, underlying,
await tx.wait() await tx.wait()
}; };
const withdrawCollateral = async (dsa, authority, referrer, currencyId, amount, underlying) => { const withdrawCollateral = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
amount: BigNumber,
underlying: boolean
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
@ -52,7 +83,14 @@ const withdrawCollateral = async (dsa, authority, referrer, currencyId, amount,
await tx.wait() await tx.wait()
}; };
const redeemNTokenRaw = async (dsa, authority, referrer, currencyId, sellTokenAssets, tokensToRedeem) => { const redeemNTokenRaw = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
sellTokenAssets: boolean,
tokensToRedeem: BigNumber
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
@ -65,7 +103,15 @@ const redeemNTokenRaw = async (dsa, authority, referrer, currencyId, sellTokenAs
await tx.wait() await tx.wait()
}; };
const redeemNTokenAndWithdraw = async (dsa, authority, referrer, currencyId, tokensToRedeem, amountToWithdraw, redeemToUnderlying) => { const redeemNTokenAndWithdraw = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
tokensToRedeem: BigNumber,
amountToWithdraw: BigNumber,
redeemToUnderlying: boolean
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
@ -78,12 +124,20 @@ const redeemNTokenAndWithdraw = async (dsa, authority, referrer, currencyId, tok
await tx.wait() await tx.wait()
}; };
const redeemNTokenAndDeleverage = async (dsa, authority, referrer, currencyId, tokensToRedeem, marketIndex, fCashAmount, minLendRate) => { const redeemNTokenAndDeleverage = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
tokensToRedeem: BigNumber,
marketIndex: number,
fCashAmount: BigNumber
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
method: "redeemNTokenAndDeleverage", method: "redeemNTokenAndDeleverage",
args: [currencyId, tokensToRedeem, marketIndex, fCashAmount, minLendRate, 0] args: [currencyId, tokensToRedeem, marketIndex, fCashAmount, 0, 0]
} }
]; ];
@ -92,17 +146,16 @@ const redeemNTokenAndDeleverage = async (dsa, authority, referrer, currencyId, t
}; };
const depositCollateralBorrowAndWithdraw = async ( const depositCollateralBorrowAndWithdraw = async (
dsa, dsa: any,
authority, authority: any,
referrer, referrer: any,
depositCurrencyId, depositCurrencyId: number,
depositUnderlying, depositType: number,
depositAmount, depositAmount: BigNumber,
borrowCurrencyId, borrowCurrencyId: number,
marketIndex, marketIndex: number,
fCashAmount, fCashAmount: BigNumber,
maxBorrowRate, redeedmUnderlying: boolean
redeedmUnderlying
) => { ) => {
const spells = [ const spells = [
{ {
@ -110,12 +163,12 @@ const depositCollateralBorrowAndWithdraw = async (
method: "depositCollateralBorrowAndWithdraw", method: "depositCollateralBorrowAndWithdraw",
args: [ args: [
depositCurrencyId, depositCurrencyId,
depositUnderlying, depositType,
depositAmount, depositAmount,
borrowCurrencyId, borrowCurrencyId,
marketIndex, marketIndex,
fCashAmount, fCashAmount,
maxBorrowRate, 0,
redeedmUnderlying, redeedmUnderlying,
0, 0,
0 0
@ -127,12 +180,19 @@ const depositCollateralBorrowAndWithdraw = async (
await tx.wait() await tx.wait()
}; };
const withdrawLend = async (dsa, authority, referrer, currencyId, marketIndex, fCashAmount, maxBorrowRate) => { const withdrawLend = async (
dsa: any,
authority: any,
referrer: any,
currencyId: number,
marketIndex: number,
fCashAmount: BigNumber
) => {
const spells = [ const spells = [
{ {
connector: "NOTIONAL-TEST-A", connector: "NOTIONAL-TEST-A",
method: "withdrawLend", method: "withdrawLend",
args: [currencyId, marketIndex, fCashAmount, maxBorrowRate, 0] args: [currencyId, marketIndex, fCashAmount, 0, 0]
} }
]; ];
@ -140,7 +200,7 @@ const withdrawLend = async (dsa, authority, referrer, currencyId, marketIndex, f
await tx.wait() await tx.wait()
}; };
module.exports = { export default {
depositCollteral, depositCollteral,
depositAndMintNToken, depositAndMintNToken,
depositAndLend, depositAndLend,

View File

@ -1,23 +1,18 @@
const { expect } = require("chai"); import { expect } from "chai";
const hre = require("hardhat"); import hre from "hardhat";
const { web3, deployments, waffle, ethers } = hre; const { waffle, ethers } = hre;
const { provider, deployContract } = waffle const { provider, deployContract } = waffle
const deployAndEnableConnector = require("../../scripts/deployAndEnableConnector.js") import { deployAndEnableConnector } from "../../../scripts/tests/deployAndEnableConnector";
const buildDSAv2 = require("../../scripts/buildDSAv2") import { buildDSAv2 } from "../../../scripts/tests/buildDSAv2"
const encodeSpells = require("../../scripts/encodeSpells.js") import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"
const getMasterSigner = require("../../scripts/getMasterSigner") import { addresses } from "../../../scripts/tests/mainnet/addresses";
import { abis } from "../../../scripts/constant/abis";
import contracts from "./notional.contracts";
import helpers from "./notional.helpers";
const addresses = require("../../scripts/constant/addresses"); import { ConnectV2Notional__factory } from "../../../typechain";
const abis = require("../../scripts/constant/abis"); import { BigNumber } from "ethers";
const constants = require("../../scripts/constant/constant");
const tokens = require("../../scripts/constant/tokens");
const contracts = require("./notional.contracts");
const helpers = require("./notional.helpers");
const connectV2NotionalArtifacts = require("../../artifacts/contracts/mainnet/connectors/notional/main.sol/ConnectV2Notional.json");
const { BigNumber } = require("ethers");
const DAI_WHALE = "0x6dfaf865a93d3b0b5cfd1b4db192d1505676645b"; const DAI_WHALE = "0x6dfaf865a93d3b0b5cfd1b4db192d1505676645b";
const CDAI_WHALE = "0x33b890d6574172e93e58528cd99123a88c0756e9"; const CDAI_WHALE = "0x33b890d6574172e93e58528cd99123a88c0756e9";
@ -35,17 +30,17 @@ const MARKET_3M = 1;
describe("Notional", function () { describe("Notional", function () {
const connectorName = "NOTIONAL-TEST-A" const connectorName = "NOTIONAL-TEST-A"
let dsaWallet0 let dsaWallet0: any;
let masterSigner; let masterSigner: any;
let instaConnectorsV2; let instaConnectorsV2: any;
let connector; let connector: any;
let notional; let notional: any;
let daiToken; let daiToken: any;
let cdaiToken; let cdaiToken: any;
let cethToken; let cethToken: any;
let daiWhale; let daiWhale: any;
let cdaiWhale; let cdaiWhale: any;
let cethWhale; let cethWhale: any;
const wallets = provider.getWallets() const wallets = provider.getWallets()
const [wallet0, wallet1, wallet2, wallet3] = wallets const [wallet0, wallet1, wallet2, wallet3] = wallets
@ -55,6 +50,7 @@ describe("Notional", function () {
params: [ params: [
{ {
forking: { forking: {
//@ts-ignore
jsonRpcUrl: hre.config.networks.hardhat.forking.url, jsonRpcUrl: hre.config.networks.hardhat.forking.url,
blockNumber: 13798624, blockNumber: 13798624,
}, },
@ -78,11 +74,11 @@ describe("Notional", function () {
params: [CETH_WHALE] params: [CETH_WHALE]
}) })
masterSigner = await getMasterSigner(wallet3) masterSigner = await getMasterSigner()
instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2);
connector = await deployAndEnableConnector({ connector = await deployAndEnableConnector({
connectorName, connectorName,
contractArtifact: connectV2NotionalArtifacts, contractArtifact: ConnectV2Notional__factory,
signer: masterSigner, signer: masterSigner,
connectors: instaConnectorsV2 connectors: instaConnectorsV2
}) })
@ -109,11 +105,6 @@ describe("Notional", function () {
ethers.provider ethers.provider
); );
cethWhale = await ethers.getSigner(CETH_WHALE); cethWhale = await ethers.getSigner(CETH_WHALE);
weth = new ethers.Contract(
contracts.WETH_TOKEN_ADDRESS,
contracts.ERC20_TOKEN_ABI,
ethers.provider
);
dsaWallet0 = await buildDSAv2(wallet0.address) dsaWallet0 = await buildDSAv2(wallet0.address)
}); });
@ -181,7 +172,8 @@ describe("Notional", function () {
value: ethers.utils.parseEther("10") value: ethers.utils.parseEther("10")
}); });
const depositAmount = ethers.utils.parseEther("10"); const depositAmount = ethers.utils.parseEther("10");
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, ETH_ID, true, depositAmount, MARKET_3M, 9e8, 0); const fcash = ethers.utils.parseUnits("9", 8);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, ETH_ID, true, depositAmount, MARKET_3M, fcash);
const portfolio = await notional.getAccountPortfolio(dsaWallet0.address); const portfolio = await notional.getAccountPortfolio(dsaWallet0.address);
expect(portfolio.length, "expect 1 lending position").to.be.equal(1); expect(portfolio.length, "expect 1 lending position").to.be.equal(1);
expect(portfolio[0][3], "expect 9 fETH").to.be.gte(ethers.utils.parseUnits("900000000", 0)); expect(portfolio[0][3], "expect 9 fETH").to.be.gte(ethers.utils.parseUnits("900000000", 0));
@ -190,7 +182,8 @@ describe("Notional", function () {
it("test_deposit_ETH_asset_and_lend", async function () { it("test_deposit_ETH_asset_and_lend", async function () {
const depositAmount = ethers.utils.parseUnits("1", 8); const depositAmount = ethers.utils.parseUnits("1", 8);
await cethToken.connect(cethWhale).transfer(dsaWallet0.address, depositAmount); await cethToken.connect(cethWhale).transfer(dsaWallet0.address, depositAmount);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, ETH_ID, false, depositAmount, MARKET_3M, 0.01e8, 0); const fcash = ethers.utils.parseUnits("0.01", 8);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, ETH_ID, false, depositAmount, MARKET_3M, fcash);
const portfolio = await notional.getAccountPortfolio(dsaWallet0.address); const portfolio = await notional.getAccountPortfolio(dsaWallet0.address);
expect(portfolio.length, "expect 1 lending position").to.be.equal(1); expect(portfolio.length, "expect 1 lending position").to.be.equal(1);
expect(portfolio[0][3], "expect 0.01 fETH").to.be.gte(ethers.utils.parseUnits("1000000", 0)); expect(portfolio[0][3], "expect 0.01 fETH").to.be.gte(ethers.utils.parseUnits("1000000", 0));
@ -199,7 +192,8 @@ describe("Notional", function () {
it("test_deposit_DAI_underlying_and_lend", async function () { it("test_deposit_DAI_underlying_and_lend", async function () {
const depositAmount = ethers.utils.parseUnits("1000", 18); const depositAmount = ethers.utils.parseUnits("1000", 18);
await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount); await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, DAI_ID, true, depositAmount, MARKET_3M, 100e8, 0); const fcash = ethers.utils.parseUnits("100", 8);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, DAI_ID, true, depositAmount, MARKET_3M, fcash);
const portfolio = await notional.getAccountPortfolio(dsaWallet0.address); const portfolio = await notional.getAccountPortfolio(dsaWallet0.address);
expect(portfolio.length, "expect 1 lending position").to.be.equal(1); expect(portfolio.length, "expect 1 lending position").to.be.equal(1);
expect(portfolio[0][3], "expect 100 fDAI").to.be.gte(ethers.utils.parseUnits("10000000000", 0)); expect(portfolio[0][3], "expect 100 fDAI").to.be.gte(ethers.utils.parseUnits("10000000000", 0));
@ -208,7 +202,8 @@ describe("Notional", function () {
it("test_deposit_DAI_asset_and_lend", async function () { it("test_deposit_DAI_asset_and_lend", async function () {
const depositAmount = ethers.utils.parseUnits("1000", 8); const depositAmount = ethers.utils.parseUnits("1000", 8);
await cdaiToken.connect(cdaiWhale).transfer(dsaWallet0.address, depositAmount); await cdaiToken.connect(cdaiWhale).transfer(dsaWallet0.address, depositAmount);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, DAI_ID, false, depositAmount, MARKET_3M, 10e8, 0); const fcash = ethers.utils.parseUnits("10", 8);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, DAI_ID, false, depositAmount, MARKET_3M, fcash);
const portfolio = await notional.getAccountPortfolio(dsaWallet0.address); const portfolio = await notional.getAccountPortfolio(dsaWallet0.address);
expect(portfolio.length, "expect 1 lending position").to.be.equal(1); expect(portfolio.length, "expect 1 lending position").to.be.equal(1);
expect(portfolio[0][3], "expect 10 fDAI").to.be.gte(ethers.utils.parseUnits("1000000000", 0)); expect(portfolio[0][3], "expect 10 fDAI").to.be.gte(ethers.utils.parseUnits("1000000000", 0));
@ -220,11 +215,12 @@ describe("Notional", function () {
value: ethers.utils.parseEther("10") value: ethers.utils.parseEther("10")
}); });
const depositAmount = ethers.utils.parseEther("10"); const depositAmount = ethers.utils.parseEther("10");
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, ETH_ID, true, depositAmount, MARKET_3M, 9e8, 0); const fcash = ethers.utils.parseUnits("9", 8);
await helpers.depositAndLend(dsaWallet0, wallet0, wallet1, ETH_ID, true, depositAmount, MARKET_3M, fcash);
const before = await notional.getAccountPortfolio(dsaWallet0.address); const before = await notional.getAccountPortfolio(dsaWallet0.address);
expect(before.length, "expect 1 lending position").to.be.equal(1); expect(before.length, "expect 1 lending position").to.be.equal(1);
expect(before[0][3], "expect 9 fETH").to.be.gte(ethers.utils.parseUnits("900000000", 0)); expect(before[0][3], "expect 9 fETH").to.be.gte(ethers.utils.parseUnits("900000000", 0));
await helpers.withdrawLend(dsaWallet0, wallet0, wallet1, ETH_ID, MARKET_3M, 9e8, 0); await helpers.withdrawLend(dsaWallet0, wallet0, wallet1, ETH_ID, MARKET_3M, fcash);
const after = await notional.getAccountPortfolio(dsaWallet0.address); const after = await notional.getAccountPortfolio(dsaWallet0.address);
expect(after.length, "expect lending position to be closed out").to.be.equal(0); expect(after.length, "expect lending position to be closed out").to.be.equal(0);
}); });
@ -237,8 +233,9 @@ describe("Notional", function () {
value: ethers.utils.parseEther("10") value: ethers.utils.parseEther("10")
}); });
const depositAmount = ethers.utils.parseEther("10"); const depositAmount = ethers.utils.parseEther("10");
const fcash = ethers.utils.parseUnits("1000", 8);
await helpers.depositCollateralBorrowAndWithdraw( await helpers.depositCollateralBorrowAndWithdraw(
dsaWallet0, wallet0, wallet1, ETH_ID, DEPOSIT_UNDERLYING, depositAmount, DAI_ID, MARKET_3M, 1000e8, 0, true dsaWallet0, wallet0, wallet1, ETH_ID, DEPOSIT_UNDERLYING, depositAmount, DAI_ID, MARKET_3M, fcash, true
); );
expect( expect(
await daiToken.balanceOf(dsaWallet0.address), await daiToken.balanceOf(dsaWallet0.address),
@ -252,8 +249,9 @@ describe("Notional", function () {
value: ethers.utils.parseEther("10") value: ethers.utils.parseEther("10")
}); });
const depositAmount = ethers.utils.parseEther("10"); const depositAmount = ethers.utils.parseEther("10");
const fcash = ethers.utils.parseUnits("1000", 8);
await helpers.depositCollateralBorrowAndWithdraw( await helpers.depositCollateralBorrowAndWithdraw(
dsaWallet0, wallet0, wallet1, ETH_ID, DEPOSIT_UNDERLYING, depositAmount, DAI_ID, MARKET_3M, 1000e8, 0, false dsaWallet0, wallet0, wallet1, ETH_ID, DEPOSIT_UNDERLYING, depositAmount, DAI_ID, MARKET_3M, fcash, false
); );
expect( expect(
await cdaiToken.balanceOf(dsaWallet0.address), await cdaiToken.balanceOf(dsaWallet0.address),
@ -264,8 +262,9 @@ describe("Notional", function () {
it("test_deposit_DAI_underlying_and_borrow_ETH", async function () { it("test_deposit_DAI_underlying_and_borrow_ETH", async function () {
const depositAmount = ethers.utils.parseUnits("20000", 18); const depositAmount = ethers.utils.parseUnits("20000", 18);
await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount); await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount);
const fcash = ethers.utils.parseUnits("1", 8);
await helpers.depositCollateralBorrowAndWithdraw( await helpers.depositCollateralBorrowAndWithdraw(
dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_UNDERLYING, depositAmount, ETH_ID, MARKET_3M, 1e8, 0, true dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_UNDERLYING, depositAmount, ETH_ID, MARKET_3M, fcash, true
); );
expect( expect(
await ethers.provider.getBalance(dsaWallet0.address), await ethers.provider.getBalance(dsaWallet0.address),
@ -276,8 +275,9 @@ describe("Notional", function () {
it("test_deposit_DAI_asset_and_borrow_ETH", async function () { it("test_deposit_DAI_asset_and_borrow_ETH", async function () {
const depositAmount = ethers.utils.parseUnits("1000000", 8); const depositAmount = ethers.utils.parseUnits("1000000", 8);
await cdaiToken.connect(cdaiWhale).transfer(dsaWallet0.address, depositAmount); await cdaiToken.connect(cdaiWhale).transfer(dsaWallet0.address, depositAmount);
const fcash = ethers.utils.parseUnits("1", 8);
await helpers.depositCollateralBorrowAndWithdraw( await helpers.depositCollateralBorrowAndWithdraw(
dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_ASSET, depositAmount, ETH_ID, MARKET_3M, 1e8, 0, true dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_ASSET, depositAmount, ETH_ID, MARKET_3M, fcash, true
); );
expect( expect(
await ethers.provider.getBalance(dsaWallet0.address), await ethers.provider.getBalance(dsaWallet0.address),
@ -288,8 +288,9 @@ describe("Notional", function () {
it("test_mint_nDAI_underlying_and_borrow_ETH", async function () { it("test_mint_nDAI_underlying_and_borrow_ETH", async function () {
const depositAmount = ethers.utils.parseUnits("20000", 18); const depositAmount = ethers.utils.parseUnits("20000", 18);
await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount); await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount);
const fcash = ethers.utils.parseUnits("1", 8);
await helpers.depositCollateralBorrowAndWithdraw( await helpers.depositCollateralBorrowAndWithdraw(
dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_UNDERLYING_MINT_NTOKEN, depositAmount, ETH_ID, MARKET_3M, 1e8, 0, true dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_UNDERLYING_MINT_NTOKEN, depositAmount, ETH_ID, MARKET_3M, fcash, true
); );
expect( expect(
await ethers.provider.getBalance(dsaWallet0.address), await ethers.provider.getBalance(dsaWallet0.address),
@ -300,8 +301,9 @@ describe("Notional", function () {
it("test_mint_nDAI_asset_and_borrow_ETH", async function () { it("test_mint_nDAI_asset_and_borrow_ETH", async function () {
const depositAmount = ethers.utils.parseUnits("1000000", 8); const depositAmount = ethers.utils.parseUnits("1000000", 8);
await cdaiToken.connect(cdaiWhale).transfer(dsaWallet0.address, depositAmount); await cdaiToken.connect(cdaiWhale).transfer(dsaWallet0.address, depositAmount);
const fcash = ethers.utils.parseUnits("1", 8);
await helpers.depositCollateralBorrowAndWithdraw( await helpers.depositCollateralBorrowAndWithdraw(
dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_ASSET_MINT_NTOKEN, depositAmount, ETH_ID, MARKET_3M, 1e8, 0, true dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_ASSET_MINT_NTOKEN, depositAmount, ETH_ID, MARKET_3M, fcash, true
); );
expect( expect(
await ethers.provider.getBalance(dsaWallet0.address), await ethers.provider.getBalance(dsaWallet0.address),
@ -377,15 +379,17 @@ describe("Notional", function () {
it("test_redeem_DAI_and_deleverage", async function () { it("test_redeem_DAI_and_deleverage", async function () {
const depositAmount = ethers.utils.parseUnits("20000", 18); const depositAmount = ethers.utils.parseUnits("20000", 18);
await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount); await daiToken.connect(daiWhale).transfer(dsaWallet0.address, depositAmount);
const fcash = ethers.utils.parseUnits("1", 8);
await helpers.depositCollateralBorrowAndWithdraw( await helpers.depositCollateralBorrowAndWithdraw(
dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_UNDERLYING, depositAmount, ETH_ID, MARKET_3M, 1e8, 0, true dsaWallet0, wallet0, wallet1, DAI_ID, DEPOSIT_UNDERLYING, depositAmount, ETH_ID, MARKET_3M, fcash, true
); );
const bal = await ethers.provider.getBalance(dsaWallet0.address); const bal = await ethers.provider.getBalance(dsaWallet0.address);
await helpers.depositAndMintNToken(dsaWallet0, wallet0, wallet1, ETH_ID, bal, true); await helpers.depositAndMintNToken(dsaWallet0, wallet0, wallet1, ETH_ID, bal, true);
const before = await notional.getAccountPortfolio(dsaWallet0.address); const before = await notional.getAccountPortfolio(dsaWallet0.address);
expect(before.length, "expect 1 fDAI debt position").to.be.equal(1); expect(before.length, "expect 1 fDAI debt position").to.be.equal(1);
expect(before[0][3], "expect fDAI debt position to equal borrow amount").to.be.lte(ethers.utils.parseUnits("-100000000", 0)); expect(before[0][3], "expect fDAI debt position to equal borrow amount").to.be.lte(ethers.utils.parseUnits("-100000000", 0));
await helpers.redeemNTokenAndDeleverage(dsaWallet0, wallet0, wallet1, ETH_ID, MaxUint96, MARKET_3M, 0.98e8, 0); const fcash2 = ethers.utils.parseUnits("0.98", 8);
await helpers.redeemNTokenAndDeleverage(dsaWallet0, wallet0, wallet1, ETH_ID, MaxUint96, MARKET_3M, fcash2);
const after = await notional.getAccountPortfolio(dsaWallet0.address); const after = await notional.getAccountPortfolio(dsaWallet0.address);
expect(after.length, "expect 1 fDAI debt position after deleverage").to.be.equal(1); expect(after.length, "expect 1 fDAI debt position after deleverage").to.be.equal(1);
expect(after[0][3], "expect fDAI debt balance to go down after deleverage").to.be.lte(ethers.utils.parseUnits("-2000000", 0)); expect(after[0][3], "expect fDAI debt balance to go down after deleverage").to.be.lte(ethers.utils.parseUnits("-2000000", 0));