@drift-labs/vaults-sdk 0.1.462 → 0.1.464

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.
@@ -8,6 +8,7 @@ const addresses_1 = require("./addresses");
8
8
  const web3_js_1 = require("@solana/web3.js");
9
9
  const spl_token_1 = require("@solana/spl-token");
10
10
  const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
11
+ const math_1 = require("./math");
11
12
  class VaultClient {
12
13
  constructor({ driftClient, program, cliMode, userMapConfig, }) {
13
14
  this.driftClient = driftClient;
@@ -27,8 +28,13 @@ class VaultClient {
27
28
  this.vaultUsers = new sdk_1.UserMap(userMapConfig);
28
29
  }
29
30
  }
31
+ /**
32
+ * Unsubscribes from the vault users map. Call this to clean up any dangling promises.
33
+ */
34
+ async unsubscribe() {
35
+ await this.vaultUsers.unsubscribe();
36
+ }
30
37
  async getVault(vault) {
31
- // @ts-ignore
32
38
  return await this.program.account.vault.fetch(vault);
33
39
  }
34
40
  async getVaultAndSlot(vault) {
@@ -48,6 +54,19 @@ class VaultClient {
48
54
  slot: vaultDepositorAndSlot.context.slot,
49
55
  };
50
56
  }
57
+ getVaultProtocolAddress(vault) {
58
+ return (0, addresses_1.getVaultProtocolAddressSync)(this.program.programId, vault);
59
+ }
60
+ async getVaultProtocol(vaultProtocol) {
61
+ return await this.program.account.vaultProtocol.fetch(vaultProtocol);
62
+ }
63
+ async getVaultProtocolAndSlot(vaultProtocol) {
64
+ const vaultProtocolAndSlot = await this.program.account.vaultProtocol.fetchAndContext(vaultProtocol);
65
+ return {
66
+ vaultProtocol: vaultProtocolAndSlot.data,
67
+ slot: vaultProtocolAndSlot.context.slot,
68
+ };
69
+ }
51
70
  async getAllVaultDepositorsWithNoWithdrawRequest(vault) {
52
71
  const filters = [
53
72
  {
@@ -103,10 +122,16 @@ class VaultClient {
103
122
  /**
104
123
  *
105
124
  * @param vault pubkey
125
+ * @param factorUnrealizedPNL add unrealized pnl to net balance
106
126
  * @returns vault equity, in USDC
107
127
  */
108
128
  async calculateVaultEquity(params) {
109
129
  try {
130
+ // defaults to true if undefined
131
+ let factorUnrealizedPNL = true;
132
+ if (params.factorUnrealizedPNL !== undefined) {
133
+ factorUnrealizedPNL = params.factorUnrealizedPNL;
134
+ }
110
135
  let vaultAccount;
111
136
  if (params.address !== undefined) {
112
137
  // @ts-ignore
@@ -120,8 +145,13 @@ class VaultClient {
120
145
  }
121
146
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
122
147
  const netSpotValue = user.getNetSpotMarketValue();
123
- const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
124
- return netSpotValue.add(unrealizedPnl);
148
+ if (factorUnrealizedPNL) {
149
+ const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
150
+ return netSpotValue.add(unrealizedPnl);
151
+ }
152
+ else {
153
+ return netSpotValue;
154
+ }
125
155
  }
126
156
  catch (err) {
127
157
  console.error('VaultClient ~ err:', err);
@@ -131,12 +161,12 @@ class VaultClient {
131
161
  /**
132
162
  *
133
163
  * @param vault pubkey
134
- * @returns vault equity, in spot deposit asset
164
+ * @param factorUnrealizedPNL add unrealized pnl to existing equity
165
+ * @returns total vault equity, in spot deposit asset
135
166
  */
136
167
  async calculateVaultEquityInDepositAsset(params) {
137
168
  let vaultAccount;
138
169
  if (params.address !== undefined) {
139
- // @ts-ignore
140
170
  vaultAccount = await this.program.account.vault.fetch(params.address);
141
171
  }
142
172
  else if (params.vault !== undefined) {
@@ -147,13 +177,90 @@ class VaultClient {
147
177
  }
148
178
  const vaultEquity = await this.calculateVaultEquity({
149
179
  vault: vaultAccount,
180
+ factorUnrealizedPNL: params.factorUnrealizedPNL,
150
181
  });
151
182
  const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
152
183
  const spotOracle = this.driftClient.getOracleDataForSpotMarket(vaultAccount.spotMarketIndex);
153
184
  const spotPrecision = sdk_1.TEN.pow(new sdk_1.BN(spotMarket.decimals));
154
185
  return vaultEquity.mul(spotPrecision).div(spotOracle.price);
155
186
  }
187
+ /**
188
+ * @param params
189
+ * @returns vault depositor equity, in spot market value (which is usually USDC)
190
+ */
191
+ async calculateWithdrawableVaultDepositorEquity(params) {
192
+ let vaultAccount;
193
+ if (params.vaultAddress !== undefined) {
194
+ vaultAccount = await this.program.account.vault.fetch(params.vaultAddress);
195
+ }
196
+ else if (params.vault !== undefined) {
197
+ vaultAccount = params.vault;
198
+ }
199
+ else {
200
+ throw new Error('Must supply vaultAddress or vault');
201
+ }
202
+ let vaultDepositorAccount;
203
+ if (params.vaultDepositorAddress !== undefined) {
204
+ vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(params.vaultDepositorAddress);
205
+ }
206
+ else if (params.vaultDepositor !== undefined) {
207
+ vaultDepositorAccount = params.vaultDepositor;
208
+ }
209
+ else {
210
+ throw new Error('Must supply vaultDepositorAddress or vaultDepositor');
211
+ }
212
+ const vaultEquity = await this.calculateVaultEquity({
213
+ vault: vaultAccount,
214
+ factorUnrealizedPNL: false,
215
+ });
216
+ return (0, math_1.calculateRealizedVaultDepositorEquity)(vaultDepositorAccount, vaultEquity, vaultAccount);
217
+ }
218
+ async calculateWithdrawableVaultDepositorEquityInDepositAsset(params) {
219
+ let vaultAccount;
220
+ if (params.vaultAddress !== undefined) {
221
+ vaultAccount = await this.program.account.vault.fetch(params.vaultAddress);
222
+ }
223
+ else if (params.vault !== undefined) {
224
+ vaultAccount = params.vault;
225
+ }
226
+ else {
227
+ throw new Error('Must supply vaultAddress or vault');
228
+ }
229
+ let vaultDepositorAccount;
230
+ if (params.vaultDepositorAddress !== undefined) {
231
+ vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(params.vaultDepositorAddress);
232
+ }
233
+ else if (params.vaultDepositor !== undefined) {
234
+ vaultDepositorAccount = params.vaultDepositor;
235
+ }
236
+ else {
237
+ throw new Error('Must supply vaultDepositorAddress or vaultDepositor');
238
+ }
239
+ let vaultProtocol = undefined;
240
+ if (vaultAccount.vaultProtocol) {
241
+ vaultProtocol = await this.program.account.vaultProtocol.fetch(this.getVaultProtocolAddress(vaultAccount.pubkey));
242
+ }
243
+ const vaultEquity = await this.calculateVaultEquity({
244
+ vault: vaultAccount,
245
+ factorUnrealizedPNL: false,
246
+ });
247
+ const vdEquity = (0, math_1.calculateRealizedVaultDepositorEquity)(vaultDepositorAccount, vaultEquity, vaultAccount, vaultProtocol);
248
+ const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
249
+ const spotOracle = this.driftClient.getOracleDataForSpotMarket(vaultAccount.spotMarketIndex);
250
+ const spotPrecision = sdk_1.TEN.pow(new sdk_1.BN(spotMarket.decimals));
251
+ return vdEquity.mul(spotPrecision).div(spotOracle.price);
252
+ }
253
+ async calculateVaultProtocolEquity(params) {
254
+ const vaultAccount = await this.program.account.vault.fetch(params.vault);
255
+ const vaultTotalEquity = await this.calculateVaultEquity({
256
+ vault: vaultAccount,
257
+ });
258
+ const vaultProtocol = this.getVaultProtocolAddress(params.vault);
259
+ const vpAccount = await this.program.account.vaultProtocol.fetch(vaultProtocol);
260
+ return (0, sdk_1.unstakeSharesToAmount)(vpAccount.protocolProfitAndFeeShares, vaultAccount.totalShares, vaultTotalEquity);
261
+ }
156
262
  async initializeVault(params) {
263
+ const { vaultProtocol: vaultProtocolParams, ...vaultParams } = params;
157
264
  const vault = (0, addresses_1.getVaultAddressSync)(this.program.programId, params.name);
158
265
  const tokenAccount = (0, addresses_1.getTokenVaultAddressSync)(this.program.programId, vault);
159
266
  const driftState = await this.driftClient.getStatePublicKey();
@@ -173,18 +280,43 @@ class VaultClient {
173
280
  tokenAccount,
174
281
  driftProgram: this.driftClient.program.programId,
175
282
  };
176
- return await this.program.methods
177
- .initializeVault(params)
178
- .preInstructions([
179
- web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
180
- units: 400000,
181
- }),
182
- web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
183
- microLamports: 300000,
184
- }),
185
- ])
186
- .accounts(accounts)
187
- .rpc();
283
+ if (vaultProtocolParams) {
284
+ const vaultProtocol = this.getVaultProtocolAddress((0, addresses_1.getVaultAddressSync)(this.program.programId, params.name));
285
+ const _params = {
286
+ ...vaultParams,
287
+ vaultProtocol: vaultProtocolParams,
288
+ };
289
+ return await this.program.methods
290
+ .initializeVaultWithProtocol(_params)
291
+ .preInstructions([
292
+ web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
293
+ units: 400000,
294
+ }),
295
+ web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
296
+ microLamports: 300000,
297
+ }),
298
+ ])
299
+ .accounts({
300
+ ...accounts,
301
+ vaultProtocol,
302
+ })
303
+ .rpc();
304
+ }
305
+ else {
306
+ const _params = vaultParams;
307
+ return await this.program.methods
308
+ .initializeVault(_params)
309
+ .preInstructions([
310
+ web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
311
+ units: 400000,
312
+ }),
313
+ web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
314
+ microLamports: 300000,
315
+ }),
316
+ ])
317
+ .accounts(accounts)
318
+ .rpc();
319
+ }
188
320
  }
