@velocity-exchange/sdk 0.0.2 → 0.0.5

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 (192) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +5 -6
  3. package/bunfig.toml +3 -0
  4. package/lib/browser/auctionSubscriber/auctionSubscriber.d.ts +4 -2
  5. package/lib/browser/auctionSubscriber/auctionSubscriber.js +9 -4
  6. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.d.ts +4 -2
  7. package/lib/browser/auctionSubscriber/auctionSubscriberGrpc.js +9 -4
  8. package/lib/browser/auctionSubscriber/types.d.ts +4 -2
  9. package/lib/browser/config.d.ts +3 -8
  10. package/lib/browser/config.js +14 -22
  11. package/lib/browser/constituentMap/constituentMap.d.ts +4 -2
  12. package/lib/browser/constituentMap/constituentMap.js +14 -10
  13. package/lib/browser/core/VelocityCore.d.ts +4 -0
  14. package/lib/browser/core/VelocityCore.js +3 -1
  15. package/lib/browser/decode/user.js +5 -5
  16. package/lib/browser/dlob/DLOB.js +1 -1
  17. package/lib/browser/dlob/DLOBNode.js +1 -2
  18. package/lib/browser/dlob/DLOBSubscriber.d.ts +3 -1
  19. package/lib/browser/dlob/DLOBSubscriber.js +19 -10
  20. package/lib/browser/dlob/types.d.ts +3 -1
  21. package/lib/browser/idl/drift.d.ts +59 -6
  22. package/lib/browser/idl/drift.json +6049 -806
  23. package/lib/browser/math/auction.d.ts +1 -1
  24. package/lib/browser/math/auction.js +2 -9
  25. package/lib/browser/math/orders.js +3 -3
  26. package/lib/browser/orderSubscriber/OrderSubscriber.d.ts +3 -1
  27. package/lib/browser/orderSubscriber/OrderSubscriber.js +19 -11
  28. package/lib/browser/orderSubscriber/types.d.ts +3 -1
  29. package/lib/browser/priorityFee/priorityFeeSubscriber.d.ts +4 -2
  30. package/lib/browser/priorityFee/priorityFeeSubscriber.js +15 -11
  31. package/lib/browser/priorityFee/priorityFeeSubscriberMap.d.ts +11 -9
  32. package/lib/browser/priorityFee/priorityFeeSubscriberMap.js +18 -18
  33. package/lib/browser/priorityFee/types.d.ts +9 -5
  34. package/lib/browser/priorityFee/types.js +1 -1
  35. package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.d.ts +3 -12
  36. package/lib/browser/swift/grpcSignedMsgUserAccountSubscriber.js +3 -9
  37. package/lib/browser/swift/signedMsgUserAccountSubscriber.d.ts +11 -8
  38. package/lib/browser/swift/signedMsgUserAccountSubscriber.js +10 -5
  39. package/lib/browser/swift/swiftOrderSubscriber.d.ts +7 -4
  40. package/lib/browser/swift/swiftOrderSubscriber.js +19 -11
  41. package/lib/browser/types.d.ts +2 -2
  42. package/lib/browser/user.d.ts +3 -1
  43. package/lib/browser/user.js +93 -86
  44. package/lib/browser/userConfig.d.ts +4 -2
  45. package/lib/browser/userMap/referrerMap.d.ts +5 -3
  46. package/lib/browser/userMap/referrerMap.js +18 -14
  47. package/lib/browser/userMap/revenueShareEscrowMap.d.ts +5 -3
  48. package/lib/browser/userMap/revenueShareEscrowMap.js +18 -14
  49. package/lib/browser/userMap/userMap.d.ts +3 -1
  50. package/lib/browser/userMap/userMap.js +24 -19
  51. package/lib/browser/userMap/userMapConfig.d.ts +4 -2
  52. package/lib/browser/userMap/userStatsMap.d.ts +5 -3
  53. package/lib/browser/userMap/userStatsMap.js +15 -11
  54. package/lib/browser/userStats.d.ts +3 -1
  55. package/lib/browser/userStats.js +21 -15
  56. package/lib/browser/userStatsConfig.d.ts +4 -2
  57. package/lib/browser/util/deprecatedAlias.d.ts +26 -0
  58. package/lib/browser/util/deprecatedAlias.js +10 -0
  59. package/lib/browser/velocityClient.d.ts +6 -6
  60. package/lib/browser/velocityClient.js +6 -6
  61. package/lib/browser/velocityClientConfig.d.ts +7 -4
  62. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts +4 -2
  63. package/lib/node/auctionSubscriber/auctionSubscriber.d.ts.map +1 -1
  64. package/lib/node/auctionSubscriber/auctionSubscriber.js +9 -4
  65. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts +4 -2
  66. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.d.ts.map +1 -1
  67. package/lib/node/auctionSubscriber/auctionSubscriberGrpc.js +9 -4
  68. package/lib/node/auctionSubscriber/types.d.ts +4 -2
  69. package/lib/node/auctionSubscriber/types.d.ts.map +1 -1
  70. package/lib/node/config.d.ts +3 -8
  71. package/lib/node/config.d.ts.map +1 -1
  72. package/lib/node/config.js +14 -22
  73. package/lib/node/constituentMap/constituentMap.d.ts +4 -2
  74. package/lib/node/constituentMap/constituentMap.d.ts.map +1 -1
  75. package/lib/node/constituentMap/constituentMap.js +14 -10
  76. package/lib/node/core/VelocityCore.d.ts +4 -0
  77. package/lib/node/core/VelocityCore.d.ts.map +1 -1
  78. package/lib/node/core/VelocityCore.js +3 -1
  79. package/lib/node/decode/user.js +5 -5
  80. package/lib/node/dlob/DLOB.js +1 -1
  81. package/lib/node/dlob/DLOBNode.js +1 -2
  82. package/lib/node/dlob/DLOBSubscriber.d.ts +3 -1
  83. package/lib/node/dlob/DLOBSubscriber.d.ts.map +1 -1
  84. package/lib/node/dlob/DLOBSubscriber.js +19 -10
  85. package/lib/node/dlob/types.d.ts +3 -1
  86. package/lib/node/dlob/types.d.ts.map +1 -1
  87. package/lib/node/idl/drift.d.ts +59 -6
  88. package/lib/node/idl/drift.d.ts.map +1 -1
  89. package/lib/node/idl/drift.json +6049 -806
  90. package/lib/node/math/auction.d.ts +1 -1
  91. package/lib/node/math/auction.d.ts.map +1 -1
  92. package/lib/node/math/auction.js +2 -9
  93. package/lib/node/math/orders.d.ts.map +1 -1
  94. package/lib/node/math/orders.js +3 -3
  95. package/lib/node/orderSubscriber/OrderSubscriber.d.ts +3 -1
  96. package/lib/node/orderSubscriber/OrderSubscriber.d.ts.map +1 -1
  97. package/lib/node/orderSubscriber/OrderSubscriber.js +19 -11
  98. package/lib/node/orderSubscriber/types.d.ts +3 -1
  99. package/lib/node/orderSubscriber/types.d.ts.map +1 -1
  100. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts +4 -2
  101. package/lib/node/priorityFee/priorityFeeSubscriber.d.ts.map +1 -1
  102. package/lib/node/priorityFee/priorityFeeSubscriber.js +15 -11
  103. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts +11 -9
  104. package/lib/node/priorityFee/priorityFeeSubscriberMap.d.ts.map +1 -1
  105. package/lib/node/priorityFee/priorityFeeSubscriberMap.js +18 -18
  106. package/lib/node/priorityFee/types.d.ts +9 -5
  107. package/lib/node/priorityFee/types.d.ts.map +1 -1
  108. package/lib/node/priorityFee/types.js +1 -1
  109. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts +3 -12
  110. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.d.ts.map +1 -1
  111. package/lib/node/swift/grpcSignedMsgUserAccountSubscriber.js +3 -9
  112. package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts +11 -8
  113. package/lib/node/swift/signedMsgUserAccountSubscriber.d.ts.map +1 -1
  114. package/lib/node/swift/signedMsgUserAccountSubscriber.js +10 -5
  115. package/lib/node/swift/swiftOrderSubscriber.d.ts +7 -4
  116. package/lib/node/swift/swiftOrderSubscriber.d.ts.map +1 -1
  117. package/lib/node/swift/swiftOrderSubscriber.js +19 -11
  118. package/lib/node/types.d.ts +2 -2
  119. package/lib/node/types.d.ts.map +1 -1
  120. package/lib/node/user.d.ts +3 -1
  121. package/lib/node/user.d.ts.map +1 -1
  122. package/lib/node/user.js +93 -86
  123. package/lib/node/userConfig.d.ts +4 -2
  124. package/lib/node/userConfig.d.ts.map +1 -1
  125. package/lib/node/userMap/referrerMap.d.ts +5 -3
  126. package/lib/node/userMap/referrerMap.d.ts.map +1 -1
  127. package/lib/node/userMap/referrerMap.js +18 -14
  128. package/lib/node/userMap/revenueShareEscrowMap.d.ts +5 -3
  129. package/lib/node/userMap/revenueShareEscrowMap.d.ts.map +1 -1
  130. package/lib/node/userMap/revenueShareEscrowMap.js +18 -14
  131. package/lib/node/userMap/userMap.d.ts +3 -1
  132. package/lib/node/userMap/userMap.d.ts.map +1 -1
  133. package/lib/node/userMap/userMap.js +24 -19
  134. package/lib/node/userMap/userMapConfig.d.ts +4 -2
  135. package/lib/node/userMap/userMapConfig.d.ts.map +1 -1
  136. package/lib/node/userMap/userStatsMap.d.ts +5 -3
  137. package/lib/node/userMap/userStatsMap.d.ts.map +1 -1
  138. package/lib/node/userMap/userStatsMap.js +15 -11
  139. package/lib/node/userStats.d.ts +3 -1
  140. package/lib/node/userStats.d.ts.map +1 -1
  141. package/lib/node/userStats.js +21 -15
  142. package/lib/node/userStatsConfig.d.ts +4 -2
  143. package/lib/node/userStatsConfig.d.ts.map +1 -1
  144. package/lib/node/util/deprecatedAlias.d.ts +27 -0
  145. package/lib/node/util/deprecatedAlias.d.ts.map +1 -0
  146. package/lib/node/util/deprecatedAlias.js +10 -0
  147. package/lib/node/velocityClient.d.ts +6 -6
  148. package/lib/node/velocityClient.d.ts.map +1 -1
  149. package/lib/node/velocityClient.js +6 -6
  150. package/lib/node/velocityClientConfig.d.ts +7 -4
  151. package/lib/node/velocityClientConfig.d.ts.map +1 -1
  152. package/package.json +1 -1
  153. package/src/auctionSubscriber/auctionSubscriber.ts +12 -6
  154. package/src/auctionSubscriber/auctionSubscriberGrpc.ts +12 -6
  155. package/src/auctionSubscriber/types.ts +5 -2
  156. package/src/config.ts +38 -27
  157. package/src/constituentMap/constituentMap.ts +17 -9
  158. package/src/core/VelocityCore.ts +6 -0
  159. package/src/decode/user.ts +5 -5
  160. package/src/dlob/DLOB.ts +1 -1
  161. package/src/dlob/DLOBNode.ts +1 -1
  162. package/src/dlob/DLOBSubscriber.ts +21 -11
  163. package/src/dlob/types.ts +3 -1
  164. package/src/idl/drift.json +23940 -18697
  165. package/src/idl/drift.ts +59 -6
  166. package/src/math/auction.ts +3 -10
  167. package/src/math/orders.ts +3 -6
  168. package/src/orderSubscriber/OrderSubscriber.ts +17 -7
  169. package/src/orderSubscriber/types.ts +3 -1
  170. package/src/priorityFee/priorityFeeSubscriber.ts +13 -9
  171. package/src/priorityFee/priorityFeeSubscriberMap.ts +23 -25
  172. package/src/priorityFee/types.ts +19 -9
  173. package/src/swift/grpcSignedMsgUserAccountSubscriber.ts +10 -23
  174. package/src/swift/signedMsgUserAccountSubscriber.ts +22 -14
  175. package/src/swift/swiftOrderSubscriber.ts +24 -16
  176. package/src/types.ts +2 -2
  177. package/src/user.ts +93 -82
  178. package/src/userConfig.ts +5 -2
  179. package/src/userMap/referrerMap.ts +19 -15
  180. package/src/userMap/revenueShareEscrowMap.ts +20 -16
  181. package/src/userMap/userMap.ts +20 -15
  182. package/src/userMap/userMapConfig.ts +6 -3
  183. package/src/userMap/userStatsMap.ts +19 -15
  184. package/src/userStats.ts +13 -7
  185. package/src/userStatsConfig.ts +5 -2
  186. package/src/util/deprecatedAlias.ts +21 -0
  187. package/src/velocityClient.ts +12 -12
  188. package/src/velocityClientConfig.ts +27 -42
  189. package/tests/auctions/test.ts +4 -4
  190. package/tests/decode/test.ts +1 -1
  191. package/tests/dlob/test.ts +5 -5
  192. package/tests/user/helpers.ts +1 -1
