Skip to content

Commit

Permalink
Adapt tests to separate MMs
Browse files Browse the repository at this point in the history
Apply minor enhancements
  • Loading branch information
naveedinno committed Sep 20, 2023
1 parent 2640bd1 commit 9e07da9
Show file tree
Hide file tree
Showing 25 changed files with 231 additions and 167 deletions.
2 changes: 1 addition & 1 deletion contracts/facets/Account/AccountFacetImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ library AccountFacetImpl {
AccountStorage.Layout storage accountLayout = AccountStorage.layout();
require(
accountLayout.partyBAllocatedBalances[msg.sender][partyA] >= amount,
"PartyBFacet: Insufficient locked balance"
"PartyBFacet: Insufficient allocated balance"
);
LibMuon.verifyPartyBUpnl(upnlSig, msg.sender, partyA);
int256 availableBalance = LibAccount.partyBAvailableForQuote(
Expand Down
2 changes: 1 addition & 1 deletion contracts/facets/PartyA/PartyAFacetImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ library PartyAFacetImpl {
lockedValues.totalForPartyA() <= notionalValue,
"PartyAFacet: Leverage can't be lower than one"
);
// TODO: shoud we check for partyB
// TODO: should we check for partyB
require(
lockedValues.lf >=
(symbolLayout.symbols[symbolId].minAcceptablePortionLF * lockedValues.totalForPartyA()) /
Expand Down
5 changes: 2 additions & 3 deletions contracts/facets/PartyB/IPartyBEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pragma solidity >=0.8.18;
import "../../storages/QuoteStorage.sol";

interface IPartyBEvents {
event LockQuote(address partyB, uint256 quoteId, QuoteStatus quoteStatus);
event LockQuote(address partyB, uint256 quoteId);
event AllocatePartyB(address partyB, address partyA, uint256 amount);
event UnlockQuote(address partyB, uint256 quoteId, QuoteStatus quoteStatus);
event AcceptCancelRequest(uint256 quoteId, QuoteStatus quoteStatus);
Expand All @@ -16,8 +16,7 @@ interface IPartyBEvents {
address partyA,
address partyB,
uint256 filledAmount,
uint256 openedPrice,
QuoteStatus quoteStatus
uint256 openedPrice
);
event FillCloseRequest(
uint256 quoteId,
Expand Down
10 changes: 4 additions & 6 deletions contracts/facets/PartyB/PartyBFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract PartyBFacet is Accessibility, Pausable, IPartyBEvents {
) external whenNotPartyBActionsPaused onlyPartyB notLiquidated(quoteId) {
PartyBFacetImpl.lockQuote(quoteId, upnlSig, true);
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
emit LockQuote(quote.partyB, quoteId, quote.quoteStatus);
emit LockQuote(quote.partyB, quoteId);
}

function lockAndOpenQuote(
Expand All @@ -31,7 +31,7 @@ contract PartyBFacet is Accessibility, Pausable, IPartyBEvents {
) external whenNotPartyBActionsPaused onlyPartyB notLiquidated(quoteId) {
Quote storage quote = QuoteStorage.layout().quotes[quoteId];
PartyBFacetImpl.lockQuote(quoteId, upnlSig, false);
emit LockQuote(quote.partyB, quoteId, quote.quoteStatus);
emit LockQuote(quote.partyB, quoteId);
uint256 newId = PartyBFacetImpl.openPosition(
quoteId,
filledAmount,
Expand All @@ -43,8 +43,7 @@ contract PartyBFacet is Accessibility, Pausable, IPartyBEvents {
quote.partyA,
quote.partyB,
filledAmount,
openedPrice,
QuoteStatus.OPENED
openedPrice
);
if (newId != 0) {
Quote storage newQuote = QuoteStorage.layout().quotes[newId];
Expand Down Expand Up @@ -104,8 +103,7 @@ contract PartyBFacet is Accessibility, Pausable, IPartyBEvents {
quote.partyA,
quote.partyB,
filledAmount,
openedPrice,
QuoteStatus.OPENED
openedPrice
);
if (newId != 0) {
Quote storage newQuote = QuoteStorage.layout().quotes[newId];
Expand Down
22 changes: 11 additions & 11 deletions test/LiquidationFacet.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,24 @@ export function shouldBehaveLikeLiquidationFacet(): void {
expect(balanceInfoOfPartyA.allocatedBalances).to.be.equal(
decimal(500).sub(await getTradingFeeForQuotes(context, [1, 2, 3, 4])),
);
expect(balanceInfoOfPartyA.totalLocked).to.be.equal(
expect(balanceInfoOfPartyA.totalLockedPartyA).to.be.equal(
await getTotalLockedValuesForQuoteIds(context, [1]),
);
expect(balanceInfoOfPartyA.pendingLockedCva).to.be.equal("0");
expect(balanceInfoOfPartyA.pendingLockedMm).to.be.equal("0");
expect(balanceInfoOfPartyA.pendingLockedMmPartyA).to.be.equal("0");
expect(balanceInfoOfPartyA.pendingLockedLf).to.be.equal("0");
expect(balanceInfoOfPartyA.totalPendingLocked).to.be.equal("0");
expect(balanceInfoOfPartyA.totalPendingLockedPartyA).to.be.equal("0");

let balanceInfoOfPartyB: BalanceInfo = await this.hedger.getBalanceInfo(user);
expect(balanceInfoOfPartyB.allocatedBalances).to.be.equal(decimal(360).toString());
expect(balanceInfoOfPartyB.lockedCva).to.be.equal(decimal(22).toString());
expect(balanceInfoOfPartyB.lockedMm).to.be.equal(decimal(75).toString());
expect(balanceInfoOfPartyB.lockedMmPartyB).to.be.equal(decimal(40).toString());
expect(balanceInfoOfPartyB.lockedLf).to.be.equal(decimal(3).toString());
expect(balanceInfoOfPartyB.totalLocked).to.be.equal(decimal(100).toString());
expect(balanceInfoOfPartyB.totalLockedPartyB).to.be.equal(decimal(65).toString());
expect(balanceInfoOfPartyB.pendingLockedCva).to.be.equal("0");
expect(balanceInfoOfPartyB.pendingLockedMm).to.be.equal("0");
expect(balanceInfoOfPartyB.pendingLockedMmPartyB).to.be.equal("0");
expect(balanceInfoOfPartyB.pendingLockedLf).to.be.equal("0");
expect(balanceInfoOfPartyB.totalPendingLocked).to.be.equal("0");
expect(balanceInfoOfPartyB.totalPendingLockedPartyB).to.be.equal("0");
});

it("Should fail to liquidate a user twice", async function() {
Expand Down Expand Up @@ -206,13 +206,13 @@ export function shouldBehaveLikeLiquidationFacet(): void {
let balanceInfo: BalanceInfo = await this.hedger.getBalanceInfo(user);
expect(balanceInfo.allocatedBalances).to.be.equal("0");
expect(balanceInfo.lockedCva).to.be.equal("0");
expect(balanceInfo.lockedMm).to.be.equal("0");
expect(balanceInfo.lockedMmPartyB).to.be.equal("0");
expect(balanceInfo.lockedLf).to.be.equal("0");
expect(balanceInfo.totalLocked).to.be.equal("0");
expect(balanceInfo.totalLockedPartyB).to.be.equal("0");
expect(balanceInfo.pendingLockedCva).to.be.equal("0");
expect(balanceInfo.pendingLockedMm).to.be.equal("0");
expect(balanceInfo.pendingLockedMmPartyB).to.be.equal("0");
expect(balanceInfo.pendingLockedLf).to.be.equal("0");
expect(balanceInfo.totalPendingLocked).to.be.equal("0");
expect(balanceInfo.totalPendingLockedPartyB).to.be.equal("0");

expect((await context.viewFacet.getQuote(5)).quoteStatus).to.be.equal(QuoteStatus.CANCELED);
});
Expand Down
18 changes: 9 additions & 9 deletions test/SendQuote.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ export function shouldBehaveLikeSendQuote(): void {
const context: RunContext = this.context;
await pausePartyA(context);
await expect(
this.user.sendQuote(limitQuoteRequestBuilder().quantity(50).cva(50).mm(1).lf(100).build()),
this.user.sendQuote(limitQuoteRequestBuilder().quantity(50).cva(50).partyAmm(1).lf(100).build()),
).to.be.revertedWith("Pausable: PartyA actions paused");
});

it("Should fail on leverage being lower than one", async function() {
await expect(
this.user.sendQuote(limitQuoteRequestBuilder().quantity(50).cva(50).mm(1).lf(100).build()),
this.user.sendQuote(limitQuoteRequestBuilder().quantity(50).cva(50).partyAmm(1).lf(100).build()),
).to.be.revertedWith("PartyAFacet: Leverage can't be lower than one");

await expect(
this.user.sendQuote(
limitQuoteRequestBuilder()
.quantity(decimal(0))
.cva(decimal(3))
.mm(decimal(75))
.partyAmm(decimal(75))
.lf(decimal(22))
.build(),
),
Expand All @@ -51,7 +51,7 @@ export function shouldBehaveLikeSendQuote(): void {
.symbolId(2)
.quantity(decimal(0))
.cva(decimal(3))
.mm(decimal(75))
.partyAmm(decimal(75))
.lf(decimal(22))
.build(),
),
Expand All @@ -64,7 +64,7 @@ export function shouldBehaveLikeSendQuote(): void {
limitQuoteRequestBuilder()
.quantity(decimal(100))
.cva(decimal(1))
.mm(decimal(1))
.partyAmm(decimal(1))
.lf(decimal(0))
.build(),
),
Expand All @@ -77,7 +77,7 @@ export function shouldBehaveLikeSendQuote(): void {
limitQuoteRequestBuilder()
.quantity(decimal(50))
.cva(decimal(1))
.mm(decimal(1))
.partyAmm(decimal(1))
.lf(decimal(1))
.build(),
),
Expand All @@ -91,7 +91,7 @@ export function shouldBehaveLikeSendQuote(): void {
.partyBWhiteList([this.user.getAddress()])
.quantity(decimal(50))
.cva(decimal(3))
.mm(decimal(5))
.partyAmm(decimal(5))
.lf(decimal(5))
.build(),
),
Expand All @@ -105,7 +105,7 @@ export function shouldBehaveLikeSendQuote(): void {
.price(decimal(16))
.quantity(decimal(500))
.cva(decimal(120))
.mm(this.user_allocated)
.partyAmm(this.user_allocated)
.lf(decimal(50))
.upnlSig(getDummySingleUpnlAndPriceSig(decimal(16)))
.build(),
Expand All @@ -117,7 +117,7 @@ export function shouldBehaveLikeSendQuote(): void {
limitQuoteRequestBuilder()
.quantity(decimal(1600))
.cva(decimal(250))
.mm(this.user_allocated)
.partyAmm(this.user_allocated)
.lf(decimal(60))
.build(),
),
Expand Down
3 changes: 2 additions & 1 deletion test/SpecificScenario.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export function shouldBehaveLikeSpecificScenario(): void {
Builder<QuoteRequest>()
.partyBWhiteList([])
.quantity("32000000000000000")
.mm("69706470325210735106")
.partyAmm("69706470325210735106")
.partyBmm("69706470325210735106")
.cva("14394116573201404621")
.lf("8104916153486468905")
.price("22207600000000000000000")
Expand Down
6 changes: 1 addition & 5 deletions test/models/EventListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ export class EventListener {
context.partyBFacet.on(Event.SEND_QUOTE, async (...args) => {
let value: SendQuoteEventObject = args[args.length - 1].args;
this.eventTrackQueues.get(Event.SEND_QUOTE)!.next(value);
if (value.quoteStatus == QuoteStatus.PENDING) {
this.queues.get(QuoteStatus.PENDING)!.next(value.quoteId);
} else {
this.queues.get(QuoteStatus.CANCELED)!.next(value.quoteId);
}
this.queues.get(QuoteStatus.PENDING)!.next(value.quoteId);
});
context.partyAFacet.on(Event.REQUEST_TO_CANCEL_QUOTE, async (...args) => {
let value: RequestToCancelQuoteEventObject = args[args.length - 1].args;
Expand Down
29 changes: 19 additions & 10 deletions test/models/Hedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,17 @@ export class Hedger {
return {
allocatedBalances: b[0],
lockedCva: b[1],
lockedMm: b[2],
lockedLf: b[3],
totalLocked: b[4],
lockedLf: b[2],
lockedMmPartyA: b[3],
lockedMmPartyB: b[4],
totalLockedPartyA: b[1].add(b[2]).add(b[3]),
totalLockedPartyB: b[1].add(b[2]).add(b[4]),
pendingLockedCva: b[5],
pendingLockedMm: b[6],
pendingLockedLf: b[7],
totalPendingLocked: b[8],
pendingLockedLf: b[6],
pendingLockedMmPartyA: b[7],
pendingLockedMmPartyB: b[8],
totalPendingLockedPartyA: b[5].add(b[6]).add(b[7]),
totalPendingLockedPartyB: b[5].add(b[6]).add(b[8]),
};
}

Expand Down Expand Up @@ -215,11 +219,16 @@ export class Hedger {
export interface BalanceInfo {
allocatedBalances: BigNumber;
lockedCva: BigNumber;
lockedMm: BigNumber;
lockedMmPartyA: BigNumber;
lockedMmPartyB: BigNumber;
lockedLf: BigNumber;
totalLocked: BigNumber;
totalLockedPartyA: BigNumber;
totalLockedPartyB: BigNumber;
pendingLockedCva: BigNumber;
pendingLockedMm: BigNumber;
pendingLockedMmPartyA: BigNumber;
pendingLockedMmPartyB: BigNumber;
pendingLockedLf: BigNumber;
totalPendingLocked: BigNumber;
totalPendingLockedPartyA: BigNumber;
totalPendingLockedPartyB: BigNumber;
}

4 changes: 2 additions & 2 deletions test/models/HedgerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class HedgerController {
await this.hedger.openPosition(
quote.id,
Builder<OpenRequest>()
.fillAmount(fillAmount)
.filledAmount(fillAmount)
.openPrice(openPrice)
.upnlPartyA(partyAUpnl)
.upnlPartyB(partyBUpnl)
Expand Down Expand Up @@ -284,7 +284,7 @@ export class HedgerController {
await this.hedger.fillCloseRequest(
quote.id,
Builder<FillCloseRequest>()
.fillAmount(fillAmount)
.filledAmount(fillAmount)
.closedPrice(closePrice)
.upnlPartyA(partyAUpnl)
.upnlPartyB(partyBUpnl)
Expand Down
38 changes: 23 additions & 15 deletions test/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { PromiseOrValue } from "../../src/types/common";
import { serializeToJson, unDecimal } from "../utils/Common";
import { logger } from "../utils/LoggerUtils";
import { getPrice } from "../utils/PriceUtils";
import { getDummySingleUpnlAndPriceSig } from "../utils/SignatureUtils";
import { QuoteStructOutput } from "../../src/types/contracts/facets/ViewFacet";
import { PositionType } from "./Enums";
import { RunContext } from "./RunContext";
Expand Down Expand Up @@ -62,8 +61,9 @@ export class User {
request.price,
request.quantity,
request.cva,
request.mm,
request.lf,
request.partyAmm,
request.partyBmm,
request.maxFundingRate,
request.deadline,
await request.upnlSig,
Expand All @@ -90,13 +90,17 @@ export class User {
return {
allocatedBalances: b[0],
lockedCva: b[1],
lockedMm: b[2],
lockedLf: b[3],
totalLocked: b[4],
lockedLf: b[2],
lockedMmPartyA: b[3],
lockedMmPartyB: b[4],
totalLockedPartyA: b[1].add(b[2]).add(b[3]),
totalLockedPartyB: b[1].add(b[2]).add(b[4]),
pendingLockedCva: b[5],
pendingLockedMm: b[6],
pendingLockedLf: b[7],
totalPendingLocked: b[8],
pendingLockedLf: b[6],
pendingLockedMmPartyA: b[7],
pendingLockedMmPartyB: b[8],
totalPendingLockedPartyA: b[5].add(b[6]).add(b[7]),
totalPendingLockedPartyB: b[5].add(b[6]).add(b[8]),
};
}

Expand Down Expand Up @@ -172,13 +176,13 @@ export class User {
if (upnl.gt(0)) {
available = balanceInfo.allocatedBalances
.add(upnl)
.sub(balanceInfo.totalLocked.add(balanceInfo.totalPendingLocked));
.sub(balanceInfo.totalLockedPartyA.add(balanceInfo.totalPendingLockedPartyA));
} else {
let mm = balanceInfo.lockedMm;
let mm = balanceInfo.lockedMmPartyA;
let mUpnl = upnl.mul(-1);
let considering_mm = mUpnl.gt(mm) ? mUpnl : mm;
available = balanceInfo.allocatedBalances
.sub(balanceInfo.lockedCva.add(balanceInfo.lockedLf).add(balanceInfo.totalPendingLocked))
.sub(balanceInfo.lockedCva.add(balanceInfo.lockedLf).add(balanceInfo.totalPendingLockedPartyA))
.sub(considering_mm);
}
return available;
Expand All @@ -188,11 +192,15 @@ export class User {
export interface BalanceInfo {
allocatedBalances: BigNumber;
lockedCva: BigNumber;
lockedMm: BigNumber;
lockedMmPartyA: BigNumber;
lockedMmPartyB: BigNumber;
lockedLf: BigNumber;
totalLocked: BigNumber;
totalLockedPartyA: BigNumber;
totalLockedPartyB: BigNumber;
pendingLockedCva: BigNumber;
pendingLockedMm: BigNumber;
pendingLockedMmPartyA: BigNumber;
pendingLockedMmPartyB: BigNumber;
pendingLockedLf: BigNumber;
totalPendingLocked: BigNumber;
totalPendingLockedPartyA: BigNumber;
totalPendingLockedPartyB: BigNumber;
}
7 changes: 4 additions & 3 deletions test/models/UserController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { getPrice } from "../utils/PriceUtils";
import { pick, randomBigNumber, randomBigNumberRatio } from "../utils/RandomUtils";
import { roundToPrecision, safeDiv } from "../utils/SafeMath";
import { getDummySingleUpnlAndPriceSig } from "../utils/SignatureUtils";
import { QuoteStructOutput } from "./../../src/types/contracts/facets/ViewFacet";
import { SymbolStructOutput } from "./../../src/types/contracts/facets/control/ControlFacet";
import { QuoteStructOutput } from "../../src/types/contracts/facets/ViewFacet";
import { SymbolStructOutput } from "../../src/types/contracts/facets/control/ControlFacet";
import { Action, actionNamesMap, ActionWrapper, expandActions, userActionsMap } from "./Actions";
import { OrderType, PositionType, QuoteStatus } from "./Enums";
import { ManagedError } from "./ManagedError";
Expand Down Expand Up @@ -312,7 +312,8 @@ export class UserController {
Builder<QuoteRequest>()
.partyBWhiteList([])
.quantity(quantity)
.mm(mm)
.partyAmm(mm)
.partyBmm(mm.div(2))
.cva(cva)
.lf(lf)
.symbolId(symbol.symbolId)
Expand Down
Loading

0 comments on commit 9e07da9

Please sign in to comment.