@drift-labs/vaults-sdk 0.6.29 → 0.6.31

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.
Files changed (46) hide show
  1. package/cli/cli.ts +31 -2
  2. package/cli/commands/adminDeleteFeeUpdate.ts +73 -0
  3. package/cli/commands/adminInitFeeUpdate.ts +73 -0
  4. package/cli/commands/applyProfitShare.ts +5 -2
  5. package/cli/commands/decodeLogs.ts +3 -2
  6. package/cli/commands/deposit.ts +2 -2
  7. package/cli/commands/forceWithdrawAll.ts +1 -1
  8. package/cli/commands/index.ts +2 -0
  9. package/cli/commands/initVault.ts +2 -2
  10. package/cli/commands/managerApplyProfitShare.ts +1 -1
  11. package/cli/commands/managerCancelWithdraw.ts +1 -1
  12. package/cli/commands/managerDeposit.ts +1 -1
  13. package/cli/commands/managerRequestWithdraw.ts +2 -2
  14. package/cli/commands/managerUpdateFees.ts +121 -0
  15. package/cli/commands/managerUpdateMarginTradingEnabled.ts +1 -1
  16. package/cli/commands/managerUpdatePoolId.ts +1 -1
  17. package/cli/commands/managerUpdateVault.ts +13 -2
  18. package/cli/commands/managerUpdateVaultDelegate.ts +1 -1
  19. package/cli/commands/managerUpdateVaultManager.ts +1 -1
  20. package/cli/commands/managerWithdraw.ts +1 -1
  21. package/cli/commands/requestWithdraw.ts +1 -1
  22. package/cli/commands/viewVault.ts +12 -1
  23. package/cli/commands/withdraw.ts +1 -1
  24. package/cli/utils.ts +12 -3
  25. package/lib/addresses.d.ts +1 -0
  26. package/lib/addresses.d.ts.map +1 -1
  27. package/lib/addresses.js +7 -0
  28. package/lib/constants/index.d.ts +2 -0
  29. package/lib/constants/index.d.ts.map +1 -1
  30. package/lib/constants/index.js +3 -1
  31. package/lib/types/drift_vaults.d.ts +275 -1
  32. package/lib/types/drift_vaults.d.ts.map +1 -1
  33. package/lib/types/drift_vaults.js +275 -1
  34. package/lib/types/types.d.ts +33 -0
  35. package/lib/types/types.d.ts.map +1 -1
  36. package/lib/types/types.js +11 -1
  37. package/lib/vaultClient.d.ts +22 -1
  38. package/lib/vaultClient.d.ts.map +1 -1
  39. package/lib/vaultClient.js +115 -22
  40. package/package.json +2 -2
  41. package/src/addresses.ts +13 -0
  42. package/src/constants/index.ts +5 -0
  43. package/src/idl/drift_vaults.json +281 -1
  44. package/src/types/drift_vaults.ts +550 -2
  45. package/src/types/types.ts +33 -0
  46. package/src/vaultClient.ts +229 -22
@@ -29,6 +29,7 @@ import {
29
29
  getVaultAddressSync,
30
30
  getVaultDepositorAddressSync,
31
31
  getVaultProtocolAddressSync,
32
+ getFeeUpdateAddressSync,
32
33
  } from './addresses';
