@drift-labs/vaults-sdk 0.2.47 → 0.2.48

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.
@@ -181,7 +181,7 @@ export declare class VaultClient {
181
181
  prepDepositTx(vaultDepositor: PublicKey, amount: BN, initVaultDepositor?: {
182
182
  authority: PublicKey;
183
183
  vault: PublicKey;
184
- }, userTokenAccount?: PublicKey): Promise<{
184
+ }, depositTokenAccount?: PublicKey): Promise<{
185
185
  vaultAccount: {
186
186
  name: number[];
187
187
  pubkey: PublicKey;
@@ -237,6 +237,8 @@ export declare class VaultClient {
237
237
  tokenProgram: PublicKey;
238
238
  };
239
239
  remainingAccounts: import("@solana/web3.js").AccountMeta[];
240
+ preIxs: TransactionInstruction[];
241
+ postIxs: TransactionInstruction[];
240
242
  }>;
241
243
  /**
242
244
  * Creates a transaction to deposit funds into the specified vault.
@@ -409,6 +409,7 @@ class VaultClient {
409
409
  isWritable: true,
410
410
  });
411
411
  }
412
+ // TODO: handle SOL deposits (need to create/destroy WSOL token accounts)
412
413
  return await this.program.methods
413
414
  .managerDeposit(amount)
414
415
  .accounts({
@@ -854,7 +855,7 @@ class VaultClient {
854
855
  return await this.createAndSendTxn([ix], txParams);
855
856
  }
856
857
  }
857
- async prepDepositTx(vaultDepositor, amount, initVaultDepositor, userTokenAccount) {
858
+ async prepDepositTx(vaultDepositor, amount, initVaultDepositor, depositTokenAccount) {
858
859
  let vaultPubKey;
859
860
  if (initVaultDepositor) {
860
861
  vaultPubKey = initVaultDepositor.vault;
@@ -883,6 +884,16 @@ class VaultClient {
883
884
  if (!spotMarket) {
884
885
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
885
886
  }
887
+ let userTokenAccount = depositTokenAccount !== null && depositTokenAccount !== void 0 ? depositTokenAccount : (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true);
888
+ const isSolDeposit = spotMarket.mint.equals(sdk_1.WRAPPED_SOL_MINT);
889
+ const preIxs = [];
890
+ const postIxs = [];
891
+ if (isSolDeposit) {
892
+ const { ixs, pubkey } = await this.driftClient.getWrappedSolAccountCreationIxs(amount, true);
893
+ userTokenAccount = pubkey;
894
+ preIxs.push(...ixs);
895
+ postIxs.push((0, spl_token_1.createCloseAccountInstruction)(userTokenAccount, this.driftClient.wallet.publicKey, this.driftClient.wallet.publicKey, []));
896
+ }
886
897
  const accounts = {
887
898
  vault: vaultPubKey,
888
899
  vaultDepositor,
@@ -891,7 +902,7 @@ class VaultClient {
891
902
  driftUser: vaultAccount.user,
892
903
  driftState: driftStateKey,
893
904
  driftSpotMarketVault: spotMarket.vault,
894
- userTokenAccount: userTokenAccount !== null && userTokenAccount !== void 0 ? userTokenAccount : (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true),
905
+ userTokenAccount,
895
906
  driftProgram: this.driftClient.program.programId,
896
907
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
897
908
  };
@@ -899,6 +910,8 @@ class VaultClient {
899
910
  vaultAccount,
900
911
  accounts,
901
912
  remainingAccounts,
913
+ preIxs,
914
+ postIxs,
902
915
  };
903
916
  }
904
917
  /**
@@ -911,7 +924,7 @@ class VaultClient {
911
924
  * @returns transaction
912
925
  */
913
926
  async createDepositTx(vaultDepositor, amount, initVaultDepositor, txParams) {
914
- const { vaultAccount, accounts, remainingAccounts } = await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
927
+ const { vaultAccount, accounts, remainingAccounts, preIxs, postIxs } = await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
915
928
  const ixs = [];
916
929
  if (initVaultDepositor) {
917
930
  ixs.push(this.createInitVaultDepositorIx(vaultAccount.pubkey, initVaultDepositor.authority));
@@ -924,7 +937,9 @@ class VaultClient {
924
937
  })
925
938
  .remainingAccounts(remainingAccounts)
926
939
  .instruction();
940
+ ixs.push(...preIxs);
927
941
  ixs.push(depositIx);
942
+ ixs.push(...postIxs);
928
943
  return await this.createTxn(ixs, txParams);
929
944
  }
930
945
  /**
@@ -937,7 +952,7 @@ class VaultClient {
937
952
  */
938
953
  async deposit(vaultDepositor, amount, initVaultDepositor, txParams, userTokenAccount) {
939
954
  if (this.cliMode) {
940
- const { vaultAccount, accounts, remainingAccounts } = await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor, userTokenAccount);
955
+ const { vaultAccount, accounts, remainingAccounts, preIxs, postIxs } = await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor, userTokenAccount);
941
956
  if (initVaultDepositor) {
942
957
  await this.initializeVaultDepositor(vaultAccount.pubkey, initVaultDepositor.authority);
943
958
  }
@@ -945,6 +960,8 @@ class VaultClient {
945
960
  .deposit(amount)
946
961
  .accounts(accounts)
947
962
  .remainingAccounts(remainingAccounts)
963
+ .preInstructions(preIxs)
964
+ .postInstructions(postIxs)
948
965
  .rpc();
949
966
  }
950
967
  else {
@@ -1021,11 +1038,23 @@ class VaultClient {
1021
1038
  if (!spotMarket) {
1022
1039
  throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
1023
1040
  }
1024
- const userAta = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true);
1025
- let createAtaIx = undefined;
1026
- const userAtaExists = await this.driftClient.connection.getAccountInfo(userAta);
1027
- if (userAtaExists === null) {
1028
- createAtaIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.driftClient.wallet.publicKey, userAta, this.driftClient.wallet.publicKey, spotMarket.mint);
1041
+ const isSolMarket = spotMarket.mint.equals(sdk_1.WRAPPED_SOL_MINT);
1042
+ // let createAtaIx: TransactionInstruction | undefined = undefined;
1043
+ let userAta = (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey, true);
1044
+ const preIxs = [];
1045
+ const postIxs = [];
1046
+ if (isSolMarket) {
1047
+ const { ixs, pubkey } = await this.driftClient.getWrappedSolAccountCreationIxs(sdk_1.ZERO, false);
1048
+ userAta = pubkey;
1049
+ preIxs.push(...ixs);
1050
+ postIxs.push((0, spl_token_1.createSyncNativeInstruction)(userAta));
1051
+ postIxs.push((0, spl_token_1.createCloseAccountInstruction)(userAta, this.driftClient.wallet.publicKey, this.driftClient.wallet.publicKey, []));
1052
+ }
1053
+ else {
1054
+ const userAtaExists = await this.driftClient.connection.getAccountInfo(userAta);
1055
+ if (userAtaExists === null) {
1056
+ preIxs.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(this.driftClient.wallet.publicKey, userAta, this.driftClient.wallet.publicKey, spotMarket.mint));
1057
+ }
1029
1058
  }
1030
1059
  const accounts = {
1031
1060
  vault: vaultDepositorAccount.vault,
@@ -1041,26 +1070,19 @@ class VaultClient {
1041
1070
  tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
1042
1071
  };
1043
1072
  if (this.cliMode) {
1044
- if (createAtaIx) {
1045
- return await this.program.methods
1046
- .withdraw()
1047
- .accounts(accounts)
1048
- .remainingAccounts(remainingAccounts)
1049
- .preInstructions([createAtaIx])
1050
- .rpc();
1051
- }
1052
- else {
1053
- return await this.program.methods
1054
- .withdraw()
1055
- .accounts(accounts)
1056
- .remainingAccounts(remainingAccounts)
1057
- .rpc();
1058
- }
1073
+ return await this.program.methods
1074
+ .withdraw()
1075
+ .accounts(accounts)
1076
+ .remainingAccounts(remainingAccounts)
1077
+ .preInstructions(preIxs)
1078
+ .postInstructions(postIxs)
1079
+ .rpc();
1059
1080
  }
1060
1081
  else {
1061
1082
  const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(txParams === null || txParams === void 0 ? void 0 : txParams.oracleFeedsToCrank);
1062
1083
  const ixs = [
1063
1084
  ...oracleFeedsToCrankIxs,
1085
+ ...preIxs,
1064
1086
  await this.program.methods
1065
1087
  .withdraw()
1066
1088
  .accounts({
@@ -1069,12 +1091,11 @@ class VaultClient {
1069
1091
  })
1070
1092
  .remainingAccounts(remainingAccounts)
1071
1093
  .instruction(),
1094
+ ...postIxs,
1072
1095
  ];
1073
- if (createAtaIx) {
1074
- ixs.unshift(createAtaIx);
1075
- }
1096
+ const creationIxs = preIxs.concat(postIxs).length;
1076
1097
  return await this.createAndSendTxn(ixs, {
1077
- cuLimit: ((_a = txParams === null || txParams === void 0 ? void 0 : txParams.cuLimit) !== null && _a !== void 0 ? _a : 650000) + (createAtaIx ? 100000 : 0),
1098
+ cuLimit: ((_a = txParams === null || txParams === void 0 ? void 0 : txParams.cuLimit) !== null && _a !== void 0 ? _a : 650000) + (creationIxs > 0 ? 200000 : 0),
1078
1099
  ...txParams,
1079
1100
  });
1080
1101
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/vaults-sdk",
3
- "version": "0.2.47",
3
+ "version": "0.2.48",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "directories": {
@@ -11,6 +11,7 @@ import {
11
11
  ZERO,
12
12
  getInsuranceFundVaultPublicKey,
13
13
  OracleSource,
14
+ WRAPPED_SOL_MINT,
14
15
  } from '@drift-labs/sdk';
15
16
  import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor';
16
17
  import { DriftVaults } from './types/drift_vaults';
@@ -36,6 +37,8 @@ import {
36
37
  } from '@solana/web3.js';
37
38
  import {
38
39
  createAssociatedTokenAccountInstruction,
40
+ createCloseAccountInstruction,
41
+ createSyncNativeInstruction,
39
42
  getAssociatedTokenAddressSync,
40
43
  TOKEN_PROGRAM_ID,
41
44
  } from '@solana/spl-token';
@@ -627,6 +630,8 @@ export class VaultClient {
627
630
  });
628
631
  }
629
632
 
633
+ // TODO: handle SOL deposits (need to create/destroy WSOL token accounts)
634
+
630
635
  return await this.program.methods
631
636
  .managerDeposit(amount)
632
637
  .accounts({
@@ -1371,7 +1376,7 @@ export class VaultClient {
1371
1376
  authority: PublicKey;
1372
1377
  vault: PublicKey;
1373
1378
  },
1374
- userTokenAccount?: PublicKey
1379
+ depositTokenAccount?: PublicKey
1375
1380
  ) {
1376
1381
  let vaultPubKey: PublicKey;
1377
1382
  if (initVaultDepositor) {
@@ -1414,6 +1419,36 @@ export class VaultClient {
1414
1419
  );
1415
1420
  }
1416
1421
 
1422
+ let userTokenAccount =
1423
+ depositTokenAccount ??
1424
+ getAssociatedTokenAddressSync(
1425
+ spotMarket.mint,
1426
+ this.driftClient.wallet.publicKey,
1427
+ true
1428
+ );
1429
+
1430
+ const isSolDeposit = spotMarket.mint.equals(WRAPPED_SOL_MINT);
1431
+
1432
+ const preIxs: TransactionInstruction[] = [];
1433
+ const postIxs: TransactionInstruction[] = [];
1434
+
1435
+ if (isSolDeposit) {
1436
+ const { ixs, pubkey } =
1437
+ await this.driftClient.getWrappedSolAccountCreationIxs(amount, true);
1438
+
1439
+ userTokenAccount = pubkey;
1440
+
1441
+ preIxs.push(...ixs);
1442
+ postIxs.push(
1443
+ createCloseAccountInstruction(
1444
+ userTokenAccount,
1445
+ this.driftClient.wallet.publicKey,
1446
+ this.driftClient.wallet.publicKey,
1447
+ []
1448
+ )
1449
+ );
1450
+ }
1451
+
1417
1452
  const accounts = {
1418
1453
  vault: vaultPubKey,
1419
1454
  vaultDepositor,
@@ -1422,13 +1457,7 @@ export class VaultClient {
1422
1457
  driftUser: vaultAccount.user,
1423
1458
  driftState: driftStateKey,
1424
1459
  driftSpotMarketVault: spotMarket.vault,
1425
- userTokenAccount:
1426
- userTokenAccount ??
1427
- getAssociatedTokenAddressSync(
1428
- spotMarket.mint,
1429
- this.driftClient.wallet.publicKey,
1430
- true
1431
- ),
1460
+ userTokenAccount,
1432
1461
  driftProgram: this.driftClient.program.programId,
1433
1462
  tokenProgram: TOKEN_PROGRAM_ID,
1434
1463
  };
@@ -1437,6 +1466,8 @@ export class VaultClient {
1437
1466
  vaultAccount,
1438
1467
  accounts,
1439
1468
  remainingAccounts,
1469
+ preIxs,
1470
+ postIxs,
1440
1471
  };
1441
1472
  }
1442
1473
 
@@ -1458,7 +1489,7 @@ export class VaultClient {
1458
1489
  },
1459
1490
  txParams?: TxParams
1460
1491
  ): Promise<VersionedTransaction> {
1461
- const { vaultAccount, accounts, remainingAccounts } =
1492
+ const { vaultAccount, accounts, remainingAccounts, preIxs, postIxs } =
1462
1493
  await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
1463
1494
 
1464
1495
  const ixs: TransactionInstruction[] = [];
@@ -1480,7 +1511,9 @@ export class VaultClient {
1480
1511
  })
1481
1512
  .remainingAccounts(remainingAccounts)
1482
1513
  .instruction();
1514
+ ixs.push(...preIxs);
1483
1515
  ixs.push(depositIx);
1516
+ ixs.push(...postIxs);
1484
1517
 
1485
1518
  return await this.createTxn(ixs, txParams);
1486
1519
  }
@@ -1504,7 +1537,7 @@ export class VaultClient {
1504
1537
  userTokenAccount?: PublicKey
1505
1538
  ): Promise<TransactionSignature> {
1506
1539
  if (this.cliMode) {
1507
- const { vaultAccount, accounts, remainingAccounts } =
1540
+ const { vaultAccount, accounts, remainingAccounts, preIxs, postIxs } =
1508
1541
  await this.prepDepositTx(
1509
1542
  vaultDepositor,
1510
1543
  amount,
@@ -1522,6 +1555,8 @@ export class VaultClient {
1522
1555
  .deposit(amount)
1523
1556
  .accounts(accounts)
1524
1557
  .remainingAccounts(remainingAccounts)
1558
+ .preInstructions(preIxs)
1559
+ .postInstructions(postIxs)
1525
1560
  .rpc();
1526
1561
  } else {
1527
1562
  const depositTxn = await this.createDepositTx(
@@ -1651,23 +1686,47 @@ export class VaultClient {
1651
1686
  );
1652
1687
  }
1653
1688
 
1654
- const userAta = getAssociatedTokenAddressSync(
1689
+ const isSolMarket = spotMarket.mint.equals(WRAPPED_SOL_MINT);
1690
+
1691
+ // let createAtaIx: TransactionInstruction | undefined = undefined;
1692
+ let userAta = getAssociatedTokenAddressSync(
1655
1693
  spotMarket.mint,
1656
1694
  this.driftClient.wallet.publicKey,
1657
1695
  true
1658
1696
  );
1659
1697
 
1660
- let createAtaIx: TransactionInstruction | undefined = undefined;
1661
- const userAtaExists = await this.driftClient.connection.getAccountInfo(
1662
- userAta
1663
- );
1664
- if (userAtaExists === null) {
1665
- createAtaIx = createAssociatedTokenAccountInstruction(
1666
- this.driftClient.wallet.publicKey,
1667
- userAta,
1668
- this.driftClient.wallet.publicKey,
1669
- spotMarket.mint
1698
+ const preIxs: TransactionInstruction[] = [];
1699
+ const postIxs: TransactionInstruction[] = [];
1700
+
1701
+ if (isSolMarket) {
1702
+ const { ixs, pubkey } =
1703
+ await this.driftClient.getWrappedSolAccountCreationIxs(ZERO, false);
1704
+
1705
+ userAta = pubkey;
1706
+ preIxs.push(...ixs);
1707
+ postIxs.push(createSyncNativeInstruction(userAta));
1708
+ postIxs.push(
1709
+ createCloseAccountInstruction(
1710
+ userAta,
1711
+ this.driftClient.wallet.publicKey,
1712
+ this.driftClient.wallet.publicKey,
1713
+ []
1714
+ )
1670
1715
  );
1716
+ } else {
1717
+ const userAtaExists = await this.driftClient.connection.getAccountInfo(
1718
+ userAta
1719
+ );
1720
+ if (userAtaExists === null) {
1721
+ preIxs.push(
1722
+ createAssociatedTokenAccountInstruction(
1723
+ this.driftClient.wallet.publicKey,
1724
+ userAta,
1725
+ this.driftClient.wallet.publicKey,
1726
+ spotMarket.mint
1727
+ )
1728
+ );
1729
+ }
1671
1730
  }
1672
1731
 
1673
1732
  const accounts = {
@@ -1685,20 +1744,13 @@ export class VaultClient {
1685
1744
  };
1686
1745
 
1687
1746
  if (this.cliMode) {
1688
- if (createAtaIx) {
1689
- return await this.program.methods
1690
- .withdraw()
1691
- .accounts(accounts)
1692
- .remainingAccounts(remainingAccounts)
1693
- .preInstructions([createAtaIx])
1694
- .rpc();
1695
- } else {
1696
- return await this.program.methods
1697
- .withdraw()
1698
- .accounts(accounts)
1699
- .remainingAccounts(remainingAccounts)
1700
- .rpc();
1701
- }
1747
+ return await this.program.methods
1748
+ .withdraw()
1749
+ .accounts(accounts)
1750
+ .remainingAccounts(remainingAccounts)
1751
+ .preInstructions(preIxs)
1752
+ .postInstructions(postIxs)
1753
+ .rpc();
1702
1754
  } else {
1703
1755
  const oracleFeedsToCrankIxs = await this.getOracleFeedsToCrank(
1704
1756
  txParams?.oracleFeedsToCrank
@@ -1706,6 +1758,7 @@ export class VaultClient {
1706
1758
 
1707
1759
  const ixs = [
1708
1760
  ...oracleFeedsToCrankIxs,
1761
+ ...preIxs,
1709
1762
  await this.program.methods
1710
1763
  .withdraw()
1711
1764
  .accounts({
@@ -1714,13 +1767,13 @@ export class VaultClient {
1714
1767
  })
1715
1768
  .remainingAccounts(remainingAccounts)
1716
1769
  .instruction(),
1770
+ ...postIxs,
1717
1771
  ];
1718
- if (createAtaIx) {
1719
- ixs.unshift(createAtaIx);
1720
- }
1721
1772
 
1773
+ const creationIxs = preIxs.concat(postIxs).length;
1722
1774
  return await this.createAndSendTxn(ixs, {
1723
- cuLimit: (txParams?.cuLimit ?? 650_000) + (createAtaIx ? 100_000 : 0),
1775
+ cuLimit:
1776
+ (txParams?.cuLimit ?? 650_000) + (creationIxs > 0 ? 200_000 : 0),
1724
1777
  ...txParams,
1725
1778
  });
1726
1779
  }