@drift-labs/sdk 2.130.0-beta.8 → 2.130.0

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 (103) hide show
  1. package/VERSION +1 -1
  2. package/lib/browser/accounts/grpcAccountSubscriber.d.ts +1 -0
  3. package/lib/browser/accounts/grpcAccountSubscriber.js +16 -12
  4. package/lib/browser/accounts/grpcProgramAccountSubscriber.d.ts +1 -0
  5. package/lib/browser/accounts/grpcProgramAccountSubscriber.js +16 -12
  6. package/lib/browser/accounts/types.d.ts +5 -0
  7. package/lib/browser/adminClient.d.ts +2 -2
  8. package/lib/browser/adminClient.js +14 -14
  9. package/lib/browser/dlob/DLOB.d.ts +3 -3
  10. package/lib/browser/dlob/DLOB.js +14 -8
  11. package/lib/browser/dlob/DLOBNode.d.ts +5 -3
  12. package/lib/browser/dlob/DLOBNode.js +12 -11
  13. package/lib/browser/dlob/DLOBSubscriber.js +1 -1
  14. package/lib/browser/dlob/NodeList.d.ts +3 -1
  15. package/lib/browser/dlob/NodeList.js +2 -2
  16. package/lib/browser/dlob/orderBookLevels.d.ts +6 -0
  17. package/lib/browser/dlob/orderBookLevels.js +5 -2
  18. package/lib/browser/driftClient.js +15 -2
  19. package/lib/browser/idl/drift.json +82 -11
  20. package/lib/browser/math/amm.d.ts +4 -0
  21. package/lib/browser/math/amm.js +25 -9
  22. package/lib/browser/math/market.d.ts +10 -0
  23. package/lib/browser/math/market.js +76 -1
  24. package/lib/browser/math/orders.d.ts +1 -0
  25. package/lib/browser/math/orders.js +13 -1
  26. package/lib/browser/math/state.d.ts +1 -0
  27. package/lib/browser/math/state.js +6 -1
  28. package/lib/browser/oracles/types.d.ts +1 -0
  29. package/lib/browser/oracles/utils.js +1 -3
  30. package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +1 -1
  31. package/lib/browser/orderSubscriber/OrderSubscriber.js +10 -3
  32. package/lib/browser/types.d.ts +7 -0
  33. package/lib/browser/types.js +6 -1
  34. package/lib/node/accounts/grpcAccountSubscriber.d.ts +1 -0
  35. package/lib/node/accounts/grpcAccountSubscriber.d.ts.map +1 -1
  36. package/lib/node/accounts/grpcAccountSubscriber.js +16 -12
  37. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts +1 -0
  38. package/lib/node/accounts/grpcProgramAccountSubscriber.d.ts.map +1 -1
  39. package/lib/node/accounts/grpcProgramAccountSubscriber.js +16 -12
  40. package/lib/node/accounts/types.d.ts +5 -0
  41. package/lib/node/accounts/types.d.ts.map +1 -1
  42. package/lib/node/adminClient.d.ts +2 -2
  43. package/lib/node/adminClient.d.ts.map +1 -1
  44. package/lib/node/adminClient.js +14 -14
  45. package/lib/node/dlob/DLOB.d.ts +3 -3
  46. package/lib/node/dlob/DLOB.d.ts.map +1 -1
  47. package/lib/node/dlob/DLOB.js +14 -8
  48. package/lib/node/dlob/DLOBNode.d.ts +5 -3
  49. package/lib/node/dlob/DLOBNode.d.ts.map +1 -1
  50. package/lib/node/dlob/DLOBNode.js +12 -11
  51. package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
  52. package/lib/node/dlob/DLOBSubscriber.js +1 -1
  53. package/lib/node/dlob/NodeList.d.ts +3 -1
  54. package/lib/node/dlob/NodeList.d.ts.map +1 -1
  55. package/lib/node/dlob/NodeList.js +2 -2
  56. package/lib/node/dlob/orderBookLevels.d.ts +6 -0
  57. package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
  58. package/lib/node/dlob/orderBookLevels.js +5 -2
  59. package/lib/node/driftClient.d.ts.map +1 -1
  60. package/lib/node/driftClient.js +15 -2
  61. package/lib/node/idl/drift.json +82 -11
  62. package/lib/node/math/amm.d.ts +4 -0
  63. package/lib/node/math/amm.d.ts.map +1 -1
  64. package/lib/node/math/amm.js +25 -9
  65. package/lib/node/math/market.d.ts +10 -0
  66. package/lib/node/math/market.d.ts.map +1 -1
  67. package/lib/node/math/market.js +76 -1
  68. package/lib/node/math/orders.d.ts +1 -0
  69. package/lib/node/math/orders.d.ts.map +1 -1
  70. package/lib/node/math/orders.js +13 -1
  71. package/lib/node/math/state.d.ts +1 -0
  72. package/lib/node/math/state.d.ts.map +1 -1
  73. package/lib/node/math/state.js +6 -1
  74. package/lib/node/oracles/types.d.ts +1 -0
  75. package/lib/node/oracles/types.d.ts.map +1 -1
  76. package/lib/node/oracles/utils.d.ts.map +1 -1
  77. package/lib/node/oracles/utils.js +1 -3
  78. package/lib/node/orderSubscriber/OrderSubscriber.d.ts +1 -1
  79. package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
  80. package/lib/node/orderSubscriber/OrderSubscriber.js +10 -3
  81. package/lib/node/types.d.ts +7 -0
  82. package/lib/node/types.d.ts.map +1 -1
  83. package/lib/node/types.js +6 -1
  84. package/package.json +1 -1
  85. package/src/accounts/grpcAccountSubscriber.ts +24 -11
  86. package/src/accounts/grpcProgramAccountSubscriber.ts +26 -11
  87. package/src/accounts/types.ts +5 -0
  88. package/src/adminClient.ts +29 -27
  89. package/src/dlob/DLOB.ts +30 -6
  90. package/src/dlob/DLOBNode.ts +21 -10
  91. package/src/dlob/DLOBSubscriber.ts +2 -1
  92. package/src/dlob/NodeList.ts +5 -2
  93. package/src/dlob/orderBookLevels.ts +8 -2
  94. package/src/driftClient.ts +18 -4
  95. package/src/idl/drift.json +83 -12
  96. package/src/math/amm.ts +49 -10
  97. package/src/math/market.ts +105 -0
  98. package/src/math/orders.ts +18 -0
  99. package/src/math/state.ts +7 -1
  100. package/src/oracles/types.ts +1 -0
  101. package/src/oracles/utils.ts +1 -3
  102. package/src/orderSubscriber/OrderSubscriber.ts +16 -2
  103. package/src/types.ts +9 -0
