@drift-labs/sdk 2.98.0-beta.2 → 2.98.0-beta.20

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 (71) hide show
  1. package/VERSION +1 -1
  2. package/lib/browser/accounts/pollingHighLeverageModeConfigAccountSubscriber.d.ts +29 -0
  3. package/lib/browser/accounts/pollingHighLeverageModeConfigAccountSubscriber.js +111 -0
  4. package/lib/browser/accounts/types.d.ts +14 -1
  5. package/lib/browser/accounts/webSocketHighLeverageModeConfigAccountSubscriber.d.ts +23 -0
  6. package/lib/browser/accounts/webSocketHighLeverageModeConfigAccountSubscriber.js +69 -0
  7. package/lib/browser/addresses/pda.d.ts +1 -0
  8. package/lib/browser/addresses/pda.js +8 -1
  9. package/lib/browser/constants/perpMarkets.js +11 -0
  10. package/lib/browser/constants/spotMarkets.js +4 -4
  11. package/lib/browser/driftClient.d.ts +20 -6
  12. package/lib/browser/driftClient.js +68 -24
  13. package/lib/browser/events/types.d.ts +3 -2
  14. package/lib/browser/events/types.js +1 -0
  15. package/lib/browser/idl/drift.json +227 -13
  16. package/lib/browser/index.d.ts +4 -0
  17. package/lib/browser/index.js +4 -0
  18. package/lib/browser/jupiter/jupiterClient.d.ts +6 -0
  19. package/lib/browser/memcmp.d.ts +3 -0
  20. package/lib/browser/memcmp.js +28 -1
  21. package/lib/browser/slot/SlothashSubscriber.d.ts +26 -0
  22. package/lib/browser/slot/SlothashSubscriber.js +85 -0
  23. package/lib/browser/types.d.ts +10 -1
  24. package/lib/browser/userMap/referrerMap.d.ts +45 -0
  25. package/lib/browser/userMap/referrerMap.js +180 -0
  26. package/lib/browser/util/digest.d.ts +4 -0
  27. package/lib/browser/util/digest.js +14 -0
  28. package/lib/node/accounts/pollingHighLeverageModeConfigAccountSubscriber.d.ts +29 -0
  29. package/lib/node/accounts/pollingHighLeverageModeConfigAccountSubscriber.js +111 -0
  30. package/lib/node/accounts/types.d.ts +14 -1
  31. package/lib/node/accounts/webSocketHighLeverageModeConfigAccountSubscriber.d.ts +23 -0
  32. package/lib/node/accounts/webSocketHighLeverageModeConfigAccountSubscriber.js +69 -0
  33. package/lib/node/addresses/pda.d.ts +1 -0
  34. package/lib/node/addresses/pda.js +8 -1
  35. package/lib/node/constants/perpMarkets.js +11 -0
  36. package/lib/node/constants/spotMarkets.js +4 -4
  37. package/lib/node/driftClient.d.ts +20 -6
  38. package/lib/node/driftClient.js +68 -24
  39. package/lib/node/events/types.d.ts +3 -2
  40. package/lib/node/events/types.js +1 -0
  41. package/lib/node/idl/drift.json +227 -13
  42. package/lib/node/index.d.ts +4 -0
  43. package/lib/node/index.js +4 -0
  44. package/lib/node/jupiter/jupiterClient.d.ts +6 -0
  45. package/lib/node/memcmp.d.ts +3 -0
  46. package/lib/node/memcmp.js +28 -1
  47. package/lib/node/slot/SlothashSubscriber.d.ts +26 -0
  48. package/lib/node/slot/SlothashSubscriber.js +85 -0
  49. package/lib/node/types.d.ts +10 -1
  50. package/lib/node/userMap/referrerMap.d.ts +45 -0
  51. package/lib/node/userMap/referrerMap.js +180 -0
  52. package/lib/node/util/digest.d.ts +4 -0
  53. package/lib/node/util/digest.js +14 -0
  54. package/package.json +1 -1
  55. package/src/accounts/pollingHighLeverageModeConfigAccountSubscriber.ts +189 -0
  56. package/src/accounts/types.ts +25 -1
  57. package/src/accounts/webSocketHighLeverageModeConfigAccountSubscriber.ts +131 -0
  58. package/src/addresses/pda.ts +13 -0
  59. package/src/constants/perpMarkets.ts +12 -0
  60. package/src/constants/spotMarkets.ts +4 -4
  61. package/src/driftClient.ts +139 -42
  62. package/src/events/types.ts +5 -1
  63. package/src/idl/drift.json +227 -13
  64. package/src/index.ts +4 -0
  65. package/src/jupiter/jupiterClient.ts +6 -0
  66. package/src/memcmp.ts +27 -0
  67. package/src/slot/SlothashSubscriber.ts +126 -0
  68. package/src/types.ts +11 -1
  69. package/src/userMap/referrerMap.ts +283 -0
  70. package/src/util/digest.ts +11 -0
  71. package/tests/ci/verifyConstants.ts +16 -2
