@drift-labs/sdk 2.49.0-beta.15 → 2.49.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 (43) hide show
  1. package/VERSION +1 -1
  2. package/lib/adminClient.d.ts +1 -0
  3. package/lib/adminClient.js +8 -0
  4. package/lib/decode/user.d.ts +3 -0
  5. package/lib/decode/user.js +328 -0
  6. package/lib/idl/drift.json +31 -1
  7. package/lib/index.d.ts +1 -0
  8. package/lib/index.js +1 -0
  9. package/lib/math/state.d.ts +5 -0
  10. package/lib/math/state.js +30 -0
  11. package/lib/orderSubscriber/OrderSubscriber.d.ts +3 -0
  12. package/lib/orderSubscriber/OrderSubscriber.js +13 -3
  13. package/lib/orderSubscriber/WebsocketSubscription.js +1 -1
  14. package/lib/orderSubscriber/types.d.ts +4 -1
  15. package/lib/types.d.ts +1 -1
  16. package/lib/user.d.ts +1 -1
  17. package/lib/user.js +7 -2
  18. package/lib/userMap/PollingSubscription.js +3 -1
  19. package/lib/userMap/WebsocketSubscription.d.ts +5 -1
  20. package/lib/userMap/WebsocketSubscription.js +3 -2
  21. package/lib/userMap/userMap.d.ts +1 -0
  22. package/lib/userMap/userMap.js +14 -6
  23. package/lib/userMap/userMapConfig.d.ts +1 -0
  24. package/package.json +2 -1
  25. package/src/adminClient.ts +14 -0
  26. package/src/decode/user.ts +357 -0
  27. package/src/idl/drift.json +32 -2
  28. package/src/index.ts +1 -0
  29. package/src/math/state.ts +29 -0
  30. package/src/orderSubscriber/OrderSubscriber.ts +27 -6
  31. package/src/orderSubscriber/WebsocketSubscription.ts +1 -3
  32. package/src/orderSubscriber/types.ts +13 -1
  33. package/src/types.ts +1 -1
  34. package/src/user.ts +12 -2
  35. package/src/userMap/PollingSubscription.ts +6 -4
  36. package/src/userMap/WebsocketSubscription.ts +5 -3
  37. package/src/userMap/userMap.ts +17 -12
  38. package/src/userMap/userMapConfig.ts +3 -0
  39. package/tests/decode/test.ts +266 -0
  40. package/tests/decode/userAccountBufferStrings.ts +102 -0
  41. package/tests/dlob/helpers.ts +1 -0
  42. package/tests/dlob/test.ts +4 -6
  43. package/tests/user/helpers.ts +0 -1
package/VERSION CHANGED
@@ -1 +1 @@
1
- 2.49.0-beta.15
1
+ 2.49.0-beta.17
@@ -37,6 +37,7 @@ export declare class AdminClient extends DriftClient {
37
37
  updateOracleGuardRails(oracleGuardRails: OracleGuardRails): Promise<TransactionSignature>;
38
38
  updateStateSettlementDuration(settlementDuration: number): Promise<TransactionSignature>;
39
39
  updateStateMaxNumberOfSubAccounts(maxNumberOfSubAccounts: number): Promise<TransactionSignature>;
40
+ updateStateMaxInitializeUserFee(maxInitializeUserFee: number): Promise<TransactionSignature>;
40
41
  updateWithdrawGuardThreshold(spotMarketIndex: number, withdrawGuardThreshold: BN): Promise<TransactionSignature>;
41
42
  updateSpotMarketIfFactor(spotMarketIndex: number, userIfFactor: BN, totalIfFactor: BN): Promise<TransactionSignature>;
42
43
  updateSpotMarketRevenueSettlePeriod(spotMarketIndex: number, revenueSettlePeriod: BN): Promise<TransactionSignature>;
@@ -455,6 +455,14 @@ class AdminClient extends driftClient_1.DriftClient {
455
455
  },
456
456
  });
457
457
  }
