@drift-labs/sdk 0.2.0-master.26 → 0.2.0-master.28

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 (143) hide show
  1. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
  2. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +3 -0
  3. package/lib/accounts/types.d.ts +2 -0
  4. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +1 -0
  5. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +3 -0
  6. package/lib/admin.d.ts +5 -2
  7. package/lib/admin.js +30 -4
  8. package/lib/clearingHouse.d.ts +3 -0
  9. package/lib/clearingHouse.js +19 -1
  10. package/lib/clearingHouseUser.d.ts +11 -9
  11. package/lib/clearingHouseUser.js +184 -108
  12. package/lib/config.js +1 -1
  13. package/lib/dlob/DLOB.d.ts +73 -0
  14. package/lib/dlob/DLOB.js +557 -0
  15. package/lib/dlob/DLOBNode.d.ts +52 -0
  16. package/lib/dlob/DLOBNode.js +82 -0
  17. package/lib/dlob/NodeList.d.ts +26 -0
  18. package/lib/dlob/NodeList.js +138 -0
  19. package/lib/idl/clearing_house.json +123 -203
  20. package/lib/index.d.ts +5 -0
  21. package/lib/index.js +5 -0
  22. package/lib/math/market.d.ts +3 -2
  23. package/lib/math/market.js +17 -12
  24. package/lib/math/orders.d.ts +3 -3
  25. package/lib/math/orders.js +31 -16
  26. package/lib/math/position.d.ts +3 -3
  27. package/lib/math/position.js +23 -16
  28. package/lib/math/repeg.js +8 -0
  29. package/lib/math/spotBalance.d.ts +3 -0
  30. package/lib/math/spotBalance.js +18 -1
  31. package/lib/math/spotPosition.d.ts +5 -1
  32. package/lib/math/spotPosition.js +16 -1
  33. package/lib/types.d.ts +25 -24
  34. package/lib/types.js +9 -3
  35. package/lib/userMap/userMap.d.ts +25 -0
  36. package/lib/userMap/userMap.js +73 -0
  37. package/lib/userMap/userStatsMap.d.ts +19 -0
  38. package/lib/userMap/userStatsMap.js +68 -0
  39. package/package.json +6 -3
  40. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +4 -0
  41. package/src/accounts/types.ts +1 -0
  42. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +6 -0
  43. package/src/admin.ts +50 -8
  44. package/src/clearingHouse.ts +30 -1
  45. package/src/clearingHouseUser.ts +366 -167
  46. package/src/config.ts +1 -1
  47. package/src/dlob/DLOB.ts +884 -0
  48. package/src/dlob/DLOBNode.ts +163 -0
  49. package/src/dlob/NodeList.ts +185 -0
  50. package/src/idl/clearing_house.json +123 -203
  51. package/src/index.ts +5 -0
  52. package/src/math/market.ts +22 -13
  53. package/src/math/orders.ts +29 -21
  54. package/src/math/position.ts +36 -22
  55. package/src/math/repeg.ts +9 -0
  56. package/src/math/spotBalance.ts +26 -0
  57. package/src/math/spotPosition.ts +42 -1
  58. package/src/types.ts +26 -24
  59. package/src/userMap/userMap.ts +100 -0
  60. package/src/userMap/userStatsMap.ts +110 -0
  61. package/tests/bn/test.ts +2 -3
  62. package/tests/dlob/helpers.ts +322 -0
  63. package/tests/dlob/test.ts +2865 -0
  64. package/my-script/.env +0 -7
  65. package/my-script/getUserStats.ts +0 -106
  66. package/my-script/multiConnections.ts +0 -119
  67. package/my-script/test-regex.ts +0 -11
  68. package/my-script/utils.ts +0 -52
  69. package/src/accounts/bulkAccountLoader.js +0 -249
  70. package/src/accounts/bulkUserStatsSubscription.js +0 -75
  71. package/src/accounts/bulkUserSubscription.js +0 -75
  72. package/src/accounts/fetch.js +0 -92
  73. package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -465
  74. package/src/accounts/pollingOracleSubscriber.js +0 -156
  75. package/src/accounts/pollingTokenAccountSubscriber.js +0 -141
  76. package/src/accounts/pollingUserAccountSubscriber.js +0 -208
  77. package/src/accounts/pollingUserStatsAccountSubscriber.js +0 -208
  78. package/src/accounts/types.js +0 -28
  79. package/src/accounts/utils.js +0 -7
  80. package/src/accounts/webSocketAccountSubscriber.js +0 -138
  81. package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -433
  82. package/src/accounts/webSocketUserAccountSubscriber.js +0 -113
  83. package/src/accounts/webSocketUserStatsAccountSubsriber.js +0 -113
  84. package/src/addresses/pda.js +0 -186
  85. package/src/admin.js +0 -1284
  86. package/src/assert/assert.js +0 -9
  87. package/src/clearingHouse.js +0 -3433
  88. package/src/clearingHouseConfig.js +0 -2
  89. package/src/clearingHouseUser.js +0 -874
  90. package/src/clearingHouseUserConfig.js +0 -2
  91. package/src/clearingHouseUserStats.js +0 -115
  92. package/src/clearingHouseUserStatsConfig.js +0 -2
  93. package/src/config.js +0 -80
  94. package/src/constants/numericConstants.js +0 -48
  95. package/src/constants/perpMarkets.js +0 -42
  96. package/src/constants/spotMarkets.js +0 -51
  97. package/src/events/eventList.js +0 -120
  98. package/src/events/eventSubscriber.js +0 -202
  99. package/src/events/fetchLogs.js +0 -117
  100. package/src/events/pollingLogProvider.js +0 -113
  101. package/src/events/sort.js +0 -41
  102. package/src/events/txEventCache.js +0 -74
  103. package/src/events/types.js +0 -25
  104. package/src/events/webSocketLogProvider.js +0 -76
  105. package/src/factory/bigNum.js +0 -393
  106. package/src/factory/oracleClient.js +0 -20
  107. package/src/index.js +0 -75
  108. package/src/math/amm.js +0 -422
  109. package/src/math/auction.js +0 -42
  110. package/src/math/conversion.js +0 -12
  111. package/src/math/funding.js +0 -296
  112. package/src/math/insurance.js +0 -27
  113. package/src/math/margin.js +0 -77
  114. package/src/math/market.js +0 -105
  115. package/src/math/oracles.js +0 -56
  116. package/src/math/orders.js +0 -153
  117. package/src/math/position.js +0 -172
  118. package/src/math/repeg.js +0 -128
  119. package/src/math/spotBalance.js +0 -176
  120. package/src/math/spotMarket.js +0 -8
  121. package/src/math/spotPosition.js +0 -8
  122. package/src/math/trade.js +0 -260
  123. package/src/math/utils.js +0 -27
  124. package/src/oracles/oracleClientCache.js +0 -20
  125. package/src/oracles/pythClient.js +0 -81
  126. package/src/oracles/quoteAssetOracleClient.js +0 -63
  127. package/src/oracles/switchboardClient.js +0 -101
  128. package/src/oracles/types.js +0 -2
  129. package/src/orderParams.js +0 -28
  130. package/src/serum/serumSubscriber.js +0 -102
  131. package/src/serum/types.js +0 -2
  132. package/src/slot/SlotSubscriber.js +0 -86
  133. package/src/token/index.js +0 -38
  134. package/src/tokenFaucet.js +0 -323
  135. package/src/tx/retryTxSender.js +0 -280
  136. package/src/tx/types.js +0 -2
  137. package/src/tx/utils.js +0 -18
  138. package/src/types.js +0 -216
  139. package/src/userName.js +0 -20
  140. package/src/util/computeUnits.js +0 -62
  141. package/src/util/promiseTimeout.js +0 -14
  142. package/src/util/tps.js +0 -61
  143. package/src/wallet.js +0 -72
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UserStatsMap = void 0;
4
+ const __1 = require("..");
5
+ const web3_js_1 = require("@solana/web3.js");
6
+ class UserStatsMap {
7
+ constructor(clearingHouse, accountSubscription) {
8
+ /**
9
+ * map from authority pubkey to ClearingHouseUserStats
10
+ */
11
+ this.userStatsMap = new Map();
12
+ this.clearingHouse = clearingHouse;
13
+ this.accountSubscription = accountSubscription;
14
+ }
15
+ async fetchAllUserStats() {
16
+ const userStatArray = [];
17
+ const programUserAccounts = (await this.clearingHouse.program.account.userStats.all());
18
+ for (const programUserAccount of programUserAccounts) {
19
+ const userStat = programUserAccount.account;
20
+ if (this.userStatsMap.has(userStat.authority.toString())) {
21
+ continue;
22
+ }
23
+ const chUserStat = new __1.ClearingHouseUserStats({
24
+ clearingHouse: this.clearingHouse,
25
+ userStatsAccountPublicKey: (0, __1.getUserStatsAccountPublicKey)(this.clearingHouse.program.programId, userStat.authority),
26
+ accountSubscription: this.accountSubscription,
27
+ });
28
+ userStatArray.push(chUserStat);
29
+ }
30
+ if (this.accountSubscription.type === 'polling') {
31
+ await (0, __1.bulkPollingUserStatsSubscribe)(userStatArray, this.accountSubscription.accountLoader);
32
+ }
33
+ for (const userStat of userStatArray) {
34
+ this.userStatsMap.set(userStat.getAccount().authority.toString(), userStat);
35
+ }
36
+ }
37
+ async addUserStat(authority) {
38
+ const userStat = new __1.ClearingHouseUserStats({
39
+ clearingHouse: this.clearingHouse,
40
+ userStatsAccountPublicKey: (0, __1.getUserStatsAccountPublicKey)(this.clearingHouse.program.programId, authority),
41
+ accountSubscription: this.accountSubscription,
42
+ });
43
+ await userStat.subscribe();
44
+ this.userStatsMap.set(authority.toString(), userStat);
45
+ }
46
+ async updateWithOrderRecord(record, userMap) {
47
+ if (!this.has(record.user.toString())) {
48
+ const takerUserAccount = await userMap.mustGet(record.user.toString());
49
+ this.addUserStat(takerUserAccount.getUserAccount().authority);
50
+ }
51
+ }
52
+ has(authorityPublicKey) {
53
+ return this.userStatsMap.has(authorityPublicKey);
54
+ }
55
+ get(authorityPublicKey) {
56
+ return this.userStatsMap.get(authorityPublicKey);
57
+ }
58
+ async mustGet(authorityPublicKey) {
59
+ if (!this.has(authorityPublicKey)) {
60
+ await this.addUserStat(new web3_js_1.PublicKey(authorityPublicKey));
61
+ }
62
+ return this.get(authorityPublicKey);
63
+ }
64
+ values() {
65
+ return this.userStatsMap.values();
66
+ }
67
+ }
68
+ exports.UserStatsMap = UserStatsMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "0.2.0-master.26",
3
+ "version": "0.2.0-master.28",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -14,7 +14,9 @@
14
14
  "build": "yarn clean && tsc",
