@drift-labs/vaults-sdk 0.5.7 → 0.5.9

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/.env.example CHANGED
@@ -1,2 +1,3 @@
1
1
  RPC_URL=
2
2
  KEYPAIR_PATH=
3
+ ENV=
package/README.md CHANGED
@@ -2,14 +2,15 @@
2
2
 
3
3
  This repo has a simple CLI for interacting with the vault (run from this `package.json`):
4
4
 
5
- This CLI utility requires an RPC node and keypair to sign transactions (similar to solana cli). You can either provide these as environment variables or in a `.env` file, or use the `--keypair` and `--url` flags.
5
+ This CLI utility requires an RPC node and keypair to sign transactions. You can either provide these as environment variables, in a `.env` file, or use cli flags (like `--keypair` and `--url`).
6
6
 
7
7
  Required Environment Variables or Flags:
8
8
 
9
9
  Environment Variable| command line flag | Description
10
10
  --------------------|-------------------|------------
11
11
  RPC_URL | --url | The RPC node to connect to for transactions
12
- KEYPAIR_PATH | --keypair | Path to keypair to sign transactions
12
+ KEYPAIR_PATH | --keypair | Path to keypair (file or base58) to sign transactions. This may also be a ledger filepath (e.g. `usb://ledger/<wallet_id>?key=0/0`)
13
+ ENV | --env | 'devnet' or 'mainnet' (default: 'mainnet')
13
14
 
14
15
 
15
16
  View available commands, run with `--help` in nested commands to get available options for each command
@@ -17,6 +18,11 @@ View available commands, run with `--help` in nested commands to get available o
17
18
  yarn cli --help
18
19
  ```
19
20
 
21
+ ## Manager Commands
22
+
23
+ The following commands are menat to be run by Vault Managers. `KEYPAIR_PATH` should be the manager's keypair.
24
+
25
+ ### Initialize a new vault
20
26
 
21
27
  Init a new vault. This will initialize a new vault and update you (the manager) as the delegate, unless `--delegate` is specified.
22
28
  ```
@@ -39,6 +45,8 @@ Options:
39
45
  -h, --help display help for command
40
46
  ```
41
47
 
48
+ ### Update Vault Params
49
+
42
50
  To update params in a vault:
43
51
  ```
44
52
  $ yarn cli manager-update-vault --help
@@ -57,39 +65,88 @@ Options:
57
65
  -h, --help display help for command
58
66
  ```
59
67
 
60
- Make a deposit into a vault (as a manager, `DEPOSIT_AMOUNT` in human precision):
68
+ ### Update Margin Trading Enabled
69
+
70
+ If you wish to trade with spot margin on the vault, you must enable margin trading:
71
+ ```
72
+ yarn cli manager-update-margin-trading-enabled --vault-address=<VAULT_ADDRESS> --enabled=<true|false>
73
+ ```
74
+
75
+ ### Manager Deposit
76
+
77
+ Make a deposit into a vault as the manager (`DEPOSIT_AMOUNT` in human precision, e.g. 5 for 5 USDC):
61
78
  ```
62
79
  yarn cli manager-deposit --vault-address=<VAULT_ADDRESS> --amount=<DEPOSIT_AMOUNT>
63
80
  ```
64
81
 
65
- Make a withdraw request from a vault (as a manager, `SHARES` in raw precision):
82
+ ### Manager Withdraw
83
+
84
+ Make a withdraw request from a vault as the manager (`SHARES` in raw precision):
66
85
  ```
67
86
  yarn cli manager-request-withdraw --vault-address=<VAULT_ADDRESS> --amount=<SHARES>
68
87
  ```
69
88
 
89
+ After the redeem period has passed, the manager can complete the withdraw:
90
+ ```
91
+ yarn cli manager-withdraw --vault-address=<VAULT_ADDRESS>
92
+ ```
93
+
94
+ ### Apply Profit Share
70
95
  Manager can trigger a profit share calculation (this looks up all `VaultDepositors` for a vault eligible for profit share and batch processes them):
71
96
  ```
