@drift-labs/sdk 2.107.0-beta.16 → 2.107.0-beta.17

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.
Files changed (36) hide show
  1. package/VERSION +1 -1
  2. package/lib/browser/dlob/DLOB.d.ts +2 -1
  3. package/lib/browser/dlob/DLOB.js +14 -13
  4. package/lib/browser/dlob/DLOBNode.d.ts +2 -2
  5. package/lib/browser/dlob/DLOBNode.js +2 -2
  6. package/lib/browser/dlob/DLOBSubscriber.d.ts +1 -0
  7. package/lib/browser/dlob/DLOBSubscriber.js +3 -2
  8. package/lib/browser/dlob/types.d.ts +2 -1
  9. package/lib/browser/math/orders.d.ts +1 -1
  10. package/lib/browser/math/orders.js +10 -1
  11. package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +2 -2
  12. package/lib/browser/orderSubscriber/OrderSubscriber.js +4 -4
  13. package/lib/browser/userMap/userMap.d.ts +1 -1
  14. package/lib/browser/userMap/userMap.js +2 -2
  15. package/lib/node/dlob/DLOB.d.ts +2 -1
  16. package/lib/node/dlob/DLOB.js +14 -13
  17. package/lib/node/dlob/DLOBNode.d.ts +2 -2
  18. package/lib/node/dlob/DLOBNode.js +2 -2
  19. package/lib/node/dlob/DLOBSubscriber.d.ts +1 -0
  20. package/lib/node/dlob/DLOBSubscriber.js +3 -2
  21. package/lib/node/dlob/types.d.ts +2 -1
  22. package/lib/node/math/orders.d.ts +1 -1
  23. package/lib/node/math/orders.js +10 -1
  24. package/lib/node/orderSubscriber/OrderSubscriber.d.ts +2 -2
  25. package/lib/node/orderSubscriber/OrderSubscriber.js +4 -4
  26. package/lib/node/userMap/userMap.d.ts +1 -1
  27. package/lib/node/userMap/userMap.js +2 -2
  28. package/package.json +1 -1
  29. package/src/dlob/DLOB.ts +43 -13
  30. package/src/dlob/DLOBNode.ts +17 -3
  31. package/src/dlob/DLOBSubscriber.ts +8 -3
  32. package/src/dlob/types.ts +2 -1
  33. package/src/driftClient.ts +2 -2
  34. package/src/math/orders.ts +12 -1
  35. package/src/orderSubscriber/OrderSubscriber.ts +7 -4
  36. package/src/userMap/userMap.ts +5 -2
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.107.0-beta.16
1
+ 2.107.0-beta.17
@@ -54,7 +54,8 @@ export declare class DLOB {
54
54
  orderLists: Map<MarketTypeStr, Map<number, MarketNodeLists>>;
55
55
  maxSlotForRestingLimitOrders: number;
56
56
  initialized: boolean;
57
- constructor();
57
+ protectedMakerView: boolean;
58
+ constructor(protectedMakerView?: boolean);
58
59
  private init;
59
60
  clear(): void;
60
61
  /**
@@ -14,11 +14,12 @@ const SUPPORTED_ORDER_TYPES = [
14
14
  'oracle',
15
15
  ];
16
16
  class DLOB {
17
- constructor() {
17
+ constructor(protectedMakerView) {
18
18
  this.openOrders = new Map();
19
19
  this.orderLists = new Map();
20
20
  this.maxSlotForRestingLimitOrders = 0;
21
21
  this.initialized = false;
22
+ this.protectedMakerView = protectedMakerView || false;
22
23
  this.init();
23
24
  }
24
25
  init() {
@@ -373,7 +374,7 @@ class DLOB {
373
374
  if (sameUser) {
374
375
  continue;
375
376
  }
376
- const makerPrice = makerNode.getPrice(oraclePriceData, slot);
377
+ const makerPrice = makerNode.getPrice(oraclePriceData, slot, this.protectedMakerView);
377
378
  const takerPrice = takerNode.getPrice(oraclePriceData, slot);
378
379
  const ordersCross = doesCross(takerPrice, makerPrice);
379
380
  if (!ordersCross) {
@@ -571,8 +572,8 @@ class DLOB {
571
572
  ];
572
573
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
573
574
  return bestNode
574
- .getPrice(oraclePriceData, slot)
575
- .lt(currentNode.getPrice(oraclePriceData, slot));
575
+ .getPrice(oraclePriceData, slot, this.protectedMakerView)
576
+ .lt(currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView));
576
577
  }, filterFcn);
577
578
  }
578
579
  *getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData, filterFcn) {
@@ -591,8 +592,8 @@ class DLOB {
591
592
  ];
592
593
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
593
594
  return bestNode
594
- .getPrice(oraclePriceData, slot)
595
- .gt(currentNode.getPrice(oraclePriceData, slot));
595
+ .getPrice(oraclePriceData, slot, this.protectedMakerView)
596
+ .gt(currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView));
596
597
  }, filterFcn);
597
598
  }
598
599
  /**
@@ -614,8 +615,8 @@ class DLOB {
614
615
  ];
615
616
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
616
617
  var _a, _b;
617
- const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot)) !== null && _a !== void 0 ? _a : __1.ZERO;
618
- const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot)) !== null && _b !== void 0 ? _b : __1.ZERO;
618
+ const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _a !== void 0 ? _a : __1.ZERO;
619
+ const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _b !== void 0 ? _b : __1.ZERO;
619
620
  if (bestNodePrice.eq(currentNodePrice)) {
620
621
  return bestNode.order.slot.lt(currentNode.order.slot);
621
622
  }
@@ -641,8 +642,8 @@ class DLOB {
641
642
  ];
642
643
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
643
644
  var _a, _b;
644
- const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot)) !== null && _a !== void 0 ? _a : __1.BN_MAX;
645
- const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot)) !== null && _b !== void 0 ? _b : __1.BN_MAX;
645
+ const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _a !== void 0 ? _a : __1.BN_MAX;
646
+ const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _b !== void 0 ? _b : __1.BN_MAX;
646
647
  if (bestNodePrice.eq(currentNodePrice)) {
647
648
  return bestNode.order.slot.lt(currentNode.order.slot);
648
649
  }
@@ -654,8 +655,8 @@ class DLOB {
654
655
  for (const askNode of this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData)) {
655
656
  const bidGenerator = this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData);
656
657
  for (const bidNode of bidGenerator) {
657
- const bidPrice = bidNode.getPrice(oraclePriceData, slot);
658
- const askPrice = askNode.getPrice(oraclePriceData, slot);
658
+ const bidPrice = bidNode.getPrice(oraclePriceData, slot, this.protectedMakerView);
659
+ const askPrice = askNode.getPrice(oraclePriceData, slot, this.protectedMakerView);
659
660
  // orders don't cross
660
661
  if (bidPrice.lt(askPrice)) {
661
662
  break;
@@ -730,7 +731,7 @@ class DLOB {
730
731
  getBestAsk(marketIndex, slot, marketType, oraclePriceData) {
731
732
  const bestAsk = this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData).next().value;
732
733
  if (bestAsk) {
733
- return bestAsk.getPrice(oraclePriceData, slot);
734
+ return bestAsk.getPrice(oraclePriceData, slot, this.protectedMakerView);
734
735
  }
735
736
  return undefined;
736
737
  }
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { BN, OraclePriceData, Order } from '..';
3
3
  export interface DLOBNode {
4
- getPrice(oraclePriceData: OraclePriceData, slot: number): BN;
4
+ getPrice(oraclePriceData: OraclePriceData, slot: number, protectedMakerView?: boolean): BN;
5
5
  isVammNode(): boolean;
6
6
  order: Order | undefined;
7
7
  isBaseFilled(): boolean;
@@ -21,7 +21,7 @@ export declare abstract class OrderNode implements DLOBNode {
21
21
  constructor(order: Order, userAccount: string, isUserProtectedMaker: boolean, isSwift?: boolean);
22
22
  abstract getSortValue(order: Order): BN;
23
23
  getLabel(): string;
24
- getPrice(oraclePriceData: OraclePriceData, slot: number): BN;
24
+ getPrice(oraclePriceData: OraclePriceData, slot: number, protectedMakerView?: boolean): BN;
25
25
  isBaseFilled(): boolean;
26
26
  isVammNode(): boolean;
27
27
  }
@@ -28,8 +28,8 @@ class OrderNode {
28
28
  }
29
29
  return msg;
30
30
  }
31
- getPrice(oraclePriceData, slot) {
32
- return (0, __1.getLimitPrice)(this.order, oraclePriceData, slot);
31
+ getPrice(oraclePriceData, slot, protectedMakerView) {
32
+ return (0, __1.getLimitPrice)(this.order, oraclePriceData, slot, undefined, protectedMakerView && this.isUserProtectedMaker);
33
33
  }
34
34
  isBaseFilled() {
35
35
  return this.order.baseAssetAmountFilled.eq(this.order.baseAssetAmount);
@@ -15,6 +15,7 @@ export declare class DLOBSubscriber {
15
15
  intervalId?: NodeJS.Timeout;
16
16
  dlob: DLOB;
17
17
  eventEmitter: StrictEventEmitter<EventEmitter, DLOBSubscriberEvents>;
18
+ protectedMakerView: boolean;
18
19
  constructor(config: DLOBSubscriptionConfig);
19
20
  subscribe(): Promise<void>;
20
21
  updateDLOB(): Promise<void>;
@@ -7,11 +7,12 @@ const types_1 = require("../types");
7
7
  const orderBookLevels_1 = require("./orderBookLevels");
8
8
  class DLOBSubscriber {
9
9
  constructor(config) {
10
- this.dlob = new DLOB_1.DLOB();
11
10
  this.driftClient = config.driftClient;
12
11
  this.dlobSource = config.dlobSource;
13
12
  this.slotSource = config.slotSource;
14
13
  this.updateFrequency = config.updateFrequency;
14
+ this.protectedMakerView = config.protectedMakerView || false;
15
+ this.dlob = new DLOB_1.DLOB(this.protectedMakerView);
15
16
  this.eventEmitter = new events_1.EventEmitter();
16
17
  }
17
18
  async subscribe() {
@@ -30,7 +31,7 @@ class DLOBSubscriber {
30
31
  }, this.updateFrequency);
31
32
  }
32
33
  async updateDLOB() {
33
- this.dlob = await this.dlobSource.getDLOB(this.slotSource.getSlot());
34
+ this.dlob = await this.dlobSource.getDLOB(this.slotSource.getSlot(), this.protectedMakerView);
34
35
  }
35
36
  getDLOB() {
36
37
  return this.dlob;
@@ -5,13 +5,14 @@ export type DLOBSubscriptionConfig = {
5
5
  dlobSource: DLOBSource;
6
6
  slotSource: SlotSource;
7
7
  updateFrequency: number;
8
+ protectedMakerView?: boolean;
8
9
  };
9
10
  export interface DLOBSubscriberEvents {
10
11
  update: (dlob: DLOB) => void;
11
12
  error: (e: Error) => void;
12
13
  }
13
14
  export interface DLOBSource {
14
- getDLOB(slot: number): Promise<DLOB>;
15
+ getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
15
16
  }
16
17
  export interface SlotSource {
17
18
  getSlot(): number;
@@ -8,7 +8,7 @@ export declare function isOrderRiskIncreasingInSameDirection(user: User, order:
8
8
  export declare function isOrderReduceOnly(user: User, order: Order): boolean;
9
9
  export declare function standardizeBaseAssetAmount(baseAssetAmount: BN, stepSize: BN): BN;
10
10
  export declare function standardizePrice(price: BN, tickSize: BN, direction: PositionDirection): BN;
11
- export declare function getLimitPrice(order: Order, oraclePriceData: OraclePriceData, slot: number, fallbackPrice?: BN): BN | undefined;
11
+ export declare function getLimitPrice(order: Order, oraclePriceData: OraclePriceData, slot: number, fallbackPrice?: BN, protectedMaker?: boolean): BN | undefined;
12
12
  export declare function hasLimitPrice(order: Order, slot: number): boolean;
13
13
  export declare function hasAuctionPrice(order: Order, slot: number): boolean;
14
14
  export declare function isFillableByVAMM(order: Order, market: PerpMarketAccount, oraclePriceData: OraclePriceData, slot: number, ts: number, minAuctionDuration: number): boolean;
@@ -96,7 +96,7 @@ function standardizePrice(price, tickSize, direction) {
96
96
  }
97
97
  }
98
98
  exports.standardizePrice = standardizePrice;
99
- function getLimitPrice(order, oraclePriceData, slot, fallbackPrice) {
99
+ function getLimitPrice(order, oraclePriceData, slot, fallbackPrice, protectedMaker) {
100
100
  let limitPrice;
101
101
  if (hasAuctionPrice(order, slot)) {
102
102
  limitPrice = (0, auction_1.getAuctionPrice)(order, slot, oraclePriceData.price);
@@ -110,6 +110,15 @@ function getLimitPrice(order, oraclePriceData, slot, fallbackPrice) {
110
110
  else {
111
111
  limitPrice = order.price;
112
112
  }
113
+ if (protectedMaker) {
114
+ const offset = limitPrice.divn(1000);
115
+ if ((0, types_1.isVariant)(order.direction, 'long')) {
116
+ limitPrice = limitPrice.sub(offset);
117
+ }
118
+ else {
119
+ limitPrice = limitPrice.add(offset);
120
+ }
121
+ }
113
122
  return limitPrice;
114
123
  }
115
124
  exports.getLimitPrice = getLimitPrice;
@@ -35,8 +35,8 @@ export declare class OrderSubscriber {
35
35
  * caller to extend the DLOB Subscriber with a custom DLOB type.
36
36
  * @returns New, empty DLOB object.
37
37
  */
38
- protected createDLOB(): DLOB;
39
- getDLOB(slot: number): Promise<DLOB>;
38
+ protected createDLOB(protectedMakerView?: boolean): DLOB;
39
+ getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
40
40
  getSlot(): number;
41
41
  unsubscribe(): Promise<void>;
42
42
  }