@@ -20,6 +20,7 @@ export class grpcProgramAccountSubscriber<
20
20
  private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
21
21
  private commitmentLevel: CommitmentLevel;
22
22
  public listenerId?: number;
23
+ private enableReconnect: boolean;
23
24
 
24
25
  private constructor(
25
26
  client: Client,
@@ -31,7 +32,8 @@ export class grpcProgramAccountSubscriber<
31
32
  options: { filters: MemcmpFilter[] } = {
32
33
  filters: [],
33
34
  },
34
- resubOpts?: ResubOpts
35
+ resubOpts?: ResubOpts,
36
+ enableReconnect = false
35
37
  ) {
36
38
  super(
37
39
  subscriptionName,
@@ -43,6 +45,7 @@ export class grpcProgramAccountSubscriber<
43
45
  );
44
46
  this.client = client;
45
47
  this.commitmentLevel = commitmentLevel;
48
+ this.enableReconnect = enableReconnect;
46
49
  }
47
50
 
48
51
  public static async create<U>(
@@ -73,7 +76,8 @@ export class grpcProgramAccountSubscriber<
73
76
  program,
74
77
  decodeBufferFn,
75
78
  options,
76
- resubOpts
79
+ resubOpts,
80
+ grpcConfigs.enableReconnect
77
81
  );
78
82
  }
79
83
 
@@ -119,15 +123,26 @@ export class grpcProgramAccountSubscriber<
119
123
  entry: {},
120
124
  transactionsStatus: {},
121
125
  };