72
97
  yarn cli apply-profit-share-all --vault-address=<VAULT_ADDRESS>
73
98
  ```
74
99
 
100
+ ## Depositor Commands
101
+
102
+
103
+ ### Deposit into a vault
104
+
105
+ #### Permissioned Vaults
75
106
 
76
- For permissioned vaults, initialize a `VaultDepositor` for someone to deposit.
107
+ Permissioned vaults require the __manager__ to initialize the `VaultDepositor` account before a depositor can deposit.
108
+
109
+ Initialize a `VaultDepositor` account for `AUTHORITY_TO_ALLOW_DEPOSIT` to deposit:
77
110
  ```
78
111
  yarn cli init-vault-depositor --vault-address=<VAULT_ADDRESS> --deposit-authority=<AUTHORITY_TO_ALLOW_DEPOSIT>
79
112
  ```
80
- Then send them the `VAULT_DEPOSITOR_ADDRESS`
81
113
 
82
114
 
83
- Make a deposit into a vault (as a non-manager, `DEPOSIT_AMOUNT` in human precision):
115
+ #### Permissioneless Vaults
116
+
117
+ Permissionless vaults allow anyone to deposit. The `deposit` instruction will initialize a `VaultDepositor` account if one does not exist.
118
+ `DEPOSIT_AMOUNT` in human precision of the deposit token (e.g. 5 for 5 USDC).
119
+
120
+ ```
121
+ yarn cli deposit --vault-address=<VAULT_ADDRESS> --deposit-authority=<DEPOSIT_AUTHORITY> --amount=<DEPOSIT_AMOUNT>
122
+ ```
123
+
124
+ Alternatively, you can pass in the `VaultDepositor` address directly:
84
125
  ```
85
126
  yarn cli deposit --vault-depositor-address=<VAULT_DEPOSITOR_ADDRESS> --amount=<DEPOSIT_AMOUNT>
86
127
  ```
87
128
 
129
+ ### Withdraw from a vault
88
130
 
89
- To print out the current state of a `Vault` or `VaultDepositor`:
131
+ Request a withdraw from a vault:
132
+ ```
133
+ yarn cli request-withdraw --vault-address=<VAULT_ADDRESS> --authority=<AUTHORITY> --amount=<WITHDRAW_AMOUNT>
134
+ ```
135
+
136
+ After the redeem period has passed, the depositor can complete the withdraw:
137
+ ```
138
+ yarn cli withdraw --vault-address=<VAULT_ADDRESS> --authority=<AUTHORITY>
139
+ ```
140
+
141
+ ## View only commands
142
+
143
+ To print out the current state of a `Vault`:
90
144
  ```
91
145
  yarn cli view-vault --vault-address=<VAULT_ADDRESS>
146
+ ```
92
147
 
148
+ To print out the current state of a `VaultDepositor`:
149
+ ```
93
150
  yarn cli view-vault-depositor --vault-depositor-address=<VAULT_DEPOSITOR_ADDRESS>
