Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crispheaney committed Oct 19, 2023
1 parent 583d1a4 commit 7cee59e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 19 deletions.
17 changes: 17 additions & 0 deletions sdk/src/idl/drift.json
Original file line number Diff line number Diff line change
Expand Up @@ -8383,6 +8383,23 @@
]
}
},
{
"name": "FillMode",
"type": {
"kind": "enum",
"variants": [
{
"name": "Fill"
},
{
"name": "PlaceAndMake"
},
{
"name": "PlaceAndTake"
}
]
}
},
{
"name": "PerpFulfillmentMethod",
"type": {
Expand Down
2 changes: 1 addition & 1 deletion tests/liquidateMaxLps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('max lp liq', () => {
const oracleGuardRails: OracleGuardRails = {
priceDivergence: {
markOraclePercentDivergence: PERCENTAGE_PRECISION,
oracleTwap5MinPercentDivergence: PERCENTAGE_PRECISION.div(new BN(10)),
oracleTwap5MinPercentDivergence: PERCENTAGE_PRECISION.mul(new BN(10)),
},
validity: {
slotsBeforeStaleForAmm: new BN(100),
Expand Down
16 changes: 10 additions & 6 deletions tests/liquidatePerp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('liquidate perp (no open orders)', () => {
console.log('deltaValueToLiq:', deltaValueToLiq.toString());
console.log('pp.base:', pp.baseAssetAmount.toString());

const expectedLiqPrice = 0.452181;
const expectedLiqPrice = 0.452190;
const liqPrice = driftClientUser.liquidationPrice(0, ZERO);
console.log('liqPrice:', liqPrice.toString());
assert(liqPrice.eq(new BN(expectedLiqPrice * PRICE_PRECISION.toNumber())));
Expand Down Expand Up @@ -288,7 +288,7 @@ describe('liquidate perp (no open orders)', () => {
const oracleGuardRails: OracleGuardRails = {
priceDivergence: {
markOraclePercentDivergence: PERCENTAGE_PRECISION,
oracleTwap5MinPercentDivergence: PERCENTAGE_PRECISION.div(new BN(10)),
oracleTwap5MinPercentDivergence: PERCENTAGE_PRECISION.muln(10),
},
validity: {
slotsBeforeStaleForAmm: new BN(100),
Expand Down Expand Up @@ -392,7 +392,7 @@ describe('liquidate perp (no open orders)', () => {
assert(
driftClient
.getUserAccount()
.perpPositions[0].quoteAssetAmount.eq(new BN(-5767508))
.perpPositions[0].quoteAssetAmount.eq(new BN(-5767653))
);

// try to add liq when bankrupt -- should fail
Expand Down Expand Up @@ -470,7 +470,11 @@ describe('liquidate perp (no open orders)', () => {
eventSubscriber.getEventsArray('LiquidationRecord')[0];
assert(isVariant(perpBankruptcyRecord.liquidationType, 'perpBankruptcy'));
assert(perpBankruptcyRecord.perpBankruptcy.marketIndex === 0);
assert(perpBankruptcyRecord.perpBankruptcy.pnl.eq(new BN(-5767508)));
console.log(perpBankruptcyRecord.perpBankruptcy.pnl.toString());
console.log(
perpBankruptcyRecord.perpBankruptcy.cumulativeFundingRateDelta.toString()
);
assert(perpBankruptcyRecord.perpBankruptcy.pnl.eq(new BN(-5767653)));
console.log(
perpBankruptcyRecord.perpBankruptcy.cumulativeFundingRateDelta.toString()
);
Expand All @@ -485,7 +489,7 @@ describe('liquidate perp (no open orders)', () => {
market.amm.cumulativeFundingRateLong.toString(),
market.amm.cumulativeFundingRateShort.toString()
);
assert(market.amm.cumulativeFundingRateLong.eq(new BN(328572000)));
assert(market.amm.cumulativeFundingRateShort.eq(new BN(-328572000)));
assert(market.amm.cumulativeFundingRateLong.eq(new BN(328580333)));
assert(market.amm.cumulativeFundingRateShort.eq(new BN(-328563667)));
});
});
12 changes: 6 additions & 6 deletions tests/liquidatePerpAndLp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('liquidate perp and lp', () => {
// const expectedLiqPrice = 0.521639;
const liqPrice = driftClientUser.liquidationPrice(0, ZERO);
console.log('liqPrice:', liqPrice.toString());
const expectedLiqPrice2 = new BN('500341');
const expectedLiqPrice2 = new BN('500350');
console.log('expected liqPrice:', expectedLiqPrice2.toString());

assert(liqPrice.eq(expectedLiqPrice2));
Expand Down Expand Up @@ -282,7 +282,7 @@ describe('liquidate perp and lp', () => {
const oracleGuardRails: OracleGuardRails = {
priceDivergence: {
markOraclePercentDivergence: PERCENTAGE_PRECISION,
oracleTwap5MinPercentDivergence: PERCENTAGE_PRECISION.div(new BN(10)),
oracleTwap5MinPercentDivergence: PERCENTAGE_PRECISION.mul(new BN(10)),
},
validity: {
slotsBeforeStaleForAmm: new BN(100),
Expand Down Expand Up @@ -389,7 +389,7 @@ describe('liquidate perp and lp', () => {
assert(
driftClient
.getUserAccount()
.perpPositions[0].quoteAssetAmount.eq(new BN(-4447508))
.perpPositions[0].quoteAssetAmount.eq(new BN(-4447653))
);

// try to add liq when bankrupt -- should fail
Expand Down Expand Up @@ -476,7 +476,7 @@ describe('liquidate perp and lp', () => {
console.log(
perpBankruptcyRecord.perpBankruptcy.cumulativeFundingRateDelta.toString()
);
assert(perpBankruptcyRecord.perpBankruptcy.pnl.eq(new BN(-4447508)));
assert(perpBankruptcyRecord.perpBankruptcy.pnl.eq(new BN(-4447653)));
console.log(
perpBankruptcyRecord.perpBankruptcy.cumulativeFundingRateDelta.toString()
);
Expand All @@ -491,7 +491,7 @@ describe('liquidate perp and lp', () => {
// market.amm.cumulativeFundingRateLong.toString(),
// market.amm.cumulativeFundingRateShort.toString()
// );
assert(market.amm.cumulativeFundingRateLong.eq(new BN(253144000)));
assert(market.amm.cumulativeFundingRateShort.eq(new BN(-253144000)));
assert(market.amm.cumulativeFundingRateLong.eq(new BN(253152333)));
assert(market.amm.cumulativeFundingRateShort.eq(new BN(-253135667)));
});
});
15 changes: 10 additions & 5 deletions tests/oracleOffsetOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,12 @@ describe('oracle offset', () => {
const position = driftClientUser.getPerpPosition(marketIndex);
const breakEvenPrice = calculateBreakEvenPrice(position);
console.log(breakEvenPrice.toString());
const entryPrice = calculateEntryPrice(position);
console.log(entryPrice.toString());
const expectedBreakEvenPrice = new BN(949810);
const expectedEntryPrice = new BN(950000);
console.log(breakEvenPrice.toString(), 'vs', expectedEntryPrice.toString());
assert(breakEvenPrice.eq(expectedEntryPrice));
const entryPrice = calculateEntryPrice(position);
assert(breakEvenPrice.eq(expectedBreakEvenPrice));
assert(entryPrice.eq(expectedEntryPrice));

await driftClient.unsubscribe();
Expand Down Expand Up @@ -452,10 +454,13 @@ describe('oracle offset', () => {

await driftClientUser.fetchAccounts();
const position = driftClientUser.getPerpPosition(marketIndex);
const entryPrice = calculateBreakEvenPrice(position);
console.log(entryPrice.toString());
const expectedEntryPrice = PRICE_PRECISION.add(priceOffset);
const breakEvenPrice = calculateBreakEvenPrice(position);
const entryPrice = calculateEntryPrice(position);
console.log(breakEvenPrice.toString());
console.log(entryPrice.toString());
const expectedEntryPrice = new BN(1050000);
const expectedBreakEvenPrice = new BN(1050210);
assert(breakEvenPrice.eq(expectedBreakEvenPrice));
assert(entryPrice.eq(expectedEntryPrice));

await driftClient.unsubscribe();
Expand Down
4 changes: 3 additions & 1 deletion tests/postOnly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,17 @@ describe('post only', () => {
await driftClientUser.fetchAccounts();
const position = driftClientUser.getPerpPosition(marketIndex);
assert(position.baseAssetAmount.abs().eq(baseAssetAmount));
assert(position.quoteBreakEvenAmount.eq(new BN(1000000)));
assert(position.quoteBreakEvenAmount.eq(new BN(1000200)));
assert(driftClient.getQuoteAssetTokenAmount().eq(usdcAmount));
assert(driftClient.getUserStats().getAccount().fees.totalFeePaid.eq(ZERO));
assert(driftClient.getUserStats().getAccount().fees.totalFeeRebate.eq(new BN(200)));

await fillerDriftClient.fetchAccounts();
const orderRecord = eventSubscriber.getEventsArray('OrderActionRecord')[0];

assert(isVariant(orderRecord.action, 'fill'));
assert(orderRecord.takerFee.eq(new BN(0)));
console.log(orderRecord.quoteAssetAmountSurplus.toString());
assert(orderRecord.quoteAssetAmountSurplus.eq(new BN(19492)));

await driftClient.unsubscribe();
Expand Down

0 comments on commit 7cee59e

Please sign in to comment.