@drift-labs/vaults-sdk 0.5.19 → 0.5.21

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.
@@ -341,6 +341,11 @@ class VaultClient {
341
341
  return (0, sdk_1.unstakeSharesToAmount)(vpAccount.protocolProfitAndFeeShares, vaultAccount.totalShares, vaultTotalEquity);
342
342
  }
343
343
  async initializeVault(params, uiTxParams) {
344
+ const ix = await this.getInitializeVaultIx(params);
345
+ return await this.createAndSendTxn([ix], uiTxParams);
346
+ }
347
+ async getInitializeVaultIx(params) {
348
+ var _a, _b, _c, _d;
344
349
  const { vaultProtocol: vaultProtocolParams, ...vaultParams } = params;
345
350
  const vault = (0, addresses_1.getVaultAddressSync)(this.program.programId, params.name);
346
351
  const tokenAccount = (0, addresses_1.getTokenVaultAddressSync)(this.program.programId, vault);
@@ -373,12 +378,11 @@ class VaultClient {
373
378
  .accounts({
374
379
  ...accounts,
375
380
  vaultProtocol,
376
- payer: uiAuthority,
377
- manager: uiAuthority,
381
+ payer: (_a = params.manager) !== null && _a !== void 0 ? _a : uiAuthority,
382
+ manager: (_b = params.manager) !== null && _b !== void 0 ? _b : uiAuthority,
378
383
  })
379
384
  .instruction();
380
- const ixs = [initializeVaultWithProtocolIx];
381
- return await this.createAndSendTxn(ixs, uiTxParams);
385
+ return initializeVaultWithProtocolIx;
382
386
  }
383
387
  else {
384
388
  const _params = vaultParams;
@@ -387,12 +391,11 @@ class VaultClient {
387
391
  .initializeVault(_params)
388
392
  .accounts({
389
393
  ...accounts,
390
- payer: uiAuthority,
391
- manager: uiAuthority,
394
+ payer: (_c = params.manager) !== null && _c !== void 0 ? _c : uiAuthority,
395
+ manager: (_d = params.manager) !== null && _d !== void 0 ? _d : uiAuthority,
392
396
  })
393
397
  .instruction();
394
- const ixs = [initializeVaultIx];
395
- return await this.createAndSendTxn(ixs, uiTxParams);
398
+ return initializeVaultIx;
396
399
  }
397
400
  }
398
401
  /**
@@ -403,17 +406,20 @@ class VaultClient {
403
406
  * @returns
404
407
  */
405
408
  async updateDelegate(vault, delegate, uiTxParams) {
409
+ const updateDelegateIx = await this.getUpdateDelegateIx(vault, delegate);
410
+ return await this.createAndSendTxn([updateDelegateIx], uiTxParams);
411
+ }
412
+ async getUpdateDelegateIx(vault, delegate) {
406
413
  const vaultAccount = await this.program.account.vault.fetch(vault);
407
414
  const accounts = {
408
415
  vault: vault,
409
416
  driftUser: vaultAccount.user,
410
417
  driftProgram: this.driftClient.program.programId,
411
418
  };
412
- const updateDelegateIx = await this.program.methods
419
+ return await this.program.methods
413
420
  .updateDelegate(delegate)
414
- .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
421
+ .accounts({ ...accounts, manager: vaultAccount.manager })
415
422
  .instruction();
416
- return await this.createAndSendTxn([updateDelegateIx], uiTxParams);
417
423
  }
418
424
  /**
419
425
  * Updates the vault margin trading status.
@@ -422,6 +428,10 @@ class VaultClient {
422
428
  * @returns
423
429
  */
424
430
  async updateMarginTradingEnabled(vault, enabled, uiTxParams) {
431
+ const updateMarginTradingEnabledIx = await this.getUpdateMarginTradingEnabledIx(vault, enabled);
432
+ return await this.createAndSendTxn([updateMarginTradingEnabledIx], uiTxParams);
433
+ }
434
+ async getUpdateMarginTradingEnabledIx(vault, enabled) {
425
435
  const vaultAccount = await this.program.account.vault.fetch(vault);
426
436
  const accounts = {
427
437
  vault: vault,
@@ -439,12 +449,11 @@ class VaultClient {
439
449
  console.error('failed to get remaining accounts', err);
440
450
  // do nothing
441
451
  }
442
- const updateMarginTradingEnabledIx = await this.program.methods
452
+ return await this.program.methods
443
453
  .updateMarginTradingEnabled(enabled)
444
- .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
454
+ .accounts({ ...accounts, manager: vaultAccount.manager })
445
455
  .remainingAccounts(remainingAccounts)
446
456
  .instruction();
447
- return await this.createAndSendTxn([updateMarginTradingEnabledIx], uiTxParams);
448
457
  }
449
458
  /**
450
459
  * Updates the vault's pool id (for isolated pools).
@@ -486,7 +495,7 @@ class VaultClient {
486
495
  }
487
496
  return await this.program.methods
488
497
  .updateUserPoolId(poolId)
489
- .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
498
+ .accounts({ ...accounts, manager: vaultAccount.manager })
490
499
  .remainingAccounts(remainingAccounts)
491
500
  .instruction();
492
501
  }
@@ -509,6 +518,16 @@ class VaultClient {
509
518
  * @returns
510
519
  */
511
520
  async managerDeposit(vault, amount, uiTxParams, managerTokenAccount) {
521
+ const managerDepositIxs = await this.getManagerDepositIx(vault, amount, managerTokenAccount);
522
+ return await this.createAndSendTxn(managerDepositIxs, uiTxParams);
523
+ }
524
+ /**
525
+ *
526
+ * @param vault vault address to deposit to
527
+ * @param amount amount to deposit
528
+ * @returns
529
+ */
530
+ async getManagerDepositIx(vault, amount, managerTokenAccount) {
512
531
  const vaultAccount = await this.program.account.vault.fetch(vault);
513
532
  const driftSpotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
514
533
  if (!driftSpotMarket) {
@@ -526,7 +545,7 @@ class VaultClient {
526
545
  driftProgram: this.driftClient.program.programId,
527
546
  driftState: await this.driftClient.getStatePublicKey(),
528
547
  driftSpotMarketVault: driftSpotMarket.vault,
529
- userTokenAccount: managerTokenAccount !== null && managerTokenAccount !== void 0 ? managerTokenAccount : (0, spl_token_1.getAssociatedTokenAddressSync)(driftSpotMarket.mint, this.driftClient.wallet.publicKey),
548
+ userTokenAccount: managerTokenAccount !== null && managerTokenAccount !== void 0 ? managerTokenAccount : (0, spl_token_1.getAssociatedTokenAddressSync)(driftSpotMarket.mint, vaultAccount.manager, true),
530
549
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
531
550
  };
532
551
  const { userTokenAccount, preIxs, postIxs } = await this.handleWSolMovement(amount, driftSpotMarket, accounts.userTokenAccount);
@@ -535,19 +554,20 @@ class VaultClient {
535
554
  .accounts({
536
555
  ...accounts,
537
556
  userTokenAccount,
538
- manager: this.driftClient.wallet.publicKey,
557
+ manager: vaultAccount.manager,
539
558
  })
540
559
  .remainingAccounts(remainingAccounts)
541
560
  .instruction();
542
- return await this.createAndSendTxn([...preIxs, managerDepositIx, ...postIxs], uiTxParams);
561
+ return [...preIxs, managerDepositIx, ...postIxs];
543
562
  }
544
563
  async managerRequestWithdraw(vault, amount, withdrawUnit, uiTxParams) {
564
+ const requestWithdrawIx = await this.getManagerRequestWithdrawIx(vault, amount, withdrawUnit);
565
+ return await this.createAndSendTxn([requestWithdrawIx], uiTxParams);
566
+ }
567
+ async getManagerRequestWithdrawIx(vault, amount, withdrawUnit) {
545
568
  this.program.idl.types;
546
569
  // @ts-ignore
547
570
  const vaultAccount = (await this.program.account.vault.fetch(vault));
548
- if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
549
- throw new Error(`Only the manager of the vault can request a withdraw.`);
550
- }
551
571
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
552
572
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
553
573
  const userStats = (await this.driftClient.program.account.userStats.fetch(userStatsKey));
@@ -557,22 +577,25 @@ class VaultClient {
557
577
  driftUser: vaultAccount.user,
558
578
  driftUserStats: userStatsKey,
559
579
  };
560
- const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
580
+ return this.program.instruction.managerRequestWithdraw(
561
581
  // @ts-ignore
562
582
  amount, withdrawUnit, {
563
583
  accounts: {
564
- manager: this.driftClient.wallet.publicKey,
584
+ manager: vaultAccount.manager,
565
585
  ...accounts,
566
586
  },
567
587
  remainingAccounts,
568
588
  });
569
- return await this.createAndSendTxn([requestWithdrawIx], uiTxParams);
570
589
  }
571
590
  async managerCancelWithdrawRequest(vault, uiTxParams) {
591
+ const ix = await this.getManagerCancelWithdrawRequestIx(vault);
592
+ return await this.createAndSendTxn([ix], uiTxParams);
593
+ }
594
+ async getManagerCancelWithdrawRequestIx(vault) {
572
595
  const vaultAccount = await this.program.account.vault.fetch(vault);
573
596
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
574
597
  const accounts = {
575
- manager: this.driftClient.wallet.publicKey,
598
+ manager: vaultAccount.manager,
576
599
  vault,
577
600
  driftUser: vaultAccount.user,
578
601
  driftUserStats: userStatsKey,
@@ -580,17 +603,17 @@ class VaultClient {
580
603
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
581
604
  const userStats = (await this.driftClient.program.account.userStats.fetch(userStatsKey));
582
605
  const remainingAccounts = this.getRemainingAccountsForUser([user.getUserAccount()], [], vaultAccount, userStats);
583
- const cancelRequestWithdrawIx = this.program.instruction.mangerCancelWithdrawRequest({
606
+ return this.program.instruction.mangerCancelWithdrawRequest({
584
607
  accounts,
585
608
  remainingAccounts,
586
609
  });
587
- return await this.createAndSendTxn([cancelRequestWithdrawIx], uiTxParams);
588
610
  }
589
611
  async managerWithdraw(vault, uiTxParams) {
612
+ const ix = await this.getManagerWithdrawIx(vault);
613
+ return this.createAndSendTxn([ix], uiTxParams);
614
+ }
615
+ async getManagerWithdrawIx(vault) {
590
616
  const vaultAccount = await this.program.account.vault.fetch(vault);
591
- if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
592
- throw new Error(`Only the manager of the vault can request a withdraw.`);
593
- }
594
617
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
595
618
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
596
619
  const userStats = (await this.driftClient.program.account.userStats.fetch(userStatsKey));
@@ -599,31 +622,51 @@ class VaultClient {
599
622
  if (!spotMarket) {
600
623
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
601
624
  }
602
- const ix = this.program.instruction.managerWithdraw({
625
+ return this.program.instruction.managerWithdraw({
603
626
  accounts: {
604
627
  vault,
605
- manager: this.driftClient.wallet.publicKey,
628
+ manager: vaultAccount.manager,
606
629
  vaultTokenAccount: vaultAccount.tokenAccount,
607
630
  driftUser: await (0, sdk_1.getUserAccountPublicKey)(this.driftClient.program.programId, vault),
608
631
  driftProgram: this.driftClient.program.programId,
609
632
  driftUserStats: (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault),
610
633
  driftState: await this.driftClient.getStatePublicKey(),
611
634
  driftSpotMarketVault: spotMarket.vault,
612
- userTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey),
635
+ userTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, vaultAccount.manager, true),
613
636
  driftSigner: this.driftClient.getStateAccount().signer,
614
637
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
615
638
  },
616
639
  remainingAccounts,
617
640
  });
618
- return this.createAndSendTxn([ix], uiTxParams);
619
641
  }
620
642
  async managerUpdateVault(vault, params, uiTxParams) {
621
- const ix = this.program.instruction.updateVault(params, {
643
+ const ix = await this.getManagerUpdateVaultIx(vault, params);
644
+ return this.createAndSendTxn([ix], uiTxParams);
645
+ }
646
+ async getManagerUpdateVaultIx(vault, params) {
647
+ const vaultAccount = await this.program.account.vault.fetch(vault);
648
+ return this.program.instruction.updateVault(params, {
622
649
  accounts: {
623
650
  vault,
624
- manager: this.driftClient.wallet.publicKey,
651
+ manager: vaultAccount.manager,
625
652
  },
626
653
  });
654
+ }
655
+ async managerUpdateVaultManager(vault, manager, uiTxParams) {
656
+ const ix = await this.getManagerUpdateVaultManagerIx(vault, manager);
657
+ return this.createAndSendTxn([ix], uiTxParams);
658
+ }
659
+ async getManagerUpdateVaultManagerIx(vault, manager) {
660
+ const vaultAccount = await this.program.account.vault.fetch(vault);
661
+ return this.program.instruction.updateVaultManager(manager, {
662
+ accounts: {
663
+ vault,
664
+ manager: vaultAccount.manager,
665
+ },
666
+ });
667
+ }
668
+ async applyProfitShare(vault, vaultDepositor, uiTxParams) {
669
+ const ix = await this.getApplyProfitShareIx(vault, vaultDepositor);
627
670
  return this.createAndSendTxn([ix], uiTxParams);
628
671
  }
629
672
  async getApplyProfitShareIx(vault, vaultDepositor) {
@@ -639,7 +682,7 @@ class VaultClient {
639
682
  const accounts = {
640
683
  vault,
641
684
  vaultDepositor,
642
- manager: this.driftClient.wallet.publicKey,
685
+ manager: vaultAccount.manager,
643
686
  driftUserStats: (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault),
644
687
  driftUser: await (0, sdk_1.getUserAccountPublicKey)(this.driftClient.program.programId, vault),
645
688
  driftState: await this.driftClient.getStatePublicKey(),
@@ -757,6 +800,7 @@ class VaultClient {
757
800
  sharesBase = vault.sharesBase;
758
801
  }
759
802
  const mintAddress = (0, addresses_1.getTokenizedVaultMintAddressSync)(this.program.programId, params.vault, sharesBase);
803
+ const vaultAccount = await this.program.account.vault.fetch(params.vault);
760
804
  const accounts = {
761
805
  vault: params.vault,
762
806
  vaultDepositor: (0, addresses_1.getTokenizedVaultAddressSync)(this.program.programId, params.vault, sharesBase),
@@ -765,10 +809,10 @@ class VaultClient {
765
809
  mint: mintAddress,
766
810
  }),
767
811
  tokenMetadataProgram: this.metaplex.programs().getTokenMetadata().address,
768
- payer: this.driftClient.wallet.publicKey,
812
+ payer: vaultAccount.manager,
769
813
  };
770
814
  const vaultTokenAta = (0, spl_token_1.getAssociatedTokenAddressSync)(mintAddress, params.vault, true);
771
- const createAtaIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.driftClient.wallet.publicKey, vaultTokenAta, params.vault, mintAddress);
815
+ const createAtaIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(vaultAccount.manager, vaultTokenAta, params.vault, mintAddress);
772
816
  return await this.createAndSendTxn([
773
817
  await this.program.methods
774
818
  .initializeTokenizedVaultDepositor({
@@ -942,6 +986,10 @@ class VaultClient {
942
986
  return this.sendTxn(depositTxn, txParams === null || txParams === void 0 ? void 0 : txParams.simulateTransaction);
943
987
  }
944
988
  async requestWithdraw(vaultDepositor, amount, withdrawUnit, txParams) {
989
+ const ixs = await this.getRequestWithdrawIx(vaultDepositor, amount, withdrawUnit, txParams === null || txParams === void 0 ? void 0 : txParams.oracleFeedsToCrank);
990
+ return await this.createAndSendTxn(ixs, txParams);
991
+ }
992
+ async getRequestWithdrawIx(vaultDepositor, amount, withdrawUnit, oracleFeedsToCrank) {
945
993
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
946
994
  const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
947
995
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
@@ -954,7 +1002,7 @@ class VaultClient {
954
1002
  driftUser: vaultAccount.user,
955
1003
  driftUserStats: userStatsKey,
956
1004
  };
957
- const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(txParams === null || txParams === void 0 ? void 0 : txParams.oracleFeedsToCrank);
1005
+ const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(oracleFeedsToCrank);
958
1006
  const requestWithdrawIx = this.program.instruction.requestWithdraw(
959
1007
  // @ts-ignore
960
1008
  amount, withdrawUnit, {
@@ -964,10 +1012,16 @@ class VaultClient {
964
1012
  },
965
1013
  remainingAccounts,
966
1014
  });
967
- return await this.createAndSendTxn([...oracleFeedsToCrankIxs, requestWithdrawIx], txParams);
1015
+ return [...oracleFeedsToCrankIxs, requestWithdrawIx];
968
1016
  }
969
1017
  async withdraw(vaultDepositor, txParams) {
970
- var _a;
1018
+ const ixs = await this.getWithdrawIx(vaultDepositor, txParams === null || txParams === void 0 ? void 0 : txParams.oracleFeedsToCrank);
1019
+ return await this.createAndSendTxn(ixs, {
1020
+ cuLimit: 850000, // overestimating to be safe
1021
+ ...txParams,
1022
+ });
1023
+ }
1024
+ async getWithdrawIx(vaultDepositor, oracleFeedsToCrank) {
971
1025
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
972
1026
  const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
973
1027
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
@@ -1010,7 +1064,7 @@ class VaultClient {
1010
1064
  driftProgram: this.driftClient.program.programId,
1011
1065
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1012
1066
  };
1013
- const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(txParams === null || txParams === void 0 ? void 0 : txParams.oracleFeedsToCrank);
1067
+ const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(oracleFeedsToCrank);
1014
1068
  const ixs = [
1015
1069
  ...oracleFeedsToCrankIxs,
1016
1070
  ...preIxs,
@@ -1024,11 +1078,7 @@ class VaultClient {
1024
1078
  .instruction(),
1025
1079
  ...postIxs,
1026
1080
  ];
1027
- const creationIxs = preIxs.concat(postIxs).length;
1028
- return await this.createAndSendTxn(ixs, {
1029
- cuLimit: ((_a = txParams === null || txParams === void 0 ? void 0 : txParams.cuLimit) !== null && _a !== void 0 ? _a : 650000) + (creationIxs > 0 ? 200000 : 0),
1030
- ...txParams,
1031
- });
1081
+ return ixs;
1032
1082
  }
1033
1083
  async forceWithdraw(vaultDepositor) {
1034
1084
  const ix = await this.getForceWithdrawIx(vaultDepositor);
@@ -1059,7 +1109,7 @@ class VaultClient {
1059
1109
  console.log(`Creating ATA for ${vaultDepositorAccount.authority.toBase58()} to ${userTokenAccount.toBase58()}`);
1060
1110
  }
1061
1111
  const accounts = {
1062
- manager: this.driftClient.wallet.publicKey,
1112
+ manager: vaultAccount.manager,
1063
1113
  vault: vaultDepositorAccount.vault,
1064
1114
  vaultDepositor,
1065
1115
  vaultTokenAccount: vaultAccount.tokenAccount,
@@ -1084,6 +1134,10 @@ class VaultClient {
1084
1134
  return ixs;
1085
1135
  }
1086
1136
  async cancelRequestWithdraw(vaultDepositor, txParams) {
1137
+ const ixs = await this.getCancelRequestWithdrawIx(vaultDepositor, txParams === null || txParams === void 0 ? void 0 : txParams.oracleFeedsToCrank);
1138
+ return await this.createAndSendTxn(ixs, txParams);
1139
+ }
1140
+ async getCancelRequestWithdrawIx(vaultDepositor, oracleFeedsToCrank) {
1087
1141
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
1088
1142
  const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
1089
1143
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
@@ -1097,14 +1151,16 @@ class VaultClient {
1097
1151
  const userStats = (await this.driftClient.program.account.userStats.fetch(userStatsKey));
1098
1152
  const remainingAccounts = this.getRemainingAccountsForUser([user.getUserAccount()], [], vaultAccount, userStats);
1099
1153
  if (this.cliMode) {
1100
- return await this.program.methods
1101
- .cancelRequestWithdraw()
1102
- .accounts(accounts)
1103
- .remainingAccounts(remainingAccounts)
1104
- .rpc();
1154
+ return [
1155
+ await this.program.methods
1156
+ .cancelRequestWithdraw()
1157
+ .accounts(accounts)
1158
+ .remainingAccounts(remainingAccounts)
1159
+ .instruction(),
1160
+ ];
1105
1161
  }
1106
1162
  else {
1107
- const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(txParams === null || txParams === void 0 ? void 0 : txParams.oracleFeedsToCrank);
1163
+ const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(oracleFeedsToCrank);
1108
1164
  const cancelRequestWithdrawIx = this.program.instruction.cancelRequestWithdraw({
1109
1165
  accounts: {
1110
1166
  authority: this.driftClient.wallet.publicKey,
@@ -1112,7 +1168,7 @@ class VaultClient {
1112
1168
  },
1113
1169
  remainingAccounts,
1114
1170
  });
1115
- return await this.createAndSendTxn([...oracleFeedsToCrankIxs, cancelRequestWithdrawIx], txParams);
1171
+ return [...oracleFeedsToCrankIxs, cancelRequestWithdrawIx];
1116
1172
  }
1117
1173
  }
1118
1174
  /**
@@ -1122,6 +1178,10 @@ class VaultClient {
1122
1178
  * @returns
1123
1179
  */
1124
1180
  async liquidate(vaultDepositor, txParams) {
1181
+ const ix = await this.getLiquidateIx(vaultDepositor);
1182
+ return await this.createAndSendTxn([ix], txParams);
1183
+ }
1184
+ async getLiquidateIx(vaultDepositor) {
1125
1185
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
1126
1186
  const vault = vaultDepositorAccount.vault;
1127
1187
  const vaultAccount = await this.program.account.vault.fetch(vault);
@@ -1144,17 +1204,16 @@ class VaultClient {
1144
1204
  .liquidate()
1145
1205
  .accounts(accounts)
1146
1206
  .remainingAccounts(remainingAccounts)
1147
- .rpc();
1207
+ .instruction();
1148
1208
  }
1149
1209
  else {
1150
- const liquidateIx = this.program.instruction.liquidate({
1210
+ return this.program.instruction.liquidate({
1151
1211
  accounts: {
1152
1212
  authority: this.driftClient.wallet.publicKey,
1153
1213
  ...accounts,
1154
1214
  },
1155
1215
  remainingAccounts,
1156
1216
  });
1157
- return await this.createAndSendTxn([liquidateIx], txParams);
1158
1217
  }
1159
1218
  }
1160
1219
  async createTxn(vaultIxs, txParams) {
@@ -1238,7 +1297,11 @@ class VaultClient {
1238
1297
  * @param spotMarketIndex spot market index of the insurance fund stake
1239
1298
  * @returns
1240
1299
  */
1241
- async initializeInsuranceFundStake(vault, spotMarketIndex) {
1300
+ async initializeInsuranceFundStake(vault, spotMarketIndex, txParams) {
1301
+ const ixs = await this.getInitializeInsuranceFundStakeIx(vault, spotMarketIndex);
1302
+ return await this.createAndSendTxn([ixs], txParams);
1303
+ }
1304
+ async getInitializeInsuranceFundStakeIx(vault, spotMarketIndex) {
1242
1305
  const vaultAccount = await this.program.account.vault.fetch(vault);
1243
1306
  const ifStakeAccountPublicKey = (0, sdk_1.getInsuranceFundStakeAccountPublicKey)(this.driftClient.program.programId, vault, spotMarketIndex);
1244
1307
  const spotMarket = this.driftClient.getSpotMarketAccount(spotMarketIndex);
@@ -1258,7 +1321,7 @@ class VaultClient {
1258
1321
  driftState: await this.driftClient.getStatePublicKey(),
1259
1322
  driftProgram: this.driftClient.program.programId,
1260
1323
  })
1261
- .rpc();
1324
+ .instruction();
1262
1325
  }
1263
1326
  /**
1264
1327
  * Adds an amount to an insurance fund stake for the vault.
@@ -1267,7 +1330,11 @@ class VaultClient {
1267
1330
  * @param amount amount to add to the insurance fund stake, in spotMarketIndex precision
1268
1331
  * @returns
1269
1332
  */
1270
- async addToInsuranceFundStake(vault, spotMarketIndex, amount, managerTokenAccount) {
1333
+ async addToInsuranceFundStake(vault, spotMarketIndex, amount, managerTokenAccount, txParams) {
1334
+ const ixs = await this.getAddToInsuranceFundStakeIx(vault, spotMarketIndex, amount, managerTokenAccount);
1335
+ return await this.createAndSendTxn([ixs], txParams);
1336
+ }
1337
+ async getAddToInsuranceFundStakeIx(vault, spotMarketIndex, amount, managerTokenAccount) {
1271
1338
  const vaultAccount = await this.program.account.vault.fetch(vault);
1272
1339
  if (!vaultAccount.manager.equals(this.driftClient.wallet.publicKey)) {
1273
1340
  throw new Error(`Only the manager of the vault can add to the insurance fund stake.`);
@@ -1279,7 +1346,7 @@ class VaultClient {
1279
1346
  throw new Error(`Spot market ${spotMarketIndex} not found on driftClient`);
1280
1347
  }
1281
1348
  if (!managerTokenAccount) {
1282
- managerTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey);
1349
+ managerTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, vaultAccount.manager, true);
1283
1350
  }
1284
1351
  const ifVaultTokenAccount = (0, addresses_1.getInsuranceFundTokenVaultAddressSync)(this.program.programId, vault, spotMarketIndex);
1285
1352
  return await this.program.methods
@@ -1298,9 +1365,13 @@ class VaultClient {
1298
1365
  driftSigner: this.driftClient.getStateAccount().signer,
1299
1366
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1300
1367
  })
1301
- .rpc();
1368
+ .instruction();
1369
+ }
1370
+ async requestRemoveInsuranceFundStake(vault, spotMarketIndex, amount, txParams) {
1371
+ const ix = await this.getRequestRemoveInsuranceFundStakeIx(vault, spotMarketIndex, amount);
1372
+ return await this.createAndSendTxn([ix], txParams);
1302
1373
  }
1303
- async requestRemoveInsuranceFundStake(vault, spotMarketIndex, amount) {
1374
+ async getRequestRemoveInsuranceFundStakeIx(vault, spotMarketIndex, amount) {
1304
1375
  const vaultAccount = await this.program.account.vault.fetch(vault);
1305
1376
  const ifStakeAccountPublicKey = (0, sdk_1.getInsuranceFundStakeAccountPublicKey)(this.driftClient.program.programId, vault, spotMarketIndex);
1306
1377
  const ifVaultPublicKey = await (0, sdk_1.getInsuranceFundVaultPublicKey)(this.driftClient.program.programId, spotMarketIndex);
@@ -1312,16 +1383,20 @@ class VaultClient {
1312
1383
  .requestRemoveInsuranceFundStake(spotMarketIndex, amount)
1313
1384
  .accounts({
1314
1385
  vault,
1315
- manager: this.driftClient.wallet.publicKey,
1386
+ manager: vaultAccount.manager,
1316
1387
  driftSpotMarket: spotMarket.pubkey,
1317
1388
  insuranceFundStake: ifStakeAccountPublicKey,
1318
1389
  insuranceFundVault: ifVaultPublicKey,
1319
1390
  driftUserStats: vaultAccount.userStats,
1320
1391
  driftProgram: this.driftClient.program.programId,
1321
1392
  })
1322
- .rpc();
1393
+ .instruction();
1394
+ }
1395
+ async cancelRequestRemoveInsuranceFundStake(vault, spotMarketIndex, txParams) {
1396
+ const ix = await this.getCancelRequestRemoveInsuranceFundStakeIx(vault, spotMarketIndex);
1397
+ return await this.createAndSendTxn([ix], txParams);
1323
1398
  }
1324
- async cancelRequestRemoveInsuranceFundStake(vault, spotMarketIndex) {
1399
+ async getCancelRequestRemoveInsuranceFundStakeIx(vault, spotMarketIndex) {
1325
1400
  const vaultAccount = await this.program.account.vault.fetch(vault);
1326
1401
  const ifStakeAccountPublicKey = (0, sdk_1.getInsuranceFundStakeAccountPublicKey)(this.driftClient.program.programId, vault, spotMarketIndex);
1327
1402
  const ifVaultPublicKey = await (0, sdk_1.getInsuranceFundVaultPublicKey)(this.driftClient.program.programId, spotMarketIndex);
@@ -1333,16 +1408,20 @@ class VaultClient {
1333
1408
  .cancelRequestRemoveInsuranceFundStake(spotMarketIndex)
1334
1409
  .accounts({
1335
1410
  vault: vault,
1336
- manager: this.driftClient.wallet.publicKey,
1411
+ manager: vaultAccount.manager,
1337
1412
  driftSpotMarket: spotMarket.pubkey,
1338
1413
  insuranceFundStake: ifStakeAccountPublicKey,
1339
1414
  insuranceFundVault: ifVaultPublicKey,
1340
1415
  driftUserStats: vaultAccount.userStats,
1341
1416
  driftProgram: this.driftClient.program.programId,
1342
1417
  })
1343
- .rpc();
1418
+ .instruction();
1419
+ }
1420
+ async removeInsuranceFundStake(vault, spotMarketIndex, managerTokenAccount, txParams) {
1421
+ const ixs = await this.getRemoveInsuranceFundStakeIx(vault, spotMarketIndex, managerTokenAccount);
1422
+ return await this.createAndSendTxn([ixs], txParams);
1344
1423
  }
1345
- async removeInsuranceFundStake(vault, spotMarketIndex, managerTokenAccount) {
1424
+ async getRemoveInsuranceFundStakeIx(vault, spotMarketIndex, managerTokenAccount) {
1346
1425
  const vaultAccount = await this.program.account.vault.fetch(vault);
1347
1426
  const ifStakeAccountPublicKey = (0, sdk_1.getInsuranceFundStakeAccountPublicKey)(this.driftClient.program.programId, vault, spotMarketIndex);
1348
1427
  const ifVaultPublicKey = await (0, sdk_1.getInsuranceFundVaultPublicKey)(this.driftClient.program.programId, spotMarketIndex);
@@ -1351,7 +1430,7 @@ class VaultClient {
1351
1430
  throw new Error(`Spot market ${spotMarketIndex} not found on driftClient`);
1352
1431
  }
1353
1432
  if (!managerTokenAccount) {
1354
- managerTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey);
1433
+ managerTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, vaultAccount.manager, true);
1355
1434
  }
1356
1435
  const ifVaultTokenAccount = (0, addresses_1.getInsuranceFundTokenVaultAddressSync)(this.program.programId, vault, spotMarketIndex);
1357
1436
  return await this.program.methods
@@ -1369,9 +1448,13 @@ class VaultClient {
1369
1448
  driftProgram: this.driftClient.program.programId,
1370
1449
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1371
1450
  })
1372
- .rpc();
1451
+ .instruction();
1373
1452
  }
1374
- async protocolRequestWithdraw(vault, amount, withdrawUnit) {
1453
+ async protocolRequestWithdraw(vault, amount, withdrawUnit, txParams) {
1454
+ const ix = await this.getProtocolRequestWithdrawIx(vault, amount, withdrawUnit);
1455
+ return await this.createAndSendTxn([ix], txParams);
1456
+ }
1457
+ async getProtocolRequestWithdrawIx(vault, amount, withdrawUnit) {
1375
1458
  // @ts-ignore
1376
1459
  const vaultAccount = (await this.program.account.vault.fetch(vault));
1377
1460
  const vp = this.getVaultProtocolAddress(vault);
@@ -1394,26 +1477,30 @@ class VaultClient {
1394
1477
  .managerRequestWithdraw(amount, withdrawUnit)
1395
1478
  .accounts(accounts)
1396
1479
  .remainingAccounts(remainingAccounts)
1397
- .rpc();
1480
+ .instruction();
1398
1481
  }
1399
1482
  else {
1400
1483
  const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
1401
1484
  // @ts-ignore
1402
1485
  amount, withdrawUnit, {
1403
1486
  accounts: {
1404
- manager: this.driftClient.wallet.publicKey,
1487
+ manager: vaultAccount.manager,
1405
1488
  ...accounts,
1406
1489
  },
1407
1490
  remainingAccounts,
1408
1491
  });
1409
- return await this.createAndSendTxn([requestWithdrawIx]);
1492
+ return requestWithdrawIx;
1410
1493
  }
1411
1494
  }
1412
- async protocolCancelWithdrawRequest(vault) {
1495
+ async protocolCancelWithdrawRequest(vault, txParams) {
1496
+ const ixs = await this.getProtocolCancelWithdrawRequestIx(vault);
1497
+ return await this.createAndSendTxn(ixs, txParams);
1498
+ }
1499
+ async getProtocolCancelWithdrawRequestIx(vault) {
1413
1500
  const vaultAccount = await this.program.account.vault.fetch(vault);
1414
1501
  const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
1415
1502
  const accounts = {
1416
- manager: this.driftClient.wallet.publicKey,
1503
+ manager: vaultAccount.manager,
1417
1504
  vault,
1418
1505
  driftUserStats: userStatsKey,
1419
1506
  driftUser: vaultAccount.user,
@@ -1422,24 +1509,30 @@ class VaultClient {
1422
1509
  const userStats = (await this.driftClient.program.account.userStats.fetch(userStatsKey));
1423
1510
  const remainingAccounts = this.getRemainingAccountsForUser([user.getUserAccount()], [], vaultAccount, userStats);
1424
1511
  if (this.cliMode) {
1425
- return await this.program.methods
1426
- .mangerCancelWithdrawRequest()
1427
- .accounts(accounts)
1428
- .remainingAccounts(remainingAccounts)
1429
- .rpc();
1512
+ return [
1513
+ await this.program.methods
1514
+ .mangerCancelWithdrawRequest()
1515
+ .accounts(accounts)
1516
+ .remainingAccounts(remainingAccounts)
1517
+ .instruction(),
1518
+ ];
1430
1519
  }
1431
1520
  else {
1432
1521
  const cancelRequestWithdrawIx = this.program.instruction.mangerCancelWithdrawRequest({
1433
1522
  accounts: {
1434
1523
  ...accounts,
1435
- manager: this.driftClient.wallet.publicKey,
1524
+ manager: vaultAccount.manager,
1436
1525
  },
1437
1526
  remainingAccounts,
1438
1527
  });
1439
- return await this.createAndSendTxn([cancelRequestWithdrawIx]);
1528
+ return [cancelRequestWithdrawIx];
1440
1529
  }
1441
1530
  }
1442
- async protocolWithdraw(vault) {
1531
+ async protocolWithdraw(vault, txParams) {
1532
+ const ixs = await this.getProtocolWithdrawIx(vault);
1533
+ return await this.createAndSendTxn(ixs, txParams);
1534
+ }
1535
+ async getProtocolWithdrawIx(vault) {
1443
1536
  const vaultAccount = await this.program.account.vault.fetch(vault);
1444
1537
  if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
1445
1538
  throw new Error(`Only the manager of the vault can request a withdraw.`);
@@ -1455,7 +1548,7 @@ class VaultClient {
1455
1548
  const ix = this.program.instruction.managerWithdraw({
1456
1549
  accounts: {
1457
1550
  vault,
1458
- manager: this.driftClient.wallet.publicKey,
1551
+ manager: vaultAccount.manager,
1459
1552
  vaultTokenAccount: vaultAccount.tokenAccount,
1460
1553
  driftUser: await (0, sdk_1.getUserAccountPublicKey)(this.driftClient.program.programId, vault),
1461
1554
  driftProgram: this.driftClient.program.programId,
@@ -1468,9 +1561,7 @@ class VaultClient {
1468
1561
  },
1469
1562
  remainingAccounts,
1470
1563
  });
1471
- return this.createAndSendTxn([ix], {
1472
- cuLimit: 1000000,
1473
- });
1564
+ return [ix];
1474
1565
  }
1475
1566
  async getOracleFeedsToCrank(oracleFeedsToCrank) {
1476
1567
  const oracleFeedsToCrankIxs = oracleFeedsToCrank
@@ -1485,14 +1576,17 @@ class VaultClient {
1485
1576
  return oracleFeedsToCrankIxs;
1486
1577
  }
1487
1578
  async updateVaultProtocol(vault, params, txParams) {
1488
- const ix = await this.program.methods
1579
+ const ix = await this.getUpdateVaultProtocolIx(vault, params);
1580
+ return await this.createAndSendTxn([ix], txParams);
1581
+ }
1582
+ async getUpdateVaultProtocolIx(vault, params) {
1583
+ return this.program.methods
1489
1584
  .updateVaultProtocol(params)
1490
1585
  .accounts({
1491
1586
  vault,
1492
1587
  vaultProtocol: this.getVaultProtocolAddress(vault),
1493
1588
  })
1494
1589
  .instruction();
1495
- return await this.createAndSendTxn([ix], txParams);
1496
1590
  }
1497
1591
  async updateCumulativeFuelAmount(vaultDepositor, txParams) {
1498
1592
  return await this.createAndSendTxn([await this.getUpdateCumulativeFuelAmountIx(vaultDepositor)], txParams);
@@ -1567,11 +1661,12 @@ class VaultClient {
1567
1661
  ], txParams);
1568
1662
  }
1569
1663
  async getManagerUpdateFuelDistributionModeIx(vault, fuelDistributionMode) {
1664
+ const vaultAccount = await this.program.account.vault.fetch(vault);
1570
1665
  return this.program.methods
1571
1666
  .managerUpdateFuelDistributionMode(fuelDistributionMode)
1572
1667
  .accounts({
1573
1668
  vault,
1574
- manager: this.driftClient.wallet.publicKey,
1669
+ manager: vaultAccount.manager,
1575
1670
  })
1576
1671
  .instruction();
1577
1672
  }