@@ -129,6 +129,42 @@
129
129
  ],
130
130
  "args": []
131
131
  },
132
+ {
133
+ "name": "initializeSwiftUserOrders",
134
+ "accounts": [
135
+ {
136
+ "name": "swiftUserOrders",
137
+ "isMut": true,
138
+ "isSigner": false
139
+ },
140
+ {
141
+ "name": "authority",
142
+ "isMut": false,
143
+ "isSigner": true
144
+ },
145
+ {
146
+ "name": "user",
147
+ "isMut": true,
148
+ "isSigner": false
149
+ },
150
+ {
151
+ "name": "payer",
152
+ "isMut": true,
153
+ "isSigner": true
154
+ },
155
+ {
156
+ "name": "rent",
157
+ "isMut": false,
158
+ "isSigner": false
159
+ },
160
+ {
161
+ "name": "systemProgram",
162
+ "isMut": false,
163
+ "isSigner": false
164
+ }
165
+ ],
166
+ "args": []
167
+ },
132
168
  {
133
169
  "name": "initializeReferrerName",
134
170
  "accounts": [
@@ -644,6 +680,63 @@
644
680
  }
645
681
  ]
646
682
  },
683
+ {
684
+ "name": "placeAndMakeSwiftPerpOrder",
685
+ "accounts": [
686
+ {
687
+ "name": "state",
688
+ "isMut": false,
689
+ "isSigner": false
690
+ },
691
+ {
692
+ "name": "user",
693
+ "isMut": true,
694
+ "isSigner": false
695
+ },
696
+ {
697
+ "name": "userStats",
698
+ "isMut": true,
699
+ "isSigner": false
700
+ },
701
+ {
702
+ "name": "taker",
703
+ "isMut": true,
704
+ "isSigner": false
705
+ },
706
+ {
707
+ "name": "takerStats",
708
+ "isMut": true,
709
+ "isSigner": false
710
+ },
711
+ {
712
+ "name": "takerSwiftUserOrders",
713
+ "isMut": false,
714
+ "isSigner": false
715
+ },
716
+ {
717
+ "name": "authority",
718
+ "isMut": false,
719
+ "isSigner": true
720
+ }
721
+ ],
722
+ "args": [
723
+ {
724
+ "name": "params",
725
+ "type": {
726
+ "defined": "OrderParams"
727
+ }
728
+ },
729
+ {
730
+ "name": "swiftOrderUuid",
731
+ "type": {
732
+ "array": [
733
+ "u8",
734
+ 8
735
+ ]
736
+ }
737
+ }
738
+ ]
739
+ },
647
740
  {
648
741
  "name": "placeSwiftTakerOrder",
649
742
  "accounts": [
@@ -662,6 +755,11 @@
662
755
  "isMut": true,
663
756
  "isSigner": false
664
757
  },
758
+ {
759
+ "name": "swiftUserOrders",
760
+ "isMut": true,
761
+ "isSigner": false
762
+ },
665
763
  {
666
764
  "name": "authority",
667
765
  "isMut": false,
@@ -686,15 +784,6 @@
686
784
  {
687
785
  "name": "swiftOrderParamsMessageBytes",
688
786
  "type": "bytes"
689
- },
690
- {
691
- "name": "swiftMessageSignature",
692
- "type": {
693
- "array": [
694
- "u8",
695
- 64
696
- ]
697
- }
698
787
  }
699
788
  ]
700
789
  },
@@ -7477,6 +7566,29 @@
7477
7566
  ]
7478
7567
  }
7479
7568
  },
