@drift-labs/vaults-sdk 0.4.32 → 0.4.33

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.
@@ -13,6 +13,11 @@ import {
13
13
  OracleSource,
14
14
  WRAPPED_SOL_MINT,
15
15
  SpotMarketAccount,
16
+ UserAccount,
17
+ UserStatsAccount,
18
+ FuelOverflowStatus,
19
+ getFuelOverflowAccountPublicKey,
20
+ FUEL_RESET_LOG_ACCOUNT,
16
21
  } from '@drift-labs/sdk';
17
22
  import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor';
18
23
  import { DriftVaults } from './types/drift_vaults';
@@ -32,7 +37,6 @@ import {
32
37
  PublicKey,
33
38
  SystemProgram,
34
39
  SYSVAR_RENT_PUBKEY,
35
- Transaction,
36
40
  TransactionInstruction,
37
41
  TransactionSignature,
38
42
  VersionedTransaction,
@@ -45,6 +49,7 @@ import {
45
49
  TOKEN_PROGRAM_ID,
46
50
  } from '@solana/spl-token';
47
51
  import {
52
+ FuelDistributionMode,
48
53
  Vault,
49
54
  VaultDepositor,
50
55
  VaultParams,
@@ -65,6 +70,7 @@ export type TxParams = {
65
70
  simulateTransaction?: boolean;
66
71
  lookupTables?: AddressLookupTableAccount[];
67
72
  oracleFeedsToCrank?: { feed: PublicKey; oracleSource: OracleSource }[];
73
+ noLut?: boolean;
68
74
  };
69
75
 
70
76
  export class VaultClient {
@@ -82,12 +88,14 @@ export class VaultClient {
82
88
  driftClient,
83
89
  program,
84
90
  metaplex,
91
+ // @deprecated, no longer used
85
92
  cliMode,
86
93
  userMapConfig,
87
94
  }: {
88
95
  driftClient: DriftClient;
89
96
  program: Program<DriftVaults>;
90
97
  metaplex?: Metaplex;
98
+ // @deprecated, no longer used
91
99
  cliMode?: boolean;
92
100
  userMapConfig?: UserMapConfig;
93
101
  }) {
@@ -110,11 +118,53 @@ export class VaultClient {
110
118
  }
111
119
  }
112
120
 
121
+ private getRemainingAccountsForUser(
122
+ userAccounts: UserAccount[],
123
+ writableSpotMarketIndexes: number[],
124
+ vaultAccount: Vault,
125
+ userStats: UserStatsAccount
126
+ ) {
127
+ const remainingAccounts = this.driftClient.getRemainingAccounts({
128
+ userAccounts,
129
+ writableSpotMarketIndexes,
130
+ });
131
+
132
+ const hasVaultProtocol = vaultAccount.vaultProtocol === true;
133
+ const hasFuelOverflow =
134
+ (userStats.fuelOverflowStatus & FuelOverflowStatus.Exists) ===
135
+ FuelOverflowStatus.Exists;
136
+
137
+ if (hasFuelOverflow) {
138
+ const fuelOverflow = getFuelOverflowAccountPublicKey(
139
+ this.driftClient.program.programId,
140
+ vaultAccount.pubkey
141
+ );
142
+ remainingAccounts.push({
143
+ pubkey: fuelOverflow,
144
+ isSigner: false,
145
+ isWritable: false,
146
+ });
147
+ }
148
+
149
+ if (hasVaultProtocol) {
150
+ const vaultProtocol = this.getVaultProtocolAddress(vaultAccount.pubkey);
151
+ remainingAccounts.push({
152
+ pubkey: vaultProtocol,
153
+ isSigner: false,
154
+ isWritable: true,
155
+ });
156
+ }
157
+
158
+ return remainingAccounts;
159
+ }
160
+
113
161
  /**
114
162
  * Unsubscribes from the vault users map. Call this to clean up any dangling promises.
115
163
  */
116
164
  public async unsubscribe() {
117
- await this.vaultUsers.unsubscribe();
165
+ if (this.vaultUsers) {
166
+ await this.vaultUsers.unsubscribe();
167
+ }
118
168
  }
119
169
 
120
170
  public async getVault(vault: PublicKey): Promise<Vault> {
@@ -204,7 +254,7 @@ export class VaultClient {
204
254
  }
205
255
 
206
256
  public async getAllVaultDepositors(
207
- vault: PublicKey
257
+ vault?: PublicKey
208
258
  ): Promise<ProgramAccount<VaultDepositor>[]> {
209
259
  const filters = [
210
260
  {
@@ -216,14 +266,16 @@ export class VaultClient {
216
266
  ),
217
267
  },
218
268
  },
219
- {
269
+ ];
270
+ if (vault) {
271
+ filters.push({
220
272
  // vault = vault
221
273
  memcmp: {
222
274
  offset: 8,
223
275
  bytes: vault.toBase58(),
224
276
  },
225
- },
226
- ];
277
+ });
278
+ }
227
279
  // @ts-ignore
228
280
  return (await this.program.account.vaultDepositor.all(
229
281
  filters
@@ -236,6 +288,13 @@ export class VaultClient {
236
288
  });
237
289
  }
238
290
 
291
+ /// useful for syncing state during tests.
292
+ public async syncVaultUsers() {
293
+ for (const user of this.vaultUsers.values()) {
294
+ await user.fetchAccounts();
295
+ }
296
+ }
297
+
239
298
  /**
240
299
  *
241
300
  * @param vault pubkey
@@ -509,15 +568,6 @@ export class VaultClient {
509
568
  driftProgram: this.driftClient.program.programId,
510
569
  };
511
570
 
512
- const preIxs = [
513
- ComputeBudgetProgram.setComputeUnitLimit({
514
- units: 400_000,
515
- }),
516
- ComputeBudgetProgram.setComputeUnitPrice({
517
- microLamports: 300_000,
518
- }),
519
- ];
520
-
521
571
  if (vaultProtocolParams) {
522
572
  const vaultProtocol = this.getVaultProtocolAddress(
523
573
  getVaultAddressSync(this.program.programId, params.name)
@@ -527,51 +577,32 @@ export class VaultClient {
527
577
  vaultProtocol: vaultProtocolParams,
528
578
  };
529
579
 
530
- if (this.cliMode) {
531
- return await this.program.methods
532
- .initializeVaultWithProtocol(_params)
533
- .preInstructions(preIxs)
534
- .accounts({
535
- ...accounts,
536
- vaultProtocol,
537
- })
538
- .rpc();
539
- } else {
540
- const uiAuthority = this.driftClient.wallet.publicKey;
541
- const initializeVaultWithProtocolIx = await this.program.methods
542
- .initializeVaultWithProtocol(_params)
543
- .accounts({
544
- ...accounts,
545
- vaultProtocol,
546
- payer: uiAuthority,
547
- manager: uiAuthority,
548
- })
549
- .instruction();
550
- const ixs = [...preIxs, initializeVaultWithProtocolIx];
551
- return await this.createAndSendTxn(ixs, uiTxParams);
552
- }
580
+ const uiAuthority = this.driftClient.wallet.publicKey;
581
+ const initializeVaultWithProtocolIx = await this.program.methods
582
+ .initializeVaultWithProtocol(_params)
583
+ .accounts({
584
+ ...accounts,
585
+ vaultProtocol,
586
+ payer: uiAuthority,
587
+ manager: uiAuthority,
588
+ })
589
+ .instruction();
590
+ const ixs = [initializeVaultWithProtocolIx];
591
+ return await this.createAndSendTxn(ixs, uiTxParams);
553
592
  } else {
554
593
  const _params: VaultParams = vaultParams;
555
594
 
556
- if (this.cliMode) {
557
- return await this.program.methods
558
- .initializeVault(_params)
559
- .preInstructions(preIxs)
560
- .accounts(accounts)
561
- .rpc();
562
- } else {
563
- const uiAuthority = this.driftClient.wallet.publicKey;
564
- const initializeVaultIx = await this.program.methods
565
- .initializeVault(_params)
566
- .accounts({
567
- ...accounts,
568
- payer: uiAuthority,
569
- manager: uiAuthority,
570
- })
571
- .instruction();
572
- const ixs = [initializeVaultIx];
573
- return await this.createAndSendTxn(ixs, uiTxParams);
574
- }
595
+ const uiAuthority = this.driftClient.wallet.publicKey;
596
+ const initializeVaultIx = await this.program.methods
597
+ .initializeVault(_params)
598
+ .accounts({
599
+ ...accounts,
600
+ payer: uiAuthority,
601
+ manager: uiAuthority,
602
+ })
603
+ .instruction();
604
+ const ixs = [initializeVaultIx];
605
+ return await this.createAndSendTxn(ixs, uiTxParams);
575
606
  }
576
607
  }
577
608
 
@@ -594,26 +625,11 @@ export class VaultClient {
594
625
  driftProgram: this.driftClient.program.programId,
595
626
  };
596
627
 
597
- if (this.cliMode) {
598
- return await this.program.methods
599
- .updateDelegate(delegate)
600
- .preInstructions([
601
- ComputeBudgetProgram.setComputeUnitLimit({
602
- units: 400_000,
603
- }),
604
- ComputeBudgetProgram.setComputeUnitPrice({
605
- microLamports: 300_000,
606
- }),
607
- ])
608
- .accounts(accounts)
609
- .rpc();
610
- } else {
611
- const updateDelegateIx = await this.program.methods
612
- .updateDelegate(delegate)
613
- .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
614
- .instruction();
615
- return await this.createAndSendTxn([updateDelegateIx], uiTxParams);
616
- }
628
+ const updateDelegateIx = await this.program.methods
629
+ .updateDelegate(delegate)
630
+ .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
631
+ .instruction();
632
+ return await this.createAndSendTxn([updateDelegateIx], uiTxParams);
617
633
  }
618
634
 
619
635
  /**
@@ -636,32 +652,37 @@ export class VaultClient {
636
652
 
637
653
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
638
654
 
639
- let remainingAccounts: AccountMeta[] = [];
655
+ const remainingAccounts: AccountMeta[] = [];
640
656
  try {
641
- remainingAccounts = this.driftClient.getRemainingAccounts({
642
- userAccounts: [user.getUserAccount()],
643
- });
657
+ const userStatsKey = getUserStatsAccountPublicKey(
658
+ this.driftClient.program.programId,
659
+ vault
660
+ );
661
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
662
+ userStatsKey
663
+ )) as UserStatsAccount;
664
+ remainingAccounts.push(
665
+ ...this.getRemainingAccountsForUser(
666
+ [user.getUserAccount()],
667
+ [],
668
+ vaultAccount,
669
+ userStats
670
+ )
671
+ );
644
672
  } catch (err) {
673
+ console.error('failed to get remaining accounts', err);
645
674
  // do nothing
646
675
  }
647
676
 
648
- if (this.cliMode) {
649
- return await this.program.methods
650
- .updateMarginTradingEnabled(enabled)
651
- .accounts(accounts)
652
- .remainingAccounts(remainingAccounts)
653
- .rpc();
654
- } else {
655
- const updateMarginTradingEnabledIx = await this.program.methods
656
- .updateMarginTradingEnabled(enabled)
657
- .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
658
- .remainingAccounts(remainingAccounts)
659
- .instruction();
660
- return await this.createAndSendTxn(
661
- [updateMarginTradingEnabledIx],
662
- uiTxParams
663
- );
664
- }
677
+ const updateMarginTradingEnabledIx = await this.program.methods
678
+ .updateMarginTradingEnabled(enabled)
679
+ .accounts({ ...accounts, manager: this.driftClient.wallet.publicKey })
680
+ .remainingAccounts(remainingAccounts)
681
+ .instruction();
682
+ return await this.createAndSendTxn(
683
+ [updateMarginTradingEnabledIx],
684
+ uiTxParams
685
+ );
665
686
  }
666
687
 
667
688
  private async handleWSolMovement(
@@ -716,19 +737,19 @@ export class VaultClient {
716
737
  }
717
738
 
718
739
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
719
-
720
- const remainingAccounts = this.driftClient.getRemainingAccounts({
721
- userAccounts: [user.getUserAccount()],
722
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
723
- });
724
- if (vaultAccount.vaultProtocol) {
725
- const vaultProtocol = this.getVaultProtocolAddress(vault);
726
- remainingAccounts.push({
727
- pubkey: vaultProtocol,
728
- isSigner: false,
729
- isWritable: true,
730
- });
731
- }
740
+ const userStatsKey = getUserStatsAccountPublicKey(
741
+ this.driftClient.program.programId,
742
+ vault
743
+ );
744
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
745
+ userStatsKey
746
+ )) as UserStatsAccount;
747
+ const remainingAccounts = this.getRemainingAccountsForUser(
748
+ [user.getUserAccount()],
749
+ [vaultAccount.spotMarketIndex],
750
+ vaultAccount,
751
+ userStats
752
+ );
732
753
 
733
754
  const accounts = {
734
755
  vault,
@@ -759,29 +780,19 @@ export class VaultClient {
759
780
  accounts.userTokenAccount
760
781
  );
761
782
 
762
- if (this.cliMode) {
763
- return await this.program.methods
764
- .managerDeposit(amount)
765
- .accounts({ ...accounts, userTokenAccount })
766
- .remainingAccounts(remainingAccounts)
767
- .preInstructions(preIxs)
768
- .postInstructions(postIxs)
769
- .rpc();
770
- } else {
771
- const managerDepositIx = await this.program.methods
772
- .managerDeposit(amount)
773
- .accounts({
774
- ...accounts,
775
- userTokenAccount,
776
- manager: this.driftClient.wallet.publicKey,
777
- })
778
- .remainingAccounts(remainingAccounts)
779
- .instruction();
780
- return await this.createAndSendTxn(
781
- [...preIxs, managerDepositIx, ...postIxs],
782
- uiTxParams
783
- );
784
- }
783
+ const managerDepositIx = await this.program.methods
784
+ .managerDeposit(amount)
785
+ .accounts({
786
+ ...accounts,
787
+ userTokenAccount,
788
+ manager: this.driftClient.wallet.publicKey,
789
+ })
790
+ .remainingAccounts(remainingAccounts)
791
+ .instruction();
792
+ return await this.createAndSendTxn(
793
+ [...preIxs, managerDepositIx, ...postIxs],
794
+ uiTxParams
795
+ );
785
796
  }
786
797
 
787
798
  public async managerRequestWithdraw(
@@ -801,23 +812,19 @@ export class VaultClient {
801
812
  }
802
813
 
803
814
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
804
- const remainingAccounts = this.driftClient.getRemainingAccounts({
805
- userAccounts: [user.getUserAccount()],
806
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
807
- });
808
- if (vaultAccount.vaultProtocol) {
809
- const vaultProtocol = this.getVaultProtocolAddress(vault);
810
- remainingAccounts.push({
811
- pubkey: vaultProtocol,
812
- isSigner: false,
813
- isWritable: true,
814
- });
815
- }
816
-
817
815
  const userStatsKey = getUserStatsAccountPublicKey(
818
816
  this.driftClient.program.programId,
819
817
  vault
820
818
  );
819
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
820
+ userStatsKey
821
+ )) as UserStatsAccount;
822
+ const remainingAccounts = this.getRemainingAccountsForUser(
823
+ [user.getUserAccount()],
824
+ [vaultAccount.spotMarketIndex],
825
+ vaultAccount,
826
+ userStats
827
+ );
821
828
 
822
829
  const accounts = {
823
830
  vault,
@@ -825,29 +832,20 @@ export class VaultClient {
825
832
  driftUserStats: userStatsKey,
826
833
  };
827
834
 
828
- if (this.cliMode) {
829
- return await this.program.methods
830
- // @ts-ignore, 0.29.0 anchor issues..
831
- .managerRequestWithdraw(amount, withdrawUnit)
832
- .accounts(accounts)
833
- .remainingAccounts(remainingAccounts)
834
- .rpc();
835
- } else {
836
- const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
837
- // @ts-ignore
838
- amount,
839
- withdrawUnit,
840
- {
841
- accounts: {
842
- manager: this.driftClient.wallet.publicKey,
843
- ...accounts,
844
- },
845
- remainingAccounts,
846
- }
847
- );
835
+ const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
836
+ // @ts-ignore
837
+ amount,
838
+ withdrawUnit,
839
+ {
840
+ accounts: {
841
+ manager: this.driftClient.wallet.publicKey,
842
+ ...accounts,
843
+ },
844
+ remainingAccounts,
845
+ }
846
+ );
848
847
 
849
- return await this.createAndSendTxn([requestWithdrawIx], uiTxParams);
850
- }
848
+ return await this.createAndSendTxn([requestWithdrawIx], uiTxParams);
851
849
  }
852
850
 
853
851
  public async managerCancelWithdrawRequest(
@@ -869,40 +867,23 @@ export class VaultClient {
869
867
  };
870
868
 
871
869
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
872
- const remainingAccounts = this.driftClient.getRemainingAccounts({
873
- userAccounts: [user.getUserAccount()],
874
- });
875
- if (vaultAccount.vaultProtocol) {
876
- const vaultProtocol = this.getVaultProtocolAddress(vault);
877
- remainingAccounts.push({
878
- pubkey: vaultProtocol,
879
- isSigner: false,
880
- isWritable: true,
881
- });
882
- }
870
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
871
+ userStatsKey
872
+ )) as UserStatsAccount;
873
+ const remainingAccounts = this.getRemainingAccountsForUser(
874
+ [user.getUserAccount()],
875
+ [],
876
+ vaultAccount,
877
+ userStats
878
+ );
883
879
 
884
- if (this.cliMode) {
885
- return await this.program.methods
886
- .mangerCancelWithdrawRequest()
887
- .accounts(accounts)
888
- .remainingAccounts(remainingAccounts)
889
- .rpc();
890
- } else {
891
- const cancelRequestWithdrawIx =
892
- this.program.instruction.mangerCancelWithdrawRequest({
893
- accounts: {
894
- ...accounts,
895
- driftUserStats: getUserStatsAccountPublicKey(
896
- this.driftClient.program.programId,
897
- vault
898
- ),
899
- manager: this.driftClient.wallet.publicKey,
900
- },
901
- remainingAccounts,
902
- });
880
+ const cancelRequestWithdrawIx =
881
+ this.program.instruction.mangerCancelWithdrawRequest({
882
+ accounts,
883
+ remainingAccounts,
884
+ });
903
885
 
904
- return await this.createAndSendTxn([cancelRequestWithdrawIx], uiTxParams);
905
- }
886
+ return await this.createAndSendTxn([cancelRequestWithdrawIx], uiTxParams);
906
887
  }
907
888
 
908
889
  public async managerWithdraw(
@@ -916,19 +897,19 @@ export class VaultClient {
916
897
  }
917
898
 
918
899
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
919
-
920
- const remainingAccounts = this.driftClient.getRemainingAccounts({
921
- userAccounts: [user.getUserAccount()],
922
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
923
- });
924
- if (vaultAccount.vaultProtocol) {
925
- const vaultProtocol = this.getVaultProtocolAddress(vault);
926
- remainingAccounts.push({
927
- pubkey: vaultProtocol,
928
- isSigner: false,
929
- isWritable: true,
930
- });
931
- }
900
+ const userStatsKey = getUserStatsAccountPublicKey(
901
+ this.driftClient.program.programId,
902
+ vault
903
+ );
904
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
905
+ userStatsKey
906
+ )) as UserStatsAccount;
907
+ const remainingAccounts = this.getRemainingAccountsForUser(
908
+ [user.getUserAccount()],
909
+ [vaultAccount.spotMarketIndex],
910
+ vaultAccount,
911
+ userStats
912
+ );
932
913
 
933
914
  const spotMarket = this.driftClient.getSpotMarketAccount(
934
915
  vaultAccount.spotMarketIndex
@@ -986,14 +967,7 @@ export class VaultClient {
986
967
  manager: this.driftClient.wallet.publicKey,
987
968
  },
988
969
  });
989
- if (this.cliMode) {
990
- return this.createAndSendTxn([ix], {
991
- cuLimit: 600_000,
992
- cuPriceMicroLamports: 10_000,
993
- });
994
- } else {
995
- return this.createAndSendTxn([ix], uiTxParams);
996
- }
970
+ return this.createAndSendTxn([ix], uiTxParams);
997
971
  }
998
972
 
999
973
  public async getApplyProfitShareIx(
@@ -1013,10 +987,19 @@ export class VaultClient {
1013
987
  );
1014
988
  }
1015
989
 
1016
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1017
- userAccounts: [user.getUserAccount()],
1018
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1019
- });
990
+ const userStatsKey = getUserStatsAccountPublicKey(
991
+ this.driftClient.program.programId,
992
+ vault
993
+ );
994
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
995
+ userStatsKey
996
+ )) as UserStatsAccount;
997
+ const remainingAccounts = this.getRemainingAccountsForUser(
998
+ [user.getUserAccount()],
999
+ [vaultAccount.spotMarketIndex],
1000
+ vaultAccount,
1001
+ userStats
1002
+ );
1020
1003
 
1021
1004
  const accounts = {
1022
1005
  vault,
@@ -1060,10 +1043,19 @@ export class VaultClient {
1060
1043
  );
1061
1044
  }
1062
1045
 
1063
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1064
- userAccounts: [user.getUserAccount()],
1065
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1066
- });
1046
+ const userStatsKey = getUserStatsAccountPublicKey(
1047
+ this.driftClient.program.programId,
1048
+ vault
1049
+ );
1050
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1051
+ userStatsKey
1052
+ )) as UserStatsAccount;
1053
+ const remainingAccounts = this.getRemainingAccountsForUser(
1054
+ [user.getUserAccount()],
1055
+ [vaultAccount.spotMarketIndex],
1056
+ vaultAccount,
1057
+ userStats
1058
+ );
1067
1059
 
1068
1060
  const accounts = {
1069
1061
  vault,
@@ -1111,10 +1103,19 @@ export class VaultClient {
1111
1103
  );
1112
1104
  }
1113
1105
 
1114
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1115
- userAccounts: [user.getUserAccount()],
1116
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1117
- });
1106
+ const userStatsKey = getUserStatsAccountPublicKey(
1107
+ this.driftClient.program.programId,
1108
+ vault
1109
+ );
1110
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1111
+ userStatsKey
1112
+ )) as UserStatsAccount;
1113
+ const remainingAccounts = this.getRemainingAccountsForUser(
1114
+ [user.getUserAccount()],
1115
+ [vaultAccount.spotMarketIndex],
1116
+ vaultAccount,
1117
+ userStats
1118
+ );
1118
1119
 
1119
1120
  const accounts = {
1120
1121
  vault,
@@ -1189,42 +1190,21 @@ export class VaultClient {
1189
1190
  payer?: PublicKey,
1190
1191
  uiTxParams?: TxParams
1191
1192
  ): Promise<TransactionSignature> {
1192
- const vaultDepositor = getVaultDepositorAddressSync(
1193
- this.program.programId,
1194
- vault,
1195
- authority || this.driftClient.wallet.publicKey
1196
- );
1197
-
1198
- const accounts = {
1199
- vaultDepositor,
1200
- vault,
1201
- authority: authority || this.driftClient.wallet.publicKey,
1202
- };
1193
+ const initIx = this.createInitVaultDepositorIx(vault, authority, payer);
1194
+ return await this.createAndSendTxn([initIx], uiTxParams);
1195
+ }
1203
1196
 
1204
- if (this.cliMode) {
1205
- return this.program.methods
1206
- .initializeVaultDepositor()
1207
- .accounts({
1208
- ...accounts,
1209
- payer: payer || authority || this.driftClient.wallet.publicKey,
1210
- rent: SYSVAR_RENT_PUBKEY,
1211
- systemProgram: SystemProgram.programId,
1212
- })
1213
- .rpc();
1214
- } else {
1215
- const initIx = this.createInitVaultDepositorIx(vault, authority, payer);
1216
- return await this.createAndSendTxn([initIx], uiTxParams);
1217
- }
1218
- }
1219
-
1220
- public async initializeTokenizedVaultDepositor(params: {
1221
- vault: PublicKey;
1222
- tokenName: string;
1223
- tokenSymbol: string;
1224
- tokenUri: string;
1225
- decimals?: number;
1226
- sharesBase?: number;
1227
- }): Promise<TransactionSignature> {
1197
+ public async initializeTokenizedVaultDepositor(
1198
+ params: {
1199
+ vault: PublicKey;
1200
+ tokenName: string;
1201
+ tokenSymbol: string;
1202
+ tokenUri: string;
1203
+ decimals?: number;
1204
+ sharesBase?: number;
1205
+ },
1206
+ uiTxParams?: TxParams
1207
+ ): Promise<TransactionSignature> {
1228
1208
  if (!this.metaplex) {
1229
1209
  throw new Error(
1230
1210
  'Metaplex instance is required when constructing VaultClient to initialize a tokenized vault depositor'
@@ -1280,24 +1260,19 @@ export class VaultClient {
1280
1260
  mintAddress
1281
1261
  );
1282
1262
 
1283
- if (!this.cliMode) {
1284
- throw new Error(
1285
- 'CLI mode is not supported for initializeTokenizedVaultDepositor'
1286
- );
1287
- }
1288
- return await this.program.methods
1289
- .initializeTokenizedVaultDepositor({
1290
- ...params,
1291
- decimals: params.decimals ?? spotMarketDecimals,
1292
- })
1293
- .preInstructions([
1294
- ComputeBudgetProgram.setComputeUnitPrice({
1295
- microLamports: 50_000,
1296
- }),
1297
- ])
1298
- .postInstructions([createAtaIx])
1299
- .accounts(accounts)
1300
- .rpc();
1263
+ return await this.createAndSendTxn(
1264
+ [
1265
+ await this.program.methods
1266
+ .initializeTokenizedVaultDepositor({
1267
+ ...params,
1268
+ decimals: params.decimals ?? spotMarketDecimals,
1269
+ })
1270
+ .accounts(accounts)
1271
+ .instruction(),
1272
+ createAtaIx,
1273
+ ],
1274
+ uiTxParams
1275
+ );
1301
1276
  }
1302
1277
 
1303
1278
  public async createTokenizeSharesIx(
@@ -1343,10 +1318,19 @@ export class VaultClient {
1343
1318
  }
1344
1319
 
1345
1320
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
1346
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1347
- userAccounts: [user.getUserAccount()],
1348
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1349
- });
1321
+ const userStatsKey = getUserStatsAccountPublicKey(
1322
+ this.driftClient.program.programId,
1323
+ vaultDepositorAccount.vault
1324
+ );
1325
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1326
+ userStatsKey
1327
+ )) as UserStatsAccount;
1328
+ const remainingAccounts = this.getRemainingAccountsForUser(
1329
+ [user.getUserAccount()],
1330
+ [vaultAccount.spotMarketIndex],
1331
+ vaultAccount,
1332
+ userStats
1333
+ );
1350
1334
 
1351
1335
  ixs.push(
1352
1336
  await this.program.methods
@@ -1387,23 +1371,7 @@ export class VaultClient {
1387
1371
  unit,
1388
1372
  mint
1389
1373
  );
1390
- if (this.cliMode) {
1391
- try {
1392
- const tx = new Transaction().add(...ixs);
1393
- const txSig = await this.driftClient.txSender.send(
1394
- tx,
1395
- undefined,
1396
- undefined,
1397
- false
1398
- );
1399
- return txSig.txSig;
1400
- } catch (e) {
1401
- console.error(e);
1402
- throw e;
1403
- }
1404
- } else {
1405
- return await this.createAndSendTxn(ixs, txParams);
1406
- }
1374
+ return await this.createAndSendTxn(ixs, txParams);
1407
1375
  }
1408
1376
 
1409
1377
  public async createRedeemTokensIx(
@@ -1436,10 +1404,19 @@ export class VaultClient {
1436
1404
  );
1437
1405
 
1438
1406
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
1439
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1440
- userAccounts: [user.getUserAccount()],
1441
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1442
- });
1407
+ const userStatsKey = getUserStatsAccountPublicKey(
1408
+ this.driftClient.program.programId,
1409
+ vaultDepositorAccount.vault
1410
+ );
1411
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1412
+ userStatsKey
1413
+ )) as UserStatsAccount;
1414
+ const remainingAccounts = this.getRemainingAccountsForUser(
1415
+ [user.getUserAccount()],
1416
+ [vaultAccount.spotMarketIndex],
1417
+ vaultAccount,
1418
+ userStats
1419
+ );
1443
1420
 
1444
1421
  return await this.program.methods
1445
1422
  .redeemTokens(tokensToBurn)
@@ -1481,23 +1458,7 @@ export class VaultClient {
1481
1458
  tokensToBurn,
1482
1459
  sharesBase
1483
1460
  );
1484
- if (this.cliMode) {
1485
- try {
1486
- const tx = new Transaction().add(ix);
1487
- const txSig = await this.driftClient.txSender.send(
1488
- tx,
1489
- undefined,
1490
- undefined,
1491
- false
1492
- );
1493
- return txSig.txSig;
1494
- } catch (e) {
1495
- console.error(e);
1496
- throw e;
1497
- }
1498
- } else {
1499
- return await this.createAndSendTxn([ix], txParams);
1500
- }
1461
+ return await this.createAndSendTxn([ix], txParams);
1501
1462
  }
1502
1463
 
1503
1464
  public async prepDepositTx(
@@ -1521,23 +1482,19 @@ export class VaultClient {
1521
1482
  const vaultAccount = await this.program.account.vault.fetch(vaultPubKey);
1522
1483
 
1523
1484
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
1524
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1525
- userAccounts: [user.getUserAccount()],
1526
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1527
- });
1528
- if (vaultAccount.vaultProtocol) {
1529
- const vaultProtocol = this.getVaultProtocolAddress(vaultPubKey);
1530
- remainingAccounts.push({
1531
- pubkey: vaultProtocol,
1532
- isSigner: false,
1533
- isWritable: true,
1534
- });
1535
- }
1536
-
1537
1485
  const userStatsKey = getUserStatsAccountPublicKey(
1538
1486
  this.driftClient.program.programId,
1539
1487
  vaultPubKey
1540
1488
  );
1489
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1490
+ userStatsKey
1491
+ )) as UserStatsAccount;
1492
+ const remainingAccounts = this.getRemainingAccountsForUser(
1493
+ [user.getUserAccount()],
1494
+ [vaultAccount.spotMarketIndex],
1495
+ vaultAccount,
1496
+ userStats
1497
+ );
1541
1498
 
1542
1499
  const driftStateKey = await this.driftClient.getStatePublicKey();
1543
1500
 
@@ -1602,10 +1559,16 @@ export class VaultClient {
1602
1559
  authority: PublicKey;
1603
1560
  vault: PublicKey;
1604
1561
  },
1605
- txParams?: TxParams
1562
+ txParams?: TxParams,
1563
+ userTokenAccount?: PublicKey
1606
1564
  ): Promise<VersionedTransaction> {
1607
1565
  const { vaultAccount, accounts, remainingAccounts, preIxs, postIxs } =
1608
- await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
1566
+ await this.prepDepositTx(
1567
+ vaultDepositor,
1568
+ amount,
1569
+ initVaultDepositor,
1570
+ userTokenAccount
1571
+ );
1609
1572
 
1610
1573
  const ixs: TransactionInstruction[] = [];
1611
1574
 
@@ -1630,7 +1593,11 @@ export class VaultClient {
1630
1593
  ixs.push(depositIx);
1631
1594
  ixs.push(...postIxs);
1632
1595
 
1633
- return await this.createTxn(ixs, txParams);
1596
+ if (txParams?.noLut ? txParams.noLut : false) {
1597
+ return await this.createTxnNoLut(ixs, txParams);
1598
+ } else {
1599
+ return await this.createTxn(ixs, txParams);
1600
+ }
1634
1601
  }
1635
1602
 
1636
1603
  /**
@@ -1651,38 +1618,15 @@ export class VaultClient {
1651
1618
  txParams?: TxParams,
1652
1619
  userTokenAccount?: PublicKey
1653
1620
  ): Promise<TransactionSignature> {
1654
- if (this.cliMode) {
1655
- const { vaultAccount, accounts, remainingAccounts, preIxs, postIxs } =
1656
- await this.prepDepositTx(
1657
- vaultDepositor,
1658
- amount,
1659
- initVaultDepositor,
1660
- userTokenAccount
1661
- );
1662
-
1663
- if (initVaultDepositor) {
1664
- await this.initializeVaultDepositor(
1665
- vaultAccount.pubkey,
1666
- initVaultDepositor.authority
1667
- );
1668
- }
1669
- return this.program.methods
1670
- .deposit(amount)
1671
- .accounts(accounts)
1672
- .remainingAccounts(remainingAccounts)
1673
- .preInstructions(preIxs)
1674
- .postInstructions(postIxs)
1675
- .rpc();
1676
- } else {
1677
- const depositTxn = await this.createDepositTx(
1678
- vaultDepositor,
1679
- amount,
1680
- initVaultDepositor,
1681
- txParams
1682
- );
1621
+ const depositTxn = await this.createDepositTx(
1622
+ vaultDepositor,
1623
+ amount,
1624
+ initVaultDepositor,
1625
+ txParams,
1626
+ userTokenAccount
1627
+ );
1683
1628
 
1684
- return this.sendTxn(depositTxn, txParams?.simulateTransaction);
1685
- }
1629
+ return this.sendTxn(depositTxn, txParams?.simulateTransaction);
1686
1630
  }
1687
1631
 
1688
1632
  public async requestWithdraw(
@@ -1698,24 +1642,19 @@ export class VaultClient {
1698
1642
  );
1699
1643
 
1700
1644
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
1701
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1702
- userAccounts: [user.getUserAccount()],
1703
- });
1704
- if (vaultAccount.vaultProtocol) {
1705
- const vaultProtocol = this.getVaultProtocolAddress(
1706
- vaultDepositorAccount.vault
1707
- );
1708
- remainingAccounts.push({
1709
- pubkey: vaultProtocol,
1710
- isSigner: false,
1711
- isWritable: true,
1712
- });
1713
- }
1714
-
1715
1645
  const userStatsKey = getUserStatsAccountPublicKey(
1716
1646
  this.driftClient.program.programId,
1717
1647
  vaultDepositorAccount.vault
1718
1648
  );
1649
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1650
+ userStatsKey
1651
+ )) as UserStatsAccount;
1652
+ const remainingAccounts = this.getRemainingAccountsForUser(
1653
+ [user.getUserAccount()],
1654
+ [],
1655
+ vaultAccount,
1656
+ userStats
1657
+ );
1719
1658
 
1720
1659
  const accounts = {
1721
1660
  vault: vaultDepositorAccount.vault,
@@ -1724,36 +1663,27 @@ export class VaultClient {
1724
1663
  driftUserStats: userStatsKey,
1725
1664
  };
1726
1665
 
1727
- if (this.cliMode) {
1728
- return await this.program.methods
1729
- // @ts-ignore
1730
- .requestWithdraw(amount, withdrawUnit)
1731
- .accounts(accounts)
1732
- .remainingAccounts(remainingAccounts)
1733
- .rpc();
1734
- } else {
1735
- const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(
1736
- txParams?.oracleFeedsToCrank
1737
- );
1666
+ const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(
1667
+ txParams?.oracleFeedsToCrank
1668
+ );
1738
1669
 
1739
- const requestWithdrawIx = this.program.instruction.requestWithdraw(
1740
- // @ts-ignore
1741
- amount,
1742
- withdrawUnit,
1743
- {
1744
- accounts: {
1745
- authority: this.driftClient.wallet.publicKey,
1746
- ...accounts,
1747
- },
1748
- remainingAccounts,
1749
- }
1750
- );
1670
+ const requestWithdrawIx = this.program.instruction.requestWithdraw(
1671
+ // @ts-ignore
1672
+ amount,
1673
+ withdrawUnit,
1674
+ {
1675
+ accounts: {
1676
+ authority: this.driftClient.wallet.publicKey,
1677
+ ...accounts,
1678
+ },
1679
+ remainingAccounts,
1680
+ }
1681
+ );
1751
1682
 
1752
- return await this.createAndSendTxn(
1753
- [...oracleFeedsToCrankIxs, requestWithdrawIx],
1754
- txParams
1755
- );
1756
- }
1683
+ return await this.createAndSendTxn(
1684
+ [...oracleFeedsToCrankIxs, requestWithdrawIx],
1685
+ txParams
1686
+ );
1757
1687
  }
1758
1688
 
1759
1689
  public async withdraw(
@@ -1767,25 +1697,19 @@ export class VaultClient {
1767
1697
  );
1768
1698
 
1769
1699
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
1770
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1771
- userAccounts: [user.getUserAccount()],
1772
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1773
- });
1774
- const vaultProtocol = this.getVaultProtocolAddress(
1775
- vaultDepositorAccount.vault
1776
- );
1777
- if (!vaultProtocol.equals(SystemProgram.programId)) {
1778
- remainingAccounts.push({
1779
- pubkey: vaultProtocol,
1780
- isSigner: false,
1781
- isWritable: true,
1782
- });
1783
- }
1784
-
1785
1700
  const userStatsKey = getUserStatsAccountPublicKey(
1786
1701
  this.driftClient.program.programId,
1787
1702
  vaultDepositorAccount.vault
1788
1703
  );
1704
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1705
+ userStatsKey
1706
+ )) as UserStatsAccount;
1707
+ const remainingAccounts = this.getRemainingAccountsForUser(
1708
+ [user.getUserAccount()],
1709
+ [vaultAccount.spotMarketIndex],
1710
+ vaultAccount,
1711
+ userStats
1712
+ );
1789
1713
 
1790
1714
  const driftStateKey = await this.driftClient.getStatePublicKey();
1791
1715
 
@@ -1855,40 +1779,29 @@ export class VaultClient {
1855
1779
  tokenProgram: TOKEN_PROGRAM_ID,
1856
1780
  };
1857
1781
 
1858
- if (this.cliMode) {
1859
- return await this.program.methods
1782
+ const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(
1783
+ txParams?.oracleFeedsToCrank
1784
+ );
1785
+
1786
+ const ixs = [
1787
+ ...oracleFeedsToCrankIxs,
1788
+ ...preIxs,
1789
+ await this.program.methods
1860
1790
  .withdraw()
1861
- .accounts(accounts)
1791
+ .accounts({
1792
+ authority: this.driftClient.wallet.publicKey,
1793
+ ...accounts,
1794
+ })
1862
1795
  .remainingAccounts(remainingAccounts)
1863
- .preInstructions(preIxs)
1864
- .postInstructions(postIxs)
1865
- .rpc();
1866
- } else {
1867
- const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(
1868
- txParams?.oracleFeedsToCrank
1869
- );
1796
+ .instruction(),
1797
+ ...postIxs,
1798
+ ];
1870
1799
 
1871
- const ixs = [
1872
- ...oracleFeedsToCrankIxs,
1873
- ...preIxs,
1874
- await this.program.methods
1875
- .withdraw()
1876
- .accounts({
1877
- authority: this.driftClient.wallet.publicKey,
1878
- ...accounts,
1879
- })
1880
- .remainingAccounts(remainingAccounts)
1881
- .instruction(),
1882
- ...postIxs,
1883
- ];
1884
-
1885
- const creationIxs = preIxs.concat(postIxs).length;
1886
- return await this.createAndSendTxn(ixs, {
1887
- cuLimit:
1888
- (txParams?.cuLimit ?? 650_000) + (creationIxs > 0 ? 200_000 : 0),
1889
- ...txParams,
1890
- });
1891
- }
1800
+ const creationIxs = preIxs.concat(postIxs).length;
1801
+ return await this.createAndSendTxn(ixs, {
1802
+ cuLimit: (txParams?.cuLimit ?? 650_000) + (creationIxs > 0 ? 200_000 : 0),
1803
+ ...txParams,
1804
+ });
1892
1805
  }
1893
1806
 
1894
1807
  public async forceWithdraw(
@@ -1908,10 +1821,19 @@ export class VaultClient {
1908
1821
  );
1909
1822
 
1910
1823
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
1911
- const remainingAccounts = this.driftClient.getRemainingAccounts({
1912
- userAccounts: [user.getUserAccount()],
1913
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
1914
- });
1824
+ const userStatsKey = getUserStatsAccountPublicKey(
1825
+ this.driftClient.program.programId,
1826
+ vaultDepositorAccount.vault
1827
+ );
1828
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1829
+ userStatsKey
1830
+ )) as UserStatsAccount;
1831
+ const remainingAccounts = this.getRemainingAccountsForUser(
1832
+ [user.getUserAccount()],
1833
+ [vaultAccount.spotMarketIndex],
1834
+ vaultAccount,
1835
+ userStats
1836
+ );
1915
1837
  if (vaultAccount.vaultProtocol) {
1916
1838
  const vaultProtocol = this.getVaultProtocolAddress(
1917
1839
  vaultDepositorAccount.vault
@@ -1923,11 +1845,6 @@ export class VaultClient {
1923
1845
  });
1924
1846
  }
1925
1847
 
1926
- const userStatsKey = getUserStatsAccountPublicKey(
1927
- this.driftClient.program.programId,
1928
- vaultDepositorAccount.vault
1929
- );
1930
-
1931
1848
  const driftStateKey = await this.driftClient.getStatePublicKey();
1932
1849
 
1933
1850
  const spotMarket = this.driftClient.getSpotMarketAccount(
@@ -2008,19 +1925,15 @@ export class VaultClient {
2008
1925
  };
2009
1926
 
2010
1927
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
2011
- const remainingAccounts = this.driftClient.getRemainingAccounts({
2012
- userAccounts: [user.getUserAccount()],
2013
- });
2014
- if (vaultAccount.vaultProtocol) {
2015
- const vaultProtocol = this.getVaultProtocolAddress(
2016
- vaultDepositorAccount.vault
2017
- );
2018
- remainingAccounts.push({
2019
- pubkey: vaultProtocol,
2020
- isSigner: false,
2021
- isWritable: true,
2022
- });
2023
- }
1928
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1929
+ userStatsKey
1930
+ )) as UserStatsAccount;
1931
+ const remainingAccounts = this.getRemainingAccountsForUser(
1932
+ [user.getUserAccount()],
1933
+ [],
1934
+ vaultAccount,
1935
+ userStats
1936
+ );
2024
1937
 
2025
1938
  if (this.cliMode) {
2026
1939
  return await this.program.methods
@@ -2061,25 +1974,29 @@ export class VaultClient {
2061
1974
  ): Promise<TransactionSignature> {
2062
1975
  const vaultDepositorAccount =
2063
1976
  await this.program.account.vaultDepositor.fetch(vaultDepositor);
2064
- const vaultPubKey = vaultDepositorAccount.vault;
1977
+ const vault = vaultDepositorAccount.vault;
2065
1978
 
2066
- const vaultAccount = await this.program.account.vault.fetch(vaultPubKey);
1979
+ const vaultAccount = await this.program.account.vault.fetch(vault);
2067
1980
 
2068
1981
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
2069
- const remainingAccounts = this.driftClient.getRemainingAccounts({
2070
- userAccounts: [user.getUserAccount()],
2071
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
2072
- });
2073
-
2074
1982
  const userStatsKey = getUserStatsAccountPublicKey(
2075
1983
  this.driftClient.program.programId,
2076
- vaultPubKey
1984
+ vault
1985
+ );
1986
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
1987
+ userStatsKey
1988
+ )) as UserStatsAccount;
1989
+ const remainingAccounts = this.getRemainingAccountsForUser(
1990
+ [user.getUserAccount()],
1991
+ [vaultAccount.spotMarketIndex],
1992
+ vaultAccount,
1993
+ userStats
2077
1994
  );
2078
1995
 
2079
1996
  const driftStateKey = await this.driftClient.getStatePublicKey();
2080
1997
 
2081
1998
  const accounts = {
2082
- vault: vaultPubKey,
1999
+ vault,
2083
2000
  vaultDepositor,
2084
2001
  vaultTokenAccount: vaultAccount.tokenAccount,
2085
2002
  driftUserStats: userStatsKey,
@@ -2116,7 +2033,7 @@ export class VaultClient {
2116
2033
  units: txParams?.cuLimit ?? 400_000,
2117
2034
  }),
2118
2035
  ComputeBudgetProgram.setComputeUnitPrice({
2119
- microLamports: txParams?.cuPriceMicroLamports ?? 1_000_000,
2036
+ microLamports: txParams?.cuPriceMicroLamports ?? 50_000,
2120
2037
  }),
2121
2038
  ...vaultIxs,
2122
2039
  ];
@@ -2133,6 +2050,31 @@ export class VaultClient {
2133
2050
  })) as VersionedTransaction;
2134
2051
  }
2135
2052
 
2053
+ public async createTxnNoLut(
2054
+ vaultIxs: TransactionInstruction[],
2055
+ txParams?: TxParams
2056
+ ): Promise<VersionedTransaction> {
2057
+ const ixs = [
2058
+ ComputeBudgetProgram.setComputeUnitLimit({
2059
+ units: txParams?.cuLimit ?? 400_000,
2060
+ }),
2061
+ ComputeBudgetProgram.setComputeUnitPrice({
2062
+ microLamports: txParams?.cuPriceMicroLamports ?? 50_000,
2063
+ }),
2064
+ ...vaultIxs,
2065
+ ];
2066
+
2067
+ const recentBlockhash =
2068
+ await this.driftClient.connection.getLatestBlockhash();
2069
+
2070
+ return this.driftClient.txHandler.generateVersionedTransaction(
2071
+ recentBlockhash,
2072
+ ixs,
2073
+ [],
2074
+ this.driftClient.wallet
2075
+ );
2076
+ }
2077
+
2136
2078
  public async sendTxn(
2137
2079
  transaction: VersionedTransaction,
2138
2080
  simulateTransaction?: boolean
@@ -2161,9 +2103,6 @@ export class VaultClient {
2161
2103
  this.driftClient.opts
2162
2104
  );
2163
2105
  if (resp.txSig !== txSig) {
2164
- console.error(
2165
- `Transaction signature mismatch with self calculated value: ${resp.txSig} !== ${txSig}`
2166
- );
2167
2106
  txSig = resp.txSig;
2168
2107
  }
2169
2108
  }
@@ -2178,7 +2117,14 @@ export class VaultClient {
2178
2117
  vaultIxs: TransactionInstruction[],
2179
2118
  txParams?: TxParams
2180
2119
  ): Promise<TransactionSignature> {
2181
- const tx = await this.createTxn(vaultIxs, txParams);
2120
+ let tx: VersionedTransaction;
2121
+ if (txParams?.noLut ? txParams.noLut : false) {
2122
+ tx = await this.createTxnNoLut(vaultIxs, txParams);
2123
+ // @ts-ignore
2124
+ tx.sign([this.driftClient.wallet.payer]);
2125
+ } else {
2126
+ tx = await this.createTxn(vaultIxs, txParams);
2127
+ }
2182
2128
  const txSig = await this.sendTxn(tx, txParams?.simulateTransaction);
2183
2129
 
2184
2130
  return txSig;
@@ -2444,23 +2390,19 @@ export class VaultClient {
2444
2390
  }
2445
2391
 
2446
2392
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
2447
- const remainingAccounts = this.driftClient.getRemainingAccounts({
2448
- userAccounts: [user.getUserAccount()],
2449
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
2450
- });
2451
- if (vaultAccount.vaultProtocol) {
2452
- const vaultProtocol = this.getVaultProtocolAddress(vault);
2453
- remainingAccounts.push({
2454
- pubkey: vaultProtocol,
2455
- isSigner: false,
2456
- isWritable: true,
2457
- });
2458
- }
2459
-
2460
2393
  const userStatsKey = getUserStatsAccountPublicKey(
2461
2394
  this.driftClient.program.programId,
2462
2395
  vault
2463
2396
  );
2397
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
2398
+ userStatsKey
2399
+ )) as UserStatsAccount;
2400
+ const remainingAccounts = this.getRemainingAccountsForUser(
2401
+ [user.getUserAccount()],
2402
+ [],
2403
+ vaultAccount,
2404
+ userStats
2405
+ );
2464
2406
 
2465
2407
  const accounts = {
2466
2408
  vault,
@@ -2511,17 +2453,15 @@ export class VaultClient {
2511
2453
  };
2512
2454
 
2513
2455
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
2514
- const remainingAccounts = this.driftClient.getRemainingAccounts({
2515
- userAccounts: [user.getUserAccount()],
2516
- });
2517
- if (vaultAccount.vaultProtocol) {
2518
- const vaultProtocol = this.getVaultProtocolAddress(vault);
2519
- remainingAccounts.push({
2520
- pubkey: vaultProtocol,
2521
- isSigner: false,
2522
- isWritable: true,
2523
- });
2524
- }
2456
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
2457
+ userStatsKey
2458
+ )) as UserStatsAccount;
2459
+ const remainingAccounts = this.getRemainingAccountsForUser(
2460
+ [user.getUserAccount()],
2461
+ [],
2462
+ vaultAccount,
2463
+ userStats
2464
+ );
2525
2465
 
2526
2466
  if (this.cliMode) {
2527
2467
  return await this.program.methods
@@ -2553,19 +2493,19 @@ export class VaultClient {
2553
2493
  }
2554
2494
 
2555
2495
  const user = await this.getSubscribedVaultUser(vaultAccount.user);
2556
-
2557
- const remainingAccounts = this.driftClient.getRemainingAccounts({
2558
- userAccounts: [user.getUserAccount()],
2559
- writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
2560
- });
2561
- if (vaultAccount.vaultProtocol) {
2562
- const vaultProtocol = this.getVaultProtocolAddress(vault);
2563
- remainingAccounts.push({
2564
- pubkey: vaultProtocol,
2565
- isSigner: false,
2566
- isWritable: true,
2567
- });
2568
- }
2496
+ const userStatsKey = getUserStatsAccountPublicKey(
2497
+ this.driftClient.program.programId,
2498
+ vault
2499
+ );
2500
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
2501
+ userStatsKey
2502
+ )) as UserStatsAccount;
2503
+ const remainingAccounts = this.getRemainingAccountsForUser(
2504
+ [user.getUserAccount()],
2505
+ [],
2506
+ vaultAccount,
2507
+ userStats
2508
+ );
2569
2509
 
2570
2510
  const spotMarket = this.driftClient.getSpotMarketAccount(
2571
2511
  vaultAccount.spotMarketIndex
@@ -2586,10 +2526,7 @@ export class VaultClient {
2586
2526
  vault
2587
2527
  ),
2588
2528
  driftProgram: this.driftClient.program.programId,
2589
- driftUserStats: getUserStatsAccountPublicKey(
2590
- this.driftClient.program.programId,
2591
- vault
2592
- ),
2529
+ driftUserStats: userStatsKey,
2593
2530
  driftState: await this.driftClient.getStatePublicKey(),
2594
2531
  driftSpotMarketVault: spotMarket.vault,
2595
2532
  userTokenAccount: getAssociatedTokenAddressSync(
@@ -2630,4 +2567,179 @@ export class VaultClient {
2630
2567
 
2631
2568
  return oracleFeedsToCrankIxs;
2632
2569
  }
2570
+
2571
+ public async updateVaultProtocol(
2572
+ vault: PublicKey,
2573
+ params: {
2574
+ protocolFee: BN | null;
2575
+ protocolProfitShare: number | null;
2576
+ },
2577
+ txParams?: TxParams
2578
+ ): Promise<TransactionSignature> {
2579
+ const ix = await this.program.methods
2580
+ .updateVaultProtocol(params)
2581
+ .accounts({
2582
+ vault,
2583
+ vaultProtocol: this.getVaultProtocolAddress(vault),
2584
+ })
2585
+ .instruction();
2586
+
2587
+ return await this.createAndSendTxn([ix], txParams);
2588
+ }
2589
+
2590
+ public async updateCumulativeFuelAmount(
2591
+ vaultDepositor: PublicKey,
2592
+ txParams?: TxParams
2593
+ ): Promise<TransactionSignature> {
2594
+ return await this.createAndSendTxn(
2595
+ [await this.getUpdateCumulativeFuelAmountIx(vaultDepositor)],
2596
+ txParams
2597
+ );
2598
+ }
2599
+
2600
+ public async getUpdateCumulativeFuelAmountIx(
2601
+ vaultDepositor: PublicKey
2602
+ ): Promise<TransactionInstruction> {
2603
+ const vaultDepositorAccount =
2604
+ await this.program.account.vaultDepositor.fetch(vaultDepositor);
2605
+ const vaultAccount = await this.program.account.vault.fetch(
2606
+ vaultDepositorAccount.vault
2607
+ );
2608
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
2609
+ const userStatsKey = getUserStatsAccountPublicKey(
2610
+ this.driftClient.program.programId,
2611
+ vaultDepositorAccount.vault
2612
+ );
2613
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
2614
+ userStatsKey
2615
+ )) as UserStatsAccount;
2616
+ const remainingAccounts = this.getRemainingAccountsForUser(
2617
+ [user.getUserAccount()],
2618
+ [],
2619
+ vaultAccount,
2620
+ userStats
2621
+ );
2622
+
2623
+ return this.program.methods
2624
+ .updateCumulativeFuelAmount()
2625
+ .accounts({
2626
+ vault: vaultDepositorAccount.vault,
2627
+ vaultDepositor,
2628
+ driftUserStats: userStatsKey,
2629
+ })
2630
+ .remainingAccounts(remainingAccounts)
2631
+ .instruction();
2632
+ }
2633
+
2634
+ public async resetFuelSeason(
2635
+ vaultDepositor: PublicKey,
2636
+ txParams?: TxParams
2637
+ ): Promise<TransactionSignature> {
2638
+ return await this.createAndSendTxn(
2639
+ [await this.getResetFuelSeasonIx(vaultDepositor)],
2640
+ txParams
2641
+ );
2642
+ }
2643
+
2644
+ public async getResetFuelSeasonIx(
2645
+ vaultDepositor: PublicKey
2646
+ ): Promise<TransactionInstruction> {
2647
+ const state = this.driftClient.getStateAccount();
2648
+ if (!state.admin.equals(this.driftClient.wallet.publicKey)) {
2649
+ throw new Error(`Only the admin wallet can reset the fuel season.`);
2650
+ }
2651
+
2652
+ const vaultDepositorAccount =
2653
+ await this.program.account.vaultDepositor.fetch(vaultDepositor);
2654
+ const vaultAccount = await this.program.account.vault.fetch(
2655
+ vaultDepositorAccount.vault
2656
+ );
2657
+ const user = await this.getSubscribedVaultUser(vaultAccount.user);
2658
+ const userStatsKey = getUserStatsAccountPublicKey(
2659
+ this.driftClient.program.programId,
2660
+ vaultDepositorAccount.vault
2661
+ );
2662
+ const userStats = (await this.driftClient.program.account.userStats.fetch(
2663
+ userStatsKey
2664
+ )) as UserStatsAccount;
2665
+ const remainingAccounts = this.getRemainingAccountsForUser(
2666
+ [user.getUserAccount()],
2667
+ [],
2668
+ vaultAccount,
2669
+ userStats
2670
+ );
2671
+
2672
+ return this.program.methods
2673
+ .resetFuelSeason()
2674
+ .accounts({
2675
+ vault: vaultDepositorAccount.vault,
2676
+ vaultDepositor,
2677
+ admin: this.driftClient.wallet.publicKey,
2678
+ driftUserStats: userStatsKey,
2679
+ driftState: await this.driftClient.getStatePublicKey(),
2680
+ // @ts-ignore
2681
+ logAccount: FUEL_RESET_LOG_ACCOUNT,
2682
+ })
2683
+ .remainingAccounts(remainingAccounts)
2684
+ .instruction();
2685
+ }
2686
+
2687
+ public async resetVaultFuelSeason(
2688
+ vault: PublicKey,
2689
+ txParams?: TxParams
2690
+ ): Promise<TransactionSignature> {
2691
+ return await this.createAndSendTxn(
2692
+ [await this.getResetVaultFuelSeasonIx(vault)],
2693
+ txParams
2694
+ );
2695
+ }
2696
+
2697
+ public async getResetVaultFuelSeasonIx(
2698
+ vault: PublicKey
2699
+ ): Promise<TransactionInstruction> {
2700
+ const state = this.driftClient.getStateAccount();
2701
+ if (!state.admin.equals(this.driftClient.wallet.publicKey)) {
2702
+ throw new Error(`Only the admin wallet can reset the fuel season.`);
2703
+ }
2704
+
2705
+ return this.program.methods
2706
+ .resetVaultFuelSeason()
2707
+ .accounts({
2708
+ vault,
2709
+ admin: this.driftClient.wallet.publicKey,
2710
+ driftState: await this.driftClient.getStatePublicKey(),
2711
+ // @ts-ignore
2712
+ logAccount: FUEL_RESET_LOG_ACCOUNT,
2713
+ })
2714
+ .instruction();
2715
+ }
2716
+
2717
+ public async managerUpdateFuelDistributionMode(
2718
+ vault: PublicKey,
2719
+ fuelDistributionMode: FuelDistributionMode,
2720
+ txParams?: TxParams
2721
+ ): Promise<TransactionSignature> {
2722
+ return await this.createAndSendTxn(
2723
+ [
2724
+ await this.getManagerUpdateFuelDistributionModeIx(
2725
+ vault,
2726
+ fuelDistributionMode
2727
+ ),
2728
+ ],
2729
+ txParams
2730
+ );
2731
+ }
2732
+
2733
+ public async getManagerUpdateFuelDistributionModeIx(
2734
+ vault: PublicKey,
2735
+ fuelDistributionMode: FuelDistributionMode
2736
+ ): Promise<TransactionInstruction> {
2737
+ return this.program.methods
2738
+ .managerUpdateFuelDistributionMode(fuelDistributionMode as number)
2739
+ .accounts({
2740
+ vault,
2741
+ manager: this.driftClient.wallet.publicKey,
2742
+ })
2743
+ .instruction();
2744
+ }
2633
2745
  }