@drift-labs/vaults-sdk 0.2.51 → 0.2.53

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.
@@ -110,7 +110,7 @@ export declare class VaultClient {
110
110
  hurdleRate: number;
111
111
  permissioned: boolean;
112
112
  vaultProtocol?: VaultProtocolParams;
113
- }): Promise<TransactionSignature>;
113
+ }, uiTxParams?: TxParams): Promise<TransactionSignature>;
114
114
  /**
115
115
  * Updates the delegate address for a vault. The delegate address will be allowed to trade
116
116
  * on behalf of the vault.
@@ -118,24 +118,25 @@ export declare class VaultClient {
118
118
  * @param delegate delegate address to update to
119
119
  * @returns
120
120
  */
121
- updateDelegate(vault: PublicKey, delegate: PublicKey): Promise<TransactionSignature>;
121
+ updateDelegate(vault: PublicKey, delegate: PublicKey, uiTxParams?: TxParams): Promise<TransactionSignature>;
122
122
  /**
123
123
  * Updates the vault margin trading status.
124
124
  * @param vault vault address to update
125
125
  * @param enabled whether to enable margin trading
126
126
  * @returns
127
127
  */
128
- updateMarginTradingEnabled(vault: PublicKey, enabled: boolean): Promise<TransactionSignature>;
128
+ updateMarginTradingEnabled(vault: PublicKey, enabled: boolean, uiTxParams?: TxParams): Promise<TransactionSignature>;
129
+ private handleWSolMovement;
129
130
  /**
130
131
  *
131
132
  * @param vault vault address to deposit to
132
133
  * @param amount amount to deposit
133
134
  * @returns
134
135
  */
135
- managerDeposit(vault: PublicKey, amount: BN): Promise<TransactionSignature>;
136
- managerRequestWithdraw(vault: PublicKey, amount: BN, withdrawUnit: WithdrawUnit): Promise<TransactionSignature>;
137
- managerCancelWithdrawRequest(vault: PublicKey): Promise<TransactionSignature>;
138
- managerWithdraw(vault: PublicKey): Promise<TransactionSignature>;
136
+ managerDeposit(vault: PublicKey, amount: BN, uiTxParams?: TxParams): Promise<TransactionSignature>;
137
+ managerRequestWithdraw(vault: PublicKey, amount: BN, withdrawUnit: WithdrawUnit, uiTxParams?: TxParams): Promise<TransactionSignature>;
138
+ managerCancelWithdrawRequest(vault: PublicKey, uiTxParams?: TxParams): Promise<TransactionSignature>;
139
+ managerWithdraw(vault: PublicKey, uiTxParams?: TxParams): Promise<TransactionSignature>;
139
140
  managerUpdateVault(vault: PublicKey, params: {
140
141
  redeemPeriod: BN | null;
141
142
  maxTokens: BN | null;
@@ -144,7 +145,7 @@ export declare class VaultClient {
144
145
  profitShare: number | null;
145
146
  hurdleRate: number | null;
146
147
  permissioned: boolean | null;
147
- }): Promise<TransactionSignature>;
148
+ }, uiTxParams?: TxParams): Promise<TransactionSignature>;
148
149
  getApplyProfitShareIx(vault: PublicKey, vaultDepositor: PublicKey): Promise<TransactionInstruction>;
149
150
  getApplyRebaseTokenizedDepositorIx(vault: PublicKey, tokenizedVaultDepositor: PublicKey): Promise<TransactionInstruction>;
150
151
  applyRebase(vault: PublicKey, vaultDepositor: PublicKey): Promise<TransactionSignature>;
