@drift-labs/sdk 2.0.9 → 2.0.11

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.
@@ -42,6 +42,7 @@ export declare class AdminClient extends DriftClient {
42
42
  updateSpotMarketMinOrderSize(spotMarketIndex: number, orderSize: BN): Promise<TransactionSignature>;
43
43
  updatePerpMarketExpiry(perpMarketIndex: number, expiryTs: BN): Promise<TransactionSignature>;
44
44
  updateSpotMarketOracle(spotMarketIndex: number, oracle: PublicKey, oracleSource: OracleSource): Promise<TransactionSignature>;
45
+ updateSpotMarketOrdersEnabled(spotMarketIndex: number, ordersEnabled: boolean): Promise<TransactionSignature>;
45
46
  updateSpotMarketExpiry(spotMarketIndex: number, expiryTs: BN): Promise<TransactionSignature>;
46
47
  updateWhitelistMint(whitelistMint?: PublicKey): Promise<TransactionSignature>;
47
48
  updateDiscountMint(discountMint: PublicKey): Promise<TransactionSignature>;
@@ -449,7 +449,7 @@ class AdminClient extends driftClient_1.DriftClient {
449
449
  });
450
450
  }
451
451
  async updateSpotMarketOracle(spotMarketIndex, oracle, oracleSource) {
452
- return await this.program.rpc.updatePerpMarketOracle(oracle, oracleSource, {
452
+ return await this.program.rpc.updateSpotMarketOracle(oracle, oracleSource, {
453
453
  accounts: {
454
454
  admin: this.wallet.publicKey,
455
455
  state: await this.getStatePublicKey(),
@@ -458,6 +458,15 @@ class AdminClient extends driftClient_1.DriftClient {
458
458
  },
459
459
  });
460
460
  }
461
+ async updateSpotMarketOrdersEnabled(spotMarketIndex, ordersEnabled) {
462
+ return await this.program.rpc.updateSpotMarketOrdersEnabled(ordersEnabled, {
463
+ accounts: {
464
+ admin: this.wallet.publicKey,
465
+ state: await this.getStatePublicKey(),
466
+ spotMarket: await (0, pda_1.getSpotMarketPublicKey)(this.program.programId, spotMarketIndex),
467
+ },
468
+ });
469
+ }
461
470
  async updateSpotMarketExpiry(spotMarketIndex, expiryTs) {
462
471
  return await this.program.rpc.updateSpotMarketExpiry(expiryTs, {
463
472
  accounts: {
@@ -82,6 +82,7 @@ export declare class DriftClient {
82
82
  getInitializeUserStatsIx(): Promise<TransactionInstruction>;
83
83
  updateUserName(name: string, subAccountId?: number): Promise<TransactionSignature>;
84
84
  updateUserCustomMarginRatio(marginRatio: number, subAccountId?: number): Promise<TransactionSignature>;
85
+ updateUserMarginTradingEnabled(marginTradingEnabled: boolean, subAccountId?: number): Promise<TransactionSignature>;
85
86
  updateUserDelegate(delegate: PublicKey, subAccountId?: number): Promise<TransactionSignature>;
86
87
  getUserAccountsForDelegate(delegate: PublicKey): Promise<UserAccount[]>;
87
88
  getUserAccountsForAuthority(authority: PublicKey): Promise<UserAccount[]>;
@@ -333,6 +333,14 @@ class DriftClient {
333
333
  },
334
334
  });
335
335
  }
336
+ async updateUserMarginTradingEnabled(marginTradingEnabled, subAccountId = 0) {
337
+ return await this.program.rpc.updateUserMarginTradingEnabled(subAccountId, marginTradingEnabled, {
338
+ accounts: {
339
+ user: await this.getUserAccountPublicKey(),
340
+ authority: this.wallet.publicKey,
341
+ },
342
+ });
343
+ }
336
344
  async updateUserDelegate(delegate, subAccountId = 0) {
337
345
  return await this.program.rpc.updateUserDelegate(subAccountId, delegate, {
338
346
  accounts: {
@@ -316,19 +316,15 @@ class BigNum {
316
316
  let val = usingCustomPrecision
317
317
  ? this.prettyPrint(useTradePrecision, precisionOverride)
318
318
  : BigNum.fromPrint(this.toFixed(2), new anchor_1.BN(2)).prettyPrint();
319
- // Append two trailing zeroes if not using custom precision
319
+ // Append trailing zeroes out to 2 decimal places if not using custom precision
320
320
  if (!usingCustomPrecision) {
321
321
  const [_, rightSide] = val.split(BigNum.delim);
322
322
  const trailingLength = (_a = rightSide === null || rightSide === void 0 ? void 0 : rightSide.length) !== null && _a !== void 0 ? _a : 0;
323
- if (trailingLength < 2) {
324
- const numHasDecimals = this.print().includes(BigNum.delim);
325
- // Handle case where pretty print won't include the decimal point
326
- if (trailingLength === 0 && numHasDecimals) {
327
- val = `${val}.00`;
328
- }
329
- else {
330
- val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
331
- }
323
+ if (trailingLength === 0) {
324
+ val = `${val}${BigNum.delim}00`;
325
+ }
326
+ else if (trailingLength === 1) {
327
+ val = `${val}0`;
332
328
  }
333
329
  }
334
330
  return `${prefix}${val.replace('-', '')}`;
@@ -733,6 +733,31 @@
733
733
  }
734
734
  ]
735
735
  },
736
+ {
737
+ "name": "updateUserMarginTradingEnabled",
738
+ "accounts": [
739
+ {
740
+ "name": "user",
741
+ "isMut": true,
742
+ "isSigner": false
743
+ },
744
+ {
745
+ "name": "authority",
746
+ "isMut": false,
747
+ "isSigner": true
748
+ }
749
+ ],
750
+ "args": [
751
+ {
752
+ "name": "subAccountId",
753
+ "type": "u16"
754
+ },
755
+ {
756
+ "name": "marginTradingEnabled",
757
+ "type": "bool"
758
+ }
759
+ ]
760
+ },
736
761
  {
737
762
  "name": "updateUserDelegate",
738
763
  "accounts": [
@@ -1,4 +1,4 @@
1
- import { User, DriftClient, OrderRecord, UserSubscriptionConfig } from '..';
1
+ import { User, DriftClient, OrderRecord, UserSubscriptionConfig, WrappedEvent } from '..';
2
2
  import { PublicKey } from '@solana/web3.js';
3
3
  export interface UserMapInterface {
4
4
  fetchAllUsers(): Promise<void>;
@@ -37,5 +37,7 @@ export declare class UserMap implements UserMapInterface {
37
37
  */
38
38
  getUserAuthority(key: string): PublicKey | undefined;
39
39
  updateWithOrderRecord(record: OrderRecord): Promise<void>;
40
+ updateWithEventRecord(record: WrappedEvent<any>): Promise<void>;
40
41
  values(): IterableIterator<User>;
42
+ size(): number;
41
43
  }
@@ -80,8 +80,51 @@ class UserMap {
80
80
  await this.addPubkey(record.user);
81
81
  }
82
82
  }
83
+ async updateWithEventRecord(record) {
84
+ if (record.eventType === 'DepositRecord') {
85
+ const depositRecord = record;
86
+ await this.mustGet(depositRecord.user.toString());
87
+ }
88
+ else if (record.eventType === 'FundingPaymentRecord') {
89
+ const fundingPaymentRecord = record;
90
+ await this.mustGet(fundingPaymentRecord.user.toString());
91
+ }
92
+ else if (record.eventType === 'LiquidationRecord') {
93
+ const liqRecord = record;
94
+ await this.mustGet(liqRecord.user.toString());
95
+ await this.mustGet(liqRecord.liquidator.toString());
96
+ }
97
+ else if (record.eventType === 'OrderRecord') {
98
+ const orderRecord = record;
99
+ await this.updateWithOrderRecord(orderRecord);
100
+ }
101
+ else if (record.eventType === 'OrderActionRecord') {
102
+ const actionRecord = record;
103
+ if (actionRecord.taker) {
104
+ await this.mustGet(actionRecord.taker.toString());
105
+ }
106
+ if (actionRecord.maker) {
107
+ await this.mustGet(actionRecord.maker.toString());
108
+ }
109
+ }
110
+ else if (record.eventType === 'SettlePnlRecord') {
111
+ const settlePnlRecord = record;
112
+ await this.mustGet(settlePnlRecord.user.toString());
113
+ }
114
+ else if (record.eventType === 'NewUserRecord') {
115
+ const newUserRecord = record;
116
+ await this.mustGet(newUserRecord.user.toString());
117
+ }
118
+ else if (record.eventType === 'LPRecord') {
119
+ const lpRecord = record;
120
+ await this.mustGet(lpRecord.user.toString());
121
+ }
122
+ }
83
123
  values() {
84
124
  return this.userMap.values();
85
125
  }
126
+ size() {
127
+ return this.userMap.size;
128
+ }
86
129
  }
87
130
  exports.UserMap = UserMap;
@@ -1,4 +1,4 @@
1
- import { DriftClient, OrderRecord, UserStats, UserStatsSubscriptionConfig } from '..';
1
+ import { DriftClient, OrderRecord, UserStats, UserStatsSubscriptionConfig, WrappedEvent } from '..';
2
2
  import { PublicKey } from '@solana/web3.js';
3
3
  import { UserMap } from './userMap';
4
4
  export declare class UserStatsMap {
@@ -12,8 +12,10 @@ export declare class UserStatsMap {
12
12
  fetchAllUserStats(): Promise<void>;
13
13
  addUserStat(authority: PublicKey): Promise<void>;
14
14
  updateWithOrderRecord(record: OrderRecord, userMap: UserMap): Promise<void>;
15
+ updateWithEventRecord(record: WrappedEvent<any>, userMap?: UserMap): Promise<void>;
15
16
  has(authorityPublicKey: string): boolean;
16
17
  get(authorityPublicKey: string): UserStats;
17
18
  mustGet(authorityPublicKey: string): Promise<UserStats>;
18
19
  values(): IterableIterator<UserStats>;
20
+ size(): number;
19
21
  }
@@ -49,6 +49,71 @@ class UserStatsMap {
49
49
  this.addUserStat(user.getUserAccount().authority);
50
50
  }
51
51
  }
52
+ async updateWithEventRecord(record, userMap) {
53
+ if (record.eventType === 'DepositRecord') {
54
+ const depositRecord = record;
55
+ await this.mustGet(depositRecord.userAuthority.toString());
56
+ }
57
+ else if (record.eventType === 'FundingPaymentRecord') {
58
+ const fundingPaymentRecord = record;
59
+ await this.mustGet(fundingPaymentRecord.userAuthority.toString());
60
+ }
61
+ else if (record.eventType === 'LiquidationRecord') {
62
+ if (!userMap) {
63
+ return;
64
+ }
65
+ const liqRecord = record;
66
+ const user = await userMap.mustGet(liqRecord.user.toString());
67
+ await this.mustGet(user.getUserAccount().authority.toString());
68
+ const liquidatorUser = await userMap.mustGet(liqRecord.liquidator.toString());
69
+ await this.mustGet(liquidatorUser.getUserAccount().authority.toString());
70
+ }
71
+ else if (record.eventType === 'OrderRecord') {
72
+ if (!userMap) {
73
+ return;
74
+ }
75
+ const orderRecord = record;
76
+ await userMap.updateWithOrderRecord(orderRecord);
77
+ }
78
+ else if (record.eventType === 'OrderActionRecord') {
79
+ if (!userMap) {
80
+ return;
81
+ }
82
+ const actionRecord = record;
83
+ if (actionRecord.taker) {
84
+ const taker = await userMap.mustGet(actionRecord.taker.toString());
85
+ await this.mustGet(taker.getUserAccount().authority.toString());
86
+ }
87
+ if (actionRecord.maker) {
88
+ const maker = await userMap.mustGet(actionRecord.maker.toString());
89
+ await this.mustGet(maker.getUserAccount().authority.toString());
90
+ }
91
+ }
92
+ else if (record.eventType === 'SettlePnlRecord') {
93
+ if (!userMap) {
94
+ return;
95
+ }
96
+ const settlePnlRecord = record;
97
+ const user = await userMap.mustGet(settlePnlRecord.user.toString());
98
+ await this.mustGet(user.getUserAccount().authority.toString());
99
+ }
100
+ else if (record.eventType === 'NewUserRecord') {
101
+ const newUserRecord = record;
102
+ await this.mustGet(newUserRecord.userAuthority.toString());
103
+ }
104
+ else if (record.eventType === 'LPRecord') {
105
+ if (!userMap) {
106
+ return;
107
+ }
108
+ const lpRecord = record;
109
+ const user = await userMap.mustGet(lpRecord.user.toString());
110
+ await this.mustGet(user.getUserAccount().authority.toString());
111
+ }
112
+ else if (record.eventType === 'InsuranceFundStakeRecord') {
113
+ const ifStakeRecord = record;
114
+ await this.mustGet(ifStakeRecord.userAuthority.toString());
115
+ }
116
+ }
52
117
  has(authorityPublicKey) {
53
118
  return this.userStatsMap.has(authorityPublicKey);
54
119
  }
@@ -64,5 +129,8 @@ class UserStatsMap {
64
129
  values() {
65
130
  return this.userStatsMap.values();
66
131
  }
132
+ size() {
133
+ return this.userStatsMap.size;
134
+ }
67
135
  }
68
136
  exports.UserStatsMap = UserStatsMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/sdk",
3
- "version": "2.0.9",
3
+ "version": "2.0.11",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "author": "crispheaney",
@@ -852,7 +852,7 @@ export class AdminClient extends DriftClient {
852
852
  oracle: PublicKey,
853
853
  oracleSource: OracleSource
854
854
  ): Promise<TransactionSignature> {
855
- return await this.program.rpc.updatePerpMarketOracle(oracle, oracleSource, {
855
+ return await this.program.rpc.updateSpotMarketOracle(oracle, oracleSource, {
856
856
  accounts: {
857
857
  admin: this.wallet.publicKey,
858
858
  state: await this.getStatePublicKey(),
@@ -865,6 +865,22 @@ export class AdminClient extends DriftClient {
865
865
  });
866
866
  }
867
867
 
868
+ public async updateSpotMarketOrdersEnabled(
869
+ spotMarketIndex: number,
870
+ ordersEnabled: boolean
871
+ ): Promise<TransactionSignature> {
872
+ return await this.program.rpc.updateSpotMarketOrdersEnabled(ordersEnabled, {
873
+ accounts: {
874
+ admin: this.wallet.publicKey,
875
+ state: await this.getStatePublicKey(),
876
+ spotMarket: await getSpotMarketPublicKey(
877
+ this.program.programId,
878
+ spotMarketIndex
879
+ ),
880
+ },
881
+ });
882
+ }
883
+
868
884
  public async updateSpotMarketExpiry(
869
885
  spotMarketIndex: number,
870
886
  expiryTs: BN
@@ -556,6 +556,22 @@ export class DriftClient {
556
556
  );
557
557
  }
558
558
 
559
+ public async updateUserMarginTradingEnabled(
560
+ marginTradingEnabled: boolean,
561
+ subAccountId = 0
562
+ ): Promise<TransactionSignature> {
563
+ return await this.program.rpc.updateUserMarginTradingEnabled(
564
+ subAccountId,
565
+ marginTradingEnabled,
566
+ {
567
+ accounts: {
568
+ user: await this.getUserAccountPublicKey(),
569
+ authority: this.wallet.publicKey,
570
+ },
571
+ }
572
+ );
573
+ }
574
+
559
575
  public async updateUserDelegate(
560
576
  delegate: PublicKey,
561
577
  subAccountId = 0
@@ -481,21 +481,15 @@ export class BigNum {
481
481
  ? this.prettyPrint(useTradePrecision, precisionOverride)
482
482
  : BigNum.fromPrint(this.toFixed(2), new BN(2)).prettyPrint();
483
483
 
484
- // Append two trailing zeroes if not using custom precision
484
+ // Append trailing zeroes out to 2 decimal places if not using custom precision
485
485
  if (!usingCustomPrecision) {
486
486
  const [_, rightSide] = val.split(BigNum.delim);
487
-
488
487
  const trailingLength = rightSide?.length ?? 0;
489
488
 
490
- if (trailingLength < 2) {
491
- const numHasDecimals = this.print().includes(BigNum.delim);
492
-
493
- // Handle case where pretty print won't include the decimal point
494
- if (trailingLength === 0 && numHasDecimals) {
495
- val = `${val}.00`;
496
- } else {
497
- val = `${val}${new Array(2 - trailingLength).fill('0').join('')}`;
498
- }
489
+ if (trailingLength === 0) {
490
+ val = `${val}${BigNum.delim}00`;
491
+ } else if (trailingLength === 1) {
492
+ val = `${val}0`;
499
493
  }
500
494
  }
501
495
 
@@ -733,6 +733,31 @@
733
733
  }
734
734
  ]
735
735
  },
736
+ {
737
+ "name": "updateUserMarginTradingEnabled",
738
+ "accounts": [
739
+ {
740
+ "name": "user",
741
+ "isMut": true,
742
+ "isSigner": false
743
+ },
744
+ {
745
+ "name": "authority",
746
+ "isMut": false,
747
+ "isSigner": true
748
+ }
749
+ ],
750
+ "args": [
751
+ {
752
+ "name": "subAccountId",
753
+ "type": "u16"
754
+ },
755
+ {
756
+ "name": "marginTradingEnabled",
757
+ "type": "bool"
758
+ }
759
+ ]
760
+ },
736
761
  {
737
762
  "name": "updateUserDelegate",
738
763
  "accounts": [
@@ -5,6 +5,14 @@ import {
5
5
  bulkPollingUserSubscribe,
6
6
  OrderRecord,
7
7
  UserSubscriptionConfig,
8
+ WrappedEvent,
9
+ DepositRecord,
10
+ FundingPaymentRecord,
11
+ LiquidationRecord,
12
+ OrderActionRecord,
13
+ SettlePnlRecord,
14
+ NewUserRecord,
15
+ LPRecord,
8
16
  } from '..';
9
17
  import { ProgramAccount } from '@project-serum/anchor';
10
18
 
@@ -120,7 +128,47 @@ export class UserMap implements UserMapInterface {
120
128
  }
121
129
  }
122
130
 
131
+ public async updateWithEventRecord(record: WrappedEvent<any>) {
132
+ if (record.eventType === 'DepositRecord') {
133
+ const depositRecord = record as DepositRecord;
134
+ await this.mustGet(depositRecord.user.toString());
135
+ } else if (record.eventType === 'FundingPaymentRecord') {
136
+ const fundingPaymentRecord = record as FundingPaymentRecord;
137
+ await this.mustGet(fundingPaymentRecord.user.toString());
138
+ } else if (record.eventType === 'LiquidationRecord') {
139
+ const liqRecord = record as LiquidationRecord;
140
+
141
+ await this.mustGet(liqRecord.user.toString());
142
+ await this.mustGet(liqRecord.liquidator.toString());
143
+ } else if (record.eventType === 'OrderRecord') {
144
+ const orderRecord = record as OrderRecord;
145
+ await this.updateWithOrderRecord(orderRecord);
146
+ } else if (record.eventType === 'OrderActionRecord') {
147
+ const actionRecord = record as OrderActionRecord;
148
+
149
+ if (actionRecord.taker) {
150
+ await this.mustGet(actionRecord.taker.toString());
151
+ }
152
+ if (actionRecord.maker) {
153
+ await this.mustGet(actionRecord.maker.toString());
154
+ }
155
+ } else if (record.eventType === 'SettlePnlRecord') {
156
+ const settlePnlRecord = record as SettlePnlRecord;
157
+ await this.mustGet(settlePnlRecord.user.toString());
158
+ } else if (record.eventType === 'NewUserRecord') {
159
+ const newUserRecord = record as NewUserRecord;
160
+ await this.mustGet(newUserRecord.user.toString());
161
+ } else if (record.eventType === 'LPRecord') {
162
+ const lpRecord = record as LPRecord;
163
+ await this.mustGet(lpRecord.user.toString());
164
+ }
165
+ }
166
+
123
167
  public values(): IterableIterator<User> {
124
168
  return this.userMap.values();
125
169
  }
170
+
171
+ public size(): number {
172
+ return this.userMap.size;
173
+ }
126
174
  }
@@ -6,6 +6,15 @@ import {
6
6
  UserStats,
7
7
  UserStatsSubscriptionConfig,
8
8
  bulkPollingUserStatsSubscribe,
9
+ WrappedEvent,
10
+ DepositRecord,
11
+ FundingPaymentRecord,
12
+ LiquidationRecord,
13
+ OrderActionRecord,
14
+ SettlePnlRecord,
15
+ NewUserRecord,
16
+ LPRecord,
17
+ InsuranceFundStakeRecord,
9
18
  } from '..';
10
19
  import { ProgramAccount } from '@project-serum/anchor';
11
20
  import { PublicKey } from '@solana/web3.js';
@@ -87,6 +96,73 @@ export class UserStatsMap {
87
96
  }
88
97
  }
89
98
 
99
+ public async updateWithEventRecord(
100
+ record: WrappedEvent<any>,
101
+ userMap?: UserMap
102
+ ) {
103
+ if (record.eventType === 'DepositRecord') {
104
+ const depositRecord = record as DepositRecord;
105
+ await this.mustGet(depositRecord.userAuthority.toString());
106
+ } else if (record.eventType === 'FundingPaymentRecord') {
107
+ const fundingPaymentRecord = record as FundingPaymentRecord;
108
+ await this.mustGet(fundingPaymentRecord.userAuthority.toString());
109
+ } else if (record.eventType === 'LiquidationRecord') {
110
+ if (!userMap) {
111
+ return;
112
+ }
113
+
114
+ const liqRecord = record as LiquidationRecord;
115
+
116
+ const user = await userMap.mustGet(liqRecord.user.toString());
117
+ await this.mustGet(user.getUserAccount().authority.toString());
118
+
119
+ const liquidatorUser = await userMap.mustGet(
120
+ liqRecord.liquidator.toString()
121
+ );
122
+ await this.mustGet(liquidatorUser.getUserAccount().authority.toString());
123
+ } else if (record.eventType === 'OrderRecord') {
124
+ if (!userMap) {
125
+ return;
126
+ }
127
+ const orderRecord = record as OrderRecord;
128
+ await userMap.updateWithOrderRecord(orderRecord);
129
+ } else if (record.eventType === 'OrderActionRecord') {
130
+ if (!userMap) {
131
+ return;
132
+ }
133
+ const actionRecord = record as OrderActionRecord;
134
+
135
+ if (actionRecord.taker) {
136
+ const taker = await userMap.mustGet(actionRecord.taker.toString());
137
+ await this.mustGet(taker.getUserAccount().authority.toString());
138
+ }
139
+ if (actionRecord.maker) {
140
+ const maker = await userMap.mustGet(actionRecord.maker.toString());
141
+ await this.mustGet(maker.getUserAccount().authority.toString());
142
+ }
143
+ } else if (record.eventType === 'SettlePnlRecord') {
144
+ if (!userMap) {
145
+ return;
146
+ }
147
+ const settlePnlRecord = record as SettlePnlRecord;
148
+ const user = await userMap.mustGet(settlePnlRecord.user.toString());
149
+ await this.mustGet(user.getUserAccount().authority.toString());
150
+ } else if (record.eventType === 'NewUserRecord') {
151
+ const newUserRecord = record as NewUserRecord;
152
+ await this.mustGet(newUserRecord.userAuthority.toString());
153
+ } else if (record.eventType === 'LPRecord') {
154
+ if (!userMap) {
155
+ return;
156
+ }
157
+ const lpRecord = record as LPRecord;
158
+ const user = await userMap.mustGet(lpRecord.user.toString());
159
+ await this.mustGet(user.getUserAccount().authority.toString());
160
+ } else if (record.eventType === 'InsuranceFundStakeRecord') {
161
+ const ifStakeRecord = record as InsuranceFundStakeRecord;
162
+ await this.mustGet(ifStakeRecord.userAuthority.toString());
163
+ }
164
+ }
165
+
90
166
  public has(authorityPublicKey: string): boolean {
91
167
  return this.userStatsMap.has(authorityPublicKey);
92
168
  }
@@ -105,4 +181,8 @@ export class UserStatsMap {
105
181
  public values(): IterableIterator<UserStats> {
106
182
  return this.userStatsMap.values();
107
183
  }
184
+
185
+ public size(): number {
186
+ return this.userStatsMap.size;
187
+ }
108
188
  }