@drift-labs/vaults-sdk 0.1.415 → 0.1.417
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/lib/accounts/vaultAccount.d.ts +6 -1
- package/lib/accounts/vaultAccount.js +116 -0
- package/lib/addresses.d.ts +1 -0
- package/lib/addresses.js +7 -0
- package/lib/math/vaultDepositor.d.ts +11 -2
- package/lib/math/vaultDepositor.js +20 -2
- package/lib/types/drift_vaults.d.ts +450 -24
- package/lib/types/drift_vaults.js +450 -24
- package/lib/types/types.d.ts +66 -1
- package/lib/vaultClient.d.ts +53 -5
- package/lib/vaultClient.js +407 -42
- package/package.json +2 -2
- package/src/accounts/vaultAccount.ts +164 -3
- package/src/addresses.ts +13 -0
- package/src/idl/drift_vaults.json +453 -24
- package/src/math/vaultDepositor.ts +36 -4
- package/src/types/drift_vaults.ts +907 -55
- package/src/types/types.ts +79 -1
- package/src/vaultClient.ts +540 -42
package/lib/vaultClient.js
CHANGED
|
@@ -8,6 +8,7 @@ const addresses_1 = require("./addresses");
|
|
|
8
8
|
const web3_js_1 = require("@solana/web3.js");
|
|
9
9
|
const spl_token_1 = require("@solana/spl-token");
|
|
10
10
|
const bytes_1 = require("@coral-xyz/anchor/dist/cjs/utils/bytes");
|
|
11
|
+
const math_1 = require("./math");
|
|
11
12
|
class VaultClient {
|
|
12
13
|
constructor({ driftClient, program, cliMode, userMapConfig, }) {
|
|
13
14
|
this.driftClient = driftClient;
|
|
@@ -27,8 +28,13 @@ class VaultClient {
|
|
|
27
28
|
this.vaultUsers = new sdk_1.UserMap(userMapConfig);
|
|
28
29
|
}
|
|
29
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Unsubscribes from the vault users map. Call this to clean up any dangling promises.
|
|
33
|
+
*/
|
|
34
|
+
async unsubscribe() {
|
|
35
|
+
await this.vaultUsers.unsubscribe();
|
|
36
|
+
}
|
|
30
37
|
async getVault(vault) {
|
|
31
|
-
// @ts-ignore
|
|
32
38
|
return await this.program.account.vault.fetch(vault);
|
|
33
39
|
}
|
|
34
40
|
async getVaultAndSlot(vault) {
|
|
@@ -48,6 +54,19 @@ class VaultClient {
|
|
|
48
54
|
slot: vaultDepositorAndSlot.context.slot,
|
|
49
55
|
};
|
|
50
56
|
}
|
|
57
|
+
getVaultProtocolAddress(vault) {
|
|
58
|
+
return (0, addresses_1.getVaultProtocolAddressSync)(this.program.programId, vault);
|
|
59
|
+
}
|
|
60
|
+
async getVaultProtocol(vaultProtocol) {
|
|
61
|
+
return await this.program.account.vaultProtocol.fetch(vaultProtocol);
|
|
62
|
+
}
|
|
63
|
+
async getVaultProtocolAndSlot(vaultProtocol) {
|
|
64
|
+
const vaultProtocolAndSlot = await this.program.account.vaultProtocol.fetchAndContext(vaultProtocol);
|
|
65
|
+
return {
|
|
66
|
+
vaultProtocol: vaultProtocolAndSlot.data,
|
|
67
|
+
slot: vaultProtocolAndSlot.context.slot,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
51
70
|
async getAllVaultDepositorsWithNoWithdrawRequest(vault) {
|
|
52
71
|
const filters = [
|
|
53
72
|
{
|
|
@@ -103,10 +122,16 @@ class VaultClient {
|
|
|
103
122
|
/**
|
|
104
123
|
*
|
|
105
124
|
* @param vault pubkey
|
|
125
|
+
* @param factorUnrealizedPNL add unrealized pnl to net balance
|
|
106
126
|
* @returns vault equity, in USDC
|
|
107
127
|
*/
|
|
108
128
|
async calculateVaultEquity(params) {
|
|
109
129
|
try {
|
|
130
|
+
// defaults to true if undefined
|
|
131
|
+
let factorUnrealizedPNL = true;
|
|
132
|
+
if (params.factorUnrealizedPNL !== undefined) {
|
|
133
|
+
factorUnrealizedPNL = params.factorUnrealizedPNL;
|
|
134
|
+
}
|
|
110
135
|
let vaultAccount;
|
|
111
136
|
if (params.address !== undefined) {
|
|
112
137
|
// @ts-ignore
|
|
@@ -120,8 +145,13 @@ class VaultClient {
|
|
|
120
145
|
}
|
|
121
146
|
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
122
147
|
const netSpotValue = user.getNetSpotMarketValue();
|
|
123
|
-
|
|
124
|
-
|
|
148
|
+
if (factorUnrealizedPNL) {
|
|
149
|
+
const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
|
|
150
|
+
return netSpotValue.add(unrealizedPnl);
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
return netSpotValue;
|
|
154
|
+
}
|
|
125
155
|
}
|
|
126
156
|
catch (err) {
|
|
127
157
|
console.error('VaultClient ~ err:', err);
|
|
@@ -131,12 +161,12 @@ class VaultClient {
|
|
|
131
161
|
/**
|
|
132
162
|
*
|
|
133
163
|
* @param vault pubkey
|
|
134
|
-
* @
|
|
164
|
+
* @param factorUnrealizedPNL add unrealized pnl to existing equity
|
|
165
|
+
* @returns total vault equity, in spot deposit asset
|
|
135
166
|
*/
|
|
136
167
|
async calculateVaultEquityInDepositAsset(params) {
|
|
137
168
|
let vaultAccount;
|
|
138
169
|
if (params.address !== undefined) {
|
|
139
|
-
// @ts-ignore
|
|
140
170
|
vaultAccount = await this.program.account.vault.fetch(params.address);
|
|
141
171
|
}
|
|
142
172
|
else if (params.vault !== undefined) {
|
|
@@ -147,13 +177,97 @@ class VaultClient {
|
|
|
147
177
|
}
|
|
148
178
|
const vaultEquity = await this.calculateVaultEquity({
|
|
149
179
|
vault: vaultAccount,
|
|
180
|
+
factorUnrealizedPNL: params.factorUnrealizedPNL,
|
|
150
181
|
});
|
|
151
182
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
152
183
|
const spotOracle = this.driftClient.getOracleDataForSpotMarket(vaultAccount.spotMarketIndex);
|
|
153
184
|
const spotPrecision = sdk_1.TEN.pow(new sdk_1.BN(spotMarket.decimals));
|
|
154
185
|
return vaultEquity.mul(spotPrecision).div(spotOracle.price);
|
|
155
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* @param params
|
|
189
|
+
* @returns vault depositor equity, in spot market value (which is usually USDC)
|
|
190
|
+
*/
|
|
191
|
+
async calculateWithdrawableVaultDepositorEquity(params) {
|
|
192
|
+
let vaultAccount;
|
|
193
|
+
if (params.vaultAddress !== undefined) {
|
|
194
|
+
vaultAccount = await this.program.account.vault.fetch(params.vaultAddress);
|
|
195
|
+
}
|
|
196
|
+
else if (params.vault !== undefined) {
|
|
197
|
+
vaultAccount = params.vault;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
throw new Error('Must supply vaultAddress or vault');
|
|
201
|
+
}
|
|
202
|
+
let vaultDepositorAccount;
|
|
203
|
+
if (params.vaultDepositorAddress !== undefined) {
|
|
204
|
+
vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(params.vaultDepositorAddress);
|
|
205
|
+
}
|
|
206
|
+
else if (params.vaultDepositor !== undefined) {
|
|
207
|
+
vaultDepositorAccount = params.vaultDepositor;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
throw new Error('Must supply vaultDepositorAddress or vaultDepositor');
|
|
211
|
+
}
|
|
212
|
+
const vaultEquity = await this.calculateVaultEquity({
|
|
213
|
+
vault: vaultAccount,
|
|
214
|
+
factorUnrealizedPNL: false,
|
|
215
|
+
});
|
|
216
|
+
return (0, math_1.calculateRealizedVaultDepositorEquity)(vaultDepositorAccount, vaultEquity, vaultAccount);
|
|
217
|
+
}
|
|
218
|
+
async calculateWithdrawableVaultDepositorEquityInDepositAsset(params) {
|
|
219
|
+
let vaultAccount;
|
|
220
|
+
if (params.vaultAddress !== undefined) {
|
|
221
|
+
vaultAccount = await this.program.account.vault.fetch(params.vaultAddress);
|
|
222
|
+
}
|
|
223
|
+
else if (params.vault !== undefined) {
|
|
224
|
+
vaultAccount = params.vault;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
throw new Error('Must supply vaultAddress or vault');
|
|
228
|
+
}
|
|
229
|
+
let vaultDepositorAccount;
|
|
230
|
+
if (params.vaultDepositorAddress !== undefined) {
|
|
231
|
+
vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(params.vaultDepositorAddress);
|
|
232
|
+
}
|
|
233
|
+
else if (params.vaultDepositor !== undefined) {
|
|
234
|
+
vaultDepositorAccount = params.vaultDepositor;
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
throw new Error('Must supply vaultDepositorAddress or vaultDepositor');
|
|
238
|
+
}
|
|
239
|
+
let vaultProtocol = undefined;
|
|
240
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
241
|
+
vaultProtocol = await this.program.account.vaultProtocol.fetch(vaultAccount.vaultProtocol);
|
|
242
|
+
}
|
|
243
|
+
const vaultEquity = await this.calculateVaultEquity({
|
|
244
|
+
vault: vaultAccount,
|
|
245
|
+
factorUnrealizedPNL: false,
|
|
246
|
+
});
|
|
247
|
+
const vdEquity = (0, math_1.calculateRealizedVaultDepositorEquity)(vaultDepositorAccount, vaultEquity, vaultAccount, vaultProtocol);
|
|
248
|
+
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
249
|
+
const spotOracle = this.driftClient.getOracleDataForSpotMarket(vaultAccount.spotMarketIndex);
|
|
250
|
+
const spotPrecision = sdk_1.TEN.pow(new sdk_1.BN(spotMarket.decimals));
|
|
251
|
+
return vdEquity.mul(spotPrecision).div(spotOracle.price);
|
|
252
|
+
}
|
|
253
|
+
async calculateVaultProtocolEquity(params) {
|
|
254
|
+
const vaultAccount = await this.program.account.vault.fetch(params.vault);
|
|
255
|
+
const vaultTotalEquity = await this.calculateVaultEquity({
|
|
256
|
+
vault: vaultAccount,
|
|
257
|
+
});
|
|
258
|
+
const vaultProtocol = this.getVaultProtocolAddress(params.vault);
|
|
259
|
+
const vpAccount = await this.program.account.vaultProtocol.fetch(vaultProtocol);
|
|
260
|
+
return (0, sdk_1.unstakeSharesToAmount)(vpAccount.protocolProfitAndFeeShares, vaultAccount.totalShares, vaultTotalEquity);
|
|
261
|
+
}
|
|
156
262
|
async initializeVault(params) {
|
|
263
|
+
// This is a workaround to make client backwards compatible.
|
|
264
|
+
// VaultProtocol is optionally undefined, but the anchor type is optionally null.
|
|
265
|
+
// Old clients will default to undefined which prevents old clients from having to pass in a null value.
|
|
266
|
+
// Instead, we can cast to null internally.
|
|
267
|
+
const _params = {
|
|
268
|
+
...params,
|
|
269
|
+
vaultProtocol: params.vaultProtocol ? params.vaultProtocol : null,
|
|
270
|
+
};
|
|
157
271
|
const vault = (0, addresses_1.getVaultAddressSync)(this.program.programId, params.name);
|
|
158
272
|
const tokenAccount = (0, addresses_1.getTokenVaultAddressSync)(this.program.programId, vault);
|
|
159
273
|
const driftState = await this.driftClient.getStatePublicKey();
|
|
@@ -173,18 +287,43 @@ class VaultClient {
|
|
|
173
287
|
tokenAccount,
|
|
174
288
|
driftProgram: this.driftClient.program.programId,
|
|
175
289
|
};
|
|
176
|
-
|
|
177
|
-
.
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
290
|
+
if (params.vaultProtocol) {
|
|
291
|
+
const vaultProtocol = this.getVaultProtocolAddress((0, addresses_1.getVaultAddressSync)(this.program.programId, params.name));
|
|
292
|
+
const remainingAccounts = [
|
|
293
|
+
{
|
|
294
|
+
pubkey: vaultProtocol,
|
|
295
|
+
isSigner: false,
|
|
296
|
+
isWritable: true,
|
|
297
|
+
},
|
|
298
|
+
];
|
|
299
|
+
return await this.program.methods
|
|
300
|
+
.initializeVault(_params)
|
|
301
|
+
.preInstructions([
|
|
302
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
303
|
+
units: 400000,
|
|
304
|
+
}),
|
|
305
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
306
|
+
microLamports: 300000,
|
|
307
|
+
}),
|
|
308
|
+
])
|
|
309
|
+
.accounts(accounts)
|
|
310
|
+
.remainingAccounts(remainingAccounts)
|
|
311
|
+
.rpc();
|
|
312
|
+
}
|
|
313
|
+
else {
|
|
314
|
+
return await this.program.methods
|
|
315
|
+
.initializeVault(_params)
|
|
316
|
+
.preInstructions([
|
|
317
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
318
|
+
units: 400000,
|
|
319
|
+
}),
|
|
320
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
321
|
+
microLamports: 300000,
|
|
322
|
+
}),
|
|
323
|
+
])
|
|
324
|
+
.accounts(accounts)
|
|
325
|
+
.rpc();
|
|
326
|
+
}
|
|
188
327
|
}
|
|
189
328
|
/**
|
|
190
329
|
* Updates the delegate address for a vault. The delegate address will be allowed to trade
|
|
@@ -215,7 +354,7 @@ class VaultClient {
|
|
|
215
354
|
/**
|
|
216
355
|
* Updates the vault margin trading status.
|
|
217
356
|
* @param vault vault address to update
|
|
218
|
-
* @param
|
|
357
|
+
* @param enabled whether to enable margin trading
|
|
219
358
|
* @returns
|
|
220
359
|
*/
|
|
221
360
|
async updateMarginTradingEnabled(vault, enabled) {
|
|
@@ -246,6 +385,14 @@ class VaultClient {
|
|
|
246
385
|
userAccounts: [user.getUserAccount()],
|
|
247
386
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
248
387
|
});
|
|
388
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
389
|
+
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
390
|
+
remainingAccounts.push({
|
|
391
|
+
pubkey: vaultProtocol,
|
|
392
|
+
isSigner: false,
|
|
393
|
+
isWritable: true,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
249
396
|
return await this.program.methods
|
|
250
397
|
.managerDeposit(amount)
|
|
251
398
|
.accounts({
|
|
@@ -274,6 +421,14 @@ class VaultClient {
|
|
|
274
421
|
userAccounts: [user.getUserAccount()],
|
|
275
422
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
276
423
|
});
|
|
424
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
425
|
+
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
426
|
+
remainingAccounts.push({
|
|
427
|
+
pubkey: vaultProtocol,
|
|
428
|
+
isSigner: false,
|
|
429
|
+
isWritable: true,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
277
432
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
|
|
278
433
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
279
434
|
const accounts = {
|
|
@@ -318,6 +473,14 @@ class VaultClient {
|
|
|
318
473
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
319
474
|
userAccounts: [user.getUserAccount()],
|
|
320
475
|
});
|
|
476
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
477
|
+
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
478
|
+
remainingAccounts.push({
|
|
479
|
+
pubkey: vaultProtocol,
|
|
480
|
+
isSigner: false,
|
|
481
|
+
isWritable: true,
|
|
482
|
+
});
|
|
483
|
+
}
|
|
321
484
|
if (this.cliMode) {
|
|
322
485
|
return await this.program.methods
|
|
323
486
|
.mangerCancelWithdrawRequest()
|
|
@@ -346,6 +509,14 @@ class VaultClient {
|
|
|
346
509
|
userAccounts: [user.getUserAccount()],
|
|
347
510
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
348
511
|
});
|
|
512
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
513
|
+
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
514
|
+
remainingAccounts.push({
|
|
515
|
+
pubkey: vaultProtocol,
|
|
516
|
+
isSigner: false,
|
|
517
|
+
isWritable: true,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
349
520
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
350
521
|
if (!spotMarket) {
|
|
351
522
|
throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
|
|
@@ -466,6 +637,14 @@ class VaultClient {
|
|
|
466
637
|
userAccounts: [user.getUserAccount()],
|
|
467
638
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
468
639
|
});
|
|
640
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
641
|
+
const vaultProtocol = this.getVaultProtocolAddress(vaultPubKey);
|
|
642
|
+
remainingAccounts.push({
|
|
643
|
+
pubkey: vaultProtocol,
|
|
644
|
+
isSigner: false,
|
|
645
|
+
isWritable: true,
|
|
646
|
+
});
|
|
647
|
+
}
|
|
469
648
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultPubKey);
|
|
470
649
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
471
650
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
@@ -490,28 +669,38 @@ class VaultClient {
|
|
|
490
669
|
remainingAccounts,
|
|
491
670
|
};
|
|
492
671
|
}
|
|
672
|
+
/**
|
|
673
|
+
* Creates a transaction to deposit funds into the specified vault.
|
|
674
|
+
* Uses the associated token account of the vault depositor authority and spot market mint,
|
|
675
|
+
* and assumes it exists before calling this function.
|
|
676
|
+
* @param vaultDepositor
|
|
677
|
+
* @param amount
|
|
678
|
+
* @param initVaultDepositor If true, will initialize the vault depositor account
|
|
679
|
+
* @returns transaction
|
|
680
|
+
*/
|
|
493
681
|
async createDepositTx(vaultDepositor, amount, initVaultDepositor, txParams) {
|
|
494
682
|
const { vaultAccount, accounts, remainingAccounts } = await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
|
|
495
|
-
const
|
|
496
|
-
accounts: {
|
|
497
|
-
authority: this.driftClient.wallet.publicKey,
|
|
498
|
-
...accounts,
|
|
499
|
-
},
|
|
500
|
-
remainingAccounts,
|
|
501
|
-
});
|
|
683
|
+
const ixs = [];
|
|
502
684
|
if (initVaultDepositor) {
|
|
503
|
-
|
|
504
|
-
return await this.createTxn([initIx, depositIx], txParams);
|
|
505
|
-
}
|
|
506
|
-
else {
|
|
507
|
-
return await this.createTxn([depositIx], txParams);
|
|
685
|
+
ixs.push(this.createInitVaultDepositorIx(vaultAccount.pubkey, initVaultDepositor.authority));
|
|
508
686
|
}
|
|
687
|
+
const depositIx = await this.program.methods
|
|
688
|
+
.deposit(amount)
|
|
689
|
+
.accounts({
|
|
690
|
+
authority: this.driftClient.wallet.publicKey,
|
|
691
|
+
...accounts,
|
|
692
|
+
})
|
|
693
|
+
.remainingAccounts(remainingAccounts)
|
|
694
|
+
.instruction();
|
|
695
|
+
ixs.push(depositIx);
|
|
696
|
+
return await this.createTxn(ixs, txParams);
|
|
509
697
|
}
|
|
510
698
|
/**
|
|
511
699
|
* Depositor funds into the specified vault.
|
|
512
700
|
* @param vaultDepositor
|
|
513
701
|
* @param amount
|
|
514
702
|
* @param initVaultDepositor If true, will initialize the vault depositor account
|
|
703
|
+
* @param txParams
|
|
515
704
|
* @returns
|
|
516
705
|
*/
|
|
517
706
|
async deposit(vaultDepositor, amount, initVaultDepositor, txParams) {
|
|
@@ -538,6 +727,14 @@ class VaultClient {
|
|
|
538
727
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
539
728
|
userAccounts: [user.getUserAccount()],
|
|
540
729
|
});
|
|
730
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
731
|
+
const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
|
|
732
|
+
remainingAccounts.push({
|
|
733
|
+
pubkey: vaultProtocol,
|
|
734
|
+
isSigner: false,
|
|
735
|
+
isWritable: true,
|
|
736
|
+
});
|
|
737
|
+
}
|
|
541
738
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
|
|
542
739
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
543
740
|
const accounts = {
|
|
@@ -577,6 +774,14 @@ class VaultClient {
|
|
|
577
774
|
userAccounts: [user.getUserAccount()],
|
|
578
775
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
579
776
|
});
|
|
777
|
+
const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
|
|
778
|
+
if (!vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
779
|
+
remainingAccounts.push({
|
|
780
|
+
pubkey: vaultProtocol,
|
|
781
|
+
isSigner: false,
|
|
782
|
+
isWritable: true,
|
|
783
|
+
});
|
|
784
|
+
}
|
|
580
785
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
|
|
581
786
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
582
787
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
@@ -603,21 +808,32 @@ class VaultClient {
|
|
|
603
808
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
604
809
|
};
|
|
605
810
|
if (this.cliMode) {
|
|
606
|
-
|
|
607
|
-
.
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
811
|
+
if (createAtaIx) {
|
|
812
|
+
return await this.program.methods
|
|
813
|
+
.withdraw()
|
|
814
|
+
.accounts(accounts)
|
|
815
|
+
.remainingAccounts(remainingAccounts)
|
|
816
|
+
.preInstructions([createAtaIx])
|
|
817
|
+
.rpc();
|
|
818
|
+
}
|
|
819
|
+
else {
|
|
820
|
+
return await this.program.methods
|
|
821
|
+
.withdraw()
|
|
822
|
+
.accounts(accounts)
|
|
823
|
+
.remainingAccounts(remainingAccounts)
|
|
824
|
+
.rpc();
|
|
825
|
+
}
|
|
611
826
|
}
|
|
612
827
|
else {
|
|
613
828
|
const ixs = [
|
|
614
|
-
this.program.
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
829
|
+
await this.program.methods
|
|
830
|
+
.withdraw()
|
|
831
|
+
.accounts({
|
|
832
|
+
authority: this.driftClient.wallet.publicKey,
|
|
833
|
+
...accounts,
|
|
834
|
+
})
|
|
835
|
+
.remainingAccounts(remainingAccounts)
|
|
836
|
+
.instruction(),
|
|
621
837
|
];
|
|
622
838
|
if (createAtaIx) {
|
|
623
839
|
ixs.unshift(createAtaIx);
|
|
@@ -636,6 +852,14 @@ class VaultClient {
|
|
|
636
852
|
userAccounts: [user.getUserAccount()],
|
|
637
853
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
638
854
|
});
|
|
855
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
856
|
+
const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
|
|
857
|
+
remainingAccounts.push({
|
|
858
|
+
pubkey: vaultProtocol,
|
|
859
|
+
isSigner: false,
|
|
860
|
+
isWritable: true,
|
|
861
|
+
});
|
|
862
|
+
}
|
|
639
863
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
|
|
640
864
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
641
865
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
@@ -697,6 +921,14 @@ class VaultClient {
|
|
|
697
921
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
698
922
|
userAccounts: [user.getUserAccount()],
|
|
699
923
|
});
|
|
924
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
925
|
+
const vaultProtocol = this.getVaultProtocolAddress(vaultDepositorAccount.vault);
|
|
926
|
+
remainingAccounts.push({
|
|
927
|
+
pubkey: vaultProtocol,
|
|
928
|
+
isSigner: false,
|
|
929
|
+
isWritable: true,
|
|
930
|
+
});
|
|
931
|
+
}
|
|
700
932
|
if (this.cliMode) {
|
|
701
933
|
return await this.program.methods
|
|
702
934
|
.cancelRequestWithdraw()
|
|
@@ -860,5 +1092,138 @@ class VaultClient {
|
|
|
860
1092
|
})
|
|
861
1093
|
.rpc();
|
|
862
1094
|
}
|
|
1095
|
+
async protocolRequestWithdraw(vault, amount, withdrawUnit) {
|
|
1096
|
+
// @ts-ignore
|
|
1097
|
+
const vaultAccount = (await this.program.account.vault.fetch(vault));
|
|
1098
|
+
const vp = this.getVaultProtocolAddress(vault);
|
|
1099
|
+
const vpAccount = (await this.program.account.vaultProtocol.fetch(vp));
|
|
1100
|
+
if (!this.driftClient.wallet.publicKey.equals(vpAccount.protocol)) {
|
|
1101
|
+
throw new Error(`Only the protocol of the vault can request a withdraw.`);
|
|
1102
|
+
}
|
|
1103
|
+
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
1104
|
+
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1105
|
+
userAccounts: [user.getUserAccount()],
|
|
1106
|
+
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
1107
|
+
});
|
|
1108
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
1109
|
+
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
1110
|
+
remainingAccounts.push({
|
|
1111
|
+
pubkey: vaultProtocol,
|
|
1112
|
+
isSigner: false,
|
|
1113
|
+
isWritable: true,
|
|
1114
|
+
});
|
|
1115
|
+
}
|
|
1116
|
+
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
|
|
1117
|
+
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
1118
|
+
const accounts = {
|
|
1119
|
+
vault,
|
|
1120
|
+
driftUserStats: userStatsKey,
|
|
1121
|
+
driftUser: vaultAccount.user,
|
|
1122
|
+
driftState: driftStateKey,
|
|
1123
|
+
};
|
|
1124
|
+
if (this.cliMode) {
|
|
1125
|
+
return await this.program.methods
|
|
1126
|
+
// @ts-ignore, 0.29.0 anchor issues..
|
|
1127
|
+
.managerRequestWithdraw(amount, withdrawUnit)
|
|
1128
|
+
.accounts(accounts)
|
|
1129
|
+
.remainingAccounts(remainingAccounts)
|
|
1130
|
+
.rpc();
|
|
1131
|
+
}
|
|
1132
|
+
else {
|
|
1133
|
+
const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
|
|
1134
|
+
// @ts-ignore
|
|
1135
|
+
amount, withdrawUnit, {
|
|
1136
|
+
accounts: {
|
|
1137
|
+
manager: this.driftClient.wallet.publicKey,
|
|
1138
|
+
...accounts,
|
|
1139
|
+
},
|
|
1140
|
+
remainingAccounts,
|
|
1141
|
+
});
|
|
1142
|
+
return await this.createAndSendTxn([requestWithdrawIx]);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
async protocolCancelWithdrawRequest(vault) {
|
|
1146
|
+
const vaultAccount = await this.program.account.vault.fetch(vault);
|
|
1147
|
+
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
|
|
1148
|
+
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
1149
|
+
const accounts = {
|
|
1150
|
+
manager: this.driftClient.wallet.publicKey,
|
|
1151
|
+
vault,
|
|
1152
|
+
driftUserStats: userStatsKey,
|
|
1153
|
+
driftUser: vaultAccount.user,
|
|
1154
|
+
driftState: driftStateKey,
|
|
1155
|
+
};
|
|
1156
|
+
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
1157
|
+
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1158
|
+
userAccounts: [user.getUserAccount()],
|
|
1159
|
+
});
|
|
1160
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
1161
|
+
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
1162
|
+
remainingAccounts.push({
|
|
1163
|
+
pubkey: vaultProtocol,
|
|
1164
|
+
isSigner: false,
|
|
1165
|
+
isWritable: true,
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
if (this.cliMode) {
|
|
1169
|
+
return await this.program.methods
|
|
1170
|
+
.mangerCancelWithdrawRequest()
|
|
1171
|
+
.accounts(accounts)
|
|
1172
|
+
.remainingAccounts(remainingAccounts)
|
|
1173
|
+
.rpc();
|
|
1174
|
+
}
|
|
1175
|
+
else {
|
|
1176
|
+
const cancelRequestWithdrawIx = this.program.instruction.mangerCancelWithdrawRequest({
|
|
1177
|
+
accounts: {
|
|
1178
|
+
...accounts,
|
|
1179
|
+
manager: this.driftClient.wallet.publicKey,
|
|
1180
|
+
},
|
|
1181
|
+
remainingAccounts,
|
|
1182
|
+
});
|
|
1183
|
+
return await this.createAndSendTxn([cancelRequestWithdrawIx]);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
async protocolWithdraw(vault) {
|
|
1187
|
+
const vaultAccount = await this.program.account.vault.fetch(vault);
|
|
1188
|
+
if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
|
|
1189
|
+
throw new Error(`Only the manager of the vault can request a withdraw.`);
|
|
1190
|
+
}
|
|
1191
|
+
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
1192
|
+
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1193
|
+
userAccounts: [user.getUserAccount()],
|
|
1194
|
+
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
1195
|
+
});
|
|
1196
|
+
if (!vaultAccount.vaultProtocol.equals(web3_js_1.SystemProgram.programId)) {
|
|
1197
|
+
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
1198
|
+
remainingAccounts.push({
|
|
1199
|
+
pubkey: vaultProtocol,
|
|
1200
|
+
isSigner: false,
|
|
1201
|
+
isWritable: true,
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
1205
|
+
if (!spotMarket) {
|
|
1206
|
+
throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
|
|
1207
|
+
}
|
|
1208
|
+
const ix = this.program.instruction.managerWithdraw({
|
|
1209
|
+
accounts: {
|
|
1210
|
+
vault,
|
|
1211
|
+
manager: this.driftClient.wallet.publicKey,
|
|
1212
|
+
vaultTokenAccount: vaultAccount.tokenAccount,
|
|
1213
|
+
driftUser: await (0, sdk_1.getUserAccountPublicKey)(this.driftClient.program.programId, vault),
|
|
1214
|
+
driftProgram: this.driftClient.program.programId,
|
|
1215
|
+
driftUserStats: (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault),
|
|
1216
|
+
driftState: await this.driftClient.getStatePublicKey(),
|
|
1217
|
+
driftSpotMarketVault: spotMarket.vault,
|
|
1218
|
+
userTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(spotMarket.mint, this.driftClient.wallet.publicKey),
|
|
1219
|
+
driftSigner: this.driftClient.getStateAccount().signer,
|
|
1220
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1221
|
+
},
|
|
1222
|
+
remainingAccounts,
|
|
1223
|
+
});
|
|
1224
|
+
return this.createAndSendTxn([ix], {
|
|
1225
|
+
cuLimit: 1000000,
|
|
1226
|
+
});
|
|
1227
|
+
}
|
|
863
1228
|
}
|
|
864
1229
|
exports.VaultClient = VaultClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drift-labs/vaults-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.417",
|
|
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.28.0",
|
|
11
11
|
"@drift-labs/competitions-sdk": "0.2.386",
|
|
12
|
-
"@drift-labs/sdk": "2.93.0-beta.
|
|
12
|
+
"@drift-labs/sdk": "2.93.0-beta.5",
|
|
13
13
|
"@solana/web3.js": "1.92.3",
|
|
14
14
|
"commander": "^11.0.0",
|
|
15
15
|
"dotenv": "^16.3.1",
|