@@ -157,7 +158,7 @@ export declare class VaultClient {
157
158
  * @param authority the authority allowed to make deposits into the vault
158
159
  * @returns
159
160
  */
160
- initializeVaultDepositor(vault: PublicKey, authority?: PublicKey, payer?: PublicKey): Promise<TransactionSignature>;
161
+ initializeVaultDepositor(vault: PublicKey, authority?: PublicKey, payer?: PublicKey, uiTxParams?: TxParams): Promise<TransactionSignature>;
161
162
  initializeTokenizedVaultDepositor(params: {
162
163
  vault: PublicKey;
163
164
  tokenName: string;
@@ -282,7 +282,7 @@ class VaultClient {
282
282
  const vpAccount = await this.program.account.vaultProtocol.fetch(vaultProtocol);
283
283
  return (0, sdk_1.unstakeSharesToAmount)(vpAccount.protocolProfitAndFeeShares, vaultAccount.totalShares, vaultTotalEquity);
284
284
  }
285
- async initializeVault(params) {
285
+ async initializeVault(params, uiTxParams) {
286
286
  const { vaultProtocol: vaultProtocolParams, ...vaultParams } = params;
287
287
  const vault = (0, addresses_1.getVaultAddressSync)(this.program.programId, params.name);
288
288
  const tokenAccount = (0, addresses_1.getTokenVaultAddressSync)(this.program.programId, vault);
@@ -303,42 +303,67 @@ class VaultClient {
303
303
  tokenAccount,
304
304
  driftProgram: this.driftClient.program.programId,
305
305
  };
306
+ const preIxs = [
307
+ web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
308
+ units: 400000,
309
+ }),
310
+ web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
311
+ microLamports: 300000,
312
+ }),
313
+ ];
306
314
  if (vaultProtocolParams) {
307
315
  const vaultProtocol = this.getVaultProtocolAddress((0, addresses_1.getVaultAddressSync)(this.program.programId, params.name));
308
316
  const _params = {
309
317
  ...vaultParams,
310
318
  vaultProtocol: vaultProtocolParams,
311
319
  };
312
- return await this.program.methods
313
- .initializeVaultWithProtocol(_params)
314
- .preInstructions([
315
- web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
316
- units: 400000,
317
- }),
318
- web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
319
- microLamports: 300000,
320
- }),
321
- ])
322
- .accounts({
323
- ...accounts,
324
- vaultProtocol,
325
- })
326
- .rpc();
320
+ if (this.cliMode) {
321
+ return await this.program.methods
322
+ .initializeVaultWithProtocol(_params)
323
+ .preInstructions(preIxs)
324
+ .accounts({
325
+ ...accounts,
326
+ vaultProtocol,
327
+ })
328
+ .rpc();
329
+ }
330
+ else {
331
+ const uiAuthority = this.driftClient.wallet.publicKey;
332
+ const initializeVaultWithProtocolIx = await this.program.methods
333
+ .initializeVaultWithProtocol(_params)
334
+ .accounts({
335
+ ...accounts,
336
+ vaultProtocol,
337
+ payer: uiAuthority,
338
+ manager: uiAuthority,
339
+ })
340
+ .instruction();
341
+ const ixs = [...preIxs, initializeVaultWithProtocolIx];
342
+ return await this.createAndSendTxn(ixs, uiTxParams);
343
+ }
327
344
  }
328
345
  else {
329
346
  const _params = vaultParams;
330
- return await this.program.methods
331
- .initializeVault(_params)
332
- .preInstructions([
333
- web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
334
- units: 400000,
335
- }),
336
- web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
337
- microLamports: 300000,
338
- }),
339
- ])
340
- .accounts(accounts)
341
- .rpc();
347
+ if (this.cliMode) {
348
+ return await this.program.methods
349
+ .initializeVault(_params)
350
+ .preInstructions(preIxs)
351
+ .accounts(accounts)
352
+ .rpc();
353
+ }
354
+ else {
355
+ const uiAuthority = this.driftClient.wallet.publicKey;
356
+ const initializeVaultIx = await this.program.methods
357
+ .initializeVault(_params)
358
+ .accounts({
359
+ ...accounts,
360
+ payer: uiAuthority,
361
+ manager: uiAuthority,
362
+ })
363
+ .instruction();
364
+ const ixs = [initializeVaultIx];
365
+ return await this.createAndSendTxn(ixs, uiTxParams);
366
+ }
342
367
  }
343
368
  }
344
369
  /**
@@ -348,24 +373,34 @@ class VaultClient {
348
373
  * @param delegate delegate address to update to
349
374
  * @returns
350
375
  */
351
- async updateDelegate(vault, delegate) {
376
+ async updateDelegate(vault, delegate, uiTxParams) {
352
377
  const vaultAccount = await this.program.account.vault.fetch(vault);
353
- return await this.program.methods
354
- .updateDelegate(delegate)
355
- .preInstructions([
356
- web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
357
- units: 400000,
358
- }),
359
- web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
360
- microLamports: 300000,
361
- }),
362
- ])
363
- .accounts({
378
+ const accounts = {
364
379
  vault: vault,
365
380
  driftUser: vaultAccount.user,
366
381
  driftProgram: this.driftClient.program.programId,
367
- })
368
- .rpc();
382
+ };
383
+ if (this.cliMode) {
384
+ return await this.program.methods
385
+ .updateDelegate(delegate)
386
+ .preInstructions([
387
+ web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
388
+ units: 400000,
389
+ }),
390
+ web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
391
+ microLamports: 300000,
392
+ }),
393
+ ])
394
+ .accounts(accounts)
395
+ .rpc();
396
+ }
397
+ else {
398
+ const updateDelegateIx = await this.program.methods
399
+ .updateDelegate(delegate)
400
+ .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
401
+ .instruction();
402
+ return await this.createAndSendTxn([updateDelegateIx], uiTxParams);
403
+ }
369
404
  }
370
405
  /**
371
406
  * Updates the vault margin trading status.
@@ -373,16 +408,38 @@ class VaultClient {
373
408
  * @param enabled whether to enable margin trading
374
409
  * @returns
375
410
  */