458
+ async updateStateMaxInitializeUserFee(maxInitializeUserFee) {
459
+ return await this.program.rpc.updateStateMaxInitializeUserFee(maxInitializeUserFee, {
460
+ accounts: {
461
+ admin: this.wallet.publicKey,
462
+ state: await this.getStatePublicKey(),
463
+ },
464
+ });
465
+ }
458
466
  async updateWithdrawGuardThreshold(spotMarketIndex, withdrawGuardThreshold) {
459
467
  const tx = await this.program.transaction.updateWithdrawGuardThreshold(withdrawGuardThreshold, {
460
468
  accounts: {
@@ -0,0 +1,3 @@
1
+ /// <reference types="node" />
2
+ import { UserAccount } from '../types';
3
+ export declare function decodeUser(buffer: Buffer): UserAccount;
@@ -0,0 +1,328 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeUser = void 0;
4
+ const types_1 = require("../types");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ const __1 = require("../");
7
+ const __2 = require("../");
8
+ function readUnsignedBigInt64LE(buffer, offset) {
9
+ return new __1.BN(buffer.subarray(offset, offset + 8), 10, 'le');
10
+ }
11
+ function readSignedBigInt64LE(buffer, offset) {
12
+ const unsignedValue = new __1.BN(buffer.subarray(offset, offset + 8), 10, 'le');
13
+ if (unsignedValue.testn(63)) {
14
+ const inverted = unsignedValue.notn(64).addn(1);
15
+ return inverted.neg();
16
+ }
17
+ else {
18
+ return unsignedValue;
19
+ }
20
+ }
21
+ function decodeUser(buffer) {
22
+ let offset = 8;
23
+ const authority = new web3_js_1.PublicKey(buffer.slice(offset, offset + 32));
24
+ offset += 32;
25
+ const delegate = new web3_js_1.PublicKey(buffer.slice(offset, offset + 32));
26
+ offset += 32;
27
+ const name = [];
28
+ for (let i = 0; i < 32; i++) {
29
+ name.push(buffer.readUint8(offset + i));
30
+ }
31
+ offset += 32;
32
+ const spotPositions = [];
33
+ for (let i = 0; i < 8; i++) {
34
+ const scaledBalance = readUnsignedBigInt64LE(buffer, offset);
35
+ const openOrders = buffer.readUInt8(offset + 35);
36
+ if (scaledBalance.eq(__2.ZERO) && openOrders === 0) {
37
+ offset += 40;
38
+ continue;
39
+ }
40
+ offset += 8;
41
+ const openBids = readSignedBigInt64LE(buffer, offset);
42
+ offset += 8;
43
+ const openAsks = readSignedBigInt64LE(buffer, offset);
44
+ offset += 8;
45
+ const cumulativeDeposits = readSignedBigInt64LE(buffer, offset);
46
+ offset += 8;
47
+ const marketIndex = buffer.readUInt16LE(offset);
48
+ offset += 2;
49
+ const balanceTypeNum = buffer.readUInt8(offset);
50
+ let balanceType;
51
+ if (balanceTypeNum === 0) {
52
+ balanceType = types_1.SpotBalanceType.DEPOSIT;
53
+ }
54
+ else {
55
+ balanceType = types_1.SpotBalanceType.BORROW;
56
+ }
57
+ offset += 6;
58
+ spotPositions.push({
59
+ scaledBalance,
60
+ openBids,
61
+ openAsks,
62
+ cumulativeDeposits,
63
+ marketIndex,
64
+ balanceType,
65
+ openOrders,
66
+ });
67
+ }
68
+ const perpPositions = [];
69
+ for (let i = 0; i < 8; i++) {
70
+ const baseAssetAmount = readSignedBigInt64LE(buffer, offset + 8);
71
+ const quoteAssetAmount = readSignedBigInt64LE(buffer, offset + 16);
72
+ const lpShares = readUnsignedBigInt64LE(buffer, offset + 64);
73
+ const openOrders = buffer.readUInt8(offset + 94);
74
+ if (baseAssetAmount.eq(__2.ZERO) &&
75
+ openOrders === 0 &&
76
+ quoteAssetAmount.eq(__2.ZERO) &&
77
+ lpShares.eq(__2.ZERO)) {
78
+ offset += 96;
79
+ continue;
80
+ }
81
+ const lastCumulativeFundingRate = readSignedBigInt64LE(buffer, offset);
82
+ offset += 24;
83
+ const quoteBreakEvenAmount = readSignedBigInt64LE(buffer, offset);
84
+ offset += 8;
85
+ const quoteEntryAmount = readSignedBigInt64LE(buffer, offset);
86
+ offset += 8;
87
+ const openBids = readSignedBigInt64LE(buffer, offset);
88
+ offset += 8;
89
+ const openAsks = readSignedBigInt64LE(buffer, offset);
90
+ offset += 8;
91
+ const settledPnl = readSignedBigInt64LE(buffer, offset);
92
+ offset += 16;
93
+ const lastBaseAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
94
+ offset += 8;
95
+ const lastQuoteAssetAmountPerLp = readSignedBigInt64LE(buffer, offset);
96
+ offset += 8;
97
+ const remainderBaseAssetAmount = buffer.readUint32LE(offset);
98
+ offset += 4;
99
+ const marketIndex = buffer.readUInt16LE(offset);
100
+ offset += 3;
101
+ const perLpBase = buffer.readUInt8(offset);
102
+ offset += 1;
103
+ perpPositions.push({
104
+ lastCumulativeFundingRate,
105
+ baseAssetAmount,
106
+ quoteAssetAmount,
107
+ quoteBreakEvenAmount,
108
+ quoteEntryAmount,
109
+ openBids,
110
+ openAsks,
111
+ settledPnl,
112
+ lpShares,
113
+ lastBaseAssetAmountPerLp,
114
+ lastQuoteAssetAmountPerLp,
115
+ remainderBaseAssetAmount,
116
+ marketIndex,
117
+ openOrders,
118
+ perLpBase,
119
+ });
120
+ }
121
+ const orders = [];
122
+ for (let i = 0; i < 32; i++) {
123
+ // skip order if it's not open
124
+ if (buffer.readUint8(offset + 82) === 0) {
125
+ offset += 96;
126
+ continue;
127
+ }
128
+ const slot = readUnsignedBigInt64LE(buffer, offset);
129
+ offset += 8;
130
+ const price = readUnsignedBigInt64LE(buffer, offset);
131
+ offset += 8;
132
+ const baseAssetAmount = readUnsignedBigInt64LE(buffer, offset);
133
+ offset += 8;
134
+ const baseAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
135
+ offset += 8;
136
+ const quoteAssetAmountFilled = readUnsignedBigInt64LE(buffer, offset);
137
+ offset += 8;
138
+ const triggerPrice = readUnsignedBigInt64LE(buffer, offset);
139
+ offset += 8;
140
+ const auctionStartPrice = readSignedBigInt64LE(buffer, offset);
141
+ offset += 8;
142
+ const auctionEndPrice = readSignedBigInt64LE(buffer, offset);
143
+ offset += 8;
144
+ const maxTs = readSignedBigInt64LE(buffer, offset);
145
+ offset += 8;
146
+ const oraclePriceOffset = buffer.readInt32LE(offset);
147
+ offset += 4;
148
+ const orderId = buffer.readUInt32LE(offset);
149
+ offset += 4;
150
+ const marketIndex = buffer.readUInt16LE(offset);
151
+ offset += 2;
152
+ const orderStatusNum = buffer.readUInt8(offset);
153
+ let status;
154
+ if (orderStatusNum === 0) {
155
+ status = types_1.OrderStatus.INIT;
156
+ }
157
+ else if (orderStatusNum === 1) {
158
+ status = types_1.OrderStatus.OPEN;
159
+ }
160
+ offset += 1;
161
+ const orderTypeNum = buffer.readUInt8(offset);
162
+ let orderType;
163
+ if (orderTypeNum === 0) {
164
+ orderType = types_1.OrderType.MARKET;
165
+ }
166
+ else if (orderTypeNum === 1) {
167
+ orderType = types_1.OrderType.LIMIT;
168
+ }
169
+ else if (orderTypeNum === 2) {
170
+ orderType = types_1.OrderType.TRIGGER_MARKET;
171
+ }
172
+ else if (orderTypeNum === 3) {
173
+ orderType = types_1.OrderType.TRIGGER_LIMIT;
174
+ }
175
+ else if (orderTypeNum === 4) {
176
+ orderType = types_1.OrderType.ORACLE;
177
+ }
178
+ offset += 1;
179
+ const marketTypeNum = buffer.readUInt8(offset);
180
+ let marketType;
181
+ if (marketTypeNum === 0) {
182
+ marketType = types_1.MarketType.SPOT;
183
+ }
184
+ else {
185
+ marketType = types_1.MarketType.PERP;
186
+ }
187
+ offset += 1;
188
+ const userOrderId = buffer.readUint8(offset);
189
+ offset += 1;
190
+ const existingPositionDirectionNum = buffer.readUInt8(offset);
191
+ let existingPositionDirection;
192
+ if (existingPositionDirectionNum === 0) {
193
+ existingPositionDirection = types_1.PositionDirection.LONG;
194
+ }
195
+ else {
196
+ existingPositionDirection = types_1.PositionDirection.SHORT;
197
+ }
198
+ offset += 1;
199
+ const positionDirectionNum = buffer.readUInt8(offset);
200
+ let direction;
201
+ if (positionDirectionNum === 0) {
202
+ direction = types_1.PositionDirection.LONG;
203
+ }
204
+ else {
205
+ direction = types_1.PositionDirection.SHORT;
206
+ }
207
+ offset += 1;
208
+ const reduceOnly = buffer.readUInt8(offset) === 1;
209
+ offset += 1;
210
+ const postOnly = buffer.readUInt8(offset) === 1;
211
+ offset += 1;
212
+ const immediateOrCancel = buffer.readUInt8(offset) === 1;
213
+ offset += 1;
214
+ const triggerConditionNum = buffer.readUInt8(offset);
215
+ let triggerCondition;
216
+ if (triggerConditionNum === 0) {
217
+ triggerCondition = types_1.OrderTriggerCondition.ABOVE;
218
+ }
219
+ else if (triggerConditionNum === 1) {
220
+ triggerCondition = types_1.OrderTriggerCondition.BELOW;
221
+ }
222
+ else if (triggerConditionNum === 2) {
223
+ triggerCondition = types_1.OrderTriggerCondition.TRIGGERED_ABOVE;
224
+ }
225
+ else if (triggerConditionNum === 3) {
226
+ triggerCondition = types_1.OrderTriggerCondition.TRIGGERED_BELOW;
227
+ }
228
+ offset += 1;
229
+ const auctionDuration = buffer.readUInt8(offset);
230
+ offset += 1;
231
+ offset += 3; // padding
232
+ orders.push({
233
+ slot,
234
+ price,
235
+ baseAssetAmount,
236
+ baseAssetAmountFilled,
237
+ quoteAssetAmountFilled,
238
+ triggerPrice,
239
+ auctionStartPrice,
240
+ auctionEndPrice,
241
+ maxTs,
242
+ oraclePriceOffset,
243
+ orderId,
244
+ marketIndex,
245
+ status,
246
+ orderType,
247
+ marketType,
248
+ userOrderId,
249
+ existingPositionDirection,
250
+ direction,
251
+ reduceOnly,
252
+ postOnly,
253
+ immediateOrCancel,
254
+ triggerCondition,
255
+ auctionDuration,
256
+ });
257
+ }
258
+ const lastAddPerpLpSharesTs = readSignedBigInt64LE(buffer, offset);
259
+ offset += 8;
260
+ const totalDeposits = readUnsignedBigInt64LE(buffer, offset);
261
+ offset += 8;
262
+ const totalWithdraws = readUnsignedBigInt64LE(buffer, offset);
263
+ offset += 8;
264
+ const totalSocialLoss = readUnsignedBigInt64LE(buffer, offset);
265
+ offset += 8;
266
+ const settledPerpPnl = readSignedBigInt64LE(buffer, offset);
267
+ offset += 8;
268
+ const cumulativeSpotFees = readSignedBigInt64LE(buffer, offset);
269
+ offset += 8;
270
+ const cumulativePerpFunding = readSignedBigInt64LE(buffer, offset);
271
+ offset += 8;
272
+ const liquidationMarginFreed = readUnsignedBigInt64LE(buffer, offset);
273
+ offset += 8;
274
+ const lastActiveSlot = readUnsignedBigInt64LE(buffer, offset);
275
+ offset += 8;
276
+ const nextOrderId = buffer.readUInt32LE(offset);
277
+ offset += 4;
278
+ const maxMarginRatio = buffer.readUInt32LE(offset);
279
+ offset += 4;
280
+ const nextLiquidationId = buffer.readUInt16LE(offset);
281
+ offset += 2;
282
+ const subAccountId = buffer.readUInt16LE(offset);
283
+ offset += 2;
284
+ const status = buffer.readUInt8(offset);
285
+ offset += 1;
286
+ const isMarginTradingEnabled = buffer.readUInt8(offset) === 1;
287
+ offset += 1;
288
+ const idle = buffer.readUInt8(offset) === 1;
289
+ offset += 1;
290
+ const openOrders = buffer.readUInt8(offset);
291
+ offset += 1;
292
+ const hasOpenOrder = buffer.readUInt8(offset) === 1;
293
+ offset += 1;
294
+ const openAuctions = buffer.readUInt8(offset);
295
+ offset += 1;
296
+ const hasOpenAuction = buffer.readUInt8(offset) === 1;
297
+ offset += 1;
298
+ // @ts-ignore
299
+ return {
300
+ authority,
301
+ delegate,
302
+ name,
303
+ spotPositions,
304
+ perpPositions,
305
+ orders,
306
+ lastAddPerpLpSharesTs,
307
+ totalDeposits,
308
+ totalWithdraws,
309
+ totalSocialLoss,
310
+ settledPerpPnl,
311
+ cumulativeSpotFees,
312
+ cumulativePerpFunding,
313
+ liquidationMarginFreed,
314
+ lastActiveSlot,
315
+ nextOrderId,
316
+ maxMarginRatio,
317
+ nextLiquidationId,
318
+ subAccountId,
319
+ status,
320
+ isMarginTradingEnabled,
321
+ idle,
322
+ openOrders,
323
+ hasOpenOrder,
324
+ openAuctions,
325
+ hasOpenAuction,
326
+ };
327
+ }
328
+ exports.decodeUser = decodeUser;
@@ -4184,6 +4184,27 @@
4184
4184
  }
4185
4185
  ]
4186
4186
  },
4187
+ {
4188
+ "name": "updateStateMaxInitializeUserFee",
4189
+ "accounts": [
4190
+ {
4191
+ "name": "admin",
4192
+ "isMut": false,
4193
+ "isSigner": true
4194
+ },
4195
+ {
4196
+ "name": "state",
4197
+ "isMut": true,
4198
+ "isSigner": false
4199
+ }
4200
+ ],
4201
+ "args": [
4202
+ {
4203
+ "name": "maxInitializeUserFee",
4204
+ "type": "u16"
4205
+ }
4206
+ ]
4207
+ },
4187
4208
  {
4188
4209
  "name": "updatePerpMarketOracle",
4189
4210
  "accounts": [
@@ -5747,12 +5768,16 @@
5747
5768
  "name": "maxNumberOfSubAccounts",
5748
5769
  "type": "u16"
5749
5770
  },
5771
+ {
5772
+ "name": "maxInitializeUserFee",
5773
+ "type": "u16"
5774
+ },
5750
5775
  {
5751
5776
  "name": "padding",
5752
5777
  "type": {
5753
5778
  "array": [
5754
5779
  "u8",
5755
- 12
5780
+ 10
5756
5781
  ]
5757
5782
  }
5758
5783
  }
@@ -11094,6 +11119,11 @@
11094
11119
  "code": 6255,
11095
11120
  "name": "InvalidMarginCalculation",
11096
11121
  "msg": "InvalidMarginCalculation"
11122
+ },
11123
+ {
11124
+ "code": 6256,
11125
+ "name": "CantPayUserInitFee",
11126
+ "msg": "CantPayUserInitFee"
11097
11127
  }
11098
11128
  ]
