@drift-labs/vaults-sdk 0.1.8 → 0.1.10

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.
package/cli/cli.ts CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  deposit,
14
14
  requestWithdraw,
15
15
  withdraw,
16
+ forceWithdraw,
16
17
  listDepositorsForVault,
17
18
  managerUpdateMarginTradingEnabled
18
19
  } from "./commands";
@@ -135,6 +136,11 @@ program
135
136
  .addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(false))
136
137
  .addOption(new Option("--authority <vaultDepositorAuthority>", "VaultDepositor authority address").makeOptionMandatory(false))
137
138
  .action((opts) => withdraw(program, opts));
139
+ program
140
+ .command("force-withdraw")
141
+ .description("Forces the vault to send out a withdraw after the redeem period has passed")
142
+ .addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(false))
143
+ .action((opts) => forceWithdraw(program, opts));
138
144
 
139
145
  program.parseAsync().then(() => {
140
146
  process.exit(0);
@@ -0,0 +1,24 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import {
3
+ OptionValues,
4
+ Command
5
+ } from "commander";
6
+ import { getCommandContext } from "../utils";
7
+
8
+ export const forceWithdraw = async (program: Command, cmdOpts: OptionValues) => {
9
+
10
+ let vaultDepositorAddress: PublicKey;
11
+ try {
12
+ vaultDepositorAddress = new PublicKey(cmdOpts.vaultDepositorAddress as string);
13
+ } catch (err) {
14
+ console.error("Invalid vault depositor address");
15
+ process.exit(1);
16
+ }
17
+
18
+ const {
19
+ driftVault
20
+ } = await getCommandContext(program, true);
21
+
22
+ const tx = await driftVault.forceWithdraw(vaultDepositorAddress);
23
+ console.log(`Forced withdraw from vault: ${tx}`);
24
+ };
@@ -11,6 +11,7 @@ export * from './applyProfitShare';
11
11
  export * from './initVaultDepositor';
12
12
  export * from './deposit';
13
13
  export * from './requestWithdraw';
14
+ export * from './forceWithdraw';
14
15
  export * from './withdraw';
15
16
  export * from './listDepositorsForVault';
16
17
  export * from './managerUpdateMarginTradingEnabled';
@@ -108,7 +108,7 @@ export const initVault = async (program: Command, cmdOpts: OptionValues) => {
108
108
  });
109
109
  console.log('');
110
110
  const answer = await new Promise(resolve => {
111
- readline.question('Is the above information correct? (yes/no) ', (answer) => {
111
+ readline.question('Is the above information correct? (yes/no) ', (answer: string) => {
112
112
  readline.close();
113
113
  resolve(answer);
114
114
  });
@@ -90,7 +90,7 @@ export const managerUpdateVault = async (program: Command, cmdOpts: OptionValues
90
90
  });
91
91
  console.log('');
92
92
  const answer = await new Promise(resolve => {
93
- readline.question('Is the above information correct? (yes/no) ', (answer) => {
93
+ readline.question('Is the above information correct? (yes/no) ', (answer: string) => {
94
94
  readline.close();
95
95
  resolve(answer);
96
96
  });
package/cli/utils.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DriftClient, Wallet, loadKeypair } from "@drift-labs/sdk";
2
- import { VAULT_PROGRAM_ID, Vault, VaultClient, decodeName } from "../src";
2
+ import { VAULT_PROGRAM_ID, Vault, VaultClient, VaultDepositor, decodeName } from "../src";
3
3
  import { Command } from "commander";
4
4
  import { Connection, Keypair } from "@solana/web3.js";
5
5
  import { AnchorProvider } from "@coral-xyz/anchor";
@@ -54,7 +54,7 @@ export function printVault(vault: Vault) {
54
54
  };
55
55
  }
56
56
 
57
- export function printVaultDepositor(vaultDepositor) {
57
+ export function printVaultDepositor(vaultDepositor: VaultDepositor) {
58
58
  console.log(`vault: ${vaultDepositor.vault.toBase58()}`);
59
59
  console.log(`pubkey: ${vaultDepositor.pubkey.toBase58()}`);
60
60
  console.log(`authority: ${vaultDepositor.authority.toBase58()}`);
@@ -93,6 +93,7 @@ export declare class VaultClient {
93
93
  }): Promise<TransactionSignature>;
94
94
  requestWithdraw(vaultDepositor: PublicKey, amount: BN, withdrawUnit: WithdrawUnit): Promise<TransactionSignature>;
95
95
  withdraw(vaultDepositor: PublicKey): Promise<TransactionSignature>;
96
+ forceWithdraw(vaultDepositor: PublicKey): Promise<TransactionSignature>;
96
97
  cancelRequestWithdraw(vaultDepositor: PublicKey): Promise<TransactionSignature>;
97
98
  /**
98
99
  * Liquidates (become delegate for) a vault.
@@ -527,6 +527,60 @@ class VaultClient {
527
527
  return await this.createAndSendTxn([withdrawIx]);
528
528
  }
529
529
  }
530
+ async forceWithdraw(vaultDepositor) {
531
+ const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
532
+ const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
533
+ const user = new sdk_1.User({
534
+ driftClient: this.driftClient,
535
+ userAccountPublicKey: vaultAccount.user,
536
+ });
537
+ await user.subscribe();
538
+ const remainingAccounts = this.driftClient.getRemainingAccounts({
539
+ userAccounts: [user.getUserAccount()],
540
+ writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
541
+ });
542
+ const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
543
+ const driftStateKey = await this.driftClient.getStatePublicKey();
544
+ const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
545
+ if (!spotMarket) {
546
+ throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
547
+ }
548
+ const accounts = {
549
+ manager: this.driftClient.wallet.publicKey,
550
+ vault: vaultDepositorAccount.vault,
551
+ vaultDepositor,
552
+ vaultTokenAccount: vaultAccount.tokenAccount,
553
+ driftUserStats: userStatsKey,
554
+ driftUser: vaultAccount.user,
555
+ driftState: driftStateKey,
556
+ driftSpotMarketVault: spotMarket.vault,
557
+ driftSigner: this.driftClient.getStateAccount().signer,
558
+ userTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, vaultDepositorAccount.authority, true),
559
+ driftProgram: this.driftClient.program.programId,
560
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
561
+ };
562
+ if (this.cliMode) {
563
+ return await this.program.methods
564
+ .forceWithdraw()
565
+ .preInstructions([
566
+ web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
567
+ units: 400000,
568
+ }),
569
+ ])
570
+ .accounts(accounts)
571
+ .remainingAccounts(remainingAccounts)
572
+ .rpc();
573
+ }
574
+ else {
575
+ const forceWithdrawIx = this.program.instruction.forceWithdraw({
576
+ accounts: {
577
+ ...accounts,
578
+ },
579
+ remainingAccounts,
580
+ });
581
+ return await this.createAndSendTxn([forceWithdrawIx]);
582
+ }
583
+ }
530
584
  async cancelRequestWithdraw(vaultDepositor) {
531
585
  const vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(vaultDepositor);
532
586
  const vaultAccount = await this.program.account.vault.fetch(vaultDepositorAccount.vault);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/vaults-sdk",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "directories": {
@@ -9,7 +9,7 @@
9
9
  "dependencies": {
10
10
  "@coral-xyz/anchor": "^0.26.0",
11
11
  "@drift-labs/competitions-sdk": "^0.2.5",
12
- "@drift-labs/sdk": "2.44.0-beta.0",
12
+ "@drift-labs/sdk": "2.45.0-beta.0",
13
13
  "@solana/web3.js": "1.73.2",
14
14
  "commander": "^11.0.0",
15
15
  "dotenv": "^16.3.1",
@@ -814,6 +814,82 @@ export class VaultClient {
814
814
  }
815
815
  }
816
816
 
817
+ public async forceWithdraw(
818
+ vaultDepositor: PublicKey
819
+ ): Promise<TransactionSignature> {
820
+ const vaultDepositorAccount =
821
+ await this.program.account.vaultDepositor.fetch(vaultDepositor);
822
+ const vaultAccount = await this.program.account.vault.fetch(
823
+ vaultDepositorAccount.vault
824
+ );
825
+
826
+ const user = new User({
827
+ driftClient: this.driftClient,
828
+ userAccountPublicKey: vaultAccount.user,
829
+ });
830
+ await user.subscribe();
831
+ const remainingAccounts = this.driftClient.getRemainingAccounts({
832
+ userAccounts: [user.getUserAccount()],
833
+ writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
834
+ });
835
+
836
+ const userStatsKey = getUserStatsAccountPublicKey(
837
+ this.driftClient.program.programId,
838
+ vaultDepositorAccount.vault
839
+ );
840
+
841
+ const driftStateKey = await this.driftClient.getStatePublicKey();
842
+
843
+ const spotMarket = this.driftClient.getSpotMarketAccount(
844
+ vaultAccount.spotMarketIndex
845
+ );
846
+ if (!spotMarket) {
847
+ throw new Error(
848
+ `Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`
849
+ );
850
+ }
851
+
852
+ const accounts = {
853
+ manager: this.driftClient.wallet.publicKey,
854
+ vault: vaultDepositorAccount.vault,
855
+ vaultDepositor,
856
+ vaultTokenAccount: vaultAccount.tokenAccount,
857
+ driftUserStats: userStatsKey,
858
+ driftUser: vaultAccount.user,
859
+ driftState: driftStateKey,
860
+ driftSpotMarketVault: spotMarket.vault,
861
+ driftSigner: this.driftClient.getStateAccount().signer,
862
+ userTokenAccount: getAssociatedTokenAddressSync(
863
+ spotMarket.mint,
864
+ vaultDepositorAccount.authority,
865
+ true
866
+ ),
867
+ driftProgram: this.driftClient.program.programId,
868
+ tokenProgram: TOKEN_PROGRAM_ID,
869
+ };
870
+
871
+ if (this.cliMode) {
872
+ return await this.program.methods
873
+ .forceWithdraw()
874
+ .preInstructions([
875
+ ComputeBudgetProgram.setComputeUnitLimit({
876
+ units: 400_000,
877
+ }),
878
+ ])
879
+ .accounts(accounts)
880
+ .remainingAccounts(remainingAccounts)
881
+ .rpc();
882
+ } else {
883
+ const forceWithdrawIx = this.program.instruction.forceWithdraw({
884
+ accounts: {
885
+ ...accounts,
886
+ },
887
+ remainingAccounts,
888
+ });
889
+ return await this.createAndSendTxn([forceWithdrawIx]);
890
+ }
891
+ }
892
+
817
893
  public async cancelRequestWithdraw(
818
894
  vaultDepositor: PublicKey
819
895
  ): Promise<TransactionSignature> {
package/tsconfig.json CHANGED
@@ -5,15 +5,19 @@
5
5
  "esModuleInterop": true,
6
6
  "declaration": true,
7
7
  "outDir": "./lib",
8
+ "rootDirs": [
9
+ "src",
10
+ "cli"
11
+ ],
8
12
  "resolveJsonModule": true,
9
13
  "skipLibCheck": true,
10
14
  "strict": true
11
15
  },
12
16
  "include": [
13
- "src"
17
+ "src/**/*"
14
18
  ],
15
19
  "exclude": [
16
20
  "node_modules",
17
21
  "tests"
18
22
  ]
19
- }
23
+ }