376
- async updateMarginTradingEnabled(vault, enabled) {
411
+ async updateMarginTradingEnabled(vault, enabled, uiTxParams) {
377
412
  const vaultAccount = await this.program.account.vault.fetch(vault);
378
- return await this.program.methods
379
- .updateMarginTradingEnabled(enabled)
380
- .accounts({
413
+ const accounts = {
381
414
  vault: vault,
382
415
  driftUser: vaultAccount.user,
383
416
  driftProgram: this.driftClient.program.programId,
384
- })
385
- .rpc();
417
+ };
418
+ if (this.cliMode) {
419
+ return await this.program.methods
420
+ .updateMarginTradingEnabled(enabled)
421
+ .accounts(accounts)
422
+ .rpc();
423
+ }
424
+ else {
425
+ const updateMarginTradingEnabledIx = await this.program.methods
426
+ .updateMarginTradingEnabled(enabled)
427
+ .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
428
+ .instruction();
429
+ return await this.createAndSendTxn([updateMarginTradingEnabledIx], uiTxParams);
430
+ }
431
+ }
432
+ async handleWSolMovement(amount, driftSpotMarket, userTokenAccount) {
433
+ const isSolDeposit = driftSpotMarket.mint.equals(sdk_1.WRAPPED_SOL_MINT);
434
+ const preIxs = [];
435
+ const postIxs = [];
436
+ if (isSolDeposit) {
437
+ const { ixs: createWSolAccountIxs, pubkey } = await this.driftClient.getWrappedSolAccountCreationIxs(amount, true);
438
+ userTokenAccount = pubkey;
439
+ preIxs.push(...createWSolAccountIxs);
440
+ postIxs.push((0, spl_token_1.createCloseAccountInstruction)(userTokenAccount, this.driftClient.wallet.publicKey, this.driftClient.wallet.publicKey, []));
441
+ }
442
+ return { userTokenAccount, preIxs, postIxs };
386
443
  }
387
444
  /**
388
445
  *
@@ -390,7 +447,7 @@ class VaultClient {
390
447
  * @param amount amount to deposit
391
448
  * @returns
392
449
  */
393
- async managerDeposit(vault, amount) {
450
+ async managerDeposit(vault, amount, uiTxParams) {
394
451
  const vaultAccount = await this.program.account.vault.fetch(vault);
395
452
  const driftSpotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
396
453
  if (!driftSpotMarket) {
@@ -409,24 +466,41 @@ class VaultClient {
409
466
  isWritable: true,
410
467
  });
411
468
  }
412
- // TODO: handle SOL deposits (need to create/destroy WSOL token accounts)
413
- return await this.program.methods
414
- .managerDeposit(amount)
415
- .accounts({
469
+ const accounts = {
416
470
  vault,
417
471
  vaultTokenAccount: vaultAccount.tokenAccount,
418
472
  driftUser: await (0, sdk_1.getUserAccountPublicKey)(this.driftClient.program.programId, vault),
419
- driftProgram: this.driftClient.program.programId,
420
473
  driftUserStats: (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault),
474
+ driftProgram: this.driftClient.program.programId,
421
475
  driftState: await this.driftClient.getStatePublicKey(),
422
476
  driftSpotMarketVault: driftSpotMarket.vault,
423
477
  userTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(driftSpotMarket.mint, this.driftClient.wallet.publicKey),
424
478
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
425
- })
426
- .remainingAccounts(remainingAccounts)
427
- .rpc();
479
+ };
480
+ const { userTokenAccount, preIxs, postIxs } = await this.handleWSolMovement(amount, driftSpotMarket, accounts.userTokenAccount);
481
+ if (this.cliMode) {
482
+ return await this.program.methods
483
+ .managerDeposit(amount)
484
+ .accounts({ ...accounts, userTokenAccount })
485
+ .remainingAccounts(remainingAccounts)
486
+ .preInstructions(preIxs)
487
+ .postInstructions(postIxs)
488
+ .rpc();
489
+ }
490
+ else {
491
+ const managerDepositIx = await this.program.methods
492
+ .managerDeposit(amount)
493
+ .accounts({
494
+ ...accounts,
495
+ userTokenAccount,
496
+ manager: this.driftClient.wallet.publicKey,
497
+ })
498
+ .remainingAccounts(remainingAccounts)
499
+ .instruction();
500
+ return await this.createAndSendTxn([...preIxs, managerDepositIx, ...postIxs], uiTxParams);
501
+ }
428
502
  }
429
- async managerRequestWithdraw(vault, amount, withdrawUnit) {
503
+ async managerRequestWithdraw(vault, amount, withdrawUnit, uiTxParams) {
430
504
  this.program.idl.types;
431
505
  // @ts-ignore
432
506
  const vaultAccount = (await this.program.account.vault.fetch(vault));
@@ -472,10 +546,10 @@ class VaultClient {
472
546
  },
473
547
  remainingAccounts,
474
548
  });
475
- return await this.createAndSendTxn([requestWithdrawIx]);
549
+ return await this.createAndSendTxn([requestWithdrawIx], uiTxParams);
476
550
  }
477
551
  }