11099
11129
  }
package/lib/index.d.ts CHANGED
@@ -55,6 +55,7 @@ export * from './math/margin';
55
55
  export * from './math/insurance';
56
56
  export * from './math/superStake';
57
57
  export * from './math/spotPosition';
58
+ export * from './math/state';
58
59
  export * from './marinade';
59
60
  export * from './orderParams';
60
61
  export * from './slot/SlotSubscriber';
package/lib/index.js CHANGED
@@ -78,6 +78,7 @@ __exportStar(require("./math/margin"), exports);
78
78
  __exportStar(require("./math/insurance"), exports);
79
79
  __exportStar(require("./math/superStake"), exports);
80
80
  __exportStar(require("./math/spotPosition"), exports);
81
+ __exportStar(require("./math/state"), exports);
81
82
  __exportStar(require("./marinade"), exports);
82
83
  __exportStar(require("./orderParams"), exports);
83
84
  __exportStar(require("./slot/SlotSubscriber"), exports);
@@ -0,0 +1,5 @@
1
+ /// <reference types="bn.js" />
2
+ import { StateAccount } from '../types';
3
+ import { BN } from '../';
4
+ export declare function calculateInitUserFee(stateAccount: StateAccount): BN;
5
+ export declare function getMaxNumberOfSubAccounts(stateAccount: StateAccount): BN;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMaxNumberOfSubAccounts = exports.calculateInitUserFee = void 0;
4
+ const __1 = require("../");
5
+ function calculateInitUserFee(stateAccount) {
6
+ const maxInitFee = new __1.BN(stateAccount.maxInitializeUserFee)
7
+ .mul(__1.LAMPORTS_PRECISION)
8
+ .divn(100);
9
+ const targetUtilization = __1.PERCENTAGE_PRECISION.muln(8).divn(10);
10
+ const accountSpaceUtilization = stateAccount.numberOfSubAccounts
11
+ .addn(1)
12
+ .mul(__1.PERCENTAGE_PRECISION)
13
+ .div(getMaxNumberOfSubAccounts(stateAccount));
14
+ if (accountSpaceUtilization.gt(targetUtilization)) {
15
+ return maxInitFee
16
+ .mul(accountSpaceUtilization.sub(targetUtilization))
17
+ .div(__1.PERCENTAGE_PRECISION.sub(targetUtilization));
18
+ }
19
+ else {
20
+ return __1.ZERO;
21
+ }
22
+ }
23
+ exports.calculateInitUserFee = calculateInitUserFee;
24
+ function getMaxNumberOfSubAccounts(stateAccount) {
25
+ if (stateAccount.maxNumberOfSubAccounts <= 100) {
26
+ return new __1.BN(stateAccount.maxNumberOfSubAccounts);
27
+ }
28
+ return new __1.BN(stateAccount.maxNumberOfSubAccounts).muln(100);
29
+ }
30
+ exports.getMaxNumberOfSubAccounts = getMaxNumberOfSubAccounts;
@@ -1,7 +1,9 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { DriftClient } from '../driftClient';
3
4
  import { UserAccount } from '../types';