15
15
  "clean": "rm -rf lib",
16
16
  "test": "mocha -r ts-node/register tests/**/*.ts",
17
- "patch-and-pub": "npm version patch --force && npm publish"
17
+ "patch-and-pub": "npm version patch --force && npm publish",
18
+ "prettify": "prettier --check './src/***/*.ts'",
19
+ "prettify:fix": "prettier --write './{src,tests}/***/*.ts'"
18
20
  },
19
21
  "keywords": [
20
22
  "drift-labs",
@@ -48,7 +50,8 @@
48
50
  "eslint-config-prettier": "^8.3.0",
49
51
  "eslint-plugin-prettier": "^3.4.0",
50
52
  "mocha": "^10.0.0",
51
- "ts-node": "^10.8.0"
53
+ "ts-node": "^10.8.0",
54
+ "prettier": "^2.4.1"
52
55
  },
53
56
  "description": "SDK for Drift Protocol v1",
54
57
  "engines": {
@@ -409,6 +409,10 @@ export class PollingClearingHouseAccountSubscriber
409
409
  return this.spotMarket.get(marketIndex.toNumber());
410
410
  }
411
411
 
412
+ public getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[] {
413
+ return Array.from(this.spotMarket.values());
414
+ }
415
+
412
416
  public getOraclePriceDataAndSlot(
413
417
  oraclePublicKey: PublicKey
414
418
  ): DataAndSlot<OraclePriceData> | undefined {
@@ -54,6 +54,7 @@ export interface ClearingHouseAccountSubscriber {
54
54
  getSpotMarketAccountAndSlot(
55
55
  marketIndex: BN
56
56
  ): DataAndSlot<SpotMarketAccount> | undefined;
57
+ getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[];
57
58
  getOraclePriceDataAndSlot(
58
59
  oraclePublicKey: PublicKey
59
60
  ): DataAndSlot<OraclePriceData> | undefined;
@@ -313,6 +313,12 @@ export class WebSocketClearingHouseAccountSubscriber
313
313
  .dataAndSlot;
314
314
  }
315
315
 
316
+ public getSpotMarketAccountsAndSlots(): DataAndSlot<SpotMarketAccount>[] {
317
+ return Array.from(this.spotMarketAccountSubscribers.values()).map(
318
+ (subscriber) => subscriber.dataAndSlot
319
+ );
320
+ }
321
+
316
322
  public getOraclePriceDataAndSlot(
317
323
  oraclePublicKey: PublicKey
318
324
  ): DataAndSlot<OraclePriceData> | undefined {
package/src/admin.ts CHANGED
@@ -274,6 +274,19 @@ export class Admin extends ClearingHouse {
274
274
  });
275
275
  }