478
- async managerCancelWithdrawRequest(vault) {
552
+ async managerCancelWithdrawRequest(vault, uiTxParams) {
479
553
  const vaultAccount = await this.program.account.vault.fetch(vault);
480
554
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
481
555
  const driftStateKey = await this.driftClient.getStatePublicKey();
@@ -513,10 +587,10 @@ class VaultClient {
513
587
  },
514
588
  remainingAccounts,
515
589
  });
516
- return await this.createAndSendTxn([cancelRequestWithdrawIx]);
590
+ return await this.createAndSendTxn([cancelRequestWithdrawIx], uiTxParams);
517
591
  }
518
592
  }
519
- async managerWithdraw(vault) {
593
+ async managerWithdraw(vault, uiTxParams) {
520
594
  const vaultAccount = await this.program.account.vault.fetch(vault);
521
595
  if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
522
596
  throw new Error(`Only the manager of the vault can request a withdraw.`);
@@ -554,21 +628,24 @@ class VaultClient {
554
628
  },
555
629
  remainingAccounts,
556
630
  });
557
- return this.createAndSendTxn([ix], {
558
- cuLimit: 1000000,
559
- });
631
+ return this.createAndSendTxn([ix], uiTxParams);
560
632
  }
561
- async managerUpdateVault(vault, params) {
633
+ async managerUpdateVault(vault, params, uiTxParams) {
562
634
  const ix = this.program.instruction.updateVault(params, {
563
635
  accounts: {
564
636
  vault,
565
637
  manager: this.driftClient.wallet.publicKey,
566
638
  },
567
639
  });
568
- return this.createAndSendTxn([ix], {
569
- cuLimit: 600000,
570
- cuPriceMicroLamports: 10000,
571
- });
640
+ if (this.cliMode) {
641
+ return this.createAndSendTxn([ix], {
642
+ cuLimit: 600000,
643
+ cuPriceMicroLamports: 10000,
644
+ });
645
+ }
646
+ else {
647
+ return this.createAndSendTxn([ix], uiTxParams);
648
+ }
572
649
  }
573
650
  async getApplyProfitShareIx(vault, vaultDepositor) {
574
651
  const vaultAccount = await this.program.account.vault.fetch(vault);
@@ -683,7 +760,7 @@ class VaultClient {
683
760
  * @param authority the authority allowed to make deposits into the vault
684
761
  * @returns
685
762
  */
686
- async initializeVaultDepositor(vault, authority, payer) {
763
+ async initializeVaultDepositor(vault, authority, payer, uiTxParams) {
687
764
  const vaultDepositor = (0, addresses_1.getVaultDepositorAddressSync)(this.program.programId, vault, authority || this.driftClient.wallet.publicKey);
688
765
  const accounts = {
689
766
  vaultDepositor,
@@ -703,7 +780,7 @@ class VaultClient {
703
780
  }
704
781
  else {
705
782
  const initIx = this.createInitVaultDepositorIx(vault, authority, payer);
706
- return await this.createAndSendTxn([initIx]);
783
+ return await this.createAndSendTxn([initIx], uiTxParams);
707
784
  }
708
785
  }
709
786
  async initializeTokenizedVaultDepositor(params) {
@@ -884,16 +961,8 @@ class VaultClient {
884
961
  if (!spotMarket) {
885
962
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
886
963
  }
887
- let userTokenAccount = depositTokenAccount !== null && depositTokenAccount !== void 0 ? depositTokenAccount : (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true);
888
- const isSolDeposit = spotMarket.mint.equals(sdk_1.WRAPPED_SOL_MINT);
889
- const preIxs = [];
890
- const postIxs = [];
891
- if (isSolDeposit) {
892
- const { ixs, pubkey } = await this.driftClient.getWrappedSolAccountCreationIxs(amount, true);
893
- userTokenAccount = pubkey;
894
- preIxs.push(...ixs);
895
- postIxs.push((0, spl_token_1.createCloseAccountInstruction)(userTokenAccount, this.driftClient.wallet.publicKey, this.driftClient.wallet.publicKey, []));
896
- }
964
+ const nonWSolUserTokenAccount = depositTokenAccount !== null && depositTokenAccount !== void 0 ? depositTokenAccount : (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true);
965
+ const { userTokenAccount, preIxs, postIxs } = await this.handleWSolMovement(amount, spotMarket, nonWSolUserTokenAccount);
897
966
  const accounts = {
898
967
  vault: vaultPubKey,
899
968
  vaultDepositor,
@@ -902,7 +971,7 @@ class VaultClient {
902
971
  driftUser: vaultAccount.user,
903
972
  driftState: driftStateKey,
904
973
  driftSpotMarketVault: spotMarket.vault,
905
- userTokenAccount,
974
+ userTokenAccount: userTokenAccount,
906
975
  driftProgram: this.driftClient.program.programId,
907
976
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
908
977
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/vaults-sdk",
3
- "version": "0.2.51",
3
+ "version": "0.2.53",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "directories": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@coral-xyz/anchor": "0.28.0",
11
- "@drift-labs/sdk": "2.107.0-beta.5",
11
+ "@drift-labs/sdk": "2.107.0-beta.6",
12
12
  "@ledgerhq/hw-app-solana": "7.2.4",
13
13
  "@ledgerhq/hw-transport": "6.31.4",
14
14
  "@ledgerhq/hw-transport-node-hid": "6.29.5",
@@ -12,6 +12,7 @@ import {
12
12
  getInsuranceFundVaultPublicKey,
13
13
  OracleSource,
14
14
  WRAPPED_SOL_MINT,
15
+ SpotMarketAccount,
15
16
  } from '@drift-labs/sdk';
16
17
  import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor';
17
18
  import { DriftVaults } from './types/drift_vaults';
@@ -455,18 +456,21 @@ export class VaultClient {
455
456
  );
456
457
  }
457
458
 
458
- public async initializeVault(params: {
459
- name: number[];
460
- spotMarketIndex: number;
461
- redeemPeriod: BN;
462
- maxTokens: BN;
463
- minDepositAmount: BN;
464
- managementFee: BN;
465
- profitShare: number;
466
- hurdleRate: number;
467
- permissioned: boolean;
468
- vaultProtocol?: VaultProtocolParams;
469
- }): Promise<TransactionSignature> {
459
+ public async initializeVault(
460
+ params: {
461
+ name: number[];
462
+ spotMarketIndex: number;
463
+ redeemPeriod: BN;
464
+ maxTokens: BN;
465
+ minDepositAmount: BN;
466
+ managementFee: BN;
467
+ profitShare: number;
468
+ hurdleRate: number;
469
+ permissioned: boolean;
470
+ vaultProtocol?: VaultProtocolParams;
471
+ },
472
+ uiTxParams?: TxParams
473
+ ): Promise<TransactionSignature> {
470
474
  const { vaultProtocol: vaultProtocolParams, ...vaultParams } = params;
471
475
  const vault = getVaultAddressSync(this.program.programId, params.name);
472
476
  const tokenAccount = getTokenVaultAddressSync(
@@ -504,6 +508,15 @@ export class VaultClient {
504
508
  driftProgram: this.driftClient.program.programId,
505
509
  };
506
510
 
511
+ const preIxs = [
512
+ ComputeBudgetProgram.setComputeUnitLimit({
513
+ units: 400_000,
514
+ }),
515
+ ComputeBudgetProgram.setComputeUnitPrice({
516
+ microLamports: 300_000,
517
+ }),
518
+ ];
519
+
507
520
  if (vaultProtocolParams) {
508
521
  const vaultProtocol = this.getVaultProtocolAddress(
509
522
  getVaultAddressSync(this.program.programId, params.name)
@@ -512,35 +525,52 @@ export class VaultClient {
512
525
  ...vaultParams,
513
526
  vaultProtocol: vaultProtocolParams,
514
527
  };
515
- return await this.program.methods
516
- .initializeVaultWithProtocol(_params)
517
- .preInstructions([
518
- ComputeBudgetProgram.setComputeUnitLimit({
519
- units: 400_000,
520
- }),
521
- ComputeBudgetProgram.setComputeUnitPrice({
522
- microLamports: 300_000,
523
- }),
524
- ])
525
- .accounts({
526
- ...accounts,
527
- vaultProtocol,
528
- })
529
- .rpc();
528
+
529
+ if (this.cliMode) {
530
+ return await this.program.methods
531
+ .initializeVaultWithProtocol(_params)
532
+ .preInstructions(preIxs)
533
+ .accounts({
534
+ ...accounts,
535
+ vaultProtocol,
536
+ })
537
+ .rpc();
538
+ } else {
539
+ const uiAuthority = this.driftClient.wallet.publicKey;
540
+ const initializeVaultWithProtocolIx = await this.program.methods
541
+ .initializeVaultWithProtocol(_params)
542
+ .accounts({
543
+ ...accounts,
544
+ vaultProtocol,
545
+ payer: uiAuthority,
546
+ manager: uiAuthority,
547
+ })
548
+ .instruction();
549
+ const ixs = [...preIxs, initializeVaultWithProtocolIx];
550
+ return await this.createAndSendTxn(ixs, uiTxParams);
551
+ }
530
552
  } else {
531
553
  const _params: VaultParams = vaultParams;
532
- return await this.program.methods
533
- .initializeVault(_params)
534
- .preInstructions([
535
- ComputeBudgetProgram.setComputeUnitLimit({
536
- units: 400_000,
537
- }),
538
- ComputeBudgetProgram.setComputeUnitPrice({
539
- microLamports: 300_000,
540
- }),
541
- ])
542
- .accounts(accounts)
543
- .rpc();
554
+
555
+ if (this.cliMode) {
556
+ return await this.program.methods
557
+ .initializeVault(_params)
558
+ .preInstructions(preIxs)
559
+ .accounts(accounts)
560
+ .rpc();
561
+ } else {
562
+ const uiAuthority = this.driftClient.wallet.publicKey;
563
+ const initializeVaultIx = await this.program.methods
564
+ .initializeVault(_params)
565
+ .accounts({
566
+ ...accounts,
567
+ payer: uiAuthority,
568
+ manager: uiAuthority,
569
+ })
570
+ .instruction();
571
+ const ixs = [initializeVaultIx];
572
+ return await this.createAndSendTxn(ixs, uiTxParams);
573
+ }
544
574
  }
545
575
  }
546
576
 
@@ -553,25 +583,36 @@ export class VaultClient {
553
583
  */
554
584
  public async updateDelegate(
555
585
  vault: PublicKey,
556
- delegate: PublicKey
586
+ delegate: PublicKey,
587
+ uiTxParams?: TxParams
557
588
  ): Promise<TransactionSignature> {
558
589
  const vaultAccount = await this.program.account.vault.fetch(vault);
559
- return await this.program.methods
560
- .updateDelegate(delegate)
561
- .preInstructions([
562
- ComputeBudgetProgram.setComputeUnitLimit({
563
- units: 400_000,
564
- }),
565
- ComputeBudgetProgram.setComputeUnitPrice({
566
- microLamports: 300_000,
567
- }),
568
- ])
569
- .accounts({
570
- vault: vault,
571
- driftUser: vaultAccount.user,
572
- driftProgram: this.driftClient.program.programId,
573
- })
574
- .rpc();
590
+ const accounts = {
591
+ vault: vault,
592
+ driftUser: vaultAccount.user,
593
+ driftProgram: this.driftClient.program.programId,
594
+ };
595
+
596
+ if (this.cliMode) {
597
+ return await this.program.methods
598
+ .updateDelegate(delegate)
599
+ .preInstructions([
600
+ ComputeBudgetProgram.setComputeUnitLimit({
601
+ units: 400_000,
602
+ }),
603
+ ComputeBudgetProgram.setComputeUnitPrice({
604
+ microLamports: 300_000,
605
+ }),
606
+ ])
607
+ .accounts(accounts)
608
+ .rpc();
609
+ } else {
610
+ const updateDelegateIx = await this.program.methods
611
+ .updateDelegate(delegate)
612
+ .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
613
+ .instruction();
614
+ return await this.createAndSendTxn([updateDelegateIx], uiTxParams);
615
+ }
575
616
  }
576
617
 
577
618
  /**
@@ -582,17 +623,60 @@ export class VaultClient {
582
623
  */
583
624
  public async updateMarginTradingEnabled(
584
625
  vault: PublicKey,
585
- enabled: boolean
626
+ enabled: boolean,
627
+ uiTxParams?: TxParams
586
628
  ): Promise<TransactionSignature> {
587
629
  const vaultAccount = await this.program.account.vault.fetch(vault);
588
- return await this.program.methods
589
- .updateMarginTradingEnabled(enabled)
590
- .accounts({
591
- vault: vault,
592
- driftUser: vaultAccount.user,
593
- driftProgram: this.driftClient.program.programId,
594
- })
595
- .rpc();
630
+ const accounts = {
631
+ vault: vault,
632
+ driftUser: vaultAccount.user,
633
+ driftProgram: this.driftClient.program.programId,
634
+ };
635
+
636
+ if (this.cliMode) {
637
+ return await this.program.methods
638
+ .updateMarginTradingEnabled(enabled)
639
+ .accounts(accounts)
640
+ .rpc();
641
+ } else {
642
+ const updateMarginTradingEnabledIx = await this.program.methods
643
+ .updateMarginTradingEnabled(enabled)
644
+ .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
645
+ .instruction();
646
+ return await this.createAndSendTxn(
647
+ [updateMarginTradingEnabledIx],
648
+ uiTxParams
649
+ );
650
+ }
651
+ }
652
+
653
+ private async handleWSolMovement(
654
+ amount: BN,
655
+ driftSpotMarket: SpotMarketAccount,
656
+ userTokenAccount: PublicKey
657
+ ) {
658
+ const isSolDeposit = driftSpotMarket.mint.equals(WRAPPED_SOL_MINT);
659
+ const preIxs: TransactionInstruction[] = [];
660
+ const postIxs: TransactionInstruction[] = [];
661
+
662
+ if (isSolDeposit) {
663
+ const { ixs: createWSolAccountIxs, pubkey } =
664
+ await this.driftClient.getWrappedSolAccountCreationIxs(amount, true);
665
+
666
+ userTokenAccount = pubkey;
667
+
668
+ preIxs.push(...createWSolAccountIxs);
669
+ postIxs.push(
670
+ createCloseAccountInstruction(
671
+ userTokenAccount,
672
+ this.driftClient.wallet.publicKey,
673
+ this.driftClient.wallet.publicKey,
674
+ []
675
+ )
676
+ );
677
+ }
678
+
679
+ return { userTokenAccount, preIxs, postIxs };
596
680
  }
597
681
 
598
682
  /**
@@ -603,7 +687,8 @@ export class VaultClient {
603
687
  */
604
688
  public async managerDeposit(
605
689
  vault: PublicKey,
606
- amount: BN
690
+ amount: BN,
691
+ uiTxParams?: TxParams
607
692
  ): Promise<TransactionSignature> {
608
693
  const vaultAccount = await this.program.account.vault.fetch(vault);
609
694
  const driftSpotMarket = this.driftClient.getSpotMarketAccount(
@@ -630,38 +715,63 @@ export class VaultClient {
630
715
  });
631
716
  }
632
717
 
633
- // TODO: handle SOL deposits (need to create/destroy WSOL token accounts)
718
+ const accounts = {
719
+ vault,
720
+ vaultTokenAccount: vaultAccount.tokenAccount,
721
+ driftUser: await getUserAccountPublicKey(
722
+ this.driftClient.program.programId,
723
+ vault
724
+ ),
725
+ driftUserStats: getUserStatsAccountPublicKey(
726
+ this.driftClient.program.programId,
727
+ vault
728
+ ),
729
+ driftProgram: this.driftClient.program.programId,
730
+ driftState: await this.driftClient.getStatePublicKey(),
731
+ driftSpotMarketVault: driftSpotMarket.vault,
732
+ userTokenAccount: getAssociatedTokenAddressSync(
733
+ driftSpotMarket.mint,
734
+ this.driftClient.wallet.publicKey
735
+ ),
736
+ tokenProgram: TOKEN_PROGRAM_ID,
737
+ };
738
+
739
+ const { userTokenAccount, preIxs, postIxs } = await this.handleWSolMovement(
740
+ amount,
741
+ driftSpotMarket,
742
+ accounts.userTokenAccount
743
+ );
634
744
 
635
- return await this.program.methods
636
- .managerDeposit(amount)
637
- .accounts({
638
- vault,
639
- vaultTokenAccount: vaultAccount.tokenAccount,
640
- driftUser: await getUserAccountPublicKey(
641
- this.driftClient.program.programId,
642
- vault
643
- ),
644
- driftProgram: this.driftClient.program.programId,
645
- driftUserStats: getUserStatsAccountPublicKey(
646
- this.driftClient.program.programId,
647
- vault
648
- ),
649
- driftState: await this.driftClient.getStatePublicKey(),
650
- driftSpotMarketVault: driftSpotMarket.vault,
651
- userTokenAccount: getAssociatedTokenAddressSync(
652
- driftSpotMarket.mint,
653
- this.driftClient.wallet.publicKey
654
- ),
655
- tokenProgram: TOKEN_PROGRAM_ID,
656
- })
657
- .remainingAccounts(remainingAccounts)
658
- .rpc();
745
+ if (this.cliMode) {
746
+ return await this.program.methods
747
+ .managerDeposit(amount)
748
+ .accounts({ ...accounts, userTokenAccount })
749
+ .remainingAccounts(remainingAccounts)
750
+ .preInstructions(preIxs)
751
+ .postInstructions(postIxs)
752
+ .rpc();
753
+ } else {
754
+ const managerDepositIx = await this.program.methods
755
+ .managerDeposit(amount)
756
+ .accounts({
757
+ ...accounts,
758
+ userTokenAccount,
759
+ manager: this.driftClient.wallet.publicKey,
760
+ })
761
+ .remainingAccounts(remainingAccounts)
762
+ .instruction();
763
+ return await this.createAndSendTxn(
764
+ [...preIxs, managerDepositIx, ...postIxs],
765
+ uiTxParams
766
+ );
767
+ }
659
768
  }
660
769
 
661
770
  public async managerRequestWithdraw(
662
771
  vault: PublicKey,
663
772
  amount: BN,
664
- withdrawUnit: WithdrawUnit
773
+ withdrawUnit: WithdrawUnit,
774
+ uiTxParams?: TxParams
665
775
  ): Promise<TransactionSignature> {
666
776
  this.program.idl.types;
667
777
  // @ts-ignore
@@ -722,12 +832,13 @@ export class VaultClient {
722
832
  }
723
833
  );
724
834
 
725
- return await this.createAndSendTxn([requestWithdrawIx]);
835
+ return await this.createAndSendTxn([requestWithdrawIx], uiTxParams);
726
836
  }
727
837
  }
728
838
 
729
839
  public async managerCancelWithdrawRequest(
730
- vault: PublicKey
840
+ vault: PublicKey,
841
+ uiTxParams?: TxParams
731
842
  ): Promise<TransactionSignature> {
732
843
  const vaultAccount = await this.program.account.vault.fetch(vault);
733
844
 
@@ -775,12 +886,13 @@ export class VaultClient {
775
886
  remainingAccounts,
776
887
  });
777
888
 
778
- return await this.createAndSendTxn([cancelRequestWithdrawIx]);
889
+ return await this.createAndSendTxn([cancelRequestWithdrawIx], uiTxParams);
779
890
  }
780
891
  }
781
892
 
782
893
  public async managerWithdraw(
783
- vault: PublicKey
894
+ vault: PublicKey,
895
+ uiTxParams?: TxParams
784
896
  ): Promise<TransactionSignature> {
785
897
  const vaultAccount = await this.program.account.vault.fetch(vault);
786
898
 
@@ -837,9 +949,7 @@ export class VaultClient {
837
949
  },
838
950
  remainingAccounts,
839
951
  });
840
- return this.createAndSendTxn([ix], {
841
- cuLimit: 1_000_000,
842
- });
952
+ return this.createAndSendTxn([ix], uiTxParams);
843
953
  }
844
954
 
845
955
  public async managerUpdateVault(
@@ -852,7 +962,8 @@ export class VaultClient {
852
962
  profitShare: number | null;
853
963
  hurdleRate: number | null;
854
964
  permissioned: boolean | null;
855
- }
965
+ },
966
+ uiTxParams?: TxParams
856
967
  ): Promise<TransactionSignature> {
857
968
  const ix = this.program.instruction.updateVault(params, {
858
969
  accounts: {
@@ -860,10 +971,14 @@ export class VaultClient {
860
971
  manager: this.driftClient.wallet.publicKey,
861
972
  },
862
973
  });
863
- return this.createAndSendTxn([ix], {
864
- cuLimit: 600_000,
865
- cuPriceMicroLamports: 10_000,
866
- });
974
+ if (this.cliMode) {
975
+ return this.createAndSendTxn([ix], {
976
+ cuLimit: 600_000,
977
+ cuPriceMicroLamports: 10_000,
978
+ });
979
+ } else {
980
+ return this.createAndSendTxn([ix], uiTxParams);
981
+ }
867
982
  }
868
983
 
869
984
  public async getApplyProfitShareIx(
@@ -1056,7 +1171,8 @@ export class VaultClient {
1056
1171
  public async initializeVaultDepositor(
1057
1172
  vault: PublicKey,
1058
1173
  authority?: PublicKey,
1059
- payer?: PublicKey
1174
+ payer?: PublicKey,
1175
+ uiTxParams?: TxParams
1060
1176
  ): Promise<TransactionSignature> {
1061
1177
  const vaultDepositor = getVaultDepositorAddressSync(
1062
1178
  this.program.programId,
@@ -1082,7 +1198,7 @@ export class VaultClient {
1082
1198
  .rpc();
1083
1199
  } else {
1084
1200
  const initIx = this.createInitVaultDepositorIx(vault, authority, payer);
1085
- return await this.createAndSendTxn([initIx]);
1201
+ return await this.createAndSendTxn([initIx], uiTxParams);
1086
1202
  }
1087
1203
  }
1088
1204
 
@@ -1419,7 +1535,7 @@ export class VaultClient {
1419
1535
  );
1420
1536
  }
1421
1537
 
1422
- let userTokenAccount =
1538
+ const nonWSolUserTokenAccount =
1423
1539
  depositTokenAccount ??
1424
1540
  getAssociatedTokenAddressSync(
1425
1541
  spotMarket.mint,
@@ -1427,27 +1543,11 @@ export class VaultClient {
1427
1543
  true
1428
1544
  );
1429
1545
 
1430
- const isSolDeposit = spotMarket.mint.equals(WRAPPED_SOL_MINT);
1431
-
1432
- const preIxs: TransactionInstruction[] = [];
1433
- const postIxs: TransactionInstruction[] = [];
1434
-
1435
- if (isSolDeposit) {
1436
- const { ixs, pubkey } =
1437
- await this.driftClient.getWrappedSolAccountCreationIxs(amount, true);
1438
-
1439
- userTokenAccount = pubkey;
1440
-
1441
- preIxs.push(...ixs);
1442
- postIxs.push(
1443
- createCloseAccountInstruction(
1444
- userTokenAccount,
1445
- this.driftClient.wallet.publicKey,
1446
- this.driftClient.wallet.publicKey,
1447
- []
1448
- )
1449
- );
1450
- }
1546
+ const { userTokenAccount, preIxs, postIxs } = await this.handleWSolMovement(
1547
+ amount,
1548
+ spotMarket,
1549
+ nonWSolUserTokenAccount
1550
+ );
1451
1551
 
1452
1552
  const accounts = {
1453
1553
  vault: vaultPubKey,
@@ -1457,7 +1557,7 @@ export class VaultClient {
1457
1557
  driftUser: vaultAccount.user,
1458
1558
  driftState: driftStateKey,
1459
1559
  driftSpotMarketVault: spotMarket.vault,
1460
- userTokenAccount,
1560
+ userTokenAccount: userTokenAccount,
1461
1561
  driftProgram: this.driftClient.program.programId,
1462
1562
  tokenProgram: TOKEN_PROGRAM_ID,
1463
1563
  };