@@ -147,11 +147,11 @@ class OrderSubscriber {
147
147
  * caller to extend the DLOB Subscriber with a custom DLOB type.
148
148
  * @returns New, empty DLOB object.
149
149
  */
150
- createDLOB() {
151
- return new DLOB_1.DLOB();
150
+ createDLOB(protectedMakerView) {
151
+ return new DLOB_1.DLOB(protectedMakerView);
152
152
  }
153
- async getDLOB(slot) {
154
- const dlob = this.createDLOB();
153
+ async getDLOB(slot, protectedMakerView) {
154
+ const dlob = this.createDLOB(protectedMakerView);
155
155
  for (const [key, { userAccount }] of this.usersAccounts.entries()) {
156
156
  const protectedMaker = (0, userStatus_1.isUserProtectedMaker)(userAccount);
157
157
  for (const order of userAccount.orders) {
@@ -66,7 +66,7 @@ export declare class UserMap implements UserMapInterface {
66
66
  * create a DLOB from all the subscribed users
67
67
  * @param slot
68
68
  */
69
- getDLOB(slot: number): Promise<DLOB>;
69
+ getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
70
70
  updateWithOrderRecord(record: OrderRecord): Promise<void>;
71
71
  updateWithEventRecord(record: WrappedEvent<any>): Promise<void>;
72
72
  values(): IterableIterator<User>;
@@ -165,8 +165,8 @@ class UserMap {
165
165
  * create a DLOB from all the subscribed users
166
166
  * @param slot
167
167
  */
168
- async getDLOB(slot) {
169
- const dlob = new __1.DLOB();
168
+ async getDLOB(slot, protectedMakerView) {
169
+ const dlob = new __1.DLOB(protectedMakerView);
170
170
  await dlob.initFromUserMap(this, slot);
171
171
  return dlob;
172
172
  }
@@ -54,7 +54,8 @@ export declare class DLOB {
54
54
  orderLists: Map<MarketTypeStr, Map<number, MarketNodeLists>>;
55
55
  maxSlotForRestingLimitOrders: number;
56
56
  initialized: boolean;
57
- constructor();
57
+ protectedMakerView: boolean;
58
+ constructor(protectedMakerView?: boolean);
58
59
  private init;
59
60
  clear(): void;
60
61
  /**
@@ -14,11 +14,12 @@ const SUPPORTED_ORDER_TYPES = [
14
14
  'oracle',
15
15
  ];
16
16
  class DLOB {
17
- constructor() {
17
+ constructor(protectedMakerView) {
18
18
  this.openOrders = new Map();
19
19
  this.orderLists = new Map();
20
20
  this.maxSlotForRestingLimitOrders = 0;
21
21
  this.initialized = false;
22
+ this.protectedMakerView = protectedMakerView || false;
22
23
  this.init();
23
24
  }
24
25
  init() {
@@ -373,7 +374,7 @@ class DLOB {
373
374
  if (sameUser) {
374
375
  continue;
375
376
  }
376
- const makerPrice = makerNode.getPrice(oraclePriceData, slot);
377
+ const makerPrice = makerNode.getPrice(oraclePriceData, slot, this.protectedMakerView);
377
378
  const takerPrice = takerNode.getPrice(oraclePriceData, slot);
378
379
  const ordersCross = doesCross(takerPrice, makerPrice);
379
380
  if (!ordersCross) {
@@ -571,8 +572,8 @@ class DLOB {
571
572
  ];
572
573
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
573
574
  return bestNode
574
- .getPrice(oraclePriceData, slot)
575
- .lt(currentNode.getPrice(oraclePriceData, slot));
575
+ .getPrice(oraclePriceData, slot, this.protectedMakerView)
576
+ .lt(currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView));
576
577
  }, filterFcn);
577
578
  }
578
579
  *getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData, filterFcn) {
@@ -591,8 +592,8 @@ class DLOB {
591
592
  ];
592
593
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
593
594
  return bestNode
594
- .getPrice(oraclePriceData, slot)
595
- .gt(currentNode.getPrice(oraclePriceData, slot));
595
+ .getPrice(oraclePriceData, slot, this.protectedMakerView)
596
+ .gt(currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView));
596
597
  }, filterFcn);
597
598
  }
598
599
  /**
@@ -614,8 +615,8 @@ class DLOB {
614
615
  ];
615
616
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
616
617
  var _a, _b;
617
- const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot)) !== null && _a !== void 0 ? _a : __1.ZERO;
618
- const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot)) !== null && _b !== void 0 ? _b : __1.ZERO;
618
+ const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _a !== void 0 ? _a : __1.ZERO;
619
+ const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _b !== void 0 ? _b : __1.ZERO;
619
620
  if (bestNodePrice.eq(currentNodePrice)) {
620
621
  return bestNode.order.slot.lt(currentNode.order.slot);
621
622
  }
@@ -641,8 +642,8 @@ class DLOB {
641
642
  ];
642
643
  yield* this.getBestNode(generatorList, oraclePriceData, slot, (bestNode, currentNode, slot, oraclePriceData) => {
643
644
  var _a, _b;
644
- const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot)) !== null && _a !== void 0 ? _a : __1.BN_MAX;
645
- const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot)) !== null && _b !== void 0 ? _b : __1.BN_MAX;
645
+ const bestNodePrice = (_a = bestNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _a !== void 0 ? _a : __1.BN_MAX;
646
+ const currentNodePrice = (_b = currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView)) !== null && _b !== void 0 ? _b : __1.BN_MAX;
646
647
  if (bestNodePrice.eq(currentNodePrice)) {
647
648
  return bestNode.order.slot.lt(currentNode.order.slot);
648
649
  }
@@ -654,8 +655,8 @@ class DLOB {
654
655
  for (const askNode of this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData)) {
655
656
  const bidGenerator = this.getRestingLimitBids(marketIndex, slot, marketType, oraclePriceData);
656
657
  for (const bidNode of bidGenerator) {
657
- const bidPrice = bidNode.getPrice(oraclePriceData, slot);
658
- const askPrice = askNode.getPrice(oraclePriceData, slot);
658
+ const bidPrice = bidNode.getPrice(oraclePriceData, slot, this.protectedMakerView);
659
+ const askPrice = askNode.getPrice(oraclePriceData, slot, this.protectedMakerView);
659
660
  // orders don't cross
660
661
  if (bidPrice.lt(askPrice)) {
661
662
  break;
@@ -730,7 +731,7 @@ class DLOB {
730
731
  getBestAsk(marketIndex, slot, marketType, oraclePriceData) {
731
732
  const bestAsk = this.getRestingLimitAsks(marketIndex, slot, marketType, oraclePriceData).next().value;
732
733
  if (bestAsk) {
733
- return bestAsk.getPrice(oraclePriceData, slot);
734
+ return bestAsk.getPrice(oraclePriceData, slot, this.protectedMakerView);
734
735
  }
735
736
  return undefined;
736
737
  }
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { BN, OraclePriceData, Order } from '..';
3
3
  export interface DLOBNode {
4
- getPrice(oraclePriceData: OraclePriceData, slot: number): BN;
4
+ getPrice(oraclePriceData: OraclePriceData, slot: number, protectedMakerView?: boolean): BN;
5
5
  isVammNode(): boolean;
6
6
  order: Order | undefined;
7
7
  isBaseFilled(): boolean;
@@ -21,7 +21,7 @@ export declare abstract class OrderNode implements DLOBNode {
21
21
  constructor(order: Order, userAccount: string, isUserProtectedMaker: boolean, isSwift?: boolean);
22
22
  abstract getSortValue(order: Order): BN;
23
23
  getLabel(): string;
24
- getPrice(oraclePriceData: OraclePriceData, slot: number): BN;
24
+ getPrice(oraclePriceData: OraclePriceData, slot: number, protectedMakerView?: boolean): BN;
25
25
  isBaseFilled(): boolean;
26
26
  isVammNode(): boolean;
27
27
  }
@@ -28,8 +28,8 @@ class OrderNode {
28
28
  }
29
29
  return msg;
30
30
  }
31
- getPrice(oraclePriceData, slot) {
32
- return (0, __1.getLimitPrice)(this.order, oraclePriceData, slot);
31
+ getPrice(oraclePriceData, slot, protectedMakerView) {
32
+ return (0, __1.getLimitPrice)(this.order, oraclePriceData, slot, undefined, protectedMakerView && this.isUserProtectedMaker);
33
33
  }
34
34
  isBaseFilled() {
35
35
  return this.order.baseAssetAmountFilled.eq(this.order.baseAssetAmount);
@@ -15,6 +15,7 @@ export declare class DLOBSubscriber {
15
15
  intervalId?: NodeJS.Timeout;
16
16
  dlob: DLOB;
17
17
  eventEmitter: StrictEventEmitter<EventEmitter, DLOBSubscriberEvents>;
18
+ protectedMakerView: boolean;
18
19
  constructor(config: DLOBSubscriptionConfig);
19
20
  subscribe(): Promise<void>;
20
21
  updateDLOB(): Promise<void>;
@@ -7,11 +7,12 @@ const types_1 = require("../types");
7
7
  const orderBookLevels_1 = require("./orderBookLevels");
8
8
  class DLOBSubscriber {
9
9
  constructor(config) {
10
- this.dlob = new DLOB_1.DLOB();
11
10
  this.driftClient = config.driftClient;
12
11
  this.dlobSource = config.dlobSource;
13
12
  this.slotSource = config.slotSource;
14
13
  this.updateFrequency = config.updateFrequency;
14
+ this.protectedMakerView = config.protectedMakerView || false;
15
+ this.dlob = new DLOB_1.DLOB(this.protectedMakerView);
15
16
  this.eventEmitter = new events_1.EventEmitter();
16
17
  }
17
18
  async subscribe() {
@@ -30,7 +31,7 @@ class DLOBSubscriber {
30
31
  }, this.updateFrequency);
31
32
  }
32
33
  async updateDLOB() {
33
- this.dlob = await this.dlobSource.getDLOB(this.slotSource.getSlot());
34
+ this.dlob = await this.dlobSource.getDLOB(this.slotSource.getSlot(), this.protectedMakerView);
34
35
  }
35
36
  getDLOB() {
36
37
  return this.dlob;
@@ -5,13 +5,14 @@ export type DLOBSubscriptionConfig = {
5
5
  dlobSource: DLOBSource;
6
6
  slotSource: SlotSource;
7
7
  updateFrequency: number;
8
+ protectedMakerView?: boolean;
8
9
  };
9
10
  export interface DLOBSubscriberEvents {
10
11
  update: (dlob: DLOB) => void;
11
12
  error: (e: Error) => void;
12
13
  }
13
14
  export interface DLOBSource {
14
- getDLOB(slot: number): Promise<DLOB>;
15
+ getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
15
16
  }
16
17
  export interface SlotSource {
17
18
  getSlot(): number;
@@ -8,7 +8,7 @@ export declare function isOrderRiskIncreasingInSameDirection(user: User, order:
8
8
  export declare function isOrderReduceOnly(user: User, order: Order): boolean;
9
9
  export declare function standardizeBaseAssetAmount(baseAssetAmount: BN, stepSize: BN): BN;
10
10
  export declare function standardizePrice(price: BN, tickSize: BN, direction: PositionDirection): BN;
11
- export declare function getLimitPrice(order: Order, oraclePriceData: OraclePriceData, slot: number, fallbackPrice?: BN): BN | undefined;
11
+ export declare function getLimitPrice(order: Order, oraclePriceData: OraclePriceData, slot: number, fallbackPrice?: BN, protectedMaker?: boolean): BN | undefined;
12
12
  export declare function hasLimitPrice(order: Order, slot: number): boolean;
13
13
  export declare function hasAuctionPrice(order: Order, slot: number): boolean;
14
14
  export declare function isFillableByVAMM(order: Order, market: PerpMarketAccount, oraclePriceData: OraclePriceData, slot: number, ts: number, minAuctionDuration: number): boolean;
@@ -96,7 +96,7 @@ function standardizePrice(price, tickSize, direction) {
96
96
  }
97
97
  }
98
98
  exports.standardizePrice = standardizePrice;
99
- function getLimitPrice(order, oraclePriceData, slot, fallbackPrice) {
99
+ function getLimitPrice(order, oraclePriceData, slot, fallbackPrice, protectedMaker) {
100
100
  let limitPrice;
101
101
  if (hasAuctionPrice(order, slot)) {
102
102
  limitPrice = (0, auction_1.getAuctionPrice)(order, slot, oraclePriceData.price);
@@ -110,6 +110,15 @@ function getLimitPrice(order, oraclePriceData, slot, fallbackPrice) {
110
110
  else {
111
111
  limitPrice = order.price;
112
112
  }
113
+ if (protectedMaker) {
114
+ const offset = limitPrice.divn(1000);
115
+ if ((0, types_1.isVariant)(order.direction, 'long')) {
116
+ limitPrice = limitPrice.sub(offset);
117
+ }
118
+ else {
119
+ limitPrice = limitPrice.add(offset);
120
+ }
121
+ }
113
122
  return limitPrice;
114
123
  }
115
124
  exports.getLimitPrice = getLimitPrice;
@@ -35,8 +35,8 @@ export declare class OrderSubscriber {
35
35
  * caller to extend the DLOB Subscriber with a custom DLOB type.
36
36
  * @returns New, empty DLOB object.
37
37
  */
38
- protected createDLOB(): DLOB;
39
- getDLOB(slot: number): Promise<DLOB>;
38
+ protected createDLOB(protectedMakerView?: boolean): DLOB;
39
+ getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
40
40
  getSlot(): number;
41
41
  unsubscribe(): Promise<void>;
42
42
  }
@@ -147,11 +147,11 @@ class OrderSubscriber {
147
147
  * caller to extend the DLOB Subscriber with a custom DLOB type.
148
148
  * @returns New, empty DLOB object.
149
149
  */
150
- createDLOB() {
151
- return new DLOB_1.DLOB();
150
+ createDLOB(protectedMakerView) {
151
+ return new DLOB_1.DLOB(protectedMakerView);
152
152
  }
153
- async getDLOB(slot) {
154
- const dlob = this.createDLOB();
153
+ async getDLOB(slot, protectedMakerView) {
154
+ const dlob = this.createDLOB(protectedMakerView);
155
155
  for (const [key, { userAccount }] of this.usersAccounts.entries()) {
156
156
  const protectedMaker = (0, userStatus_1.isUserProtectedMaker)(userAccount);
157
157
  for (const order of userAccount.orders) {
@@ -66,7 +66,7 @@ export declare class UserMap implements UserMapInterface {
66
66
  * create a DLOB from all the subscribed users
67
67
  * @param slot
68
68
  */
69
- getDLOB(slot: number): Promise<DLOB>;
69
+ getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
70
70
  updateWithOrderRecord(record: OrderRecord): Promise<void>;
71
71
  updateWithEventRecord(record: WrappedEvent<any>): Promise<void>;
72
72
  values(): IterableIterator<User>;
@@ -165,8 +165,8 @@ class UserMap {
165
165
  * create a DLOB from all the subscribed users
166
166
  * @param slot
167
167
  */
168
- async getDLOB(slot) {
169
- const dlob = new __1.DLOB();
168
+ async getDLOB(slot, protectedMakerView) {
169
+ const dlob = new __1.DLOB(protectedMakerView);
170
170
  await dlob.initFromUserMap(this, slot);
171
171
  return dlob;
172
172
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.107.0-beta.16",
3
+ "version": "2.107.0-beta.17",
4
4
  "main": "lib/node/index.js",
5
5
  "types": "lib/node/index.d.ts",
6
6
  "browser": "./lib/browser/index.js",
package/src/dlob/DLOB.ts CHANGED
@@ -109,7 +109,10 @@ export class DLOB {
109
109
 
110
110
  initialized = false;
111
111
 
112
- public constructor() {
112
+ protectedMakerView: boolean;
113
+
114
+ public constructor(protectedMakerView?: boolean) {
115
+ this.protectedMakerView = protectedMakerView || false;
113
116
  this.init();
114
117
  }
115
118
 
@@ -762,7 +765,11 @@ export class DLOB {
762
765
  continue;
763
766
  }
764
767
 
765
- const makerPrice = makerNode.getPrice(oraclePriceData, slot);
768
+ const makerPrice = makerNode.getPrice(
769
+ oraclePriceData,
770
+ slot,
771
+ this.protectedMakerView
772
+ );
766
773
  const takerPrice = takerNode.getPrice(oraclePriceData, slot);
767
774
 
768
775
  const ordersCross = doesCross(takerPrice, makerPrice);
@@ -1082,8 +1089,10 @@ export class DLOB {
1082
1089
  slot,
1083
1090
  (bestNode, currentNode, slot, oraclePriceData) => {
1084
1091
  return bestNode
1085
- .getPrice(oraclePriceData, slot)
1086
- .lt(currentNode.getPrice(oraclePriceData, slot));
1092
+ .getPrice(oraclePriceData, slot, this.protectedMakerView)
1093
+ .lt(
1094
+ currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView)
1095
+ );
1087
1096
  },
1088
1097
  filterFcn
1089
1098
  );
@@ -1120,8 +1129,10 @@ export class DLOB {
1120
1129
  slot,
1121
1130
  (bestNode, currentNode, slot, oraclePriceData) => {
1122
1131
  return bestNode
1123
- .getPrice(oraclePriceData, slot)
1124
- .gt(currentNode.getPrice(oraclePriceData, slot));
1132
+ .getPrice(oraclePriceData, slot, this.protectedMakerView)
1133
+ .gt(
1134
+ currentNode.getPrice(oraclePriceData, slot, this.protectedMakerView)
1135
+ );
1125
1136
  },
1126
1137
  filterFcn
1127
1138
  );
@@ -1158,9 +1169,15 @@ export class DLOB {
1158
1169
  oraclePriceData,
1159
1170
  slot,
1160
1171
  (bestNode, currentNode, slot, oraclePriceData) => {
1161
- const bestNodePrice = bestNode.getPrice(oraclePriceData, slot) ?? ZERO;
1172
+ const bestNodePrice =
1173
+ bestNode.getPrice(oraclePriceData, slot, this.protectedMakerView) ??
1174
+ ZERO;
1162
1175
  const currentNodePrice =
1163
- currentNode.getPrice(oraclePriceData, slot) ?? ZERO;
1176
+ currentNode.getPrice(
1177
+ oraclePriceData,
1178
+ slot,
1179
+ this.protectedMakerView
1180
+ ) ?? ZERO;
1164
1181
 
1165
1182
  if (bestNodePrice.eq(currentNodePrice)) {
1166
1183
  return bestNode.order.slot.lt(currentNode.order.slot);
@@ -1204,9 +1221,14 @@ export class DLOB {
1204
1221
  slot,
1205
1222
  (bestNode, currentNode, slot, oraclePriceData) => {
1206
1223
  const bestNodePrice =
1207
- bestNode.getPrice(oraclePriceData, slot) ?? BN_MAX;
1224
+ bestNode.getPrice(oraclePriceData, slot, this.protectedMakerView) ??
1225
+ BN_MAX;
1208
1226
  const currentNodePrice =
1209
- currentNode.getPrice(oraclePriceData, slot) ?? BN_MAX;
1227
+ currentNode.getPrice(
1228
+ oraclePriceData,
1229
+ slot,
1230
+ this.protectedMakerView
1231
+ ) ?? BN_MAX;
1210
1232
 
1211
1233
  if (bestNodePrice.eq(currentNodePrice)) {
1212
1234
  return bestNode.order.slot.lt(currentNode.order.slot);
@@ -1240,8 +1262,16 @@ export class DLOB {
1240
1262
  );
1241
1263
 
1242
1264
  for (const bidNode of bidGenerator) {
1243
- const bidPrice = bidNode.getPrice(oraclePriceData, slot);
1244
- const askPrice = askNode.getPrice(oraclePriceData, slot);
1265
+ const bidPrice = bidNode.getPrice(
1266
+ oraclePriceData,
1267
+ slot,
1268
+ this.protectedMakerView
1269
+ );
1270
+ const askPrice = askNode.getPrice(
1271
+ oraclePriceData,
1272
+ slot,
1273
+ this.protectedMakerView
1274
+ );
1245
1275
 
1246
1276
  // orders don't cross
1247
1277
  if (bidPrice.lt(askPrice)) {
@@ -1356,7 +1386,7 @@ export class DLOB {
1356
1386
  ).next().value;
1357
1387
 
1358
1388
  if (bestAsk) {
1359
- return bestAsk.getPrice(oraclePriceData, slot);
1389
+ return bestAsk.getPrice(oraclePriceData, slot, this.protectedMakerView);
1360
1390
  }
1361
1391
  return undefined;
1362
1392
  }
@@ -13,7 +13,11 @@ import {
13
13
  import { getOrderSignature } from './NodeList';
14
14
 
15
15
  export interface DLOBNode {
16
- getPrice(oraclePriceData: OraclePriceData, slot: number): BN;
16
+ getPrice(
17
+ oraclePriceData: OraclePriceData,
18
+ slot: number,
19
+ protectedMakerView?: boolean
20
+ ): BN;
17
21
  isVammNode(): boolean;
18
22
  order: Order | undefined;
19
23
  isBaseFilled(): boolean;
@@ -75,8 +79,18 @@ export abstract class OrderNode implements DLOBNode {
75
79
  return msg;
76
80
  }
77
81
 
78
- getPrice(oraclePriceData: OraclePriceData, slot: number): BN {
79
- return getLimitPrice(this.order, oraclePriceData, slot);
82
+ getPrice(
83
+ oraclePriceData: OraclePriceData,
84
+ slot: number,
85
+ protectedMakerView?: boolean
86
+ ): BN {
87
+ return getLimitPrice(
88
+ this.order,
89
+ oraclePriceData,
90
+ slot,
91
+ undefined,
92
+ protectedMakerView && this.isUserProtectedMaker
93
+ );
80
94
  }
81
95
 
82
96
  isBaseFilled(): boolean {
@@ -23,14 +23,16 @@ export class DLOBSubscriber {
23
23
  slotSource: SlotSource;
24
24
  updateFrequency: number;
25
25
  intervalId?: NodeJS.Timeout;
26
- dlob = new DLOB();
26
+ dlob: DLOB;
27
27
  public eventEmitter: StrictEventEmitter<EventEmitter, DLOBSubscriberEvents>;
28
-
28
+ protectedMakerView: boolean;
29
29
  constructor(config: DLOBSubscriptionConfig) {
30
30
  this.driftClient = config.driftClient;
31
31
  this.dlobSource = config.dlobSource;
32
32
  this.slotSource = config.slotSource;
33
33
  this.updateFrequency = config.updateFrequency;
34
+ this.protectedMakerView = config.protectedMakerView || false;
35
+ this.dlob = new DLOB(this.protectedMakerView);
34
36
  this.eventEmitter = new EventEmitter();
35
37
  }
36
38
 
@@ -52,7 +54,10 @@ export class DLOBSubscriber {
52
54
  }
53
55
 
54
56
  async updateDLOB(): Promise<void> {
55
- this.dlob = await this.dlobSource.getDLOB(this.slotSource.getSlot());
57
+ this.dlob = await this.dlobSource.getDLOB(
58
+ this.slotSource.getSlot(),
59
+ this.protectedMakerView
60
+ );
56
61
  }
57
62
 
58
63
  public getDLOB(): DLOB {
package/src/dlob/types.ts CHANGED
@@ -6,6 +6,7 @@ export type DLOBSubscriptionConfig = {
6
6
  dlobSource: DLOBSource;
7
7
  slotSource: SlotSource;
8
8
  updateFrequency: number;
9
+ protectedMakerView?: boolean;
9
10
  };
10
11
 
11
12
  export interface DLOBSubscriberEvents {
@@ -14,7 +15,7 @@ export interface DLOBSubscriberEvents {
14
15
  }
15
16
 
16
17
  export interface DLOBSource {
17
- getDLOB(slot: number): Promise<DLOB>;
18
+ getDLOB(slot: number, protectedMakerView?: boolean): Promise<DLOB>;
18
19
  }
19
20
 
20
21
  export interface SlotSource {
@@ -182,7 +182,7 @@ import nacl from 'tweetnacl';
182
182
  import { Slothash } from './slot/SlothashSubscriber';
183
183
  import { getOracleId } from './oracles/oracleId';
184
184
  import { SignedSwiftOrderParams } from './swift/types';
185
- import { hash } from '@coral-xyz/anchor/dist/cjs/utils/sha256';
185
+ import { sha256 } from '@noble/hashes/sha256';
186
186
 
187
187
  type RemainingAccountParams = {
188
188
  userAccounts: UserAccount[];
@@ -5884,7 +5884,7 @@ export class DriftClient {
5884
5884
  orderParamsMessage: SwiftOrderParamsMessage
5885
5885
  ): Buffer {
5886
5886
  const anchorIxName = 'global' + ':' + 'swiftOrderMessageParams';
5887
- const prefix = Buffer.from(hash(anchorIxName).slice(0, 8));
5887
+ const prefix = Buffer.from(sha256(anchorIxName).slice(0, 8));
5888
5888
  const buf = Buffer.concat([
5889
5889
  prefix,
5890
5890
  this.program.coder.types.encode(
@@ -154,7 +154,8 @@ export function getLimitPrice(
154
154
  order: Order,
155
155
  oraclePriceData: OraclePriceData,
156
156
  slot: number,
157
- fallbackPrice?: BN
157
+ fallbackPrice?: BN,
158
+ protectedMaker?: boolean
158
159
  ): BN | undefined {
159
160
  let limitPrice;
160
161
  if (hasAuctionPrice(order, slot)) {
@@ -170,6 +171,16 @@ export function getLimitPrice(
170
171
  limitPrice = order.price;
171
172
  }
172
173
 
174
+ if (protectedMaker) {
175
+ const offset = limitPrice.divn(1000);
176
+
177
+ if (isVariant(order.direction, 'long')) {
178
+ limitPrice = limitPrice.sub(offset);
179
+ } else {
180
+ limitPrice = limitPrice.add(offset);
181
+ }
182
+ }
183
+
173
184
  return limitPrice;
174
185
  }
175
186
 
@@ -223,12 +223,15 @@ export class OrderSubscriber {
223
223
  * caller to extend the DLOB Subscriber with a custom DLOB type.
224
224
  * @returns New, empty DLOB object.
225
225
  */
226
- protected createDLOB(): DLOB {
227
- return new DLOB();
226
+ protected createDLOB(protectedMakerView?: boolean): DLOB {
227
+ return new DLOB(protectedMakerView);
228
228
  }
229
229
 
230
- public async getDLOB(slot: number): Promise<DLOB> {
231
- const dlob = this.createDLOB();
230
+ public async getDLOB(
231
+ slot: number,
232
+ protectedMakerView?: boolean
233
+ ): Promise<DLOB> {
234
+ const dlob = this.createDLOB(protectedMakerView);
232
235
  for (const [key, { userAccount }] of this.usersAccounts.entries()) {
233
236
  const protectedMaker = isUserProtectedMaker(userAccount);
234
237
  for (const order of userAccount.orders) {
@@ -281,8 +281,11 @@ export class UserMap implements UserMapInterface {
281
281
  * create a DLOB from all the subscribed users
282
282
  * @param slot
283
283
  */
284
- public async getDLOB(slot: number): Promise<DLOB> {
285
- const dlob = new DLOB();
284
+ public async getDLOB(
285
+ slot: number,
286
+ protectedMakerView?: boolean
287
+ ): Promise<DLOB> {
288
+ const dlob = new DLOB(protectedMakerView);
286
289
  await dlob.initFromUserMap(this, slot);
287
290
  return dlob;
288
291
  }