@katanaperps/katana-perps-sdk 2.1.0-beta.15 → 2.1.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"buySellPanelEstimate.d.ts","sourceRoot":"","sources":["../../src/orderbook/buySellPanelEstimate.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EACV,wCAAwC,EACxC,iBAAiB,EACjB,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,6BAA6B,CAAC;AAUrC;;;;;;;GAOG;AACH,MAAM,MAAM,4BAA4B,GACpC;IACE,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,SAAS,CAAC;IAC1B,wBAAwB,CAAC,EAAE,SAAS,CAAC;CACtC,GACD;IACE,YAAY,CAAC,EAAE,SAAS,CAAC;IACzB,aAAa,EAAE,MAAM,CAAC;IACtB,wBAAwB,CAAC,EAAE,SAAS,CAAC;CACtC,GACD;IACE,YAAY,CAAC,EAAE,SAAS,CAAC;IACzB,aAAa,CAAC,EAAE,SAAS,CAAC;IAC1B,sFAAsF;IACtF,wBAAwB,EAAE,MAAM,CAAC;CAClC,CAAC;AAEN;;;;;;;GAOG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,SAAS,CAAC;IAChB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,0CAA0C;IAC1C,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,0BAA0B;IAC1B,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,GAAG,4BAA4B,CAAC;AAEjC,MAAM,WAAW,wBAAwB;IACvC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E;IAC7E,oBAAoB,EAAE,OAAO,CAAC;IAC9B;;;OAGG;IACH,sBAAsB,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,2BAA2B,EAAE,OAAO,CAAC;IACrC;;;OAGG;IACH,2BAA2B,EAAE,OAAO,CAAC;IACrC,8EAA8E;IAC9E,2BAA2B,EAAE,OAAO,CAAC;IACrC,kGAAkG;IAClG,kBAAkB,EAAE,OAAO,CAAC;IAC5B,wHAAwH;IACxH,gCAAgC,EAAE,OAAO,CAAC;IAC1C,wGAAwG;IACxG,yBAAyB,EAAE,OAAO,CAAC;IACnC,wFAAwF;IACxF,gCAAgC,EAAE,OAAO,CAAC;IAC1C,+EAA+E;IAC/E,wBAAwB,EAAE,OAAO,CAAC;IAClC;;;OAGG;IACH,kCAAkC,EAAE,OAAO,CAAC;CAC7C;AAojCD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,6BAA6B,CAC3C,IAAI,EAAE,wBAAwB,GAC7B,oBAAoB,CA4BtB"}
@@ -154,6 +154,7 @@ enforceExecutionPriceLimit) {
154
154
  takerTradeFee: BigInt(0),
155
155
  takerGasFee: BigInt(0),
156
156
  selfTradeBaseQuantity: BigInt(0),
157
+ selfTradeQuoteQuantity: BigInt(0),
157
158
  selfTradeEncountered: false,
158
159
  executionPriceLimitExceeded: false,
159
160
  standingOrdersAfterSelfTrade: standingOrdersCopy,
@@ -231,6 +232,7 @@ enforceExecutionPriceLimit) {
231
232
  if (selfPart > BigInt(0)) {
232
233
  result.selfTradeEncountered = true;
233
234
  result.selfTradeBaseQuantity += selfPart;
235
+ result.selfTradeQuoteQuantity += (0, _pipmath_1.multiplyPips)(selfPart, level.price, context.isMarketOrder && context.isBuy);
234
236
  // Reduce the wallet's own resting orders at this price.
235
237
  let toReduce = selfPart;
236
238
  for (const order of ownOrders) {
@@ -364,8 +366,7 @@ enforceExecutionPriceLimit = true) {
364
366
  const unboundedQuote = context.makerLevels.reduce((sum, level) => sum + (0, _pipmath_1.multiplyPips)(level.size, level.price), BigInt(1));
365
367
  const maxFill = matchTakerOrder(context, { quoteQuantity: unboundedQuote }, reduceOnlyMaximumBaseQuantity, enforceExecutionPriceLimit);
366
368
  fullyFillable =
367
- maxFill.tradeQuoteQuantity +
368
- (0, _pipmath_1.multiplyPips)(maxFill.selfTradeBaseQuantity, context.indexPrice) >=
369
+ maxFill.tradeQuoteQuantity + maxFill.selfTradeQuoteQuantity >=
369
370
  quantity.quoteQuantity;
370
371
  }
371
372
  if (!fullyFillable) {
@@ -373,8 +374,15 @@ enforceExecutionPriceLimit = true) {
373
374
  return estimate;
374
375
  }
375
376
  }
376
- estimate.tradeBaseQuantity = fill.tradeBaseQuantity;
377
- estimate.tradeQuoteQuantity = fill.tradeQuoteQuantity;
377
+ // The crossing (trade) quantity includes any self-traded quantity: the taker
378
+ // order must be submitted with enough quantity to traverse the book and reach
379
+ // the wallet's own resting order, otherwise that self-trade would not occur.
380
+ // (Position, collateral and liquidation effects below use only the real
381
+ // fills, since a self-trade does not change the position or quote balance.)
382
+ estimate.tradeBaseQuantity =
383
+ fill.tradeBaseQuantity + fill.selfTradeBaseQuantity;
384
+ estimate.tradeQuoteQuantity =
385
+ fill.tradeQuoteQuantity + fill.selfTradeQuoteQuantity;
378
386
  // Determine the resting (maker) portion. Reduce-only limit orders may rest on
379
387
  // the books (their reducing portion); whether the resting quantity is valid is
380
388
  // checked below via `reduceOnlyOpenPositionSizeExceeded`.
@@ -387,10 +395,11 @@ enforceExecutionPriceLimit = true) {
387
395
  makerBaseQuantity = fill.remainingBaseQuantity;
388
396
  }