7569
+ {
7570
+ "name": "SwiftUserOrders",
7571
+ "type": {
7572
+ "kind": "struct",
7573
+ "fields": [
7574
+ {
7575
+ "name": "userPubkey",
7576
+ "type": "publicKey"
7577
+ },
7578
+ {
7579
+ "name": "swiftOrderData",
7580
+ "type": {
7581
+ "array": [
7582
+ {
7583
+ "defined": "SwiftOrderId"
7584
+ },
7585
+ 32
7586
+ ]
7587
+ }
7588
+ }
7589
+ ]
7590
+ }
7591
+ },
7480
7592
  {
7481
7593
  "name": "User",
7482
7594
  "type": {
@@ -8441,6 +8553,15 @@
8441
8553
  "type": {
8442
8554
  "kind": "struct",
8443
8555
  "fields": [
8556
+ {
8557
+ "name": "uuid",
8558
+ "type": {
8559
+ "array": [
8560
+ "u8",
8561
+ 8
8562
+ ]
8563
+ }
8564
+ },
8444
8565
  {
8445
8566
  "name": "swiftOrderSignature",
8446
8567
  "type": {
@@ -8468,10 +8589,6 @@
8468
8589
  "defined": "OrderParams"
8469
8590
  }
8470
8591
  },
8471
- {
8472
- "name": "expectedOrderId",
8473
- "type": "i32"
8474
- },
8475
8592
  {
8476
8593
  "name": "subAccountId",
8477
8594
  "type": "u16"
@@ -9651,6 +9768,31 @@
9651
9768
  ]
9652
9769
  }
9653
9770
  },
9771
+ {
9772
+ "name": "SwiftOrderId",
9773
+ "type": {
9774
+ "kind": "struct",
9775
+ "fields": [
9776
+ {
9777
+ "name": "uuid",
9778
+ "type": {
9779
+ "array": [
9780
+ "u8",
9781
+ 8
9782
+ ]
9783
+ }
9784
+ },
9785
+ {
9786
+ "name": "maxSlot",
9787
+ "type": "u64"
9788
+ },
9789
+ {
9790
+ "name": "orderId",
9791
+ "type": "u32"
9792
+ }
9793
+ ]
9794
+ }
9795
+ },
9654
9796
  {
9655
9797
  "name": "UserFees",
9656
9798
  "type": {
@@ -11529,6 +11671,53 @@
11529
11671
  }
11530
11672
  ]
11531
11673
  },
11674
+ {
11675
+ "name": "SwiftOrderRecord",
11676
+ "fields": [
11677
+ {
11678
+ "name": "user",
11679
+ "type": "publicKey",
11680
+ "index": false
11681
+ },
11682
+ {
11683
+ "name": "hash",
11684
+ "type": "string",
11685
+ "index": false
11686
+ },
11687
+ {
11688
+ "name": "matchingOrderParams",
11689
+ "type": {
11690
+ "defined": "OrderParams"
11691
+ },
11692
+ "index": false
11693
+ },
11694
+ {
11695
+ "name": "userOrderId",
11696
+ "type": "u32",
11697
+ "index": false
11698
+ },
11699
+ {
11700
+ "name": "swiftOrderMaxSlot",
11701
+ "type": "u64",
11702
+ "index": false
11703
+ },
11704
+ {
11705
+ "name": "swiftOrderUuid",
11706
+ "type": {
11707
+ "array": [
11708
+ "u8",
11709
+ 8
11710
+ ]
11711
+ },
11712
+ "index": false
11713
+ },
11714
+ {
11715
+ "name": "ts",
11716
+ "type": "i64",
11717
+ "index": false
11718
+ }
11719
+ ]
11720
+ },
11532
11721
  {
11533
11722
  "name": "OrderRecord",
11534
11723
  "fields": [
@@ -13648,6 +13837,31 @@
13648
13837
  "code": 6296,
13649
13838
  "name": "InvalidRFQMatch",
13650
13839
  "msg": "RFQ matches must be valid"
13840
+ },
13841
+ {
13842
+ "code": 6297,
13843
+ "name": "InvalidSwiftUserAccount",
13844
+ "msg": "Invalid swift user account"
13845
+ },
13846
+ {
13847
+ "code": 6298,
13848
+ "name": "SwiftUserAccountWrongMutability",
13849
+ "msg": "Swift account wrong mutability"
13850
+ },
13851
+ {
13852
+ "code": 6299,
13853
+ "name": "SwiftUserOrdersAccountFull",
13854
+ "msg": "SwiftUserAccount has too many active orders"
13855
+ },
13856
+ {
13857
+ "code": 6300,
13858
+ "name": "SwiftOrderDoesNotExist",
13859
+ "msg": "Order with swift uuid does not exist"
13860
+ },
13861
+ {
13862
+ "code": 6301,
13863
+ "name": "InvalidSwiftOrderId",
13864
+ "msg": "Swift order id cannot be 0s"
13651
13865
  }
13652
13866
  ],
13653
13867
  "metadata": {
package/src/index.ts CHANGED
@@ -11,6 +11,7 @@ export * from './constants/perpMarkets';
11
11
  export * from './accounts/fetch';
12
12
  export * from './accounts/webSocketDriftClientAccountSubscriber';
13
13
  export * from './accounts/webSocketInsuranceFundStakeAccountSubscriber';
14
+ export * from './accounts/webSocketHighLeverageModeConfigAccountSubscriber';
14
15
  export * from './accounts/bulkAccountLoader';
15
16
  export * from './accounts/bulkUserSubscription';
16
17
  export * from './accounts/bulkUserStatsSubscription';
@@ -20,6 +21,7 @@ export * from './accounts/pollingTokenAccountSubscriber';
20
21
  export * from './accounts/pollingUserAccountSubscriber';
21
22
  export * from './accounts/pollingUserStatsAccountSubscriber';
22
23
  export * from './accounts/pollingInsuranceFundStakeAccountSubscriber';
24
+ export * from './accounts/pollingHighLeverageModeConfigAccountSubscriber';
23
25
  export * from './accounts/basicUserAccountSubscriber';
24
26
  export * from './accounts/oneShotUserAccountSubscriber';
25
27
  export * from './accounts/types';
@@ -65,6 +67,7 @@ export * from './math/tiers';
65
67
  export * from './marinade';
66
68
  export * from './orderParams';
67
69
  export * from './slot/SlotSubscriber';
70
+ export * from './slot/SlothashSubscriber';
68
71
  export * from './wallet';
69
72
  export * from './keypair';
70
73
  export * from './types';
@@ -105,6 +108,7 @@ export * from './dlob/DLOBApiClient';
105
108
  export * from './dlob/types';
106
109
  export * from './dlob/orderBookLevels';
107
110
  export * from './userMap/userMap';
111
+ export * from './userMap/referrerMap';
108
112
  export * from './userMap/userStatsMap';
109
113
  export * from './userMap/userMapConfig';
110
114
  export * from './math/bankruptcy';
@@ -216,6 +216,12 @@ export interface QuoteResponse {
216
216
  * @memberof QuoteResponse
217
217
  */
218
218
  error?: string;
219
+ /**
220
+ *
221
+ * @type {string}
222
+ * @memberof QuoteResponse
223
+ */
224
+ errorCode?: string;
219
225
  }
220
226
 
221
227
  export class JupiterClient {
package/src/memcmp.ts CHANGED
@@ -56,3 +56,30 @@ export function getUserWithName(name: string): MemcmpFilter {
56
56
  },
57
57
  };
58
58
  }
59
+
60
+ export function getUserStatsFilter(): MemcmpFilter {
61
+ return {
62
+ memcmp: {
63
+ offset: 0,
64
+ bytes: bs58.encode(BorshAccountsCoder.accountDiscriminator('UserStats')),
65
+ },
66
+ };
67
+ }
68
+
69
+ export function getUserStatsIsReferredFilter(): MemcmpFilter {
70
+ return {
71
+ memcmp: {
72
+ offset: 188,
73
+ bytes: bs58.encode(Buffer.from(Uint8Array.from([2]))),
74
+ },
75
+ };
76
+ }
77
+
78
+ export function getUserStatsIsReferredOrReferrerFilter(): MemcmpFilter {
79
+ return {
80
+ memcmp: {
81
+ offset: 188,
82
+ bytes: bs58.encode(Buffer.from(Uint8Array.from([3]))),
83
+ },
84
+ };
85
+ }
@@ -0,0 +1,126 @@
1
+ import {
2
+ Commitment,
3
+ Connection,
4
+ SYSVAR_SLOT_HASHES_PUBKEY,
5
+ } from '@solana/web3.js';
6
+ import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
7
+ import { BN } from '@coral-xyz/anchor';
8
+
9
+ // eslint-disable-next-line @typescript-eslint/ban-types
10
+ type SlothashSubscriberConfig = {
11
+ resubTimeoutMs?: number;
12
+ commitment?: Commitment;
13
+ }; // for future customization
14
+
15
+ export type Slothash = {
16
+ slot: number;
17
+ hash: string;
18
+ };
19
+
20
+ export class SlothashSubscriber {
21
+ currentSlothash: Slothash;
22
+ subscriptionId: number;
23
+ commitment: Commitment;
24
+
25
+ // Reconnection
26
+ timeoutId?: NodeJS.Timeout;
27
+ resubTimeoutMs?: number;
28
+ isUnsubscribing = false;
29
+ receivingData = false;
30
+
31
+ public constructor(
32
+ private connection: Connection,
33
+ config?: SlothashSubscriberConfig
34
+ ) {
35
+ this.resubTimeoutMs = config?.resubTimeoutMs;
36
+ this.commitment = config?.commitment ?? 'processed';
37
+ if (this.resubTimeoutMs < 1000) {
38
+ console.log(
39
+ 'resubTimeoutMs should be at least 1000ms to avoid spamming resub'
40
+ );
41
+ }
42
+ }
43
+
44
+ public async subscribe(): Promise<void> {
45
+ if (this.subscriptionId != null) {
46
+ return;
47
+ }
48
+
49
+ const currentAccountData = await this.connection.getAccountInfo(
50
+ SYSVAR_SLOT_HASHES_PUBKEY,
51
+ this.commitment
52
+ );
53
+ if (currentAccountData == null) {
54
+ throw new Error('Failed to retrieve current slot hash');
55
+ }
56
+ this.currentSlothash = deserializeSlothash(currentAccountData.data);
57
+
58
+ this.subscriptionId = this.connection.onAccountChange(
59
+ SYSVAR_SLOT_HASHES_PUBKEY,
60
+ (slothashInfo, context) => {
61
+ if (!this.currentSlothash || this.currentSlothash.slot < context.slot) {
62
+ if (this.resubTimeoutMs && !this.isUnsubscribing) {
63
+ this.receivingData = true;
64
+ clearTimeout(this.timeoutId);
65
+ this.setTimeout();
66
+ }
67
+ this.currentSlothash = deserializeSlothash(slothashInfo.data);
68
+ }
69
+ },
70
+ this.commitment
71
+ );
72
+
73
+ if (this.resubTimeoutMs) {
74
+ this.receivingData = true;
75
+ this.setTimeout();
76
+ }
77
+ }
78
+
79
+ private setTimeout(): void {
80
+ this.timeoutId = setTimeout(async () => {
81
+ if (this.isUnsubscribing) {
82
+ // If we are in the process of unsubscribing, do not attempt to resubscribe
83
+ return;
84
+ }
85
+
86
+ if (this.receivingData) {
87
+ console.log(
88
+ `No new slot in ${this.resubTimeoutMs}ms, slot subscriber resubscribing`
89
+ );
90
+ await this.unsubscribe(true);
91
+ this.receivingData = false;
92
+ await this.subscribe();
93
+ }
94
+ }, this.resubTimeoutMs);
95
+ }
96
+
97
+ public getSlothash(): Slothash {
98
+ return this.currentSlothash;
99
+ }
100
+
101
+ public async unsubscribe(onResub = false): Promise<void> {
102
+ if (!onResub) {
103
+ this.resubTimeoutMs = undefined;
104
+ }
105
+ this.isUnsubscribing = true;
106
+ clearTimeout(this.timeoutId);
107
+ this.timeoutId = undefined;
108
+
109
+ if (this.subscriptionId != null) {
110
+ await this.connection.removeSlotChangeListener(this.subscriptionId);
111
+ this.subscriptionId = undefined;
112
+ this.isUnsubscribing = false;
113
+ } else {
114
+ this.isUnsubscribing = false;
115
+ }
116
+ }
117
+ }
118
+
119
+ function deserializeSlothash(data: Buffer): Slothash {
120
+ const slotNumber = new BN(data.subarray(8, 16), 10, 'le');
121
+ const hash = bs58.encode(data.subarray(16, 48));
122
+ return {
123
+ slot: slotNumber.toNumber(),
124
+ hash,
125
+ };
126
+ }
package/src/types.ts CHANGED
@@ -541,6 +541,16 @@ export type SettlePnlRecord = {
541
541
  explanation: SettlePnlExplanation;
542
542
  };
543
543
 
544
+ export type SwiftOrderRecord = {
545
+ ts: BN;
546
+ user: PublicKey;
547
+ hash: string;
548
+ matchingOrderParams: OrderParams;
549
+ swiftOrderMaxSlot: BN;
550
+ swiftOrderUuid: Uint8Array;
551
+ userOrderId: number;
552
+ };
553
+
544
554
  export type OrderRecord = {
545
555
  ts: BN;
546
556
  user: PublicKey;
@@ -1068,11 +1078,11 @@ export const DefaultOrderParams: OrderParams = {
1068
1078
  export type SwiftServerMessage = {
1069
1079
  slot: BN;
1070
1080
  swiftOrderSignature: Uint8Array;
1081
+ uuid: Uint8Array; // From buffer of standard UUID string
1071
1082
  };
1072
1083
 
1073
1084
  export type SwiftOrderParamsMessage = {
1074
1085
  swiftOrderParams: OptionalOrderParams;
1075
- expectedOrderId: number;
1076
1086
  subAccountId: number;
1077
1087
  takeProfitOrderParams: SwiftTriggerOrderParams | null;
1078
1088
  stopLossOrderParams: SwiftTriggerOrderParams | null;