@drift-labs/vaults-sdk 0.1.190 → 0.1.192

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.
@@ -515,6 +515,12 @@ class VaultClient {
515
515
  if (!spotMarket) {
516
516
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
517
517
  }
518
+ const userAta = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true);
519
+ let createAtaIx = undefined;
520
+ const userAtaExists = await this.driftClient.connection.getAccountInfo(userAta);
521
+ if (userAtaExists === null) {
522
+ createAtaIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.driftClient.wallet.publicKey, userAta, this.driftClient.wallet.publicKey, spotMarket.mint);
523
+ }
518
524
  const accounts = {
519
525
  vault: vaultDepositorAccount.vault,
520
526
  vaultDepositor,
@@ -524,7 +530,7 @@ class VaultClient {
524
530
  driftState: driftStateKey,
525
531
  driftSpotMarketVault: spotMarket.vault,
526
532
  driftSigner: this.driftClient.getStateAccount().signer,
527
- userTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true),
533
+ userTokenAccount: userAta,
528
534
  driftProgram: this.driftClient.program.programId,
529
535
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
530
536
  };
@@ -536,15 +542,20 @@ class VaultClient {
536
542
  .rpc();
537
543
  }
538
544
  else {
539
- const withdrawIx = this.program.instruction.withdraw({
540
- accounts: {
541
- authority: this.driftClient.wallet.publicKey,
542
- ...accounts,
543
- },
544
- remainingAccounts,
545
- });
546
- return await this.createAndSendTxn([withdrawIx], {
547
- cuLimit: (_a = txParams === null || txParams === void 0 ? void 0 : txParams.cuLimit) !== null && _a !== void 0 ? _a : 650000,
545
+ const ixs = [
546
+ this.program.instruction.withdraw({
547
+ accounts: {
548
+ authority: this.driftClient.wallet.publicKey,
549
+ ...accounts,
550
+ },
551
+ remainingAccounts,
552
+ }),
553
+ ];
554
+ if (createAtaIx) {
555
+ ixs.unshift(createAtaIx);
556
+ }
557
+ return await this.createAndSendTxn(ixs, {
558
+ cuLimit: ((_a = txParams === null || txParams === void 0 ? void 0 : txParams.cuLimit) !== null && _a !== void 0 ? _a : 650000) + (createAtaIx ? 100000 : 0),
548
559
  ...txParams,
549
560
  });
550
561
  }
@@ -681,7 +692,7 @@ class VaultClient {
681
692
  * Used for UI wallet adapters compatibility
682
693
  */
683
694
  async createAndSendTxn(vaultIxs, txParams) {
684
- var _a, _b;
695
+ var _a, _b, _c;
685
696
  const ixs = [
686
697
  web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
687
698
  units: (_a = txParams === null || txParams === void 0 ? void 0 : txParams.cuLimit) !== null && _a !== void 0 ? _a : 400000,
@@ -691,15 +702,20 @@ class VaultClient {
691
702
  }),
692
703
  ...vaultIxs,
693
704
  ];
694
- const tx = await this.driftClient.txSender.getVersionedTransaction(ixs, [], undefined, undefined);
705
+ const tx = await this.driftClient.txSender.getVersionedTransaction(ixs, [], undefined, this.driftClient.opts);
695
706
  let txSig = bytes_1.bs58.encode(tx.signatures[0]);
696
707
  if (txParams === null || txParams === void 0 ? void 0 : txParams.simulateTransaction) {
697
- const resp = await this.driftClient.connection.simulateTransaction(tx, {
698
- // replaceRecentBlockhash: true,
699
- sigVerify: false,
700
- commitment: this.driftClient.connection.commitment,
701
- });
702
- console.log(`Simulated transaction:\n${JSON.stringify(resp, null, 2)}`);
708
+ try {
709
+ const resp = await this.driftClient.connection.simulateTransaction(tx, {
710
+ sigVerify: false,
711
+ commitment: this.driftClient.connection.commitment,
712
+ });
713
+ console.log(`Simulated transaction:\n${JSON.stringify(resp, null, 2)}`);
714
+ }
715
+ catch (e) {
716
+ const err = e;
717
+ console.error(`Error simulating transaction: ${err.message}\n:${(_c = err.stack) !== null && _c !== void 0 ? _c : ''}`);
718
+ }
703
719
  }
704
720
  else {
705
721
  const resp = await this.driftClient.sendTransaction(tx, [], this.driftClient.opts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/vaults-sdk",
3
- "version": "0.1.190",
3
+ "version": "0.1.192",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "directories": {
@@ -29,6 +29,7 @@ import {
29
29
  TransactionSignature,
30
30
  } from '@solana/web3.js';
31
31
  import {
32
+ createAssociatedTokenAccountInstruction,
32
33
  getAssociatedTokenAddressSync,
33
34
  TOKEN_PROGRAM_ID,
34
35
  } from '@solana/spl-token';
@@ -825,6 +826,25 @@ export class VaultClient {
825
826
  );
826
827
  }
827
828
 
829
+ const userAta = getAssociatedTokenAddressSync(
830
+ spotMarket.mint,
831
+ this.driftClient.wallet.publicKey,
832
+ true
833
+ );
834
+
835
+ let createAtaIx: TransactionInstruction | undefined = undefined;
836
+ const userAtaExists = await this.driftClient.connection.getAccountInfo(
837
+ userAta
838
+ );
839
+ if (userAtaExists === null) {
840
+ createAtaIx = createAssociatedTokenAccountInstruction(
841
+ this.driftClient.wallet.publicKey,
842
+ userAta,
843
+ this.driftClient.wallet.publicKey,
844
+ spotMarket.mint
845
+ );
846
+ }
847
+
828
848
  const accounts = {
829
849
  vault: vaultDepositorAccount.vault,
830
850
  vaultDepositor,
@@ -834,11 +854,7 @@ export class VaultClient {
834
854
  driftState: driftStateKey,
835
855
  driftSpotMarketVault: spotMarket.vault,
836
856
  driftSigner: this.driftClient.getStateAccount().signer,
837
- userTokenAccount: getAssociatedTokenAddressSync(
838
- spotMarket.mint,
839
- this.driftClient.wallet.publicKey,
840
- true
841
- ),
857
+ userTokenAccount: userAta,
842
858
  driftProgram: this.driftClient.program.programId,
843
859
  tokenProgram: TOKEN_PROGRAM_ID,
844
860
  };
@@ -850,16 +866,21 @@ export class VaultClient {
850
866
  .remainingAccounts(remainingAccounts)
851
867
  .rpc();
852
868
  } else {
853
- const withdrawIx = this.program.instruction.withdraw({
854
- accounts: {
855
- authority: this.driftClient.wallet.publicKey,
856
- ...accounts,
857
- },
858
- remainingAccounts,
859
- });
869
+ const ixs = [
870
+ this.program.instruction.withdraw({
871
+ accounts: {
872
+ authority: this.driftClient.wallet.publicKey,
873
+ ...accounts,
874
+ },
875
+ remainingAccounts,
876
+ }),
877
+ ];
878
+ if (createAtaIx) {
879
+ ixs.unshift(createAtaIx);
880
+ }
860
881
 
861
- return await this.createAndSendTxn([withdrawIx], {
862
- cuLimit: txParams?.cuLimit ?? 650_000,
882
+ return await this.createAndSendTxn(ixs, {
883
+ cuLimit: (txParams?.cuLimit ?? 650_000) + (createAtaIx ? 100_000 : 0),
863
884
  ...txParams,
864
885
  });
865
886
  }
@@ -1064,17 +1085,23 @@ export class VaultClient {
1064
1085
  ixs,
1065
1086
  [],
1066
1087
  undefined,
1067
- undefined
1088
+ this.driftClient.opts
1068
1089
  );
1069
1090
 
1070
1091
  let txSig = bs58.encode(tx.signatures[0]);
1071
1092
  if (txParams?.simulateTransaction) {
1072
- const resp = await this.driftClient.connection.simulateTransaction(tx, {
1073
- // replaceRecentBlockhash: true,
1074
- sigVerify: false,
1075
- commitment: this.driftClient.connection.commitment,
1076
- });
1077
- console.log(`Simulated transaction:\n${JSON.stringify(resp, null, 2)}`);
1093
+ try {
1094
+ const resp = await this.driftClient.connection.simulateTransaction(tx, {
1095
+ sigVerify: false,
1096
+ commitment: this.driftClient.connection.commitment,
1097
+ });
1098
+ console.log(`Simulated transaction:\n${JSON.stringify(resp, null, 2)}`);
1099
+ } catch (e) {
1100
+ const err = e as Error;
1101
+ console.error(
1102
+ `Error simulating transaction: ${err.message}\n:${err.stack ?? ''}`
1103
+ );
1104
+ }
1078
1105
  } else {
1079
1106
  const resp = await this.driftClient.sendTransaction(
1080
1107
  tx,