389
397
  else {
390
- // Convert the remaining quote budget to base at the limit price.
398
+ // Convert the remaining quote budget to base at the limit price. The
399
+ // crossing portion (real fills + self-trades) has consumed the rest.
391
400
  const remainingQuote = (0, _pipmath_1.maxBigInt)(quantity.quoteQuantity -
392
401
  fill.tradeQuoteQuantity -
393
- (0, _pipmath_1.multiplyPips)(fill.selfTradeBaseQuantity, context.limitPrice), BigInt(0));
402
+ fill.selfTradeQuoteQuantity, BigInt(0));
394
403
  makerBaseQuantity =
395
404
  context.limitPrice > BigInt(0) ?
396
405
  (0, _pipmath_1.dividePips)(remainingQuote, context.limitPrice)
@@ -328,11 +328,13 @@ describe('orderbook/buySellPanelEstimate', () => {
328
328
  order: { side: request_1.OrderSide.buy, baseQuantity: (0, _pipmath_1.decimalToPip)('5') },
329
329
  });
330
330
  expect(estimate.selfTradeEncountered).to.equal(true);
331
- // Only the 2 units not belonging to the wallet are actually traded
332
- testHelpers.assertBigintsEqual(estimate.tradeBaseQuantity, (0, _pipmath_1.decimalToPip)('2'));
333
- testHelpers.assertBigintsEqual(estimate.tradeQuoteQuantity, (0, _pipmath_1.decimalToPip)('200'));
334
- // Freeing the 30 held by the canceled own order more than offsets the new
335
- // position’s margin (20) and fee (0.2): collateral increases.
331
+ // The order must cross all 5 (the 2 others + the 3 own units) to reach the
332
+ // wallet's own order, so the self-traded 3 count toward the trade quantity.
333
+ testHelpers.assertBigintsEqual(estimate.tradeBaseQuantity, (0, _pipmath_1.decimalToPip)('5'));
334
+ testHelpers.assertBigintsEqual(estimate.tradeQuoteQuantity, (0, _pipmath_1.decimalToPip)('500'));
335
+ // Cost reflects only the real 2-unit fill: freeing the 30 held by the
336
+ // canceled own order more than offsets the new position’s margin (20) and
337
+ // fee (0.2), so collateral increases.
336
338
  testHelpers.assertBigintsEqual(estimate.cost, (0, _pipmath_1.decimalToPip)('-9.8'));
337
339
  });
338
340
  it('flags an order that exceeds free collateral', () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@katanaperps/katana-perps-sdk",
3
- "version": "2.1.0-beta.15",
3
+ "version": "2.1.0-beta.16",
4
4
  "description": "Katana Perps SDK for Javascript in the browser and Node.js",
5
5
  "repository": "git@github.com:katanaperps/katana-perps-sdk-js.git",
6
6
  "license": "MIT",