Skip to content

Commit a28966f

Browse files
committed
updated unit tests
1 parent f74a65a commit a28966f

13 files changed

+1228
-1792
lines changed

spot-vaults/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"ethers": "^6.6.0",
6363
"ethers-v5": "npm:ethers@^5.7.0",
6464
"ganache-cli": "latest",
65-
"hardhat": "^2.22.8",
65+
"hardhat": "^2.22.10",
6666
"hardhat-gas-reporter": "latest",
6767
"lodash": "^4.17.21",
6868
"prettier": "^2.7.1",

spot-vaults/test/BillBroker.ts

+145-189
Large diffs are not rendered by default.

spot-vaults/test/BillBroker_deposit_redeem.ts

+36-72
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ethers, upgrades } from "hardhat";
22
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers";
33
import { expect } from "chai";
4-
import { DMock, usdFP, perpFP, lpAmtFP, percentageFP, priceFP } from "./helpers";
4+
import { DMock, usdFP, perpFP, lpAmtFP, percFP, priceFP } from "./helpers";
55

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

2324
const BillBroker = await ethers.getContractFactory("BillBroker");
2425
const billBroker = await upgrades.deployProxy(
2526
BillBroker.connect(deployer),
26-
["BillBroker LP", "LP token", usd.target, perp.target, pricingStrategy.target],
27+
["BillBroker LP", "LP token", usd.target, perp.target, oracle.target],
2728
{
2829
initializer: "init(string,string,address,address,address)",
2930
},
@@ -45,7 +46,7 @@ describe("BillBroker", function () {
4546
await perp.mint(await deployer.getAddress(), perpFP("2000"));
4647
await usd.mint(await otherUser.getAddress(), usdFP("2000"));
4748
await perp.mint(await otherUser.getAddress(), perpFP("2000"));
48-
return { deployer, otherUser, usd, perp, pricingStrategy, billBroker };
49+
return { deployer, otherUser, usd, perp, oracle, billBroker };
4950
}
5051

5152
async function assetRatio(billBroker) {
@@ -145,7 +146,7 @@ describe("BillBroker", function () {
145146
perpFP("100"),
146147
);
147148
await billBroker.updateFees({
148-
mintFeePerc: percentageFP("0.1"),
149+
mintFeePerc: percFP("0.1"),
149150
burnFeePerc: 0n,
150151
perpToUSDSwapFeePercs: {
151152
lower: 0n,
@@ -259,7 +260,7 @@ describe("BillBroker", function () {
259260
perpFP("200"),
260261
);
261262
await billBroker.updateFees({
262-
mintFeePerc: percentageFP("0.1"),
263+
mintFeePerc: percFP("0.1"),
263264
burnFeePerc: 0n,
264265
perpToUSDSwapFeePercs: {
265266
lower: 0n,
@@ -347,7 +348,7 @@ describe("BillBroker", function () {
347348
perpFP("100"),
348349
);
349350
await billBroker.updateFees({
350-
mintFeePerc: percentageFP("0.1"),
351+
mintFeePerc: percFP("0.1"),
351352
burnFeePerc: 0n,
352353
perpToUSDSwapFeePercs: {
353354
lower: 0n,
@@ -565,7 +566,7 @@ describe("BillBroker", function () {
565566
it("should withhold fees and mint lp tokens", async function () {
566567
const { billBroker, usd, perp, deployer } = await loadFixture(setupContracts);
567568
await billBroker.updateFees({
568-
mintFeePerc: percentageFP("0.1"),
569+
mintFeePerc: percFP("0.1"),
569570
burnFeePerc: 0n,
570571
perpToUSDSwapFeePercs: {
571572
lower: 0n,
@@ -645,15 +646,14 @@ describe("BillBroker", function () {
645646
it("should revert", async function () {
646647
const { billBroker } = await loadFixture(setupContracts);
647648
await billBroker.pause();
648-
await expect(billBroker.depositUSD(usdFP("115"), percentageFP("1"))).to.be
649-
.reverted;
649+
await expect(billBroker.depositUSD(usdFP("115"), percFP("1"))).to.be.reverted;
650650
});
651651
});
652652

653653
describe("when usdAmtIn is zero", function () {
654654
it("should return zero", async function () {
655655
const { billBroker } = await loadFixture(setupContracts);
656-
const r = await billBroker.depositUSD.staticCall(0n, percentageFP("1"));
656+
const r = await billBroker.depositUSD.staticCall(0n, percFP("1"));
657657
expect(r).to.eq(0n);
658658
});
659659
});
@@ -744,7 +744,7 @@ describe("BillBroker", function () {
744744

745745
await usd.approve(billBroker.target, usdFP("10"));
746746
await expect(
747-
billBroker.depositUSD(usdFP("10"), percentageFP("0.50")),
747+
billBroker.depositUSD(usdFP("10"), percFP("0.50")),
748748
).to.be.revertedWithCustomError(billBroker, "SlippageTooHigh");
749749
});
750750
});
@@ -763,7 +763,7 @@ describe("BillBroker", function () {
763763

764764
await usd.approve(billBroker.target, usdFP("10"));
765765
await expect(() =>
766-
billBroker.depositUSD(usdFP("10"), percentageFP("1")),
766+
billBroker.depositUSD(usdFP("10"), percFP("1")),
767767
).to.changeTokenBalance(usd, deployer, usdFP("-10"));
768768
});
769769

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

781781
await usd.approve(billBroker.target, usdFP("10"));
782782
await expect(() =>
783-
billBroker.depositUSD(usdFP("10"), percentageFP("1")),
783+
billBroker.depositUSD(usdFP("10"), percFP("1")),
784784
).to.changeTokenBalance(
785785
billBroker,
786786
deployer,
@@ -803,7 +803,7 @@ describe("BillBroker", function () {
803803
);
804804
await usd.approve(billBroker.target, usdFP("10"));
805805
const r = await billBroker.reserveState.staticCall();
806-
await expect(billBroker.depositUSD(usdFP("10"), percentageFP("1")))
806+
await expect(billBroker.depositUSD(usdFP("10"), percFP("1")))
807807
.to.emit(billBroker, "DepositUSD")
808808
.withArgs(usdFP("10"), r);
809809
expect(await billBroker.totalSupply()).to.eq(
@@ -823,7 +823,7 @@ describe("BillBroker", function () {
823823
);
824824

825825
await usd.approve(billBroker.target, usdFP("10"));
826-
const r = await billBroker.depositUSD.staticCall(usdFP("10"), percentageFP("1"));
826+
const r = await billBroker.depositUSD.staticCall(usdFP("10"), percFP("1"));
827827
expect(r).to.eq(lpAmtFP("9.130434782608695652173913"));
828828
});
829829
});
@@ -832,7 +832,7 @@ describe("BillBroker", function () {
832832
it("should withhold fees and mint lp tokens", async function () {
833833
const { billBroker, usd, perp, deployer } = await loadFixture(setupContracts);
834834
await billBroker.updateFees({
835-
mintFeePerc: percentageFP("0.1"),
835+
mintFeePerc: percFP("0.1"),
836836
burnFeePerc: 0n,
837837
perpToUSDSwapFeePercs: {
838838
lower: 0n,
@@ -856,7 +856,7 @@ describe("BillBroker", function () {
856856

857857
await usd.approve(billBroker.target, usdFP("10"));
858858
await expect(() =>
859-
billBroker.depositUSD(usdFP("10"), percentageFP("1")),
859+
billBroker.depositUSD(usdFP("10"), percFP("1")),
860860
).to.changeTokenBalance(
861861
billBroker,
862862
deployer,
@@ -871,15 +871,14 @@ describe("BillBroker", function () {
871871
it("should revert", async function () {
872872
const { billBroker } = await loadFixture(setupContracts);
873873
await billBroker.pause();
874-
await expect(billBroker.depositPerp(perpFP("100"), percentageFP("1"))).to.be
875-
.reverted;
874+
await expect(billBroker.depositPerp(perpFP("100"), percFP("1"))).to.be.reverted;
876875
});
877876
});
878877

879878
describe("when perpAmtIn is zero", function () {
880879
it("should return zero", async function () {
881880
const { billBroker } = await loadFixture(setupContracts);
882-
const r = await billBroker.depositPerp.staticCall(0n, percentageFP("1"));
881+
const r = await billBroker.depositPerp.staticCall(0n, percFP("1"));
883882
expect(r).to.eq(0n);
884883
});
885884
});
@@ -959,7 +958,7 @@ describe("BillBroker", function () {
959958

960959
await perp.approve(billBroker.target, perpFP("10"));
961960
await expect(
962-
billBroker.depositPerp(perpFP("10"), percentageFP("1.85")),
961+
billBroker.depositPerp(perpFP("10"), percFP("1.85")),
963962
).to.be.revertedWithCustomError(billBroker, "SlippageTooHigh");
964963
});
965964
});
@@ -978,7 +977,7 @@ describe("BillBroker", function () {
978977

979978
await perp.approve(billBroker.target, perpFP("10"));
980979
await expect(() =>
981-
billBroker.depositPerp(perpFP("10"), percentageFP("1")),
980+
billBroker.depositPerp(perpFP("10"), percFP("1")),
982981
).to.changeTokenBalance(perp, deployer, perpFP("-10"));
983982
});
984983

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

996995
await perp.approve(billBroker.target, perpFP("10"));
997996
await expect(() =>
998-
billBroker.depositPerp(perpFP("10"), percentageFP("1")),
997+
billBroker.depositPerp(perpFP("10"), percFP("1")),
999998
).to.changeTokenBalance(billBroker, deployer, lpAmtFP("11"));
1000999
expect(await billBroker.totalSupply()).to.eq(lpAmtFP("341"));
10011000
});
@@ -1013,7 +1012,7 @@ describe("BillBroker", function () {
10131012

10141013
await perp.approve(billBroker.target, perpFP("10"));
10151014
const r = await billBroker.reserveState.staticCall();
1016-
await expect(billBroker.depositPerp(perpFP("10"), percentageFP("1")))
1015+
await expect(billBroker.depositPerp(perpFP("10"), percFP("1")))
10171016
.to.emit(billBroker, "DepositPerp")
10181017
.withArgs(perpFP("10"), r);
10191018
expect(await billBroker.totalSupply()).to.eq(lpAmtFP("341"));
@@ -1031,10 +1030,7 @@ describe("BillBroker", function () {
10311030
);
10321031

10331032
await perp.approve(billBroker.target, perpFP("10"));
1034-
const r = await billBroker.depositPerp.staticCall(
1035-
perpFP("10"),
1036-
percentageFP("1"),
1037-
);
1033+
const r = await billBroker.depositPerp.staticCall(perpFP("10"), percFP("1"));
10381034
expect(r).to.eq(lpAmtFP("11"));
10391035
});
10401036
});
@@ -1052,7 +1048,7 @@ describe("BillBroker", function () {
10521048
);
10531049

10541050
await billBroker.updateFees({
1055-
mintFeePerc: percentageFP("0.1"),
1051+
mintFeePerc: percFP("0.1"),
10561052
burnFeePerc: 0n,
10571053
perpToUSDSwapFeePercs: {
10581054
lower: 0n,
@@ -1066,7 +1062,7 @@ describe("BillBroker", function () {
10661062
});
10671063
await perp.approve(billBroker.target, perpFP("10"));
10681064
await expect(() =>
1069-
billBroker.depositPerp(perpFP("10"), percentageFP("1")),
1065+
billBroker.depositPerp(perpFP("10"), percFP("1")),
10701066
).to.changeTokenBalance(billBroker, deployer, lpAmtFP("9.9"));
10711067
});
10721068
});
@@ -1129,7 +1125,7 @@ describe("BillBroker", function () {
11291125
const { billBroker, usd, perp } = await loadFixture(setupContracts);
11301126
await billBroker.updateFees({
11311127
mintFeePerc: 0n,
1132-
burnFeePerc: percentageFP("0.1"),
1128+
burnFeePerc: percFP("0.1"),
11331129
perpToUSDSwapFeePercs: {
11341130
lower: 0n,
11351131
upper: 0n,
@@ -1170,15 +1166,7 @@ describe("BillBroker", function () {
11701166
await billBroker.swapUSDForPerps(usdFP("115"), 0n);
11711167
expect(await perp.balanceOf(billBroker.target)).to.eq(0n);
11721168

1173-
const s = await billBroker.reserveState.staticCall();
1174-
expect(
1175-
await billBroker.assetRatio({
1176-
usdBalance: s[0],
1177-
perpBalance: s[1],
1178-
usdPrice: s[2],
1179-
perpPrice: s[3],
1180-
}),
1181-
).to.eq(ethers.MaxUint256);
1169+
expect(await assetRatio(billBroker)).to.eq(ethers.MaxUint256);
11821170

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

1205-
const s = await billBroker.reserveState.staticCall();
1206-
expect(
1207-
await billBroker.assetRatio({
1208-
usdBalance: s[0],
1209-
perpBalance: s[1],
1210-
usdPrice: s[2],
1211-
perpPrice: s[3],
1212-
}),
1213-
).to.eq(0);
1193+
expect(await assetRatio(billBroker)).to.eq(0);
12141194

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

1429-
const s = await billBroker.reserveState.staticCall();
1430-
expect(
1431-
await billBroker.assetRatio({
1432-
usdBalance: s[0],
1433-
perpBalance: s[1],
1434-
usdPrice: s[2],
1435-
perpPrice: s[3],
1436-
}),
1437-
).to.eq(ethers.MaxUint256);
1409+
expect(await assetRatio(billBroker)).to.eq(ethers.MaxUint256);
14381410

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

1466-
const s = await billBroker.reserveState.staticCall();
1467-
expect(
1468-
await billBroker.assetRatio({
1469-
usdBalance: s[0],
1470-
perpBalance: s[1],
1471-
usdPrice: s[2],
1472-
perpPrice: s[3],
1473-
}),
1474-
).to.eq(0);
1438+
expect(await assetRatio(billBroker)).to.eq(0);
14751439

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

0 commit comments

Comments
 (0)