122
- this.stream.on('error', (error) => {
123
- // @ts-ignore
124
- if (error.code === 1) {
125
- // expected: 1 CANCELLED: Cancelled on client
126
- return;
127
- } else {
128
- console.error('GRPC unexpected error caught:', error);
129
- }
130
- });
126
+
127
+ if (this.enableReconnect) {
128
+ this.stream.on('error', (error) => {
129
+ // @ts-ignore
130
+ if (error.code === 1) {
131
+ // expected: 1 CANCELLED: Cancelled on client
132
+ console.error(
133
+ 'GRPC (grpcProgramAccountSubscriber) Cancelled on client caught:',
134
+ error
135
+ );
136
+ return;
137
+ } else {
138
+ console.error(
139
+ 'GRPC (grpcProgramAccountSubscriber) unexpected error caught:',
140
+ error
141
+ );
142
+ }
143
+ });
144
+ }
145
+
131
146
  this.stream.on('data', (chunk: SubscribeUpdate) => {
132
147
  if (!chunk.account) {
133
148
  return;
@@ -226,6 +226,11 @@ export type GrpcConfigs = {
226
226
  token: string;
227
227
  commitmentLevel?: CommitmentLevel;
228
228
  channelOptions?: ChannelOptions;
229
+ /**
230
+ * Whether to enable automatic reconnection on connection loss .
231
+ * Defaults to false, will throw on connection loss.
232
+ */
233
+ enableReconnect?: boolean;
229
234
  };
230
235
 
231
236
  export interface HighLeverageModeConfigAccountSubscriber {
@@ -4552,33 +4552,6 @@ export class AdminClient extends DriftClient {
4552
4552
  });
4553
4553
  }
4554
4554
 
4555
- public async updateDisableBitFlagsMMOracle(
4556
- disable: boolean
4557
- ): Promise<TransactionSignature> {
4558
- const updateDisableBitFlagsMMOracleIx =
4559
- await this.getUpdateDisableBitFlagsMMOracleIx(disable);
4560
-
4561
- const tx = await this.buildTransaction(updateDisableBitFlagsMMOracleIx);
4562
- const { txSig } = await this.sendTransaction(tx, [], this.opts);
4563
-
4564
- return txSig;
4565
- }
4566
- public async getUpdateDisableBitFlagsMMOracleIx(
4567
- disable: boolean
4568
- ): Promise<TransactionInstruction> {
4569
- return await this.program.instruction.updateDisableBitflagsMmOracle(
4570
- disable,
4571
- {
4572
- accounts: {
4573
- admin: this.isSubscribed
4574
- ? this.getStateAccount().admin
4575
- : this.wallet.publicKey,
4576
- state: await this.getStatePublicKey(),
4577
- },
4578
- }
4579
- );
4580
- }
4581
-
4582
4555
  public async zeroMMOracleFields(
4583
4556
  marketIndex: number
4584
4557
  ): Promise<TransactionSignature> {
@@ -4591,6 +4564,7 @@ export class AdminClient extends DriftClient {
4591
4564
 
4592
4565
  return txSig;
4593
4566
  }
4567
+
4594
4568
  public async getZeroMMOracleFieldsIx(
4595
4569
  marketIndex: number
4596
4570
  ): Promise<TransactionInstruction> {
@@ -4607,4 +4581,32 @@ export class AdminClient extends DriftClient {
4607
4581
  },
4608
4582
  });
4609
4583
  }
4584
+
4585
+ public async updateFeatureBitFlagsMMOracle(
4586
+ enable: boolean
4587
+ ): Promise<TransactionSignature> {
4588
+ const updateFeatureBitFlagsMMOracleIx =
4589
+ await this.getUpdateFeatureBitFlagsMMOracleIx(enable);
4590
+
4591
+ const tx = await this.buildTransaction(updateFeatureBitFlagsMMOracleIx);
4592
+ const { txSig } = await this.sendTransaction(tx, [], this.opts);
4593
+
4594
+ return txSig;
4595
+ }
4596
+
4597
+ public async getUpdateFeatureBitFlagsMMOracleIx(
4598
+ enable: boolean
4599
+ ): Promise<TransactionInstruction> {
4600
+ return await this.program.instruction.updateFeatureBitFlagsMmOracle(
4601
+ enable,
4602
+ {
4603
+ accounts: {
4604
+ admin: this.useHotWalletAdmin
4605
+ ? this.wallet.publicKey
4606
+ : this.getStateAccount().admin,
4607
+ state: await this.getStatePublicKey(),
4608
+ },
4609
+ }
4610
+ );
4611
+ }
4610
4612
  }