189
321
  /**
190
322
  * Updates the delegate address for a vault. The delegate address will be allowed to trade
@@ -215,7 +347,7 @@ class VaultClient {
215
347
  /**
216
348
  * Updates the vault margin trading status.
217
349
  * @param vault vault address to update
218
- * @param enabeld whether to enable margin trading
350
+ * @param enabled whether to enable margin trading
219
351
  * @returns
220
352
  */
221
353
  async updateMarginTradingEnabled(vault, enabled) {
@@ -246,6 +378,14 @@ class VaultClient {
246
378
  userAccounts: [user.getUserAccount()],
247
379
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
248
380
  });
381
+ if (vaultAccount.vaultProtocol) {
382
+ const vaultProtocol = this.getVaultProtocolAddress(vault);
383
+ remainingAccounts.push({
384
+ pubkey: vaultProtocol,
385
+ isSigner: false,
386
+ isWritable: true,
387
+ });
388
+ }
249
389
  return await this.program.methods
250
390
  .managerDeposit(amount)
251
391
  .accounts({
@@ -274,6 +414,14 @@ class VaultClient {
274
414
  userAccounts: [user.getUserAccount()],
275
415
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
276
416
  });
417
+ if (vaultAccount.vaultProtocol) {
418
+ const vaultProtocol = this.getVaultProtocolAddress(vault);
419
+ remainingAccounts.push({
420
+ pubkey: vaultProtocol,
421
+ isSigner: false,
422
+ isWritable: true,
423
+ });
424
+ }
277
425
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
278
426
  const driftStateKey = await this.driftClient.getStatePublicKey();
279
427
  const accounts = {
@@ -318,6 +466,14 @@ class VaultClient {
318
466
  const remainingAccounts = this.driftClient.getRemainingAccounts({
319
467
  userAccounts: [user.getUserAccount()],
320
468
  });
469
+ if (vaultAccount.vaultProtocol) {
470
+ const vaultProtocol = this.getVaultProtocolAddress(vault);
471
+ remainingAccounts.push({
472
+ pubkey: vaultProtocol,
473
+ isSigner: false,
474
+ isWritable: true,
475
+ });
476
+ }
321
477
  if (this.cliMode) {
322
478
  return await this.program.methods
323
479
  .mangerCancelWithdrawRequest()
@@ -346,6 +502,14 @@ class VaultClient {
346
502
  userAccounts: [user.getUserAccount()],
347
503
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
348
504
  });
505
+ if (vaultAccount.vaultProtocol) {
506
+ const vaultProtocol = this.getVaultProtocolAddress(vault);
507
+ remainingAccounts.push({
508
+ pubkey: vaultProtocol,
509
+ isSigner: false,
510
+ isWritable: true,
511
+ });
512
+ }
349
513
  const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
350
514
  if (!spotMarket) {
351
515
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
@@ -466,6 +630,14 @@ class VaultClient {
466
630
  userAccounts: [user.getUserAccount()],
467
631
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
468
632
  });
633
+ if (vaultAccount.vaultProtocol) {
634
+ const vaultProtocol = this.getVaultProtocolAddress(vaultPubKey);
635
+ remainingAccounts.push({
636
+ pubkey: vaultProtocol,
637
+ isSigner: false,
638
+ isWritable: true,
639
+ });
640
+ }
469
641
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultPubKey);
470
642
  const driftStateKey = await this.driftClient.getStatePublicKey();
471
643
  const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
@@ -490,28 +662,38 @@ class VaultClient {
490
662
  remainingAccounts,
491
663
  };
492
664
  }
665
+ /**
666
+ * Creates a transaction to deposit funds into the specified vault.
667
+ * Uses the associated token account of the vault depositor authority and spot market mint,
668
+ * and assumes it exists before calling this function.
669
+ * @param vaultDepositor
670
+ * @param amount
671
+ * @param initVaultDepositor If true, will initialize the vault depositor account
672
+ * @returns transaction
673
+ */
493
674
  async createDepositTx(vaultDepositor, amount, initVaultDepositor, txParams) {
494
675
  const { vaultAccount, accounts, remainingAccounts } = await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
495
- const depositIx = this.program.instruction.deposit(amount, {
496
- accounts: {
497
- authority: this.driftClient.wallet.publicKey,
498
- ...accounts,
499
- },
500
- remainingAccounts,
501
- });
676
+ const ixs = [];
502
677
  if (initVaultDepositor) {
503
- const initIx = this.createInitVaultDepositorIx(vaultAccount.pubkey, initVaultDepositor.authority);
504
- return await this.createTxn([initIx, depositIx], txParams);
505
- }
506
- else {
507
- return await this.createTxn([depositIx], txParams);
678
+ ixs.push(this.createInitVaultDepositorIx(vaultAccount.pubkey, initVaultDepositor.authority));
508
679
  }
680
+ const depositIx = await this.program.methods
681
+ .deposit(amount)
682
+ .accounts({
683
+ authority: this.driftClient.wallet.publicKey,
684
+ ...accounts,
685
+ })
686
+ .remainingAccounts(remainingAccounts)
687
+ .instruction();
688
+ ixs.push(depositIx);
689
+ return await this.createTxn(ixs, txParams);
509
690
  }
510
691
  /**
511
692
  * Depositor funds into the specified vault.
512
693
  * @param vaultDepositor
513
694
  * @param amount
514
695
  * @param initVaultDepositor If true, will initialize the vault depositor account
696
+ * @param txParams
515
697
  * @returns
516
698
  */
517
699
  async deposit(vaultDepositor, amount, initVaultDepositor, txParams) {
@@ -538,6 +720,14 @@ class VaultClient {
538
720
  const remainingAccounts = this.driftClient.getRemainingAccounts({
539
721
  userAccounts: [user.getUserAccount()],
540
722
  });
723
+ if (vaultAccount.vaultProtocol) {
724
+ const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
725
+ remainingAccounts.push({
726
+ pubkey: vaultProtocol,
727
+ isSigner: false,
728
+ isWritable: true,
729
+ });
730
+ }
541
731
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
542
732
  const driftStateKey = await this.driftClient.getStatePublicKey();
543
733
  const accounts = {
@@ -577,6 +767,14 @@ class VaultClient {
577
767
  userAccounts: [user.getUserAccount()],
578
768
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
579
769
  });
770
+ const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
771
+ if (!vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
772
+ remainingAccounts.push({
773
+ pubkey: vaultProtocol,
774
+ isSigner: false,
775
+ isWritable: true,
776
+ });
777
+ }
580
778
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
581
779
  const driftStateKey = await this.driftClient.getStatePublicKey();
582
780
  const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
@@ -603,21 +801,32 @@ class VaultClient {
603
801
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
604
802
  };
605
803
  if (this.cliMode) {
606
- return await this.program.methods
607
- .withdraw()
608
- .accounts(accounts)
609
- .remainingAccounts(remainingAccounts)
610
- .rpc();
804
+ if (createAtaIx) {
805
+ return await this.program.methods
806
+ .withdraw()
807
+ .accounts(accounts)
808
+ .remainingAccounts(remainingAccounts)
809
+ .preInstructions([createAtaIx])
810
+ .rpc();
811
+ }
812
+ else {
813
+ return await this.program.methods
814
+ .withdraw()
815
+ .accounts(accounts)
816
+ .remainingAccounts(remainingAccounts)
817
+ .rpc();
818
+ }
611
819
  }
612
820
  else {
613
821
  const ixs = [
614
- this.program.instruction.withdraw({
615
- accounts: {
616
- authority: this.driftClient.wallet.publicKey,
617
- ...accounts,
618
- },
619
- remainingAccounts,
620
- }),
822
+ await this.program.methods
823
+ .withdraw()
824
+ .accounts({
825
+ authority: this.driftClient.wallet.publicKey,
826
+ ...accounts,
827
+ })
828
+ .remainingAccounts(remainingAccounts)
829
+ .instruction(),
621
830
  ];
622
831
  if (createAtaIx) {
623
832
  ixs.unshift(createAtaIx);
@@ -636,6 +845,14 @@ class VaultClient {
636
845
  userAccounts: [user.getUserAccount()],
637
846
  writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
638
847
  });
848
+ if (vaultAccount.vaultProtocol) {
849
+ const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
850
+ remainingAccounts.push({
851
+ pubkey: vaultProtocol,
852
+ isSigner: false,
853
+ isWritable: true,
854
+ });
855
+ }
639
856
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
640
857
  const driftStateKey = await this.driftClient.getStatePublicKey();
641
858
  const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
@@ -697,6 +914,14 @@ class VaultClient {
697
914
  const remainingAccounts = this.driftClient.getRemainingAccounts({
698
915
  userAccounts: [user.getUserAccount()],
699
916
  });
917
+ if (vaultAccount.vaultProtocol) {
918
+ const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
919
+ remainingAccounts.push({
920
+ pubkey: vaultProtocol,
921
+ isSigner: false,
922
+ isWritable: true,
923
+ });
924
+ }
700
925
  if (this.cliMode) {
701
926
  return await this.program.methods
702
927
  .cancelRequestWithdraw()
@@ -860,5 +1085,138 @@ class VaultClient {
860
1085
  })
861
1086
  .rpc();
862
1087
  }
1088
+ async protocolRequestWithdraw(vault, amount, withdrawUnit) {
1089
+ // @ts-ignore
1090
+ const vaultAccount = (await this.program.account.vault.fetch(vault));
1091
+ const vp = this.getVaultProtocolAddress(vault);
1092
+ const vpAccount = (await this.program.account.vaultProtocol.fetch(vp));
1093
+ if (!this.driftClient.wallet.publicKey.equals(vpAccount.protocol)) {
1094
+ throw new Error(`Only the protocol of the vault can request a withdraw.`);
1095
+ }
1096
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
1097
+ const remainingAccounts = this.driftClient.getRemainingAccounts({
1098
+ userAccounts: [user.getUserAccount()],
1099
+ writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1100
+ });
1101
+ if (vaultAccount.vaultProtocol) {
1102
+ const vaultProtocol = this.getVaultProtocolAddress(vault);
1103
+ remainingAccounts.push({
1104
+ pubkey: vaultProtocol,
1105
+ isSigner: false,
1106
+ isWritable: true,
1107
+ });
1108
+ }
1109
+ const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
1110
+ const driftStateKey = await this.driftClient.getStatePublicKey();
1111
+ const accounts = {
1112
+ vault,
1113
+ driftUserStats: userStatsKey,
1114
+ driftUser: vaultAccount.user,
1115
+ driftState: driftStateKey,
1116
+ };
1117
+ if (this.cliMode) {
1118
+ return await this.program.methods
1119
+ // @ts-ignore, 0.29.0 anchor issues..
1120
+ .managerRequestWithdraw(amount, withdrawUnit)
1121
+ .accounts(accounts)
1122
+ .remainingAccounts(remainingAccounts)
1123
+ .rpc();
1124
+ }
1125
+ else {
1126
+ const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
1127
+ // @ts-ignore
1128
+ amount, withdrawUnit, {
1129
+ accounts: {
1130
+ manager: this.driftClient.wallet.publicKey,
1131
+ ...accounts,
1132
+ },
1133
+ remainingAccounts,
1134
+ });
1135
+ return await this.createAndSendTxn([requestWithdrawIx]);
1136
+ }
1137
+ }
1138
+ async protocolCancelWithdrawRequest(vault) {
1139
+ const vaultAccount = await this.program.account.vault.fetch(vault);
1140
+ const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
1141
+ const driftStateKey = await this.driftClient.getStatePublicKey();
1142
+ const accounts = {
1143
+ manager: this.driftClient.wallet.publicKey,
1144
+ vault,
1145
+ driftUserStats: userStatsKey,
1146
+ driftUser: vaultAccount.user,
1147
+ driftState: driftStateKey,
1148
+ };
1149
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
1150
+ const remainingAccounts = this.driftClient.getRemainingAccounts({
1151
+ userAccounts: [user.getUserAccount()],
1152
+ });
1153
+ if (vaultAccount.vaultProtocol) {
1154
+ const vaultProtocol = this.getVaultProtocolAddress(vault);
1155
+ remainingAccounts.push({
1156
+ pubkey: vaultProtocol,
1157
+ isSigner: false,
1158
+ isWritable: true,
1159
+ });
1160
+ }
1161
+ if (this.cliMode) {
1162
+ return await this.program.methods
1163
+ .mangerCancelWithdrawRequest()
1164
+ .accounts(accounts)
1165
+ .remainingAccounts(remainingAccounts)
1166
+ .rpc();
1167
+ }
1168
+ else {
1169
+ const cancelRequestWithdrawIx = this.program.instruction.mangerCancelWithdrawRequest({
1170
+ accounts: {
1171
+ ...accounts,
1172
+ manager: this.driftClient.wallet.publicKey,
1173
+ },
1174
+ remainingAccounts,
1175
+ });
1176
+ return await this.createAndSendTxn([cancelRequestWithdrawIx]);
1177
+ }
1178
+ }
1179
+ async protocolWithdraw(vault) {
1180
+ const vaultAccount = await this.program.account.vault.fetch(vault);
1181
+ if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
1182
+ throw new Error(`Only the manager of the vault can request a withdraw.`);
1183
+ }
1184
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
1185
+ const remainingAccounts = this.driftClient.getRemainingAccounts({
1186
+ userAccounts: [user.getUserAccount()],
1187
+ writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1188
+ });
1189
+ if (vaultAccount.vaultProtocol) {
1190
+ const vaultProtocol = this.getVaultProtocolAddress(vault);
1191
+ remainingAccounts.push({
1192
+ pubkey: vaultProtocol,
1193
+ isSigner: false,
1194
+ isWritable: true,
1195
+ });
1196
+ }
1197
+ const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
1198
+ if (!spotMarket) {
1199
+ throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
1200
+ }
1201
+ const ix = this.program.instruction.managerWithdraw({
1202
+ accounts: {
1203
+ vault,
1204
+ manager: this.driftClient.wallet.publicKey,
1205
+ vaultTokenAccount: vaultAccount.tokenAccount,
1206
+ driftUser: await (0, sdk_1.getUserAccountPublicKey)(this.driftClient.program.programId, vault),
1207
+ driftProgram: this.driftClient.program.programId,
1208
+ driftUserStats: (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault),
1209
+ driftState: await this.driftClient.getStatePublicKey(),
1210
+ driftSpotMarketVault: spotMarket.vault,
1211
+ userTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey),
1212
+ driftSigner: this.driftClient.getStateAccount().signer,
1213
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1214
+ },
1215
+ remainingAccounts,
1216
+ });
1217
+ return this.createAndSendTxn([ix], {
1218
+ cuLimit: 1000000,
1219
+ });
1220
+ }
863
1221
  }
864
1222
  exports.VaultClient = VaultClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/vaults-sdk",
3
- "version": "0.1.462",
3
+ "version": "0.1.464",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "directories": {
@@ -9,7 +9,7 @@
9
9
  "dependencies": {
10
10
  "@coral-xyz/anchor": "0.28.0",
11
11
  "@drift-labs/competitions-sdk": "0.2.386",
12
- "@drift-labs/sdk": "2.96.0-beta.9",
12
+ "@drift-labs/sdk": "2.96.0-beta.11",
13
13
  "@solana/web3.js": "1.92.3",
14
14
  "commander": "^11.0.0",
15
15
  "dotenv": "^16.3.1",