package/src/user.ts CHANGED
@@ -128,7 +128,11 @@ import {
128
128
  export type MarginType = 'Cross' | 'Isolated';
129
129
 
130
130
  export class User {
131
- driftClient: VelocityClient;
131
+ velocityClient: VelocityClient;
132
+ /** @deprecated Use `velocityClient` instead. `driftClient` will be removed in a future major. */
133
+ public get driftClient(): VelocityClient {
134
+ return this.velocityClient;
135
+ }
132
136
  userAccountPublicKey: PublicKey;
133
137
  accountSubscriber: UserAccountSubscriber;
134
138
  _isSubscribed = false;
@@ -143,17 +147,19 @@ export class User {
143
147
  }
144
148
 
145
149
  public constructor(config: UserConfig) {
146
- this.driftClient = config.driftClient;
150
+ // Type-system guarantees at least one of the two is supplied.
151
+ const velocityClient = (config.velocityClient ?? config.driftClient)!;
152
+ this.velocityClient = velocityClient;
147
153
  this.userAccountPublicKey = config.userAccountPublicKey;
148
154
  if (config.accountSubscription?.type === 'polling') {
149
155
  this.accountSubscriber = new PollingUserAccountSubscriber(
150
- config.driftClient.connection,
156
+ velocityClient.connection,
151
157
  config.userAccountPublicKey,
152
158
  config.accountSubscription.accountLoader,
153
159
  (
154
- this.driftClient.program.account as any
160
+ this.velocityClient.program.account as any
155
161
  ).user.coder.accounts.decodeUnchecked.bind(
156
- (this.driftClient.program.account as any).user.coder.accounts
162
+ (this.velocityClient.program.account as any).user.coder.accounts
157
163
  )
158
164
  );
159
165
  } else if (config.accountSubscription?.type === 'custom') {
@@ -167,7 +173,7 @@ export class User {
167
173
  } else {
168
174
  this.accountSubscriber = new grpcUserAccountSubscriber(
169
175
  config.accountSubscription.grpcConfigs,
170
- config.driftClient.program,
176
+ velocityClient.program,
171
177
  config.userAccountPublicKey,
172
178
  {
173
179
  resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
@@ -181,13 +187,13 @@ export class User {
181
187
  config.accountSubscription?.programUserAccountSubscriber
182
188
  ) {
183
189
  this.accountSubscriber = new WebSocketProgramUserAccountSubscriber(
184
- config.driftClient.program,
190
+ velocityClient.program,
185
191
  config.userAccountPublicKey,
186
192
  config.accountSubscription.programUserAccountSubscriber
187
193
  );
188
194
  } else {
189
195
  this.accountSubscriber = new WebSocketUserAccountSubscriber(
190
- config.driftClient.program,
196
+ velocityClient.program,
191
197
  config.userAccountPublicKey,
192
198
  {
193
199
  resubTimeoutMs: config.accountSubscription?.resubTimeoutMs,
@@ -332,7 +338,7 @@ export class User {
332
338
  if (spotPosition === undefined) {
333
339
  return ZERO;
334
340
  }
335
- const spotMarket = this.driftClient.getSpotMarketAccount(marketIndex);
341
+ const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
336
342
  return getSignedTokenAmount(
337
343
  getTokenAmount(
338
344
  spotPosition.scaledBalance,
@@ -369,8 +375,9 @@ export class User {
369
375
  public getIsolatePerpPositionTokenAmount(perpMarketIndex: number): BN {
370
376
  const perpPosition = this.getPerpPosition(perpMarketIndex);
371
377
  if (!perpPosition) return ZERO;
372
- const perpMarket = this.driftClient.getPerpMarketAccount(perpMarketIndex);
373
- const spotMarket = this.driftClient.getSpotMarketAccount(
378
+ const perpMarket =
379
+ this.velocityClient.getPerpMarketAccount(perpMarketIndex);
380
+ const spotMarket = this.velocityClient.getSpotMarketAccount(
374
381
  perpMarket.quoteSpotMarketIndex
375
382
  );
376
383
  if (perpPosition === undefined) {
@@ -392,10 +399,10 @@ export class User {
392
399
  return total;
393
400
  }
394
401
 
395
- const perpMarket = this.driftClient.getPerpMarketAccount(
402
+ const perpMarket = this.velocityClient.getPerpMarketAccount(
396
403
  perpPosition.marketIndex
397
404
  );
398
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
405
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(
399
406
  perpMarket.quoteSpotMarketIndex
400
407
  );
401
408
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(
@@ -506,7 +513,7 @@ export class User {
506
513
 
507
514
  public async exists(): Promise<boolean> {
508
515
  const userAccountRPCResponse =
509
- await this.driftClient.connection.getParsedAccountInfo(
516
+ await this.velocityClient.connection.getParsedAccountInfo(
510
517
  this.userAccountPublicKey
511
518
  );
512
519
  return userAccountRPCResponse.value !== null;
@@ -538,7 +545,7 @@ export class User {
538
545
  ): BN {
539
546
  const perpPosition = this.getPerpPositionOrEmpty(marketIndex);
540
547
 
541
- const perpMarket = this.driftClient.getPerpMarketAccount(marketIndex);
548
+ const perpMarket = this.velocityClient.getPerpMarketAccount(marketIndex);
542
549
  const oraclePriceData = this.getOracleDataForPerpMarket(marketIndex);
543
550
  const worstCaseBaseAssetAmount = perpPosition
544
551
  ? calculateWorstCaseBaseAssetAmount(
@@ -599,7 +606,7 @@ export class User {
599
606
  this.getUserAccount().maxMarginRatio
600
607
  );
601
608
  const marginRatio = calculateMarketMarginRatio(
602
- this.driftClient.getPerpMarketAccount(marketIndex),
609
+ this.velocityClient.getPerpMarketAccount(marketIndex),
603
610
  baseAssetAmount,
604
611
  'Initial',
605
612
  maxMarginRatio
@@ -797,14 +804,14 @@ export class User {
797
804
  marketIndex !== undefined ? pos.marketIndex === marketIndex : true
798
805
  )
799
806
  .reduce((unrealizedPnl, perpPosition) => {
800
- const market = this.driftClient.getPerpMarketAccount(
807
+ const market = this.velocityClient.getPerpMarketAccount(
801
808
  perpPosition.marketIndex
802
809
  );
803
810
  const oraclePriceData = this.getMMOracleDataForPerpMarket(
804
811
  market.marketIndex
805
812
  );
806
813
 
807
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
814
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(
808
815
  market.quoteSpotMarketIndex
809
816
  );
810
817
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(
@@ -873,7 +880,7 @@ export class User {
873
880
  marketIndex !== undefined ? pos.marketIndex === marketIndex : true
874
881
  )
875
882
  .reduce((pnl, perpPosition) => {
876
- const market = this.driftClient.getPerpMarketAccount(
883
+ const market = this.velocityClient.getPerpMarketAccount(
877
884
  perpPosition.marketIndex
878
885
  );
879
886
  return pnl.add(calculateUnsettledFundingPnl(market, perpPosition));
@@ -908,7 +915,7 @@ export class User {
908
915
  }
909
916
 
910
917
  const spotMarketAccount: SpotMarketAccount =
911
- this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
918
+ this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
912
919
 
913
920
  const oraclePriceData = this.getOracleDataForSpotMarket(
914
921
  spotPosition.marketIndex
@@ -1215,7 +1222,9 @@ export class User {
1215
1222
  if (this.isBeingLiquidated()) {
1216
1223
  liquidationBufferMap.set(
1217
1224
  'cross',
1218
- new BN(this.driftClient.getStateAccount().liquidationMarginBufferRatio)
1225
+ new BN(
1226
+ this.velocityClient.getStateAccount().liquidationMarginBufferRatio
1227
+ )
1219
1228
  );
1220
1229
  }
1221
1230
  for (const position of this.getActivePerpPositions()) {
@@ -1226,7 +1235,7 @@ export class User {
1226
1235
  liquidationBufferMap.set(
1227
1236
  position.marketIndex,
1228
1237
  new BN(
1229
- this.driftClient.getStateAccount().liquidationMarginBufferRatio
1238
+ this.velocityClient.getStateAccount().liquidationMarginBufferRatio
1230
1239
  )
1231
1240
  );
1232
1241
  }
@@ -1295,7 +1304,7 @@ export class User {
1295
1304
  includeOpenOrders?: boolean,
1296
1305
  strict = false
1297
1306
  ): BN {
1298
- const market = this.driftClient.getPerpMarketAccount(
1307
+ const market = this.velocityClient.getPerpMarketAccount(
1299
1308
  perpPosition.marketIndex
1300
1309
  );
1301
1310
 
@@ -1348,12 +1357,11 @@ export class User {
1348
1357
  marginRatio = ZERO;
1349
1358
  }
1350
1359
 
1351
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
1360
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(
1352
1361
  market.quoteSpotMarketIndex
1353
1362
  );
1354
- const quoteOraclePriceData = this.driftClient.getOracleDataForSpotMarket(
1355
- QUOTE_SPOT_MARKET_INDEX
1356
- );
1363
+ const quoteOraclePriceData =
1364
+ this.velocityClient.getOracleDataForSpotMarket(QUOTE_SPOT_MARKET_INDEX);
1357
1365
 
1358
1366
  let quotePrice;
1359
1367
  if (strict) {
@@ -1437,7 +1445,7 @@ export class User {
1437
1445
  includeOpenOrders = false
1438
1446
  ): BN {
1439
1447
  const userPosition = this.getPerpPositionOrEmpty(marketIndex);
1440
- const market = this.driftClient.getPerpMarketAccount(
1448
+ const market = this.velocityClient.getPerpMarketAccount(
1441
1449
  userPosition.marketIndex
1442
1450
  );
1443
1451
  return calculateBaseAssetValueWithOracle(
@@ -1458,7 +1466,7 @@ export class User {
1458
1466
  includeOpenOrders = false
1459
1467
  ): BN {
1460
1468
  const userPosition = this.getPerpPositionOrEmpty(marketIndex);
1461
- const market = this.driftClient.getPerpMarketAccount(
1469
+ const market = this.velocityClient.getPerpMarketAccount(
1462
1470
  userPosition.marketIndex
1463
1471
  );
1464
1472
 
@@ -1497,7 +1505,9 @@ export class User {
1497
1505
  amountToClose?: BN,
1498
1506
  useAMMClose = false
1499
1507
  ): [BN, BN] {
1500
- const market = this.driftClient.getPerpMarketAccount(position.marketIndex);
1508
+ const market = this.velocityClient.getPerpMarketAccount(
1509
+ position.marketIndex
1510
+ );
1501
1511
 
1502
1512
  const entryPrice = calculateEntryPrice(position);
1503
1513
 
@@ -1600,14 +1610,14 @@ export class User {
1600
1610
  undefined,
1601
1611
  includeOpenOrders
1602
1612
  );
1603
- const perpMarket = this.driftClient.getPerpMarketAccount(
1613
+ const perpMarket = this.velocityClient.getPerpMarketAccount(
1604
1614
  perpPosition.marketIndex
1605
1615
  );
1606
1616
 
1607
1617
  const oraclePriceData = this.getOracleDataForPerpMarket(
1608
1618
  perpPosition.marketIndex
1609
1619
  );
1610
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
1620
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(
1611
1621
  perpMarket.quoteSpotMarketIndex
1612
1622
  );
1613
1623
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(
@@ -1711,7 +1721,7 @@ export class User {
1711
1721
  }
1712
1722
 
1713
1723
  getSpotMarketAccountsWithDustPosition() {
1714
- const spotMarketAccounts = this.driftClient.getSpotMarketAccounts();
1724
+ const spotMarketAccounts = this.velocityClient.getSpotMarketAccounts();
1715
1725
 
1716
1726
  const dustPositionAccounts: SpotMarketAccount[] = [];
1717
1727
 
@@ -2079,7 +2089,7 @@ export class User {
2079
2089
  continue;
2080
2090
  }
2081
2091
 
2082
- const market = this.driftClient.getPerpMarketAccount(
2092
+ const market = this.velocityClient.getPerpMarketAccount(
2083
2093
  userPosition.marketIndex
2084
2094
  );
2085
2095
  if (
@@ -2120,7 +2130,7 @@ export class User {
2120
2130
  totalCollateral.sub(maintenanceMarginRequirement)
2121
2131
  );
2122
2132
 
2123
- const market = this.driftClient.getSpotMarketAccount(marketIndex);
2133
+ const market = this.velocityClient.getSpotMarketAccount(marketIndex);
2124
2134
  let signedTokenAmount = getSignedTokenAmount(
2125
2135
  getTokenAmount(
2126
2136
  currentSpotPosition.scaledBalance,
@@ -2141,11 +2151,11 @@ export class User {
2141
2151
  );
2142
2152
 
2143
2153
  const oracle = market.oracle;
2144
- const perpMarketWithSameOracle = this.driftClient
2154
+ const perpMarketWithSameOracle = this.velocityClient
2145
2155
  .getPerpMarketAccounts()
2146
2156
  .find((market) => market.amm.oracle.equals(oracle));
2147
2157
  const oraclePrice =
2148
- this.driftClient.getOracleDataForSpotMarket(marketIndex).price;
2158
+ this.velocityClient.getOracleDataForSpotMarket(marketIndex).price;
2149
2159
  if (perpMarketWithSameOracle) {
2150
2160
  const perpPosition = this.getPerpPositionOrEmpty(
2151
2161
  perpMarketWithSameOracle.marketIndex
@@ -2211,13 +2221,13 @@ export class User {
2211
2221
  offsetCollateral = ZERO,
2212
2222
  marginType?: MarginType
2213
2223
  ): BN {
2214
- const market = this.driftClient.getPerpMarketAccount(marketIndex);
2224
+ const market = this.velocityClient.getPerpMarketAccount(marketIndex);
2215
2225
 
2216
2226
  const oracle =
2217
- this.driftClient.getPerpMarketAccount(marketIndex).amm.oracle;
2227
+ this.velocityClient.getPerpMarketAccount(marketIndex).amm.oracle;
2218
2228
 
2219
2229
  const oraclePrice =
2220
- this.driftClient.getOracleDataForPerpMarket(marketIndex).price;
2230
+ this.velocityClient.getOracleDataForPerpMarket(marketIndex).price;
2221
2231
 
2222
2232
  const currentPerpPosition = this.getPerpPositionOrEmpty(marketIndex);
2223
2233
 
@@ -2310,7 +2320,7 @@ export class User {
2310
2320
  return new BN(-1);
2311
2321
  }
2312
2322
 
2313
- const spotMarketWithSameOracle = this.driftClient
2323
+ const spotMarketWithSameOracle = this.velocityClient
2314
2324
  .getSpotMarketAccounts()
2315
2325
  .find((market) => market.oracle.equals(oracle));
2316
2326
  if (spotMarketWithSameOracle) {
@@ -2397,7 +2407,7 @@ export class User {
2397
2407
 
2398
2408
  // assume worst fee tier
2399
2409
  const takerFeeTier =
2400
- this.driftClient.getStateAccount().perpFeeStructure.feeTiers[0];
2410
+ this.velocityClient.getStateAccount().perpFeeStructure.feeTiers[0];
2401
2411
  const takerFee = newPositionValue
2402
2412
  .muln(takerFeeTier.feeNumerator)
2403
2413
  .divn(takerFeeTier.feeDenominator);
@@ -2527,7 +2537,8 @@ export class User {
2527
2537
  if (signedTokenAmount.gt(ZERO)) {
2528
2538
  const assetWeight = calculateAssetWeight(
2529
2539
  signedTokenAmount,
2530
- this.driftClient.getOracleDataForSpotMarket(market.marketIndex).price,
2540
+ this.velocityClient.getOracleDataForSpotMarket(market.marketIndex)
2541
+ .price,
2531
2542
  market,
2532
2543
  marginCategory
2533
2544
  );
@@ -2592,7 +2603,7 @@ export class User {
2592
2603
  this.getUserAccount().maxMarginRatio
2593
2604
  );
2594
2605
  return calculateMarginUSDCRequiredForTrade(
2595
- this.driftClient,
2606
+ this.velocityClient,
2596
2607
  targetMarketIndex,
2597
2608
  baseSize,
2598
2609
  maxMarginRatio,
@@ -2611,7 +2622,7 @@ export class User {
2611
2622
  this.getUserAccount().maxMarginRatio
2612
2623
  );
2613
2624
  return calculateCollateralDepositRequiredForTrade(
2614
- this.driftClient,
2625
+ this.velocityClient,
2615
2626
  targetMarketIndex,
2616
2627
  baseSize,
2617
2628
  collateralIndex,
@@ -2650,7 +2661,7 @@ export class User {
2650
2661
  const oracleData = this.getMMOracleDataForPerpMarket(targetMarketIndex);
2651
2662
 
2652
2663
  const marketAccount =
2653
- this.driftClient.getPerpMarketAccount(targetMarketIndex);
2664
+ this.velocityClient.getPerpMarketAccount(targetMarketIndex);
2654
2665
 
2655
2666
  // add any position we have on the opposite side of the current trade, because we can "flip" the size of this position without taking any extra leverage.
2656
2667
  const oppositeSizeLiabilityValue = targetingSameSide
@@ -2777,9 +2788,9 @@ export class User {
2777
2788
  currentQuoteAssetValue?: BN,
2778
2789
  currentSpotMarketNetValue?: BN
2779
2790
  ): BN {
2780
- const market = this.driftClient.getSpotMarketAccount(targetMarketIndex);
2791
+ const market = this.velocityClient.getSpotMarketAccount(targetMarketIndex);
2781
2792
  const oraclePrice =
2782
- this.driftClient.getOracleDataForSpotMarket(targetMarketIndex).price;
2793
+ this.velocityClient.getOracleDataForSpotMarket(targetMarketIndex).price;
2783
2794
 
2784
2795
  currentQuoteAssetValue = this.getSpotMarketAssetValue(
2785
2796
  QUOTE_SPOT_MARKET_INDEX
@@ -2869,8 +2880,8 @@ export class User {
2869
2880
  calculateSwap?: (inAmount: BN) => BN;
2870
2881
  iterationLimit?: number;
2871
2882
  }): { inAmount: BN; outAmount: BN; leverage: BN } {
2872
- const inMarket = this.driftClient.getSpotMarketAccount(inMarketIndex);
2873
- const outMarket = this.driftClient.getSpotMarketAccount(outMarketIndex);
2883
+ const inMarket = this.velocityClient.getSpotMarketAccount(inMarketIndex);
2884
+ const outMarket = this.velocityClient.getSpotMarketAccount(outMarketIndex);
2874
2885
 
2875
2886
  const inOraclePriceData = this.getOracleDataForSpotMarket(inMarketIndex);
2876
2887
  const inOraclePrice = inOraclePriceData.price;
@@ -3127,7 +3138,7 @@ export class User {
3127
3138
  const marginCategory = 'Initial';
3128
3139
 
3129
3140
  const spotMarketAccount: SpotMarketAccount =
3130
- this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
3141
+ this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
3131
3142
 
3132
3143
  const { freeCollateralContribution } = getWorstCaseTokenAmounts(
3133
3144
  spotPosition,
@@ -3151,7 +3162,7 @@ export class User {
3151
3162
  let totalLiabilityValue = ZERO;
3152
3163
 
3153
3164
  const spotMarketAccount: SpotMarketAccount =
3154
- this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
3165
+ this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
3155
3166
 
3156
3167
  const { tokenValue, ordersValue } = getWorstCaseTokenAmounts(
3157
3168
  spotPosition,
@@ -3197,8 +3208,8 @@ export class User {
3197
3208
  inAmount: BN;
3198
3209
  outAmount: BN;
3199
3210
  }): BN {
3200
- const inMarket = this.driftClient.getSpotMarketAccount(inMarketIndex);
3201
- const outMarket = this.driftClient.getSpotMarketAccount(outMarketIndex);
3211
+ const inMarket = this.velocityClient.getSpotMarketAccount(inMarketIndex);
3212
+ const outMarket = this.velocityClient.getSpotMarketAccount(outMarketIndex);
3202
3213
 
3203
3214
  const inOraclePriceData = this.getOracleDataForSpotMarket(inMarketIndex);
3204
3215
  const inOraclePrice = inOraclePriceData.price;
@@ -3400,7 +3411,8 @@ export class User {
3400
3411
 
3401
3412
  const currentPosition = this.getPerpPositionOrEmpty(targetMarketIndex);
3402
3413
 
3403
- const perpMarket = this.driftClient.getPerpMarketAccount(targetMarketIndex);
3414
+ const perpMarket =
3415
+ this.velocityClient.getPerpMarketAccount(targetMarketIndex);
3404
3416
  const oracleData = this.getOracleDataForPerpMarket(targetMarketIndex);
3405
3417
 
3406
3418
  let {
@@ -3468,11 +3480,11 @@ export class User {
3468
3480
  }
3469
3481
 
3470
3482
  public getUserFeeTier(marketType: MarketType, now?: BN) {
3471
- const state = this.driftClient.getStateAccount();
3483
+ const state = this.velocityClient.getStateAccount();
3472
3484
 
3473
3485
  const feeTierIndex = 0;
3474
3486
  if (isVariant(marketType, 'perp')) {
3475
- const userStatsAccount: UserStatsAccount = this.driftClient
3487
+ const userStatsAccount: UserStatsAccount = this.velocityClient
3476
3488
  .getUserStats()
3477
3489
  .getAccount();
3478
3490
 
@@ -3538,7 +3550,7 @@ export class User {
3538
3550
  */
3539
3551
  public calculateFeeForQuoteAmount(quoteAmount: BN, marketIndex?: number): BN {
3540
3552
  if (marketIndex !== undefined) {
3541
- const takerFeeMultiplier = this.driftClient.getMarketFees(
3553
+ const takerFeeMultiplier = this.velocityClient.getMarketFees(
3542
3554
  MarketType.PERP,
3543
3555
  marketIndex,
3544
3556
  this
@@ -3563,7 +3575,7 @@ export class User {
3563
3575
  */
3564
3576
  public getWithdrawalLimit(marketIndex: number, reduceOnly?: boolean): BN {
3565
3577
  const nowTs = new BN(Math.floor(Date.now() / 1000));
3566
- const spotMarket = this.driftClient.getSpotMarketAccount(marketIndex);
3578
+ const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
3567
3579
 
3568
3580
  // eslint-disable-next-line prefer-const
3569
3581
  let { borrowLimit, withdrawLimit } = calculateWithdrawLimit(
@@ -3655,7 +3667,7 @@ export class User {
3655
3667
  depositAmount: BN;
3656
3668
  maxDepositAmount: BN;
3657
3669
  } {
3658
- const spotMarket = this.driftClient.getSpotMarketAccount(marketIndex);
3670
+ const spotMarket = this.velocityClient.getSpotMarketAccount(marketIndex);
3659
3671
  const maxDepositAmount = spotMarket.withdrawGuardThreshold.div(new BN(10));
3660
3672
  const position = this.getSpotPosition(marketIndex);
3661
3673
 
@@ -3765,9 +3777,9 @@ export class User {
3765
3777
  ): { canDelete: boolean; reason?: string } {
3766
3778
  const userAccount = this.getUserAccount();
3767
3779
  const userStatsAccountToUse =
3768
- userStatsAccount || this.driftClient.getUserStats().getAccount();
3780
+ userStatsAccount || this.velocityClient.getUserStats().getAccount();
3769
3781
  const nowInSeconds = now || new BN(Math.floor(Date.now() / 1000));
3770
- const stateAccount = this.driftClient.getStateAccount();
3782
+ const stateAccount = this.velocityClient.getStateAccount();
3771
3783
 
3772
3784
  // Referrer cannot delete sub_account_id 0
3773
3785
  const isReferrer =
@@ -3836,7 +3848,7 @@ export class User {
3836
3848
  safestPerpTier = Math.min(
3837
3849
  safestPerpTier,
3838
3850
  getPerpMarketTierNumber(
3839
- this.driftClient.getPerpMarketAccount(perpPosition.marketIndex)
3851
+ this.velocityClient.getPerpMarketAccount(perpPosition.marketIndex)
3840
3852
  )
3841
3853
  );
3842
3854
  }
@@ -3849,7 +3861,7 @@ export class User {
3849
3861
  safestSpotTier = Math.min(
3850
3862
  safestSpotTier,
3851
3863
  getSpotMarketTierNumber(
3852
- this.driftClient.getSpotMarketAccount(spotPosition.marketIndex)
3864
+ this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex)
3853
3865
  )
3854
3866
  );
3855
3867
  }
@@ -3873,12 +3885,12 @@ export class User {
3873
3885
  quoteOraclePriceData?: OraclePriceData;
3874
3886
  includeOpenOrders?: boolean;
3875
3887
  }): HealthComponent {
3876
- const perpMarket = this.driftClient.getPerpMarketAccount(
3888
+ const perpMarket = this.velocityClient.getPerpMarketAccount(
3877
3889
  perpPosition.marketIndex
3878
3890
  );
3879
3891
  const _oraclePriceData =
3880
3892
  oraclePriceData ||
3881
- this.driftClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
3893
+ this.velocityClient.getOracleDataForPerpMarket(perpMarket.marketIndex);
3882
3894
  const oraclePrice = _oraclePriceData.price;
3883
3895
 
3884
3896
  let worstCaseBaseAmount;
@@ -3914,7 +3926,7 @@ export class User {
3914
3926
 
3915
3927
  const _quoteOraclePriceData =
3916
3928
  quoteOraclePriceData ||
3917
- this.driftClient.getOracleDataForSpotMarket(QUOTE_SPOT_MARKET_INDEX);
3929
+ this.velocityClient.getOracleDataForSpotMarket(QUOTE_SPOT_MARKET_INDEX);
3918
3930
 
3919
3931
  let marginRequirement = worstCaseLiabilityValue
3920
3932
  .mul(_quoteOraclePriceData.price)
@@ -3948,17 +3960,16 @@ export class User {
3948
3960
  };
3949
3961
 
3950
3962
  for (const perpPosition of this.getActivePerpPositions()) {
3951
- const perpMarket = this.driftClient.getPerpMarketAccount(
3963
+ const perpMarket = this.velocityClient.getPerpMarketAccount(
3952
3964
  perpPosition.marketIndex
3953
3965
  );
3954
3966
 
3955
- const oraclePriceData = this.driftClient.getOracleDataForPerpMarket(
3967
+ const oraclePriceData = this.velocityClient.getOracleDataForPerpMarket(
3956
3968
  perpMarket.marketIndex
3957
3969
  );
3958
3970
 
3959
- const quoteOraclePriceData = this.driftClient.getOracleDataForSpotMarket(
3960
- QUOTE_SPOT_MARKET_INDEX
3961
- );
3971
+ const quoteOraclePriceData =
3972
+ this.velocityClient.getOracleDataForSpotMarket(QUOTE_SPOT_MARKET_INDEX);
3962
3973
 
3963
3974
  healthComponents.perpPositions.push(
3964
3975
  this.getPerpPositionHealth({
@@ -3969,7 +3980,7 @@ export class User {
3969
3980
  })
3970
3981
  );
3971
3982
 
3972
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
3983
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(
3973
3984
  perpMarket.quoteSpotMarketIndex
3974
3985
  );
3975
3986
 
@@ -4013,7 +4024,7 @@ export class User {
4013
4024
  let netQuoteValue = ZERO;
4014
4025
  for (const spotPosition of this.getActiveSpotPositions()) {
4015
4026
  const spotMarketAccount: SpotMarketAccount =
4016
- this.driftClient.getSpotMarketAccount(spotPosition.marketIndex);
4027
+ this.velocityClient.getSpotMarketAccount(spotPosition.marketIndex);
4017
4028
 
4018
4029
  const oraclePriceData = this.getOracleDataForSpotMarket(
4019
4030
  spotPosition.marketIndex
@@ -4074,7 +4085,7 @@ export class User {
4074
4085
  }
4075
4086
 
4076
4087
  if (!netQuoteValue.eq(ZERO)) {
4077
- const spotMarketAccount = this.driftClient.getQuoteSpotMarketAccount();
4088
+ const spotMarketAccount = this.velocityClient.getQuoteSpotMarketAccount();
4078
4089
  const oraclePriceData = this.getOracleDataForSpotMarket(
4079
4090
  QUOTE_SPOT_MARKET_INDEX
4080
4091
  );
@@ -4148,15 +4159,15 @@ export class User {
4148
4159
  }
4149
4160
 
4150
4161
  private getMMOracleDataForPerpMarket(marketIndex: number): MMOraclePriceData {
4151
- return this.driftClient.getMMOracleDataForPerpMarket(marketIndex);
4162
+ return this.velocityClient.getMMOracleDataForPerpMarket(marketIndex);
4152
4163
  }
4153
4164
 
4154
4165
  private getOracleDataForPerpMarket(marketIndex: number): OraclePriceData {
4155
- return this.driftClient.getOracleDataForPerpMarket(marketIndex);
4166
+ return this.velocityClient.getOracleDataForPerpMarket(marketIndex);
4156
4167
  }
4157
4168
 
4158
4169
  private getOracleDataForSpotMarket(marketIndex: number): OraclePriceData {
4159
- return this.driftClient.getOracleDataForSpotMarket(marketIndex);
4170
+ return this.velocityClient.getOracleDataForSpotMarket(marketIndex);
4160
4171
  }
4161
4172
 
4162
4173
  /**
@@ -4223,7 +4234,7 @@ export class User {
4223
4234
 
4224
4235
  const isQuote = spotPosition.marketIndex === QUOTE_SPOT_MARKET_INDEX;
4225
4236
 
4226
- const spotMarket = this.driftClient.getSpotMarketAccount(
4237
+ const spotMarket = this.velocityClient.getSpotMarketAccount(
4227
4238
  spotPosition.marketIndex
4228
4239
  );
4229
4240
  const oraclePriceData = this.getOracleDataForSpotMarket(
@@ -4329,10 +4340,10 @@ export class User {
4329
4340
 
4330
4341
  // PERP POSITIONS
4331
4342
  for (const marketPosition of this.getActivePerpPositions()) {
4332
- const market = this.driftClient.getPerpMarketAccount(
4343
+ const market = this.velocityClient.getPerpMarketAccount(
4333
4344
  marketPosition.marketIndex
4334
4345
  );
4335
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
4346
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(
4336
4347
  market.quoteSpotMarketIndex
4337
4348
  );
4338
4349
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(
@@ -4438,7 +4449,7 @@ export class User {
4438
4449
  // derive isolated quote deposit value, mirroring on-chain logic
4439
4450
  let depositValue = ZERO;
4440
4451
  if (marketPosition.isolatedPositionScaledBalance?.gt(ZERO)) {
4441
- const quoteSpotMarket = this.driftClient.getSpotMarketAccount(
4452
+ const quoteSpotMarket = this.velocityClient.getSpotMarketAccount(
4442
4453
  market.quoteSpotMarketIndex
4443
4454
  );
4444
4455
  const quoteOraclePriceData = this.getOracleDataForSpotMarket(
package/src/userConfig.ts CHANGED
@@ -5,13 +5,16 @@ import { GrpcConfigs, UserAccountSubscriber } from './accounts/types';
5
5
  import { WebSocketProgramAccountSubscriber } from './accounts/webSocketProgramAccountSubscriber';
6
6
  import { UserAccount } from './types';
7
7
  import { grpcMultiUserAccountSubscriber } from './accounts/grpcMultiUserAccountSubscriber';
8
+ import { AtLeastOne } from './util/deprecatedAlias';
8
9
 
9
- export type UserConfig = {
10
+ type UserConfigBase = {
10
11
  accountSubscription?: UserSubscriptionConfig;
11
- driftClient: VelocityClient;
12
12
  userAccountPublicKey: PublicKey;
13
13
  };
14
14
 
15
+ export type UserConfig = UserConfigBase &
16
+ AtLeastOne<'velocityClient', 'driftClient', VelocityClient>;
17
+
15
18
  export type UserSubscriptionConfig =
16
19
  | {
17
20
  type: 'grpc';