Skip to content

Commit

Permalink
updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Sep 18, 2024
1 parent f74a65a commit a28966f
Show file tree
Hide file tree
Showing 13 changed files with 1,228 additions and 1,792 deletions.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion spot-vaults/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"ethers": "^6.6.0",
"ethers-v5": "npm:ethers@^5.7.0",
"ganache-cli": "latest",
"hardhat": "^2.22.8",
"hardhat": "^2.22.10",
"hardhat-gas-reporter": "latest",
"lodash": "^4.17.21",
"prettier": "^2.7.1",
Expand Down
334 changes: 145 additions & 189 deletions spot-vaults/test/BillBroker.ts

Large diffs are not rendered by default.

108 changes: 36 additions & 72 deletions spot-vaults/test/BillBroker_deposit_redeem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers, upgrades } from "hardhat";
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { expect } from "chai";
import { DMock, usdFP, perpFP, lpAmtFP, percentageFP, priceFP } from "./helpers";
import { DMock, usdFP, perpFP, lpAmtFP, percFP, priceFP } from "./helpers";

describe("BillBroker", function () {
async function setupContracts() {
Expand All @@ -14,16 +14,17 @@ describe("BillBroker", function () {
await usd.init("USD token", "usd", 6);
const perp = await Token.deploy();
await perp.init("Perp token", "perp", 9);
const pricingStrategy = new DMock("SpotAppraiser");
await pricingStrategy.deploy();
await pricingStrategy.mockMethod("decimals()", [18]);
await pricingStrategy.mockMethod("perpPrice()", [priceFP("1.15"), true]);
await pricingStrategy.mockMethod("usdPrice()", [priceFP("1"), true]);
const oracle = new DMock("IPerpPricer");
await oracle.deploy();
await oracle.mockMethod("decimals()", [18]);
await oracle.mockMethod("perpFmvUsdPrice()", [priceFP("1.15"), true]);
await oracle.mockMethod("usdPrice()", [priceFP("1"), true]);
await oracle.mockMethod("perpBeta()", [percFP("1"), true]);

const BillBroker = await ethers.getContractFactory("BillBroker");
const billBroker = await upgrades.deployProxy(
BillBroker.connect(deployer),
["BillBroker LP", "LP token", usd.target, perp.target, pricingStrategy.target],
["BillBroker LP", "LP token", usd.target, perp.target, oracle.target],
{
initializer: "init(string,string,address,address,address)",
},
Expand All @@ -45,7 +46,7 @@ describe("BillBroker", function () {
await perp.mint(await deployer.getAddress(), perpFP("2000"));
await usd.mint(await otherUser.getAddress(), usdFP("2000"));
await perp.mint(await otherUser.getAddress(), perpFP("2000"));
return { deployer, otherUser, usd, perp, pricingStrategy, billBroker };
return { deployer, otherUser, usd, perp, oracle, billBroker };
}

async function assetRatio(billBroker) {
Expand Down Expand Up @@ -145,7 +146,7 @@ describe("BillBroker", function () {
perpFP("100"),
);
await billBroker.updateFees({
mintFeePerc: percentageFP("0.1"),
mintFeePerc: percFP("0.1"),
burnFeePerc: 0n,
perpToUSDSwapFeePercs: {
lower: 0n,
Expand Down Expand Up @@ -259,7 +260,7 @@ describe("BillBroker", function () {
perpFP("200"),
);
await billBroker.updateFees({
mintFeePerc: percentageFP("0.1"),
mintFeePerc: percFP("0.1"),
burnFeePerc: 0n,
perpToUSDSwapFeePercs: {
lower: 0n,
Expand Down Expand Up @@ -347,7 +348,7 @@ describe("BillBroker", function () {
perpFP("100"),
);
await billBroker.updateFees({
mintFeePerc: percentageFP("0.1"),
mintFeePerc: percFP("0.1"),
burnFeePerc: 0n,
perpToUSDSwapFeePercs: {
lower: 0n,
Expand Down Expand Up @@ -565,7 +566,7 @@ describe("BillBroker", function () {
it("should withhold fees and mint lp tokens", async function () {
const { billBroker, usd, perp, deployer } = await loadFixture(setupContracts);
await billBroker.updateFees({
mintFeePerc: percentageFP("0.1"),
mintFeePerc: percFP("0.1"),
burnFeePerc: 0n,
perpToUSDSwapFeePercs: {
lower: 0n,
Expand Down Expand Up @@ -645,15 +646,14 @@ describe("BillBroker", function () {
it("should revert", async function () {
const { billBroker } = await loadFixture(setupContracts);
await billBroker.pause();
await expect(billBroker.depositUSD(usdFP("115"), percentageFP("1"))).to.be
.reverted;
await expect(billBroker.depositUSD(usdFP("115"), percFP("1"))).to.be.reverted;
});
});

describe("when usdAmtIn is zero", function () {
it("should return zero", async function () {
const { billBroker } = await loadFixture(setupContracts);
const r = await billBroker.depositUSD.staticCall(0n, percentageFP("1"));
const r = await billBroker.depositUSD.staticCall(0n, percFP("1"));
expect(r).to.eq(0n);
});
});
Expand Down Expand Up @@ -744,7 +744,7 @@ describe("BillBroker", function () {

await usd.approve(billBroker.target, usdFP("10"));
await expect(
billBroker.depositUSD(usdFP("10"), percentageFP("0.50")),
billBroker.depositUSD(usdFP("10"), percFP("0.50")),
).to.be.revertedWithCustomError(billBroker, "SlippageTooHigh");
});
});
Expand All @@ -763,7 +763,7 @@ describe("BillBroker", function () {

await usd.approve(billBroker.target, usdFP("10"));
await expect(() =>
billBroker.depositUSD(usdFP("10"), percentageFP("1")),
billBroker.depositUSD(usdFP("10"), percFP("1")),
).to.changeTokenBalance(usd, deployer, usdFP("-10"));
});

Expand All @@ -780,7 +780,7 @@ describe("BillBroker", function () {

await usd.approve(billBroker.target, usdFP("10"));
await expect(() =>
billBroker.depositUSD(usdFP("10"), percentageFP("1")),
billBroker.depositUSD(usdFP("10"), percFP("1")),
).to.changeTokenBalance(
billBroker,
deployer,
Expand All @@ -803,7 +803,7 @@ describe("BillBroker", function () {
);
await usd.approve(billBroker.target, usdFP("10"));
const r = await billBroker.reserveState.staticCall();
await expect(billBroker.depositUSD(usdFP("10"), percentageFP("1")))
await expect(billBroker.depositUSD(usdFP("10"), percFP("1")))
.to.emit(billBroker, "DepositUSD")
.withArgs(usdFP("10"), r);
expect(await billBroker.totalSupply()).to.eq(
Expand All @@ -823,7 +823,7 @@ describe("BillBroker", function () {
);

await usd.approve(billBroker.target, usdFP("10"));
const r = await billBroker.depositUSD.staticCall(usdFP("10"), percentageFP("1"));
const r = await billBroker.depositUSD.staticCall(usdFP("10"), percFP("1"));
expect(r).to.eq(lpAmtFP("9.130434782608695652173913"));
});
});
Expand All @@ -832,7 +832,7 @@ describe("BillBroker", function () {
it("should withhold fees and mint lp tokens", async function () {
const { billBroker, usd, perp, deployer } = await loadFixture(setupContracts);
await billBroker.updateFees({
mintFeePerc: percentageFP("0.1"),
mintFeePerc: percFP("0.1"),
burnFeePerc: 0n,
perpToUSDSwapFeePercs: {
lower: 0n,
Expand All @@ -856,7 +856,7 @@ describe("BillBroker", function () {

await usd.approve(billBroker.target, usdFP("10"));
await expect(() =>
billBroker.depositUSD(usdFP("10"), percentageFP("1")),
billBroker.depositUSD(usdFP("10"), percFP("1")),
).to.changeTokenBalance(
billBroker,
deployer,
Expand All @@ -871,15 +871,14 @@ describe("BillBroker", function () {
it("should revert", async function () {
const { billBroker } = await loadFixture(setupContracts);
await billBroker.pause();
await expect(billBroker.depositPerp(perpFP("100"), percentageFP("1"))).to.be
.reverted;
await expect(billBroker.depositPerp(perpFP("100"), percFP("1"))).to.be.reverted;
});
});

describe("when perpAmtIn is zero", function () {
it("should return zero", async function () {
const { billBroker } = await loadFixture(setupContracts);
const r = await billBroker.depositPerp.staticCall(0n, percentageFP("1"));
const r = await billBroker.depositPerp.staticCall(0n, percFP("1"));
expect(r).to.eq(0n);
});
});
Expand Down Expand Up @@ -959,7 +958,7 @@ describe("BillBroker", function () {

await perp.approve(billBroker.target, perpFP("10"));
await expect(
billBroker.depositPerp(perpFP("10"), percentageFP("1.85")),
billBroker.depositPerp(perpFP("10"), percFP("1.85")),
).to.be.revertedWithCustomError(billBroker, "SlippageTooHigh");
});
});
Expand All @@ -978,7 +977,7 @@ describe("BillBroker", function () {

await perp.approve(billBroker.target, perpFP("10"));
await expect(() =>
billBroker.depositPerp(perpFP("10"), percentageFP("1")),
billBroker.depositPerp(perpFP("10"), percFP("1")),
).to.changeTokenBalance(perp, deployer, perpFP("-10"));
});

Expand All @@ -995,7 +994,7 @@ describe("BillBroker", function () {

await perp.approve(billBroker.target, perpFP("10"));
await expect(() =>
billBroker.depositPerp(perpFP("10"), percentageFP("1")),
billBroker.depositPerp(perpFP("10"), percFP("1")),
).to.changeTokenBalance(billBroker, deployer, lpAmtFP("11"));
expect(await billBroker.totalSupply()).to.eq(lpAmtFP("341"));
});
Expand All @@ -1013,7 +1012,7 @@ describe("BillBroker", function () {

await perp.approve(billBroker.target, perpFP("10"));
const r = await billBroker.reserveState.staticCall();
await expect(billBroker.depositPerp(perpFP("10"), percentageFP("1")))
await expect(billBroker.depositPerp(perpFP("10"), percFP("1")))
.to.emit(billBroker, "DepositPerp")
.withArgs(perpFP("10"), r);
expect(await billBroker.totalSupply()).to.eq(lpAmtFP("341"));
Expand All @@ -1031,10 +1030,7 @@ describe("BillBroker", function () {
);

await perp.approve(billBroker.target, perpFP("10"));
const r = await billBroker.depositPerp.staticCall(
perpFP("10"),
percentageFP("1"),
);
const r = await billBroker.depositPerp.staticCall(perpFP("10"), percFP("1"));
expect(r).to.eq(lpAmtFP("11"));
});
});
Expand All @@ -1052,7 +1048,7 @@ describe("BillBroker", function () {
);

await billBroker.updateFees({
mintFeePerc: percentageFP("0.1"),
mintFeePerc: percFP("0.1"),
burnFeePerc: 0n,
perpToUSDSwapFeePercs: {
lower: 0n,
Expand All @@ -1066,7 +1062,7 @@ describe("BillBroker", function () {
});
await perp.approve(billBroker.target, perpFP("10"));
await expect(() =>
billBroker.depositPerp(perpFP("10"), percentageFP("1")),
billBroker.depositPerp(perpFP("10"), percFP("1")),
).to.changeTokenBalance(billBroker, deployer, lpAmtFP("9.9"));
});
});
Expand Down Expand Up @@ -1129,7 +1125,7 @@ describe("BillBroker", function () {
const { billBroker, usd, perp } = await loadFixture(setupContracts);
await billBroker.updateFees({
mintFeePerc: 0n,
burnFeePerc: percentageFP("0.1"),
burnFeePerc: percFP("0.1"),
perpToUSDSwapFeePercs: {
lower: 0n,
upper: 0n,
Expand Down Expand Up @@ -1170,15 +1166,7 @@ describe("BillBroker", function () {
await billBroker.swapUSDForPerps(usdFP("115"), 0n);
expect(await perp.balanceOf(billBroker.target)).to.eq(0n);

const s = await billBroker.reserveState.staticCall();
expect(
await billBroker.assetRatio({
usdBalance: s[0],
perpBalance: s[1],
usdPrice: s[2],
perpPrice: s[3],
}),
).to.eq(ethers.MaxUint256);
expect(await assetRatio(billBroker)).to.eq(ethers.MaxUint256);

const r = await billBroker.computeRedemptionAmts.staticCall(lpAmtFP("100"));
expect(r[0]).to.eq(usdFP("106.976744"));
Expand All @@ -1202,15 +1190,7 @@ describe("BillBroker", function () {
await billBroker.swapPerpsForUSD(perpFP("100"), 0n);
expect(await usd.balanceOf(billBroker.target)).to.eq(0n);

const s = await billBroker.reserveState.staticCall();
expect(
await billBroker.assetRatio({
usdBalance: s[0],
perpBalance: s[1],
usdPrice: s[2],
perpPrice: s[3],
}),
).to.eq(0);
expect(await assetRatio(billBroker)).to.eq(0);

const r = await billBroker.computeRedemptionAmts.staticCall(lpAmtFP("100"));
expect(r[0]).to.eq(0n);
Expand Down Expand Up @@ -1426,15 +1406,7 @@ describe("BillBroker", function () {
await billBroker.swapUSDForPerps(usdFP("115"), 0n);
expect(await perp.balanceOf(billBroker.target)).to.eq(0n);

const s = await billBroker.reserveState.staticCall();
expect(
await billBroker.assetRatio({
usdBalance: s[0],
perpBalance: s[1],
usdPrice: s[2],
perpPrice: s[3],
}),
).to.eq(ethers.MaxUint256);
expect(await assetRatio(billBroker)).to.eq(ethers.MaxUint256);

const perpBal = await perp.balanceOf(await deployer.getAddress());
await expect(() => billBroker.redeem(lpAmtFP("100"))).to.changeTokenBalance(
Expand Down Expand Up @@ -1463,15 +1435,7 @@ describe("BillBroker", function () {
await billBroker.swapPerpsForUSD(perpFP("100"), 0n);
expect(await usd.balanceOf(billBroker.target)).to.eq(0n);

const s = await billBroker.reserveState.staticCall();
expect(
await billBroker.assetRatio({
usdBalance: s[0],
perpBalance: s[1],
usdPrice: s[2],
perpPrice: s[3],
}),
).to.eq(0);
expect(await assetRatio(billBroker)).to.eq(0);

const usdBal = await usd.balanceOf(await deployer.getAddress());
await expect(() => billBroker.redeem(lpAmtFP("100"))).to.changeTokenBalance(
Expand Down
Loading

0 comments on commit a28966f

Please sign in to comment.