@drift-labs/vaults-sdk 0.1.108 → 0.1.109

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.
@@ -37,10 +37,15 @@ export const vaultInvariantChecks = async (program: Command, cmdOpts: OptionValu
37
37
  const spotMarket = driftVault.driftClient.getSpotMarketAccount(vault.spotMarketIndex);
38
38
  const spotPrecision = new BN(10).pow(new BN(spotMarket!.decimals));
39
39
 
40
- const allVaultDepositors = await driftVault.getAllVaultDepositors(vaultAddress);
40
+ let allVaultDepositors = await driftVault.getAllVaultDepositors(vaultAddress);
41
+
42
+ // Sort allVaultDepositors by vaultShares in descending order
43
+ allVaultDepositors = allVaultDepositors.sort((a, b) => b.account.vaultShares.cmp(a.account.vaultShares));
41
44
 
42
45
  let totalUserShares = new BN(0);
43
46
  let totalUserProfitSharePaid = new BN(0);
47
+ let totalUserProfitShareSharesPaid = new BN(0);
48
+ let totalPendingProfitShareAmount = new BN(0);
44
49
 
45
50
  for (const vd of allVaultDepositors) {
46
51
  totalUserShares = totalUserShares.add(vd.account.vaultShares);
@@ -54,10 +59,12 @@ export const vaultInvariantChecks = async (program: Command, cmdOpts: OptionValu
54
59
  // console.log(`Profit share paid: ${vd.publicKey.toBase58()} (auth: ${vd.account.authority.toBase58()}): ${Math.ceil(profitSharePaid * 100.0)}%`);
55
60
  }
56
61
  totalUserProfitSharePaid = totalUserProfitSharePaid.add(vd.account.profitShareFeePaid);
62
+ totalUserProfitShareSharesPaid = totalUserProfitShareSharesPaid.add(vd.account.cumulativeProfitShareAmount);
57
63
 
58
64
  const pendingProfitShares = calculateApplyProfitShare(vd.account, vaultEquity, vault);
65
+ totalPendingProfitShareAmount = totalPendingProfitShareAmount.add(pendingProfitShares.profitShareAmount);
66
+
59
67
  console.log(`Pending profit shares: ${vd.publicKey.toBase58()} (auth: ${vd.account.authority.toBase58()}): $${convertToNumber(pendingProfitShares.profitShareAmount, spotPrecision)}`);
60
- console.log(` . ${pendingProfitShares.profitShareShares}, ${pendingProfitShares.profitShareAmount}`);
61
68
  }
62
69
  console.log(`==== Vault Depositor Shares == vault.user_shares ====`);
63
70
  console.log(`total vd shares: ${totalUserShares.toString()}`);
@@ -69,4 +76,8 @@ export const vaultInvariantChecks = async (program: Command, cmdOpts: OptionValu
69
76
  console.log(`total vault d profitshares: ${totalUserProfitSharePaid.toString()}`);
70
77
  console.log(`vault total profit shares: ${vault.managerTotalProfitShare.toString()}`);
71
78
  console.log(`diff: ${vault.managerTotalProfitShare.sub(totalUserProfitSharePaid)}`);
79
+
80
+ console.log(``);
81
+ console.log(`==== Pending profit shares to realize ====`);
82
+ console.log(`${convertToNumber(totalPendingProfitShareAmount, spotPrecision)}`);
72
83
  };
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  /// <reference types="@coral-xyz/anchor/node_modules/@solana/web3.js" />
3
3
  /// <reference types="@pythnetwork/client/node_modules/@solana/web3.js" />
4
- import { BN, DriftClient } from '@drift-labs/sdk';
4
+ import { BN, DriftClient, User } from '@drift-labs/sdk';
5
5
  import { Program, ProgramAccount } from '@coral-xyz/anchor';
6
6
  import { DriftVaults } from './types/drift_vaults';
7
7
  import { CompetitionsClient } from '@drift-labs/competitions-sdk';
@@ -11,6 +11,10 @@ export declare class VaultClient {
11
11
  driftClient: DriftClient;
12
12
  program: Program<DriftVaults>;
13
13
  cliMode: boolean;
14
+ /**
15
+ * Cache map of drift user accounts of vaults.
16
+ */
17
+ readonly vaultUsers: Map<string, User>;
14
18
  constructor({ driftClient, program, cliMode, }: {
15
19
  driftClient: DriftClient;
16
20
  program: Program<DriftVaults>;
@@ -20,6 +24,7 @@ export declare class VaultClient {
20
24
  getVaultDepositor(vaultDepositor: PublicKey): Promise<any>;
21
25
  getAllVaultDepositorsWithNoWithdrawRequest(vault: PublicKey): Promise<ProgramAccount<VaultDepositor>[]>;
22
26
  getAllVaultDepositors(vault: PublicKey): Promise<ProgramAccount<VaultDepositor>[]>;
27
+ getSubscribedVaultUser(vaultDriftUserAccountPubKey: PublicKey): Promise<User>;
23
28
  /**
24
29
  *
25
30
  * @param vault pubkey
@@ -10,6 +10,10 @@ const spl_token_1 = require("@solana/spl-token");
10
10
  const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
11
11
  class VaultClient {
12
12
  constructor({ driftClient, program, cliMode, }) {
13
+ /**
14
+ * Cache map of drift user accounts of vaults.
15
+ */
16
+ this.vaultUsers = new Map();
13
17
  this.driftClient = driftClient;
14
18
  this.program = program;
15
19
  this.cliMode = !!cliMode;
@@ -68,6 +72,21 @@ class VaultClient {
68
72
  // @ts-ignore
69
73
  return (await this.program.account.vaultDepositor.all(filters));
70
74
  }
75
+ async getSubscribedVaultUser(vaultDriftUserAccountPubKey) {
76
+ let vaultUser = this.vaultUsers.get(vaultDriftUserAccountPubKey.toBase58());
77
+ if (!vaultUser) {
78
+ vaultUser = new sdk_1.User({
79
+ driftClient: this.driftClient,
80
+ userAccountPublicKey: vaultDriftUserAccountPubKey,
81
+ });
82
+ await vaultUser.subscribe();
83
+ this.vaultUsers.set(vaultDriftUserAccountPubKey.toBase58(), vaultUser);
84
+ }
85
+ if (!(vaultUser === null || vaultUser === void 0 ? void 0 : vaultUser.isSubscribed)) {
86
+ await vaultUser.subscribe();
87
+ }
88
+ return vaultUser;
89
+ }
71
90
  /**
72
91
  *
73
92
  * @param vault pubkey
@@ -85,11 +104,7 @@ class VaultClient {
85
104
  else {
86
105
  throw new Error('Must supply address or vault');
87
106
  }
88
- const user = new sdk_1.User({
89
- driftClient: this.driftClient,
90
- userAccountPublicKey: vaultAccount.user,
91
- });
92
- await user.subscribe();
107
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
93
108
  const netSpotValue = user.getNetSpotMarketValue();
94
109
  const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
95
110
  return netSpotValue.add(unrealizedPnl);
@@ -166,11 +181,7 @@ class VaultClient {
166
181
  if (!driftSpotMarket) {
167
182
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
168
183
  }
169
- const user = new sdk_1.User({
170
- driftClient: this.driftClient,
171
- userAccountPublicKey: vaultAccount.user,
172
- });
173
- await user.subscribe();
184
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
174
185
  const remainingAccounts = this.driftClient.getRemainingAccounts({
175
186
  userAccounts: [user.getUserAccount()],
176
187
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -197,11 +208,7 @@ class VaultClient {
197
208
  if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
198
209
  throw new Error(`Only the manager of the vault can request a withdraw.`);
199
210
  }
200
- const user = new sdk_1.User({
201
- driftClient: this.driftClient,
202
- userAccountPublicKey: vaultAccount.user,
203
- });
204
- await user.subscribe();
211
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
205
212
  const remainingAccounts = this.driftClient.getRemainingAccounts({
206
213
  userAccounts: [user.getUserAccount()],
207
214
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -243,11 +250,7 @@ class VaultClient {
243
250
  driftUser: vaultAccount.user,
244
251
  driftState: driftStateKey,
245
252
  };
246
- const user = new sdk_1.User({
247
- driftClient: this.driftClient,
248
- userAccountPublicKey: vaultAccount.user,
249
- });
250
- await user.subscribe();
253
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
251
254
  const remainingAccounts = this.driftClient.getRemainingAccounts({
252
255
  userAccounts: [user.getUserAccount()],
253
256
  });
@@ -274,11 +277,7 @@ class VaultClient {
274
277
  if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
275
278
  throw new Error(`Only the manager of the vault can request a withdraw.`);
276
279
  }
277
- const user = new sdk_1.User({
278
- driftClient: this.driftClient,
279
- userAccountPublicKey: vaultAccount.user,
280
- });
281
- await user.subscribe();
280
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
282
281
  const remainingAccounts = this.driftClient.getRemainingAccounts({
283
282
  userAccounts: [user.getUserAccount()],
284
283
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -318,11 +317,7 @@ class VaultClient {
318
317
  }
319
318
  async getApplyProfitShareIx(vault, vaultDepositor) {
320
319
  const vaultAccount = await this.program.account.vault.fetch(vault);
321
- const user = new sdk_1.User({
322
- driftClient: this.driftClient,
323
- userAccountPublicKey: vaultAccount.user,
324
- });
325
- await user.subscribe();
320
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
326
321
  const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
327
322
  if (!spotMarket) {
328
323
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
@@ -406,11 +401,7 @@ class VaultClient {
406
401
  vaultPubKey = vaultDepositorAccount.vault;
407
402
  }
408
403
  const vaultAccount = await this.program.account.vault.fetch(vaultPubKey);
409
- const user = new sdk_1.User({
410
- driftClient: this.driftClient,
411
- userAccountPublicKey: vaultAccount.user,
412
- });
413
- await user.subscribe();
404
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
414
405
  const remainingAccounts = this.driftClient.getRemainingAccounts({
415
406
  userAccounts: [user.getUserAccount()],
416
407
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -463,11 +454,7 @@ class VaultClient {
463
454
  async requestWithdraw(vaultDepositor, amount, withdrawUnit) {
464
455
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
465
456
  const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
466
- const user = new sdk_1.User({
467
- driftClient: this.driftClient,
468
- userAccountPublicKey: vaultAccount.user,
469
- });
470
- await user.subscribe();
457
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
471
458
  const remainingAccounts = this.driftClient.getRemainingAccounts({
472
459
  userAccounts: [user.getUserAccount()],
473
460
  });
@@ -501,11 +488,7 @@ class VaultClient {
501
488
  async withdraw(vaultDepositor) {
502
489
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
503
490
  const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
504
- const user = new sdk_1.User({
505
- driftClient: this.driftClient,
506
- userAccountPublicKey: vaultAccount.user,
507
- });
508
- await user.subscribe();
491
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
509
492
  const remainingAccounts = this.driftClient.getRemainingAccounts({
510
493
  userAccounts: [user.getUserAccount()],
511
494
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -550,11 +533,7 @@ class VaultClient {
550
533
  async forceWithdraw(vaultDepositor) {
551
534
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
552
535
  const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
553
- const user = new sdk_1.User({
554
- driftClient: this.driftClient,
555
- userAccountPublicKey: vaultAccount.user,
556
- });
557
- await user.subscribe();
536
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
558
537
  const remainingAccounts = this.driftClient.getRemainingAccounts({
559
538
  userAccounts: [user.getUserAccount()],
560
539
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -613,11 +592,7 @@ class VaultClient {
613
592
  driftUser: vaultAccount.user,
614
593
  driftState: driftStateKey,
615
594
  };
616
- const user = new sdk_1.User({
617
- driftClient: this.driftClient,
618
- userAccountPublicKey: vaultAccount.user,
619
- });
620
- await user.subscribe();
595
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
621
596
  const remainingAccounts = this.driftClient.getRemainingAccounts({
622
597
  userAccounts: [user.getUserAccount()],
623
598
  });
@@ -649,11 +624,7 @@ class VaultClient {
649
624
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
650
625
  const vaultPubKey = vaultDepositorAccount.vault;
651
626
  const vaultAccount = await this.program.account.vault.fetch(vaultPubKey);
652
- const user = new sdk_1.User({
653
- driftClient: this.driftClient,
654
- userAccountPublicKey: vaultAccount.user,
655
- });
656
- await user.subscribe();
627
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
657
628
  const remainingAccounts = this.driftClient.getRemainingAccounts({
658
629
  userAccounts: [user.getUserAccount()],
659
630
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/vaults-sdk",
3
- "version": "0.1.108",
3
+ "version": "0.1.109",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "directories": {
@@ -8,8 +8,8 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@coral-xyz/anchor": "^0.26.0",
11
- "@drift-labs/competitions-sdk": "0.2.73",
12
- "@drift-labs/sdk": "2.54.0-beta.3",
11
+ "@drift-labs/competitions-sdk": "0.2.131",
12
+ "@drift-labs/sdk": "2.54.0-beta.9",
13
13
  "@solana/web3.js": "1.73.2",
14
14
  "commander": "^11.0.0",
15
15
  "dotenv": "^16.3.1",
@@ -42,6 +42,11 @@ export class VaultClient {
42
42
  program: Program<DriftVaults>;
43
43
  cliMode: boolean;
44
44
 
45
+ /**
46
+ * Cache map of drift user accounts of vaults.
47
+ */
48
+ readonly vaultUsers: Map<string, User> = new Map<string, User>();
49
+
45
50
  constructor({
46
51
  driftClient,
47
52
  program,
@@ -126,6 +131,25 @@ export class VaultClient {
126
131
  )) as ProgramAccount<VaultDepositor>[];
127
132
  }
128
133
 
134
+ public async getSubscribedVaultUser(vaultDriftUserAccountPubKey: PublicKey) {
135
+ let vaultUser = this.vaultUsers.get(vaultDriftUserAccountPubKey.toBase58());
136
+
137
+ if (!vaultUser) {
138
+ vaultUser = new User({
139
+ driftClient: this.driftClient,
140
+ userAccountPublicKey: vaultDriftUserAccountPubKey,
141
+ });
142
+ await vaultUser.subscribe();
143
+ this.vaultUsers.set(vaultDriftUserAccountPubKey.toBase58(), vaultUser);
144
+ }
145
+
146
+ if (!vaultUser?.isSubscribed) {
147
+ await vaultUser.subscribe();
148
+ }
149
+
150
+ return vaultUser;
151
+ }
152
+
129
153
  /**
130
154
  *
131
155
  * @param vault pubkey
@@ -145,11 +169,7 @@ export class VaultClient {
145
169
  throw new Error('Must supply address or vault');
146
170
  }
147
171
 
148
- const user = new User({
149
- driftClient: this.driftClient,
150
- userAccountPublicKey: vaultAccount.user,
151
- });
152
- await user.subscribe();
172
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
153
173
 
154
174
  const netSpotValue = user.getNetSpotMarketValue();
155
175
  const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
@@ -273,11 +293,7 @@ export class VaultClient {
273
293
  );
274
294
  }
275
295
 
276
- const user = new User({
277
- driftClient: this.driftClient,
278
- userAccountPublicKey: vaultAccount.user,
279
- });
280
- await user.subscribe();
296
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
281
297
 
282
298
  const remainingAccounts = this.driftClient.getRemainingAccounts({
283
299
  userAccounts: [user.getUserAccount()],
@@ -324,11 +340,7 @@ export class VaultClient {
324
340
  throw new Error(`Only the manager of the vault can request a withdraw.`);
325
341
  }
326
342
 
327
- const user = new User({
328
- driftClient: this.driftClient,
329
- userAccountPublicKey: vaultAccount.user,
330
- });
331
- await user.subscribe();
343
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
332
344
  const remainingAccounts = this.driftClient.getRemainingAccounts({
333
345
  userAccounts: [user.getUserAccount()],
334
346
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -391,11 +403,7 @@ export class VaultClient {
391
403
  driftState: driftStateKey,
392
404
  };
393
405
 
394
- const user = new User({
395
- driftClient: this.driftClient,
396
- userAccountPublicKey: vaultAccount.user,
397
- });
398
- await user.subscribe();
406
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
399
407
  const remainingAccounts = this.driftClient.getRemainingAccounts({
400
408
  userAccounts: [user.getUserAccount()],
401
409
  });
@@ -429,11 +437,7 @@ export class VaultClient {
429
437
  throw new Error(`Only the manager of the vault can request a withdraw.`);
430
438
  }
431
439
 
432
- const user = new User({
433
- driftClient: this.driftClient,
434
- userAccountPublicKey: vaultAccount.user,
435
- });
436
- await user.subscribe();
440
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
437
441
 
438
442
  const remainingAccounts = this.driftClient.getRemainingAccounts({
439
443
  userAccounts: [user.getUserAccount()],
@@ -506,11 +510,7 @@ export class VaultClient {
506
510
  ): Promise<TransactionInstruction> {
507
511
  const vaultAccount = await this.program.account.vault.fetch(vault);
508
512
 
509
- const user = new User({
510
- driftClient: this.driftClient,
511
- userAccountPublicKey: vaultAccount.user,
512
- });
513
- await user.subscribe();
513
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
514
514
 
515
515
  const spotMarket = this.driftClient.getSpotMarketAccount(
516
516
  vaultAccount.spotMarketIndex
@@ -635,11 +635,7 @@ export class VaultClient {
635
635
 
636
636
  const vaultAccount = await this.program.account.vault.fetch(vaultPubKey);
637
637
 
638
- const user = new User({
639
- driftClient: this.driftClient,
640
- userAccountPublicKey: vaultAccount.user,
641
- });
642
- await user.subscribe();
638
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
643
639
  const remainingAccounts = this.driftClient.getRemainingAccounts({
644
640
  userAccounts: [user.getUserAccount()],
645
641
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -722,11 +718,7 @@ export class VaultClient {
722
718
  vaultDepositorAccount.vault
723
719
  );
724
720
 
725
- const user = new User({
726
- driftClient: this.driftClient,
727
- userAccountPublicKey: vaultAccount.user,
728
- });
729
- await user.subscribe();
721
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
730
722
  const remainingAccounts = this.driftClient.getRemainingAccounts({
731
723
  userAccounts: [user.getUserAccount()],
732
724
  });
@@ -778,11 +770,7 @@ export class VaultClient {
778
770
  vaultDepositorAccount.vault
779
771
  );
780
772
 
781
- const user = new User({
782
- driftClient: this.driftClient,
783
- userAccountPublicKey: vaultAccount.user,
784
- });
785
- await user.subscribe();
773
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
786
774
  const remainingAccounts = this.driftClient.getRemainingAccounts({
787
775
  userAccounts: [user.getUserAccount()],
788
776
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -850,11 +838,7 @@ export class VaultClient {
850
838
  vaultDepositorAccount.vault
851
839
  );
852
840
 
853
- const user = new User({
854
- driftClient: this.driftClient,
855
- userAccountPublicKey: vaultAccount.user,
856
- });
857
- await user.subscribe();
841
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
858
842
  const remainingAccounts = this.driftClient.getRemainingAccounts({
859
843
  userAccounts: [user.getUserAccount()],
860
844
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
@@ -941,11 +925,7 @@ export class VaultClient {
941
925
  driftState: driftStateKey,
942
926
  };
943
927
 
944
- const user = new User({
945
- driftClient: this.driftClient,
946
- userAccountPublicKey: vaultAccount.user,
947
- });
948
- await user.subscribe();
928
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
949
929
  const remainingAccounts = this.driftClient.getRemainingAccounts({
950
930
  userAccounts: [user.getUserAccount()],
951
931
  });
@@ -985,11 +965,7 @@ export class VaultClient {
985
965
 
986
966
  const vaultAccount = await this.program.account.vault.fetch(vaultPubKey);
987
967
 
988
- const user = new User({
989
- driftClient: this.driftClient,
990
- userAccountPublicKey: vaultAccount.user,
991
- });
992
- await user.subscribe();
968
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
993
969
  const remainingAccounts = this.driftClient.getRemainingAccounts({
994
970
  userAccounts: [user.getUserAccount()],
995
971
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],