33
34
  import {
34
35
  AccountMeta,
@@ -49,6 +50,8 @@ import {
49
50
  TOKEN_PROGRAM_ID,
50
51
  } from '@solana/spl-token';
51
52
  import {
53
+ FeeUpdate,
54
+ FeeUpdateStatus,
52
55
  FuelDistributionMode,
53
56
  Vault,
54
57
  VaultDepositor,
@@ -63,6 +66,7 @@ import { UserMapConfig } from '@drift-labs/sdk';
63
66
  import { calculateRealizedVaultDepositorEquity } from './math';
64
67
  import { Metaplex } from '@metaplex-foundation/js';
65
68
  import { getOrCreateATAInstruction } from './utils';
69
+ import { VAULT_ADMIN_KEY } from './constants';
66
70
 
67
71
  type OracleFeedConfig = {
68
72
  feed: PublicKey;
@@ -133,7 +137,10 @@ export class VaultClient {
133
137
  userAccounts: UserAccount[],
134
138
  writableSpotMarketIndexes: number[],
135
139
  vaultAccount: Vault,
136
- userStats: UserStatsAccount
140
+ userStats: UserStatsAccount,
141
+ skipVaultProtocol = false,
142
+ skipFuelOverflow = false,
143
+ skipFeeUpdate = false
137
144
  ) {
138
145
  const remainingAccounts = this.driftClient.getRemainingAccounts({
139
146
  userAccounts,
@@ -145,7 +152,23 @@ export class VaultClient {
145
152
  (userStats.fuelOverflowStatus & FuelOverflowStatus.Exists) ===
146
153
  FuelOverflowStatus.Exists;
147
154
 
148
- if (hasFuelOverflow) {
155
+ const hasFeeUpdate =
156
+ (vaultAccount.feeUpdateStatus & FeeUpdateStatus.PendingFeeUpdate) ===
157
+ FeeUpdateStatus.PendingFeeUpdate;
158
+
159
+ if (hasFeeUpdate && !skipFeeUpdate) {
160
+ const feeUpdate = getFeeUpdateAddressSync(
161
+ this.program.programId,
162
+ vaultAccount.pubkey
163
+ );
164
+ remainingAccounts.push({
165
+ pubkey: feeUpdate,
166
+ isSigner: false,
167
+ isWritable: true,
168
+ });
169
+ }
170
+
171
+ if (hasFuelOverflow && !skipFuelOverflow) {
149
172
  const fuelOverflow = getFuelOverflowAccountPublicKey(
150
173
  this.driftClient.program.programId,
151
174
  vaultAccount.pubkey
@@ -157,7 +180,7 @@ export class VaultClient {
157
180
  });
158
181
  }
159
182
 
160
- if (hasVaultProtocol) {
183
+ if (hasVaultProtocol && !skipVaultProtocol) {
161
184
  const vaultProtocol = this.getVaultProtocolAddress(vaultAccount.pubkey);
162
185
  remainingAccounts.push({
163
186
  pubkey: vaultProtocol,
@@ -169,6 +192,18 @@ export class VaultClient {
169
192
  return remainingAccounts;
170
193
  }
171
194
 
195
+ private async checkIfAccountExists(account: PublicKey): Promise<boolean> {
196
+ try {
197
+ const accountInfo = await this.driftClient.connection.getAccountInfo(
198
+ account
199
+ );
200
+ return accountInfo != null;
201
+ } catch (e) {
202
+ // Doesn't already exist
203
+ return false;
204
+ }
205
+ }
206
+
172
207
  /**
173
208
  * Unsubscribes from the vault users map. Call this to clean up any dangling promises.
174
209
  */
@@ -182,6 +217,10 @@ export class VaultClient {
182
217
  return await this.program.account.vault.fetch(vault);
183
218
  }
184
219
 
220
+ public async getFeeUpdate(feeUpdate: PublicKey): Promise<FeeUpdate> {
221
+ return await this.program.account.feeUpdate.fetch(feeUpdate);
222
+ }
223
+
185
224
  public async getVaultAndSlot(
186
225
  vault: PublicKey
187
226
  ): Promise<{ vault: Vault; slot: number }> {
@@ -909,7 +948,10 @@ export class VaultClient {
909
948
  [user.getUserAccount()],
910
949
  [vaultAccount.spotMarketIndex],
911
950
  vaultAccount,
912
- userStats
951
+ userStats,
952
+ false,
953
+ true,
954
+ true
913
955
  );
914
956
 
915
957
  const accounts = {
@@ -991,7 +1033,10 @@ export class VaultClient {
991
1033
  [user.getUserAccount()],
992
1034
  [vaultAccount.spotMarketIndex],
993
1035
  vaultAccount,
994
- userStats
1036
+ userStats,
1037
+ false,
1038
+ true,
1039
+ true
995
1040
  );
996
1041
 
997
1042
  const accounts = {
@@ -1047,7 +1092,10 @@ export class VaultClient {
1047
1092
  [user.getUserAccount()],
1048
1093
  [],
1049
1094
  vaultAccount,
1050
- userStats
1095
+ userStats,
1096
+ false,
1097
+ true,
1098
+ true
1051
1099
  );
1052
1100
 
1053
1101
  return this.program.instruction.mangerCancelWithdrawRequest({
@@ -1081,7 +1129,10 @@ export class VaultClient {
1081
1129
  [user.getUserAccount()],
1082
1130
  [vaultAccount.spotMarketIndex],
1083
1131
  vaultAccount,
1084
- userStats
1132
+ userStats,
1133
+ false,
1134
+ false,
1135
+ false
1085
1136
  );
1086
1137
 
1087
1138
  const spotMarket = this.driftClient.getSpotMarketAccount(
@@ -1218,7 +1269,10 @@ export class VaultClient {
1218
1269
  [user.getUserAccount()],
1219
1270
  [vaultAccount.spotMarketIndex],
1220
1271
  vaultAccount,
1221
- userStats
1272
+ userStats,
1273
+ false,
1274
+ false,
1275
+ false
1222
1276
  );
1223
1277
 
1224
1278
  const accounts = {
@@ -1274,7 +1328,10 @@ export class VaultClient {
1274
1328
  [user.getUserAccount()],
1275
1329
  [vaultAccount.spotMarketIndex],
1276
1330
  vaultAccount,
1277
- userStats
1331
+ userStats,
1332
+ false,
1333
+ true,
1334
+ true
1278
1335
  );
1279
1336
 
1280
1337
  const accounts = {
@@ -1334,7 +1391,10 @@ export class VaultClient {
1334
1391
  [user.getUserAccount()],
1335
1392
  [vaultAccount.spotMarketIndex],
1336
1393
  vaultAccount,
1337
- userStats
1394
+ userStats,
1395
+ false,
1396
+ true,
1397
+ true
1338
1398
  );
1339
1399
 
1340
1400
  const accounts = {
@@ -1551,7 +1611,10 @@ export class VaultClient {
1551
1611
  [user.getUserAccount()],
1552
1612
  [vaultAccount.spotMarketIndex],
1553
1613
  vaultAccount,
1554
- userStats
1614
+ userStats,
1615
+ false,
1616
+ true,
1617
+ true
1555
1618
  );
1556
1619
 
1557
1620
  ixs.push(
@@ -1637,7 +1700,10 @@ export class VaultClient {
1637
1700
  [user.getUserAccount()],
1638
1701
  [vaultAccount.spotMarketIndex],
1639
1702
  vaultAccount,
1640
- userStats
1703
+ userStats,
1704
+ false,
1705
+ true,
1706
+ true
1641
1707
  );
1642
1708
 
1643
1709
  return await this.program.methods
@@ -1715,7 +1781,10 @@ export class VaultClient {
1715
1781
  [user.getUserAccount()],
1716
1782
  [vaultAccount.spotMarketIndex],
1717
1783
  vaultAccount,
1718
- userStats
1784
+ userStats,
1785
+ false,
1786
+ false,
1787
+ false
1719
1788
  );
1720
1789
 
1721
1790
  const driftStateKey = await this.driftClient.getStatePublicKey();
@@ -1890,7 +1959,10 @@ export class VaultClient {
1890
1959
  [user.getUserAccount()],
1891
1960
  [vaultAccount.spotMarketIndex],
1892
1961
  vaultAccount,
1893
- userStats
1962
+ userStats,
1963
+ false,
1964
+ false,
1965
+ false
1894
1966
  );
1895
1967
 
1896
1968
  const accounts = {
@@ -1956,7 +2028,10 @@ export class VaultClient {
1956
2028
  [user.getUserAccount()],
1957
2029
  [vaultAccount.spotMarketIndex],
1958
2030
  vaultAccount,
1959
- userStats
2031
+ userStats,
2032
+ false,
2033
+ false,
2034
+ false
1960
2035
  );
1961
2036
 
1962
2037
  const driftStateKey = await this.driftClient.getStatePublicKey();
@@ -2077,7 +2152,10 @@ export class VaultClient {
2077
2152
  [user.getUserAccount()],
2078
2153
  [vaultAccount.spotMarketIndex],
2079
2154
  vaultAccount,
2080
- userStats
2155
+ userStats,
2156
+ false,
2157
+ false,
2158
+ false
2081
2159
  );
2082
2160
  if (vaultAccount.vaultProtocol) {
2083
2161
  const vaultProtocol = this.getVaultProtocolAddress(
@@ -2188,7 +2266,10 @@ export class VaultClient {
2188
2266
  [user.getUserAccount()],
2189
2267
  [vaultAccount.spotMarketIndex],
2190
2268
  vaultAccount,
2191
- userStats
2269
+ userStats,
2270
+ false,
2271
+ false,
2272
+ false
2192
2273
  );
2193
2274
 
2194
2275
  if (this.cliMode) {
@@ -2234,6 +2315,9 @@ export class VaultClient {
2234
2315
  public async getLiquidateIx(
2235
2316
  vaultDepositor: PublicKey
2236
2317
  ): Promise<TransactionInstruction> {
2318
+ if (!this.driftClient.wallet.publicKey.equals(VAULT_ADMIN_KEY)) {
2319
+ throw new Error('Only vault admin can liquidate');
2320
+ }
2237
2321
  const vaultDepositorAccount =
2238
2322
  await this.program.account.vaultDepositor.fetch(vaultDepositor);
2239
2323
  const vault = vaultDepositorAccount.vault;
@@ -2252,7 +2336,10 @@ export class VaultClient {
2252
2336
  [user.getUserAccount()],
2253
2337
  [vaultAccount.spotMarketIndex],
2254
2338
  vaultAccount,
2255
- userStats
2339
+ userStats,
2340
+ false,
2341
+ true,
2342
+ true
2256
2343
  );
2257
2344
 
2258
2345
  const driftStateKey = await this.driftClient.getStatePublicKey();
@@ -2265,6 +2352,7 @@ export class VaultClient {
2265
2352
  driftUser: vaultAccount.user,
2266
2353
  driftState: driftStateKey,
2267
2354
  driftProgram: this.driftClient.program.programId,
2355
+ authority: vaultDepositorAccount.authority,
2268
2356
  };
2269
2357
 
2270
2358
  if (this.cliMode) {
@@ -2276,8 +2364,8 @@ export class VaultClient {
2276
2364
  } else {
2277
2365
  return this.program.instruction.liquidate({
2278
2366
  accounts: {
2279
- authority: this.driftClient.wallet.publicKey,
2280
2367
  ...accounts,
2368
+ admin: this.driftClient.wallet.publicKey,
2281
2369
  },
2282
2370
  remainingAccounts,
2283
2371
  });
@@ -2745,7 +2833,10 @@ export class VaultClient {
2745
2833
  [user.getUserAccount()],
2746
2834
  [],
2747
2835
  vaultAccount,
2748
- userStats
2836
+ userStats,
2837
+ false,
2838
+ true,
2839
+ true
2749
2840
  );
2750
2841
 
2751
2842
  const accounts = {
@@ -2812,7 +2903,10 @@ export class VaultClient {
2812
2903
  [user.getUserAccount()],
2813
2904
  [],
2814
2905
  vaultAccount,
2815
- userStats
2906
+ userStats,
2907
+ false,
2908
+ true,
2909
+ true
2816
2910
  );
2817
2911
 
2818
2912
  if (this.cliMode) {
@@ -2866,7 +2960,10 @@ export class VaultClient {
2866
2960
  [user.getUserAccount()],
2867
2961
  [],
2868
2962
  vaultAccount,
2869
- userStats
2963
+ userStats,
2964
+ false,
2965
+ true,
2966
+ true
2870
2967
  );
2871
2968
 
2872
2969
  const spotMarket = this.driftClient.getSpotMarketAccount(
@@ -3250,4 +3347,114 @@ export class VaultClient {
3250
3347
  })
3251
3348
  .instruction();
3252
3349
  }
3350
+
3351
+ public async adminInitFeeUpdate(
3352
+ vault: PublicKey,
3353
+ uiTxParams?: TxParams
3354
+ ): Promise<TransactionSignature> {
3355
+ const ix = await this.getAdminInitFeeUpdateIx(vault);
3356
+ return await this.createAndSendTxn([ix], uiTxParams);
3357
+ }
3358
+
3359
+ public async getAdminInitFeeUpdateIx(
3360
+ vault: PublicKey
3361
+ ): Promise<TransactionInstruction> {
3362
+ const feeUpdate = getFeeUpdateAddressSync(this.program.programId, vault);
3363
+
3364
+ return this.program.instruction.adminInitFeeUpdate({
3365
+ accounts: {
3366
+ vault,
3367
+ admin: this.driftClient.wallet.publicKey,
3368
+ feeUpdate,
3369
+ systemProgram: SystemProgram.programId,
3370
+ },
3371
+ });
3372
+ }
3373
+
3374
+ public async adminDeleteFeeUpdate(
3375
+ vault: PublicKey,
3376
+ uiTxParams?: TxParams
3377
+ ): Promise<TransactionSignature> {
3378
+ const ix = await this.getAdminDeleteFeeUpdateIx(vault);
3379
+ return await this.createAndSendTxn([ix], uiTxParams);
3380
+ }
3381
+
3382
+ public async getAdminDeleteFeeUpdateIx(
3383
+ vault: PublicKey
3384
+ ): Promise<TransactionInstruction> {
3385
+ const feeUpdate = getFeeUpdateAddressSync(this.program.programId, vault);
3386
+
3387
+ return this.program.instruction.adminDeleteFeeUpdate({
3388
+ accounts: {
3389
+ vault,
3390
+ admin: this.driftClient.wallet.publicKey,
3391
+ feeUpdate,
3392
+ },
3393
+ });
3394
+ }
3395
+
3396
+ public async managerUpdateFees(
3397
+ vault: PublicKey,
3398
+ params: {
3399
+ timelockDuration: BN;
3400
+ newManagementFee: BN | null;
3401
+ newProfitShare: number | null;
3402
+ newHurdleRate: number | null;
3403
+ },
3404
+ uiTxParams?: TxParams
3405
+ ): Promise<TransactionSignature> {
3406
+ const feeUpdate = getFeeUpdateAddressSync(this.program.programId, vault);
3407
+ const ixs: TransactionInstruction[] = [];
3408
+ if (!(await this.checkIfAccountExists(feeUpdate))) {
3409
+ throw new Error(
3410
+ 'Fee update account does not exist, it must be created by an admin first'
3411
+ );
3412
+ }
3413
+ ixs.push(await this.getManagerUpdateFeesIx(vault, params));
3414
+ return await this.createAndSendTxn(ixs, uiTxParams);
3415
+ }
3416
+
3417
+ public async getManagerUpdateFeesIx(
3418
+ vault: PublicKey,
3419
+ params: {
3420
+ timelockDuration: BN;
3421
+ newManagementFee: BN | null;
3422
+ newProfitShare: number | null;
3423
+ newHurdleRate: number | null;
3424
+ }
3425
+ ): Promise<TransactionInstruction> {
3426
+ const vaultAccount = await this.program.account.vault.fetch(vault);
3427
+ const feeUpdate = getFeeUpdateAddressSync(this.program.programId, vault);
3428
+
3429
+ return this.program.instruction.managerUpdateFees(params, {
3430
+ accounts: {
3431
+ vault,
3432
+ manager: vaultAccount.manager,
3433
+ feeUpdate,
3434
+ },
3435
+ });
3436
+ }
3437
+
3438
+ public async managerCancelFeeUpdate(
3439
+ vault: PublicKey,
3440
+ uiTxParams?: TxParams
3441
+ ): Promise<TransactionSignature> {
3442
+ const ix = await this.getManagerCancelFeeUpdateIx(vault);
3443
+ return await this.createAndSendTxn([ix], uiTxParams);
3444
+ }
3445
+
3446
+ public async getManagerCancelFeeUpdateIx(
3447
+ vault: PublicKey
3448
+ ): Promise<TransactionInstruction> {
3449
+ const vaultAccount = await this.program.account.vault.fetch(vault);
3450
+ const feeUpdate = getFeeUpdateAddressSync(this.program.programId, vault);
3451
+
3452
+ return this.program.instruction.managerCancelFeeUpdate({
3453
+ accounts: {
3454
+ vault,
3455
+ manager: vaultAccount.manager,
3456
+ feeUpdate,
3457
+ },
3458
+ });
3459
+ }
3253
3460
  }