276
276
 
277
+ public async updateConcentrationScale(
278
+ marketIndex: BN,
279
+ concentrationScale: BN
280
+ ): Promise<TransactionSignature> {
281
+ return await this.program.rpc.updateConcentrationScale(concentrationScale, {
282
+ accounts: {
283
+ state: await this.getStatePublicKey(),
284
+ admin: this.wallet.publicKey,
285
+ market: await getMarketPublicKey(this.program.programId, marketIndex),
286
+ },
287
+ });
288
+ }
289
+
277
290
  public async moveAmmToPrice(
278
291
  perpMarketIndex: BN,
279
292
  targetPrice: BN
@@ -654,14 +667,31 @@ export class Admin extends ClearingHouse {
654
667
  public async updateSpotMarketIfFactor(
655
668
  marketIndex: BN,
656
669
  userIfFactor: BN,
657
- totalIfFactor: BN,
658
- liquidationIfFactor: BN
670
+ totalIfFactor: BN
659
671
  ): Promise<TransactionSignature> {
660
672
  return await this.program.rpc.updateSpotMarketIfFactor(
661
673
  marketIndex,
662
674
  userIfFactor,
663
675
  totalIfFactor,
664
- liquidationIfFactor,
676
+ {
677
+ accounts: {
678
+ admin: this.wallet.publicKey,
679
+ state: await this.getStatePublicKey(),
680
+ spotMarket: await getSpotMarketPublicKey(
681
+ this.program.programId,
682
+ marketIndex
683
+ ),
684
+ },
685
+ }
686
+ );
687
+ }
688
+
689
+ public async updateSpotMarketRevenueSettlePeriod(
690
+ marketIndex: BN,
691
+ revenueSettlePeriod: BN
692
+ ): Promise<TransactionSignature> {
693
+ return await this.program.rpc.updateSpotMarketRevenueSettlePeriod(
694
+ revenueSettlePeriod,
665
695
  {
666
696
  accounts: {
667
697
  admin: this.wallet.publicKey,
@@ -830,13 +860,25 @@ export class Admin extends ClearingHouse {
830
860
  });
831
861
  }
832
862
 
833
- public async updateAuctionDuration(
834
- minDuration: BN | number,
835
- maxDuration: BN | number
863
+ public async updatePerpAuctionDuration(
864
+ minDuration: BN | number
836
865
  ): Promise<TransactionSignature> {
837
- return await this.program.rpc.updateAuctionDuration(
866
+ return await this.program.rpc.updatePerpAuctionDuration(
838
867
  typeof minDuration === 'number' ? minDuration : minDuration.toNumber(),
839
- typeof maxDuration === 'number' ? maxDuration : maxDuration.toNumber(),
868
+ {
869
+ accounts: {
870
+ admin: this.wallet.publicKey,
871
+ state: await this.getStatePublicKey(),
872
+ },
873
+ }
874
+ );
875
+ }
876
+
877
+ public async updateSpotAuctionDuration(
878
+ defaultAuctionDuration: number
879
+ ): Promise<TransactionSignature> {
880
+ return await this.program.rpc.updateSpotAuctionDuration(
881
+ defaultAuctionDuration,
840
882
  {
841
883
  accounts: {
842
884
  admin: this.wallet.publicKey,
@@ -305,6 +305,12 @@ export class ClearingHouse {
305
305
  return this.accountSubscriber.getSpotMarketAccountAndSlot(marketIndex).data;
306
306
  }
307
307
 
308
+ public getSpotMarketAccounts(): SpotMarketAccount[] {
309
+ return this.accountSubscriber
310
+ .getSpotMarketAccountsAndSlots()
311
+ .map((value) => value.data);
312
+ }
313
+
308
314
  public getQuoteSpotMarketAccount(): SpotMarketAccount {
309
315
  return this.accountSubscriber.getSpotMarketAccountAndSlot(
310
316
  QUOTE_SPOT_MARKET_INDEX
@@ -472,6 +478,22 @@ export class ClearingHouse {
472
478
  });
473
479
  }
474
480
 
481
+ public async updateUserCustomMarginRatio(
482
+ marginRatio: number,
483
+ userId = 0
484
+ ): Promise<TransactionSignature> {
485
+ return await this.program.rpc.updateUserCustomMarginRatio(
486
+ userId,
487
+ marginRatio,
488
+ {
489
+ accounts: {
490
+ user: await this.getUserAccountPublicKey(),
491
+ authority: this.wallet.publicKey,
492
+ },
493
+ }
494
+ );
495
+ }
496
+
475
497
  public getUser(userId?: number): ClearingHouseUser {
476
498
  userId = userId ?? this.activeUserId;
477
499
  if (!this.users.has(userId)) {
@@ -595,7 +617,7 @@ export class ClearingHouse {
595
617
  perpMarketAccountMap.set(params.readablePerpMarketIndex.toNumber(), {
596
618
  pubkey: marketAccount.pubkey,
597
619
  isSigner: false,
598
- isWritable: true,
620
+ isWritable: false,
599
621
  });
600
622
  oracleAccountMap.set(marketAccount.amm.oracle.toString(), {
601
623
  pubkey: marketAccount.amm.oracle,
@@ -3177,6 +3199,13 @@ export class ClearingHouse {
3177
3199
  return oracleData;
3178
3200
  }
3179
3201
 
3202
+ public getOracleDataForSpotMarket(marketIndex: BN): OraclePriceData {
3203
+ const oracleKey = this.getSpotMarketAccount(marketIndex).oracle;
3204
+ const oracleData = this.getOraclePriceDataAndSlot(oracleKey).data;
3205
+
3206
+ return oracleData;
3207
+ }
3208
+
3180
3209
  public async initializeInsuranceFundStake(
3181
3210
  marketIndex: BN
3182
3211
  ): Promise<TransactionSignature> {