package/src/dlob/DLOB.ts CHANGED
@@ -11,6 +11,7 @@ import { decodeName } from '../userName';
11
11
  import { DLOBNode, DLOBNodeType, TriggerOrderNode } from './DLOBNode';
12
12
  import { DriftClient } from '../driftClient';
13
13
  import {
14
+ calculateOrderBaseAssetAmount,
14
15
  getLimitPrice,
15
16
  isOrderExpired,
16
17
  isRestingLimitOrder,
@@ -180,7 +181,26 @@ export class DLOB {
180
181
  const protectedMaker = isUserProtectedMaker(userAccount);
181
182
 
182
183
  for (const order of userAccount.orders) {
183
- this.insertOrder(order, userAccountPubkeyString, slot, protectedMaker);
184
+ let baseAssetAmount = order.baseAssetAmount;
185
+ if (order.reduceOnly) {
186
+ const existingBaseAmount =
187
+ userAccount.perpPositions.find(
188
+ (pos) =>
189
+ pos.marketIndex === order.marketIndex && pos.openOrders > 0
190
+ )?.baseAssetAmount || ZERO;
191
+ baseAssetAmount = calculateOrderBaseAssetAmount(
192
+ order,
193
+ existingBaseAmount
194
+ );
195
+ }
196
+
197
+ this.insertOrder(
198
+ order,
199
+ userAccountPubkeyString,
200
+ slot,
201
+ protectedMaker,
202
+ baseAssetAmount
203
+ );
184
204
  }
185
205
  }
186
206
 
@@ -193,6 +213,7 @@ export class DLOB {
193
213
  userAccount: string,
194
214
  slot: number,
195
215
  isUserProtectedMaker: boolean,
216
+ baseAssetAmount: BN,
196
217
  onInsert?: OrderBookCallback
197
218
  ): void {
198
219
  if (!isVariant(order.status, 'open')) {
@@ -220,7 +241,8 @@ export class DLOB {
220
241
  marketType,
221
242
  userAccount,
222
243
  isUserProtectedMaker,
223
- this.protectedMakerParamsMap[marketType].get(order.marketIndex)
244
+ this.protectedMakerParamsMap[marketType].get(order.marketIndex),
245
+ baseAssetAmount
224
246
  );
225
247
 
226
248
  if (onInsert) {
@@ -232,6 +254,7 @@ export class DLOB {
232
254
  order: Order,
233
255
  userAccount: string,
234
256
  isUserProtectedMaker: boolean,
257
+ baseAssetAmount?: BN,
235
258
  onInsert?: OrderBookCallback
236
259
  ): void {
237
260
  const marketType = getVariant(order.marketType) as MarketTypeStr;
@@ -251,7 +274,8 @@ export class DLOB {
251
274
  marketType,
252
275
  userAccount,
253
276
  isUserProtectedMaker,
254
- this.protectedMakerParamsMap[marketType].get(order.marketIndex)
277
+ this.protectedMakerParamsMap[marketType].get(order.marketIndex),
278
+ baseAssetAmount
255
279
  );
256
280
  if (onInsert) {
257
281
  onInsert();
@@ -1558,7 +1582,7 @@ export class DLOB {
1558
1582
  public findNodesToTrigger(
1559
1583
  marketIndex: number,
1560
1584
  slot: number,
1561
- oraclePrice: BN,
1585
+ triggerPrice: BN,
1562
1586
  marketType: MarketType,
1563
1587
  stateAccount: StateAccount
1564
1588
  ): NodeToTrigger[] {
@@ -1575,7 +1599,7 @@ export class DLOB {
1575
1599
  : undefined;
1576
1600
  if (triggerAboveList) {
1577
1601
  for (const node of triggerAboveList.getGenerator()) {
1578
- if (oraclePrice.gt(node.order.triggerPrice)) {
1602
+ if (triggerPrice.gt(node.order.triggerPrice)) {
1579
1603
  nodesToTrigger.push({
1580
1604
  node: node,
1581
1605
  });
@@ -1590,7 +1614,7 @@ export class DLOB {
1590
1614
  : undefined;
1591
1615
  if (triggerBelowList) {
1592
1616
  for (const node of triggerBelowList.getGenerator()) {
1593
- if (oraclePrice.lt(node.order.triggerPrice)) {
1617
+ if (triggerPrice.lt(node.order.triggerPrice)) {
1594
1618
  nodesToTrigger.push({
1595
1619
  node: node,
1596
1620
  });
@@ -20,6 +20,7 @@ export interface DLOBNode {
20
20
  isProtectedMaker: boolean;
21
21
  protectedMakerParams?: ProtectedMakerParams;
22
22
  isSignedMsg: boolean | undefined;
23
+ baseAssetAmount: BN;
23
24
  }
24
25
 
25
26
  export abstract class OrderNode implements DLOBNode {
@@ -30,6 +31,7 @@ export abstract class OrderNode implements DLOBNode {
30
31
  haveTrigger = false;
31
32
  isProtectedMaker: boolean;
32
33
  protectedMakerParams?: ProtectedMakerParams;
34
+ baseAssetAmount: BN;
33
35
  isSignedMsg: boolean;
34
36
 
35
37
  constructor(
@@ -37,6 +39,7 @@ export abstract class OrderNode implements DLOBNode {
37
39
  userAccount: string,
38
40
  isProtectedMaker: boolean,
39
41
  protectedMakerParams?: ProtectedMakerParams,
42
+ baseAssetAmount?: BN,
40
43
  isSignedMsg = false
41
44
  ) {
42
45
  // Copy the order over to the node
@@ -45,6 +48,7 @@ export abstract class OrderNode implements DLOBNode {
45
48
  this.sortValue = this.getSortValue(order);
46
49
  this.isProtectedMaker = isProtectedMaker;
47
50
  this.protectedMakerParams = protectedMakerParams;
51
+ this.baseAssetAmount = baseAssetAmount ?? order.baseAssetAmount;
48
52
  this.isSignedMsg = isSignedMsg;
49
53
  }
50
54
 
@@ -156,8 +160,8 @@ export class SignedMsgOrderNode extends OrderNode {
156
160
  next?: SignedMsgOrderNode;
157
161
  previous?: SignedMsgOrderNode;
158
162
 
159
- constructor(order: Order, userAccount: string) {
160
- super(order, userAccount, false, undefined, true);
163
+ constructor(order: Order, userAccount: string, baseAssetAmount?: BN) {
164
+ super(order, userAccount, false, undefined, baseAssetAmount, true);
161
165
  }
162
166
 
163
167
  getSortValue(order: Order): BN {
@@ -189,7 +193,8 @@ export function createNode<T extends DLOBNodeType>(
189
193
  order: Order,
190
194
  userAccount: string,
191
195
  isProtectedMaker: boolean,
192
- protectedMakerParams?: ProtectedMakerParams
196
+ protectedMakerParams?: ProtectedMakerParams,
197
+ baseAssetAmount?: BN
193
198
  ): DLOBNodeMap[T] {
194
199
  switch (nodeType) {
195
200
  case 'floatingLimit':
@@ -197,45 +202,51 @@ export function createNode<T extends DLOBNodeType>(
197
202
  order,
198
203
  userAccount,
199
204
  isProtectedMaker,
200
- protectedMakerParams
205
+ protectedMakerParams,
206
+ baseAssetAmount
201
207
  );
202
208
  case 'protectedFloatingLimit':
203
209
  return new FloatingLimitOrderNode(
204
210
  order,
205
211
  userAccount,
206
212
  isProtectedMaker,
207
- protectedMakerParams
213
+ protectedMakerParams,
214
+ baseAssetAmount
208
215
  );
209
216
  case 'restingLimit':
210
217
  return new RestingLimitOrderNode(
211
218
  order,
212
219
  userAccount,
213
220
  isProtectedMaker,
214
- protectedMakerParams
221
+ protectedMakerParams,
222
+ baseAssetAmount
215
223
  );
216
224
  case 'takingLimit':
217
225
  return new TakingLimitOrderNode(
218
226
  order,
219
227
  userAccount,
220
228
  isProtectedMaker,
221
- protectedMakerParams
229
+ protectedMakerParams,
230
+ baseAssetAmount
222
231
  );
223
232
  case 'market':
224
233
  return new MarketOrderNode(
225
234
  order,
226
235
  userAccount,
227
236
  isProtectedMaker,
228
- undefined
237
+ undefined,
238
+ baseAssetAmount
229
239
  );
230
240
  case 'trigger':
231
241
  return new TriggerOrderNode(
232
242
  order,
233
243
  userAccount,
234
244
  isProtectedMaker,
235
- undefined
245
+ undefined,
246
+ baseAssetAmount
236
247
  );
237
248
  case 'signedMsg':
238
- return new SignedMsgOrderNode(order, userAccount);
249
+ return new SignedMsgOrderNode(order, userAccount, baseAssetAmount);
239
250
  default:
240
251
  throw Error(`Unknown DLOBNode type ${nodeType}`);
241
252
  }
@@ -139,7 +139,8 @@ export class DLOBSubscriber {
139
139
  fallbackL2Generators = [
140
140
  getVammL2Generator({
141
141
  marketAccount: this.driftClient.getPerpMarketAccount(marketIndex),
142
- oraclePriceData,
142
+ oraclePriceData:
143
+ this.driftClient.getMMOracleDataForPerpMarket(marketIndex),
143
144
  numOrders: numVammOrders ?? depth,
144
145
  topOfBookQuoteAmounts:
145
146
  marketIndex < 3
@@ -5,6 +5,7 @@ import {
5
5
  ProtectedMakerParams,
6
6
  } from '../types';
7
7
  import { createNode, DLOBNode, DLOBNodeMap } from './DLOBNode';
8
+ import { BN } from '@coral-xyz/anchor';
8
9
 
9
10
  export type SortDirection = 'asc' | 'desc';
10
11
 
@@ -42,7 +43,8 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
42
43
  marketType: MarketTypeStr,
43
44
  userAccount: string,
44
45
  isProtectedMaker: boolean,
45
- protectedMakerParamsMap?: ProtectedMakerParams
46
+ protectedMakerParamsMap?: ProtectedMakerParams,
47
+ baseAssetAmount?: BN
46
48
  ): void {
47
49
  if (!isVariant(order.status, 'open')) {
48
50
  return;
@@ -53,7 +55,8 @@ export class NodeList<NodeType extends keyof DLOBNodeMap>
53
55
  order,
54
56
  userAccount,
55
57
  isProtectedMaker,
56
- protectedMakerParamsMap
58
+ protectedMakerParamsMap,
59
+ baseAssetAmount
57
60
  );
58
61
 
59
62
  const orderSignature = getOrderSignature(order.orderId, userAccount);
@@ -78,22 +78,28 @@ export const MAJORS_TOP_OF_BOOK_QUOTE_AMOUNTS = [
78
78
  new BN(50000).mul(QUOTE_PRECISION),
79
79
  ];
80
80
 
81
+ const INDICATIVE_QUOTES_PUBKEY = 'inDNdu3ML4vG5LNExqcwuCQtLcCU8KfK5YM2qYV3JJz';
82
+
81
83
  /**
82
84
  * Get an {@link Generator<L2Level>} generator from a {@link Generator<DLOBNode>}
83
85
  * @param dlobNodes e.g. {@link DLOB#getRestingLimitAsks} or {@link DLOB#getRestingLimitBids}
84
86
  * @param oraclePriceData
85
87
  * @param slot
86
88
  */
87
- const INDICATIVE_QUOTES_PUBKEY = 'inDNdu3ML4vG5LNExqcwuCQtLcCU8KfK5YM2qYV3JJz';
88
89
  export function* getL2GeneratorFromDLOBNodes(
89
90
  dlobNodes: Generator<DLOBNode>,
90
91
  oraclePriceData: OraclePriceData,
91
92
  slot: number
92
93
  ): Generator<L2Level> {
93
94
  for (const dlobNode of dlobNodes) {
94
- const size = dlobNode.order.baseAssetAmount.sub(
95
+ const size = dlobNode.baseAssetAmount.sub(
95
96
  dlobNode.order.baseAssetAmountFilled
96
97
  ) as BN;
98
+
99
+ if (size.lte(ZERO)) {
100
+ continue;
101
+ }
102
+
97
103
  yield {
98
104
  size,
99
105
  price: dlobNode.getPrice(oraclePriceData, slot),
@@ -123,6 +123,8 @@ import { TxSender, TxSigAndSlot } from './tx/types';
123
123
  import {
124
124
  BASE_PRECISION,
125
125
  GOV_SPOT_MARKET_INDEX,
126
+ ONE,
127
+ PERCENTAGE_PRECISION,
126
128
  PRICE_PRECISION,
127
129
  QUOTE_PRECISION,
128
130
  QUOTE_SPOT_MARKET_INDEX,
@@ -8485,15 +8487,25 @@ export class DriftClient {
8485
8487
  }
8486
8488
 
8487
8489
  public getOracleDataForPerpMarket(marketIndex: number): OraclePriceData {
8488
- return this.accountSubscriber.getOraclePriceDataAndSlotForPerpMarket(
8489
- marketIndex
8490
- ).data;
8490
+ const perpMarket = this.getPerpMarketAccount(marketIndex);
8491
+ const isMMOracleActive = !perpMarket.amm.mmOracleSlot.eq(ZERO);
8492
+ return {
8493
+ ...this.accountSubscriber.getOraclePriceDataAndSlotForPerpMarket(
8494
+ marketIndex
8495
+ ).data,
8496
+ isMMOracleActive,
8497
+ };
8491
8498
  }
8492
8499
 
8493
8500
  public getMMOracleDataForPerpMarket(marketIndex: number): OraclePriceData {
8494
8501
  const perpMarket = this.getPerpMarketAccount(marketIndex);
8495
8502
  const oracleData = this.getOracleDataForPerpMarket(marketIndex);
8496
8503
  const stateAccountAndSlot = this.accountSubscriber.getStateAccountAndSlot();
8504
+ const pctDiff = perpMarket.amm.mmOraclePrice
8505
+ .sub(oracleData.price)
8506
+ .abs()
8507
+ .mul(PERCENTAGE_PRECISION)
8508
+ .div(BN.max(oracleData.price, ONE));
8497
8509
  if (
8498
8510
  !isOracleValid(
8499
8511
  perpMarket,
@@ -8502,7 +8514,8 @@ export class DriftClient {
8502
8514
  stateAccountAndSlot.slot
8503
8515
  ) ||
8504
8516
  perpMarket.amm.mmOraclePrice.eq(ZERO) ||
8505
- perpMarket.amm.mmOracleSlot < oracleData.slot
8517
+ perpMarket.amm.mmOracleSlot < oracleData.slot ||
8518
+ pctDiff.gt(PERCENTAGE_PRECISION.divn(100)) // 1% threshold
8506
8519
  ) {
8507
8520
  return { ...oracleData, fetchedWithMMOracle: true };
8508
8521
  } else {
@@ -8517,6 +8530,7 @@ export class DriftClient {
8517
8530
  confidence: conf,
8518
8531
  hasSufficientNumberOfDataPoints: true,
8519
8532
  fetchedWithMMOracle: true,
8533
+ isMMOracleActive: oracleData.isMMOracleActive,
8520
8534
  };
8521
8535
  }
8522
8536
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "2.129.0",
2
+ "version": "2.130.0",
3
3
  "name": "drift",
4
4
  "instructions": [
5
5
  {
@@ -2099,7 +2099,12 @@
2099
2099
  "isSigner": false
2100
2100
  }
2101
2101
  ],
2102
- "args": []
2102
+ "args": [
2103
+ {
2104
+ "name": "disableMaintenance",
2105
+ "type": "bool"
2106
+ }
2107
+ ]
2103
2108
  },
2104
2109
  {
2105
2110
  "name": "updateUserFuelBonus",
@@ -7531,7 +7536,7 @@
7531
7536
  ]
7532
7537
  },
7533
7538
  {
7534
- "name": "updateDisableBitflagsMmOracle",
7539
+ "name": "updateFeatureBitFlagsMmOracle",
7535
7540
  "accounts": [
7536
7541
  {
7537
7542
  "name": "admin",
@@ -7546,7 +7551,7 @@
7546
7551
  ],
7547
7552
  "args": [
7548
7553
  {
7549
- "name": "disable",
7554
+ "name": "enable",
7550
7555
  "type": "bool"
7551
7556
  }
7552
7557
  ]
@@ -7795,11 +7800,24 @@
7795
7800
  "type": "u8"
7796
7801
  },
7797
7802
  {
7798
- "name": "padding",
7803
+ "name": "padding1",
7799
7804
  "type": {
7800
7805
  "array": [
7801
7806
  "u8",
7802
- 31
7807
+ 3
7808
+ ]
7809
+ }
7810
+ },
7811
+ {
7812
+ "name": "currentMaintenanceUsers",
7813
+ "type": "u32"
7814
+ },
7815
+ {
7816
+ "name": "padding2",
7817
+ "type": {
7818
+ "array": [
7819
+ "u8",
7820
+ 24
7803
7821
  ]
7804
7822
  }
7805
7823
  }
@@ -8324,12 +8342,20 @@
8324
8342
  "name": "protectedMakerDynamicDivisor",
8325
8343
  "type": "u8"
8326
8344
  },
8345
+ {
8346
+ "name": "padding1",
8347
+ "type": "u32"
8348
+ },
8349
+ {
8350
+ "name": "lastFillPrice",
8351
+ "type": "u64"
8352
+ },
8327
8353
  {
8328
8354
  "name": "padding",
8329
8355
  "type": {
8330
8356
  "array": [
8331
8357
  "u8",
8332
- 36
8358
+ 24
8333
8359
  ]
8334
8360
  }
8335
8361
  }
@@ -9060,7 +9086,7 @@
9060
9086
  "type": "u16"
9061
9087
  },
9062
9088
  {
9063
- "name": "disableBitFlags",
9089
+ "name": "featureBitFlags",
9064
9090
  "type": "u8"
9065
9091
  },
9066
9092
  {
@@ -10640,7 +10666,7 @@
10640
10666
  "name": "lastFundingRate",
10641
10667
  "docs": [
10642
10668
  "last funding rate in this perp market (unit is quote per base)",
10643
- "precision: QUOTE_PRECISION"
10669
+ "precision: FUNDING_RATE_PRECISION"
10644
10670
  ],
10645
10671
  "type": "i64"
10646
10672
  },
@@ -10648,7 +10674,7 @@
10648
10674
  "name": "lastFundingRateLong",
10649
10675
  "docs": [
10650
10676
  "last funding rate for longs in this perp market (unit is quote per base)",
10651
- "precision: QUOTE_PRECISION"
10677
+ "precision: FUNDING_RATE_PRECISION"
10652
10678
  ],
10653
10679
  "type": "i64"
10654
10680
  },
@@ -11078,9 +11104,13 @@
11078
11104
  "type": {
11079
11105
  "array": [
11080
11106
  "u8",
11081
- 11
11107
+ 3
11082
11108
  ]
11083
11109
  }
11110
+ },
11111
+ {
11112
+ "name": "lastFundingOracleTwap",
11113
+ "type": "i64"
11084
11114
  }
11085
11115
  ]
11086
11116
  }
@@ -12002,6 +12032,26 @@
12002
12032
  ]
12003
12033
  }
12004
12034
  },
12035
+ {
12036
+ "name": "LogMode",
12037
+ "type": {
12038
+ "kind": "enum",
12039
+ "variants": [
12040
+ {
12041
+ "name": "None"
12042
+ },
12043
+ {
12044
+ "name": "ExchangeOracle"
12045
+ },
12046
+ {
12047
+ "name": "MMOracle"
12048
+ },
12049
+ {
12050
+ "name": "SafeMMOracle"
12051
+ }
12052
+ ]
12053
+ }
12054
+ },
12005
12055
  {
12006
12056
  "name": "PositionUpdateType",
12007
12057
  "type": {
@@ -12751,6 +12801,20 @@
12751
12801
  ]
12752
12802
  }
12753
12803
  },
12804
+ {
12805
+ "name": "FeatureBitFlags",
12806
+ "type": {
12807
+ "kind": "enum",
12808
+ "variants": [
12809
+ {
12810
+ "name": "MmOracleUpdate"
12811
+ },
12812
+ {
12813
+ "name": "EnableMedianTriggerPrice"
12814
+ }
12815
+ ]
12816
+ }
12817
+ },
12754
12818
  {
12755
12819
  "name": "UserStatus",
12756
12820
  "type": {
@@ -13674,6 +13738,13 @@
13674
13738
  "option": "u64"
13675
13739
  },
13676
13740
  "index": false
13741
+ },
13742
+ {
13743
+ "name": "triggerPrice",
13744
+ "type": {
13745
+ "option": "u64"
13746
+ },
13747
+ "index": false
13677
13748
  }
13678
13749
  ]
13679
13750
  },
@@ -15982,4 +16053,4 @@
15982
16053
  "metadata": {
15983
16054
  "address": "dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH"
15984
16055
  }
15985
- }
16056
+ }