4
5
  import { Commitment } from '@solana/web3.js';
6
+ import { Buffer } from 'buffer';
5
7
  import { DLOB } from '../dlob/DLOB';
6
8
  import { OrderSubscriberConfig, OrderSubscriberEvents } from './types';
7
9
  import { PollingSubscription } from './PollingSubscription';
@@ -20,6 +22,7 @@ export declare class OrderSubscriber {
20
22
  fetchPromise?: Promise<void>;
21
23
  fetchPromiseResolver: () => void;
22
24
  mostRecentSlot: number;
25
+ decodeFn: (name: string, data: Buffer) => UserAccount;
23
26
  constructor(config: OrderSubscriberConfig);
24
27
  subscribe(): Promise<void>;
25
28
  fetch(): Promise<void>;
@@ -9,8 +9,10 @@ const PollingSubscription_1 = require("./PollingSubscription");
9
9
  const WebsocketSubscription_1 = require("./WebsocketSubscription");
10
10
  const events_1 = require("events");
11
11
  const index_1 = require("../index");
12
+ const user_1 = require("../decode/user");
12
13
  class OrderSubscriber {
13
14
  constructor(config) {
15
+ var _a;
14
16
  this.usersAccounts = new Map();
15
17
  this.driftClient = config.driftClient;
16
18
  this.commitment = config.subscriptionConfig.commitment || 'processed';
@@ -28,6 +30,13 @@ class OrderSubscriber {
28
30
  resubTimeoutMs: config.subscriptionConfig.resubTimeoutMs,
29
31
  });
30
32
  }
33
+ if ((_a = config.fastDecode) !== null && _a !== void 0 ? _a : true) {
34
+ this.decodeFn = (name, data) => (0, user_1.decodeUser)(data);
35
+ }
36
+ else {
37
+ this.decodeFn =
38
+ this.driftClient.program.account.user.coder.accounts.decodeUnchecked.bind(this.driftClient.program.account.user.coder.accounts);
39
+ }
31
40
  this.eventEmitter = new events_1.EventEmitter();
32
41
  }