94
151
  ```
95
152
 
package/cli/cli.ts CHANGED
@@ -30,7 +30,8 @@ const program = new Command();
30
30
  program
31
31
  .addOption(new Option("-u, --url <url>", "RPC URL to use for requests").env("RPC_URL").makeOptionMandatory(true))
32
32
  .addOption(new Option("-k, --keypair <filepath>", "Path to keypair file").env("KEYPAIR_PATH"))
33
- .addOption(new Option("--commitment <commitment>", "State commitment to use").default("confirmed"));
33
+ .addOption(new Option("--commitment <commitment>", "State commitment to use").default("confirmed"))
34
+ .addOption(new Option("--env <env>", "Environment to use (devnet, mainnet-beta)").default("mainnet-beta").env("ENV"));
34
35
  program
35
36
  .command("init-vault")
36
37
  .description("Initialize a new vault")
@@ -126,20 +127,25 @@ program
126
127
  .action((opts) => initVaultDepositor(program, opts));
127
128
  program
128
129
  .command("deposit")
129
- .description("Deposit into a vault via VaultDepositor")
130
- .addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(true))
130
+ .description("Deposit into a vault via VaultDepositor (creates a VaultDepositor if one does not exist)")
131
+ .addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(false))
132
+ .addOption(new Option("--vault-address <address>", "Address of the vault to deposit into").makeOptionMandatory(false))
133
+ .addOption(new Option("--deposit-authority <vaultDepositorAuthority>", "VaultDepositor authority address").makeOptionMandatory(false))
131
134
  .addOption(new Option("--amount <amount>", "Amount to deposit (human format, 5 for 5 USDC)").makeOptionMandatory(true))
132
135
  .action((opts) => deposit(program, opts));
133
136
  program
134
137
  .command("request-withdraw")
135
138
  .description("Make a request to withdraw shares from the vaultm, redeem period starts now")
136
- .addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(true))
139
+ .addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(false))
140
+ .addOption(new Option("--vault-address <address>", "Address of the vault to deposit into").makeOptionMandatory(false))
141
+ .addOption(new Option("--authority <vaultDepositorAuthority>", "VaultDepositor authority address").makeOptionMandatory(false))
137
142
  .addOption(new Option("--amount <amount>", "Amount of shares to withdraw (raw format, as expected in the program)").makeOptionMandatory(true))
138
143
  .action((opts) => requestWithdraw(program, opts));
139
144
  program
140
145
  .command("withdraw")
141
146
  .description("Initiate the withdraw, after the redeem period has passed")
142
147
  .addOption(new Option("--vault-depositor-address <vaultDepositorAddress>", "VaultDepositor address").makeOptionMandatory(false))
148
+ .addOption(new Option("--vault-address <address>", "Address of the vault to deposit into").makeOptionMandatory(false))
143
149
  .addOption(new Option("--authority <vaultDepositorAuthority>", "VaultDepositor authority address").makeOptionMandatory(false))
144
150
  .action((opts) => withdraw(program, opts));
145
151
  program
@@ -5,15 +5,16 @@ import {
5
5
  Command
6
6
  } from "commander";
7
7
  import { getCommandContext } from "../utils";
8
+ import { getVaultDepositorAddressSync, VAULT_PROGRAM_ID } from "../../src";
8
9
 
9
10
  export const deposit = async (program: Command, cmdOpts: OptionValues) => {
10
11
 
11
- let vaultDepositorAddress: PublicKey;
12
- try {
13
- vaultDepositorAddress = new PublicKey(cmdOpts.vaultDepositorAddress as string);
14
- } catch (err) {
15
- console.error("Invalid vault depositor address");
16
- process.exit(1);
12
+ // verify correct args provided
13
+ if (!cmdOpts.vaultDepositorAddress) {
14
+ if (!cmdOpts.vaultAddress || !cmdOpts.depositAuthority) {
15
+ console.error("Must provide --vault-address and --deposit-authority if not providing --vault-depositor-address");
16
+ process.exit(1);
17
+ }
17
18
  }
18
19
 
19
20
  const {
@@ -21,18 +22,55 @@ export const deposit = async (program: Command, cmdOpts: OptionValues) => {
21
22
  driftVault
22
23
  } = await getCommandContext(program, true);
23
24
 
24
- const vaultDepositorAccount =
25
- await driftVault.program.account.vaultDepositor.fetch(vaultDepositorAddress);
26
- const vaultAddress = vaultDepositorAccount.vault;
27
- const vaultAccount = await driftVault.program.account.vault.fetch(vaultAddress);
28
- const spotMarket = driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
29
- if (!spotMarket) {
30
- throw new Error("No spot market found");
25
+ let vaultDepositorAddress: PublicKey;
26
+ let vaultAddress: PublicKey | undefined;
27
+ let depositAuthority: PublicKey | undefined;
28
+ if (cmdOpts.vaultDepositorAddress) {
29
+ vaultDepositorAddress = new PublicKey(cmdOpts.vaultDepositorAddress as string);
30
+ } else {
31
+ vaultAddress = new PublicKey(cmdOpts.vaultAddress as string);
32
+ depositAuthority = new PublicKey(cmdOpts.depositAuthority as string);
33
+ vaultDepositorAddress = getVaultDepositorAddressSync(VAULT_PROGRAM_ID, vaultAddress, depositAuthority);
31
34
  }
32
- const spotPrecision = TEN.pow(new BN(spotMarket.decimals));
33
- const depositBN = new BN(cmdOpts.amount * spotPrecision.toNumber());
34
35
 
35
- console.log(`depositing: ${depositBN.toString()}`);
36
- const tx = await driftVault.deposit(vaultDepositorAddress, depositBN);
37
- console.log(`Deposited ${cmdOpts.amount} to vault as manager: ${tx}`);
36
+ const vaultDepositorAccount =
37
+ await driftVault.program.account.vaultDepositor.fetchNullable(vaultDepositorAddress);
38
+ if (!vaultDepositorAccount) {
39
+ if (!vaultAddress || !depositAuthority) {
40
+ console.error("Must provide --vault-address and --deposit-authority if not providing --vault-depositor-address, and VaultDepositor account does not exist");
41
+ process.exit(1);
42
+ }
43
+
44
+ const vaultAccount = await driftVault.program.account.vault.fetch(vaultAddress);
45
+ const spotMarket = driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
46
+ if (!spotMarket) {
47
+ throw new Error("No spot market found");
48
+ }
49
+ const spotPrecision = TEN.pow(new BN(spotMarket.decimals));
50
+ const depositBN = new BN(cmdOpts.amount * spotPrecision.toNumber());
51
+
52
+ console.log(`depositing (initializing VaultDepositor account): ${depositBN.toString()}`);
53
+ const tx = await driftVault.deposit(
54
+ vaultDepositorAddress,
55
+ depositBN,
56
+ {
57
+ authority: depositAuthority,
58
+ vault: vaultAddress
59
+ });
60
+ console.log(`Deposited ${cmdOpts.amount} to vault: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
61
+ } else {
62
+ // VaultDepositor exists
63
+ const vaultAddress = vaultDepositorAccount.vault;
64
+ const vaultAccount = await driftVault.program.account.vault.fetch(vaultAddress);
65
+ const spotMarket = driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
66
+ if (!spotMarket) {
67
+ throw new Error("No spot market found");
68
+ }
69
+ const spotPrecision = TEN.pow(new BN(spotMarket.decimals));
70
+ const depositBN = new BN(cmdOpts.amount * spotPrecision.toNumber());
71
+
72
+ console.log(`depositing (existing VaultDepositor account): ${depositBN.toString()}`);
73
+ const tx = await driftVault.deposit(vaultDepositorAddress, depositBN);
74
+ console.log(`Deposited ${cmdOpts.amount} to vault: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
75
+ }
38
76
  };
@@ -131,12 +131,12 @@ export const initVault = async (program: Command, cmdOpts: OptionValues) => {
131
131
  permissioned,
132
132
  minDepositAmount: minDepositAmountBN,
133
133
  });
134
- console.log(`Initialized vault, tx: https://solscan.io/tx/${initTx}`);
134
+ console.log(`Initialized vault, tx: https://solscan.io/tx/${initTx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
135
135
 
136
136
  const vaultAddress = getVaultAddressSync(VAULT_PROGRAM_ID, vaultNameBytes);
137
137
  console.log(`\nNew vault address: ${vaultAddress}\n`);
138
138
 
139
139
  console.log(`Updating the drift account delegate to: ${delegate}...`);
140
140
  const updateDelegateTx = await driftVault.updateDelegate(vaultAddress, delegate);
141
- console.log(`update delegate tx: https://solscan.io/tx/${updateDelegateTx}`);
141
+ console.log(`update delegate tx: https://solscan.io/tx/${updateDelegateTx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
142
142
  };
@@ -16,9 +16,10 @@ export const managerCancelWithdraw = async (program: Command, cmdOpts: OptionVal
16
16
  }
17
17
 
18
18
  const {
19
- driftVault
19
+ driftVault,
20
+ driftClient
20
21
  } = await getCommandContext(program, true);
21
22
 
22
23
  const tx = await driftVault.managerCancelWithdrawRequest(vaultAddress);
23
- console.log(`Canceled withdraw as vault manager: https://solscan.io/tx/${tx}`);
24
+ console.log(`Canceled withdraw as vault manager: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
24
25
  };
@@ -30,5 +30,5 @@ export const managerDeposit = async (program: Command, cmdOpts: OptionValues) =>
30
30
  const depositBN = new BN(cmdOpts.amount * spotPrecision.toNumber());
31
31
 
32
32
  const tx = await driftVault.managerDeposit(vaultAddress, depositBN);
33
- console.log(`Deposited ${cmdOpts.amount} to vault as manager: ${tx}`);
33
+ console.log(`Deposited ${cmdOpts.amount} to vault as manager: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
34
34
  };
@@ -28,7 +28,7 @@ export const managerRequestWithdraw = async (program: Command, cmdOpts: OptionVa
28
28
 
29
29
  if (cmdOpts.shares && !cmdOpts.amount) {
30
30
  const tx = await driftVault.managerRequestWithdraw(vaultAddress, new BN(cmdOpts.shares), WithdrawUnit.SHARES);
31
- console.log(`Requested to withraw ${cmdOpts.shares} shares as vault manager: https://solscan.io/tx/${tx}`);
31
+ console.log(`Requested to withraw ${cmdOpts.shares} shares as vault manager: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
32
32
  } else if (cmdOpts.amount && !cmdOpts.shares) {
33
33
  const vault = await driftVault.getVault(vaultAddress);
34
34
  const spotMarket = driftClient.getSpotMarketAccount(vault.spotMarketIndex);
@@ -39,10 +39,8 @@ export const managerRequestWithdraw = async (program: Command, cmdOpts: OptionVa
39
39
  const spotPrecision = TEN.pow(new BN(spotMarket.decimals));
40
40
  const amount = parseFloat(cmdOpts.amount);
41
41
  const amountBN = numberToSafeBN(amount, spotPrecision);
42
- console.log(amount);
43
- console.log(amountBN.toString());
44
42
  const tx = await driftVault.managerRequestWithdraw(vaultAddress, amountBN, WithdrawUnit.TOKEN);
45
- console.log(`Requested to withdraw ${amount} ${decodeName(spotMarket.name)} as vault manager: https://solscan.io/tx/${tx}`);
43
+ console.log(`Requested to withdraw ${amount} ${decodeName(spotMarket.name)} as vault manager: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
46
44
 
47
45
  } else {
48
46
  console.error("Error: Either shares or amount must be provided, but not both.");
@@ -16,11 +16,12 @@ export const managerUpdateMarginTradingEnabled= async (program: Command, cmdOpts
16
16
  }
17
17
 
18
18
  const {
19
- driftVault
19
+ driftVault,
20
+ driftClient
20
21
  } = await getCommandContext(program, true);
21
22
 
22
23
  const enabled = cmdOpts.enabled ? (cmdOpts.enabled as string).toLowerCase() === "true" : false;
23
24
 
24
25
  const tx = await driftVault.updateMarginTradingEnabled(vaultAddress, enabled);
25
- console.log(`Updated margin trading vault manager: https://solscan.io/tx/${tx}`);
26
+ console.log(`Updated margin trading vault manager: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
26
27
  };
@@ -16,7 +16,8 @@ export const managerUpdateVaultDelegate = async (program: Command, cmdOpts: Opti
16
16
  }
17
17
 
18
18
  const {
19
- driftVault
19
+ driftVault,
20
+ driftClient
20
21
  } = await getCommandContext(program, true);
21
22
 
22
23
  let delegate = cmdOpts.delegate;
@@ -31,5 +32,5 @@ export const managerUpdateVaultDelegate = async (program: Command, cmdOpts: Opti
31
32
  }
32
33
 
33
34
  const tx = await driftVault.updateDelegate(vaultAddress, delegate);
34
- console.log(`Updated vault delegate to ${delegate.toBase58()}: https://solscan.io/tx/${tx}`);
35
+ console.log(`Updated vault delegate to ${delegate.toBase58()}: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
35
36
  };
@@ -16,9 +16,10 @@ export const managerWithdraw = async (program: Command, cmdOpts: OptionValues) =
16
16
  }
17
17
 
18
18
  const {
19
- driftVault
19
+ driftVault,
20
+ driftClient
20
21
  } = await getCommandContext(program, true);
21
22
 
22
23
  const tx = await driftVault.managerWithdraw(vaultAddress);
23
- console.log(`Withrew as vault manager: https://solscan.io/tx/${tx}`);
24
+ console.log(`Withrew as vault manager: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
24
25
  };
@@ -6,23 +6,39 @@ import {
6
6
  } from "commander";
7
7
  import { getCommandContext } from "../utils";
8
8
  import { WithdrawUnit } from "../../src/types/types";
9
+ import { getVaultDepositorAddressSync, VAULT_PROGRAM_ID } from "../../src";
9
10
 
10
11
  export const requestWithdraw = async (program: Command, cmdOpts: OptionValues) => {
11
12
 
12
- let vaultDepositorAddress: PublicKey;
13
- try {
14
- vaultDepositorAddress = new PublicKey(cmdOpts.vaultDepositorAddress as string);
15
- } catch (err) {
16
- console.error("Invalid vault depositor address");
17
- process.exit(1);
13
+ // verify correct args provided
14
+ if (!cmdOpts.vaultDepositorAddress) {
15
+ if (!cmdOpts.vaultAddress || !cmdOpts.authority) {
16
+ console.error("Must provide --vault-address and --authority if not providing --vault-depositor-address");
17
+ process.exit(1);
18
+ }
18
19
  }
19
20
 
20
21
  const {
21
- driftVault
22
+ driftVault,
23
+ driftClient
22
24
  } = await getCommandContext(program, true);
23
25
 
26
+ let vaultDepositorAddress: PublicKey;
27
+ if (cmdOpts.vaultDepositorAddress) {
28
+ try {
29
+ vaultDepositorAddress = new PublicKey(cmdOpts.vaultDepositorAddress as string);
30
+ } catch (err) {
31
+ console.error("Invalid vault depositor address");
32
+ process.exit(1);
33
+ }
34
+ } else {
35
+ const vaultAddress = new PublicKey(cmdOpts.vaultAddress as string);
36
+ const authority = new PublicKey(cmdOpts.authority as string);
37
+ vaultDepositorAddress = getVaultDepositorAddressSync(VAULT_PROGRAM_ID, vaultAddress, authority);
38
+ }
39
+
24
40
  const withdrawAmountBN = new BN(cmdOpts.amount);
25
41
 
26
42
  const tx = await driftVault.requestWithdraw(vaultDepositorAddress, withdrawAmountBN, WithdrawUnit.SHARES);
27
- console.log(`Requsted to withdraw ${cmdOpts.amount} shares from the vault: ${tx}`);
43
+ console.log(`Requested to withdraw ${cmdOpts.amount} shares from the vault: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
28
44
  };
@@ -4,21 +4,37 @@ import {
4
4
  Command
5
5
  } from "commander";
6
6
  import { getCommandContext } from "../utils";
7
+ import { getVaultDepositorAddressSync, VAULT_PROGRAM_ID } from "../../src";
7
8
 
8
9
  export const withdraw = async (program: Command, cmdOpts: OptionValues) => {
9
10
 
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);
11
+ // verify correct args provided
12
+ if (!cmdOpts.vaultDepositorAddress) {
13
+ if (!cmdOpts.vaultAddress || !cmdOpts.authority) {
14
+ console.error("Must provide --vault-address and --authority if not providing --vault-depositor-address");
15
+ process.exit(1);
16
+ }
16
17
  }
17
18
 
18
19
  const {
19
- driftVault
20
+ driftVault,
21
+ driftClient
20
22
  } = await getCommandContext(program, true);
21
23
 
24
+ let vaultDepositorAddress: PublicKey;
25
+ if (cmdOpts.vaultDepositorAddress) {
26
+ try {
27
+ vaultDepositorAddress = new PublicKey(cmdOpts.vaultDepositorAddress as string);
28
+ } catch (err) {
29
+ console.error("Invalid vault depositor address");
30
+ process.exit(1);
31
+ }
32
+ } else {
33
+ const vaultAddress = new PublicKey(cmdOpts.vaultAddress as string);
34
+ const authority = new PublicKey(cmdOpts.authority as string);
35
+ vaultDepositorAddress = getVaultDepositorAddressSync(VAULT_PROGRAM_ID, vaultAddress, authority);
36
+ }
37
+
22
38
  const tx = await driftVault.withdraw(vaultDepositorAddress);
23
- console.log(`Withdrew from vault: ${tx}`);
39
+ console.log(`Withdrew from vault: https://solscan.io/tx/${tx}${driftClient.env === "devnet" ? "?cluster=devnet" : ""}`);
24
40
  };
package/cli/utils.ts CHANGED
@@ -131,14 +131,17 @@ export async function getCommandContext(program: Command, needToSign: boolean):
131
131
  console.log("opts.keypair:", opts.keypair.replace(/./g, '*'));
132
132
  }
133
133
 
134
+ let loadedKeypair = false;
134
135
  wallet = new Wallet(Keypair.generate());
135
136
 
136
137
  if (isLedgerUrl) {
137
138
  wallet = await getLedgerWallet(opts.keypair) as unknown as Wallet;
139
+ loadedKeypair = true;
138
140
  } else if (opts.keypair) {
139
141
  try {
140
142
  const keypair = loadKeypair(opts.keypair as string);
141
143
  wallet = new Wallet(keypair);
144
+ loadedKeypair = true;
142
145
  } catch (e) {
143
146
  console.error(`Need to provide a valid keypair: ${e}`);
144
147
  process.exit(1);
@@ -149,13 +152,19 @@ export async function getCommandContext(program: Command, needToSign: boolean):
149
152
  }
150
153
  }
151
154
 
152
- console.log(`Loaded wallet address: ${wallet.publicKey.toBase58()}`);
155
+
156
+ const driftEnv = opts.env as DriftEnv;
157
+ console.log("driftEnv:", driftEnv);
158
+
159
+ if (loadedKeypair) {
160
+ console.log(`Loaded wallet address: ${wallet.publicKey.toBase58()}`);
161
+ }
153
162
 
154
163
  const connection = new Connection(opts.url, {
155
164
  commitment: opts.commitment,
156
165
  });
157
166
 
158
- const driftEnv = process.env.DRIFT_ENV ?? "mainnet-beta";
167
+
159
168
  const driftClient = new DriftClient({
160
169
  connection,
161
170
  wallet,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@drift-labs/vaults-sdk",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "directories": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "dependencies": {
10
10
  "@coral-xyz/anchor": "0.29.0",
11
- "@drift-labs/sdk": "2.113.0-beta.4",
11
+ "@drift-labs/sdk": "2.113.0-beta.5",
12
12
  "@metaplex-foundation/js": "0.20.1",
13
13
  "@solana/web3.js": "1.92.3",
14
14
  "commander": "11.1.0",