33
42
  async subscribe() {
@@ -83,6 +92,7 @@ class OrderSubscriber {
83
92
  if (!this.mostRecentSlot || slot > this.mostRecentSlot) {
84
93
  this.mostRecentSlot = slot;
85
94
  }
95
+ this.eventEmitter.emit('updateReceived', new web3_js_1.PublicKey(key), slot, dataType);
86
96
  const slotAndUserAccount = this.usersAccounts.get(key);
87
97
  if (!slotAndUserAccount || slotAndUserAccount.slot <= slot) {
88
98
  let userAccount;
@@ -95,19 +105,19 @@ class OrderSubscriber {
95
105
  slotAndUserAccount.userAccount.lastActiveSlot.gt(newLastActiveSlot)) {
96
106
  return;
97
107
  }
98
- userAccount =
99
- this.driftClient.program.account.user.coder.accounts.decodeUnchecked('User', buffer);
108
+ userAccount = this.decodeFn('User', buffer);
100
109
  }
101
110
  else {
102
111
  userAccount = data;
103
112
  }
113
+ this.eventEmitter.emit('userUpdated', userAccount, new web3_js_1.PublicKey(key), slot, dataType);
104
114
  const newOrders = userAccount.orders.filter((order) => {
105
115
  var _a;
106
116
  return order.slot.toNumber() > ((_a = slotAndUserAccount === null || slotAndUserAccount === void 0 ? void 0 : slotAndUserAccount.slot) !== null && _a !== void 0 ? _a : 0) &&
107
117
  order.slot.toNumber() <= slot;
108
118
  });
109
119
  if (newOrders.length > 0) {
110
- this.eventEmitter.emit('onUpdate', userAccount, newOrders, new web3_js_1.PublicKey(key), slot, dataType);
120
+ this.eventEmitter.emit('orderCreated', userAccount, newOrders, new web3_js_1.PublicKey(key), slot, dataType);
111
121
  }
112
122
  if (userAccount.hasOpenOrder) {
113
123
  this.usersAccounts.set(key, { slot, userAccount });
@@ -14,7 +14,7 @@ class WebsocketSubscription {
14
14
  if (this.subscriber) {
15
15
  return;
16
16
  }
17
- this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('OrderSubscriber', 'User', this.orderSubscriber.driftClient.program, this.orderSubscriber.driftClient.program.account.user.coder.accounts.decodeUnchecked.bind(this.orderSubscriber.driftClient.program.account.user.coder.accounts), {
17
+ this.subscriber = new webSocketProgramAccountSubscriber_1.WebSocketProgramAccountSubscriber('OrderSubscriber', 'User', this.orderSubscriber.driftClient.program, this.orderSubscriber.decodeFn, {
18
18
  filters: [(0, memcmp_1.getUserFilter)(), (0, memcmp_1.getNonIdleUserFilter)()],
19
19
  commitment: this.commitment,
20
20
  }, this.resubTimeoutMs);
@@ -13,7 +13,10 @@ export type OrderSubscriberConfig = {
13
13
  resubTimeoutMs?: number;
14
14
  commitment?: Commitment;
15
15
  };
16
+ fastDecode?: boolean;
16
17
  };
17
18
  export interface OrderSubscriberEvents {
18
- onUpdate: (account: UserAccount, updatedOrders: Order[], pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
19
+ orderCreated: (account: UserAccount, updatedOrders: Order[], pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
20
+ userUpdated: (account: UserAccount, pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
21
+ updateReceived: (pubkey: PublicKey, slot: number, dataType: 'raw' | 'decoded') => void;
19
22
  }
package/lib/types.d.ts CHANGED
@@ -629,6 +629,7 @@ export type StateAccount = {
629
629
  lpCooldownTime: BN;
630
630
  initialPctToLiquidate: number;
631
631
  liquidationDuration: number;
632
+ maxInitializeUserFee: number;
632
633
  };
633
634
  export type PerpMarketAccount = {
634
635
  status: MarketStatus;
@@ -914,7 +915,6 @@ export type Order = {
914
915
  price: BN;
915
916
  baseAssetAmount: BN;
916
917
  baseAssetAmountFilled: BN;
917
- quoteAssetAmount: BN;
918
918
  quoteAssetAmountFilled: BN;
919
919
  direction: PositionDirection;
920
920
  reduceOnly: boolean;
package/lib/user.d.ts CHANGED
@@ -95,7 +95,7 @@ export declare class User {
95
95
  * @returns : the dust base asset amount (ie, < stepsize)
96
96
  * @returns : pnl from settle
97
97
  */
98
- getPerpPositionWithLPSettle(marketIndex: number, originalPosition?: PerpPosition, burnLpShares?: boolean): [PerpPosition, BN, BN];
98
+ getPerpPositionWithLPSettle(marketIndex: number, originalPosition?: PerpPosition, burnLpShares?: boolean, includeRemainderInBaseAmount?: boolean): [PerpPosition, BN, BN];
99
99
  /**
100
100
  * calculates Buying Power = free collateral / initial margin ratio
101
101
  * @returns : Precision QUOTE_PRECISION
package/lib/user.js CHANGED
@@ -251,7 +251,7 @@ class User {
251
251
  * @returns : the dust base asset amount (ie, < stepsize)
252
252
  * @returns : pnl from settle
253
253
  */
254
- getPerpPositionWithLPSettle(marketIndex, originalPosition, burnLpShares = false) {
254
+ getPerpPositionWithLPSettle(marketIndex, originalPosition, burnLpShares = false, includeRemainderInBaseAmount = false) {
255
255
  var _a;
256
256
  originalPosition =
257
257
  (_a = originalPosition !== null && originalPosition !== void 0 ? originalPosition : this.getPerpPosition(marketIndex)) !== null && _a !== void 0 ? _a : this.getEmptyPosition(marketIndex);
@@ -396,7 +396,12 @@ class User {
396
396
  else {
397
397
  position.lastCumulativeFundingRate = numericConstants_1.ZERO;
398
398
  }
399
- return [position, remainderBaa, pnl];
399
+ const remainderBeforeRemoval = new _1.BN(position.remainderBaseAssetAmount);
400
+ if (includeRemainderInBaseAmount) {
401
+ position.baseAssetAmount = position.baseAssetAmount.add(remainderBeforeRemoval);
402
+ position.remainderBaseAssetAmount = 0;
403
+ }
404
+ return [position, remainderBeforeRemoval, pnl];
400
405
  }
401
406
  /**
402
407
  * calculates Buying Power = free collateral / initial margin ratio
@@ -11,7 +11,9 @@ class PollingSubscription {
11
11
  if (this.intervalId) {
12
12
  return;
13
13
  }
14
- this.intervalId = setInterval(this.userMap.sync.bind(this.userMap), this.frequency);
14
+ if (this.frequency > 0) {
15
+ this.intervalId = setInterval(this.userMap.sync.bind(this.userMap), this.frequency);
16
+ }
15
17
  if (!this.skipInitialLoad) {
16
18
  await this.userMap.sync();
17
19
  }
@@ -1,4 +1,6 @@
1
+ /// <reference types="node" />
1
2
  import { UserMap } from './userMap';
3
+ import { UserAccount } from '../types';
2
4
  import { Commitment } from '@solana/web3.js';
3
5
  export declare class WebsocketSubscription {
4
6
  private userMap;
@@ -6,13 +8,15 @@ export declare class WebsocketSubscription {
6
8
  private skipInitialLoad;
7
9
  private resubTimeoutMs?;
8
10
  private includeIdle?;
11
+ private decodeFn;
9
12
  private subscriber;
10
- constructor({ userMap, commitment, skipInitialLoad, resubTimeoutMs, includeIdle, }: {
13
+ constructor({ userMap, commitment, skipInitialLoad, resubTimeoutMs, includeIdle, decodeFn, }: {
11
14
  userMap: UserMap;
12
15
  commitment: Commitment;
13
16
  skipInitialLoad?: boolean;
14
17
  resubTimeoutMs?: number;
15
18
  includeIdle?: boolean;
19
+ decodeFn: (name: string, data: Buffer) => UserAccount;
16
20
  });
17
21
  subscribe(): Promise<void>;
18
22
  unsubscribe(): Promise<void>;