@drift-labs/vaults-sdk 0.1.422 → 0.1.424
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 +1 -6
- package/lib/accounts/vaultAccount.js +0 -116
- package/lib/addresses.d.ts +0 -1
- package/lib/addresses.js +0 -7
- package/lib/math/vaultDepositor.d.ts +2 -11
- package/lib/math/vaultDepositor.js +2 -20
- package/lib/types/drift_vaults.d.ts +24 -450
- package/lib/types/drift_vaults.js +24 -450
- package/lib/types/types.d.ts +1 -66
- package/lib/vaultClient.d.ts +5 -53
- package/lib/vaultClient.js +42 -407
- package/package.json +2 -2
- package/src/accounts/vaultAccount.ts +3 -164
- package/src/addresses.ts +0 -13
- package/src/idl/drift_vaults.json +24 -453
- package/src/math/vaultDepositor.ts +4 -36
- package/src/types/drift_vaults.ts +55 -907
- package/src/types/types.ts +1 -79
- package/src/vaultClient.ts +42 -540
package/lib/vaultClient.js
CHANGED
|
@@ -8,7 +8,6 @@ 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");
|
|
12
11
|
class VaultClient {
|
|
13
12
|
constructor({ driftClient, program, cliMode, userMapConfig, }) {
|
|
14
13
|
this.driftClient = driftClient;
|
|
@@ -28,13 +27,8 @@ class VaultClient {
|
|
|
28
27
|
this.vaultUsers = new sdk_1.UserMap(userMapConfig);
|
|
29
28
|
}
|
|
30
29
|
}
|
|
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
|
-
}
|
|
37
30
|
async getVault(vault) {
|
|
31
|
+
// @ts-ignore
|
|
38
32
|
return await this.program.account.vault.fetch(vault);
|
|
39
33
|
}
|
|
40
34
|
async getVaultAndSlot(vault) {
|
|
@@ -54,19 +48,6 @@ class VaultClient {
|
|
|
54
48
|
slot: vaultDepositorAndSlot.context.slot,
|
|
55
49
|
};
|
|
56
50
|
}
|
|
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
|
-
}
|
|
70
51
|
async getAllVaultDepositorsWithNoWithdrawRequest(vault) {
|
|
71
52
|
const filters = [
|
|
72
53
|
{
|
|
@@ -122,16 +103,10 @@ class VaultClient {
|
|
|
122
103
|
/**
|
|
123
104
|
*
|
|
124
105
|
* @param vault pubkey
|
|
125
|
-
* @param factorUnrealizedPNL add unrealized pnl to net balance
|
|
126
106
|
* @returns vault equity, in USDC
|
|
127
107
|
*/
|
|
128
108
|
async calculateVaultEquity(params) {
|
|
129
109
|
try {
|
|
130
|
-
// defaults to true if undefined
|
|
131
|
-
let factorUnrealizedPNL = true;
|
|
132
|
-
if (params.factorUnrealizedPNL !== undefined) {
|
|
133
|
-
factorUnrealizedPNL = params.factorUnrealizedPNL;
|
|
134
|
-
}
|
|
135
110
|
let vaultAccount;
|
|
136
111
|
if (params.address !== undefined) {
|
|
137
112
|
// @ts-ignore
|
|
@@ -145,13 +120,8 @@ class VaultClient {
|
|
|
145
120
|
}
|
|
146
121
|
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
147
122
|
const netSpotValue = user.getNetSpotMarketValue();
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return netSpotValue.add(unrealizedPnl);
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
return netSpotValue;
|
|
154
|
-
}
|
|
123
|
+
const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
|
|
124
|
+
return netSpotValue.add(unrealizedPnl);
|
|
155
125
|
}
|
|
156
126
|
catch (err) {
|
|
157
127
|
console.error('VaultClient ~ err:', err);
|
|
@@ -161,12 +131,12 @@ class VaultClient {
|
|
|
161
131
|
/**
|
|
162
132
|
*
|
|
163
133
|
* @param vault pubkey
|
|
164
|
-
* @
|
|
165
|
-
* @returns total vault equity, in spot deposit asset
|
|
134
|
+
* @returns vault equity, in spot deposit asset
|
|
166
135
|
*/
|
|
167
136
|
async calculateVaultEquityInDepositAsset(params) {
|
|
168
137
|
let vaultAccount;
|
|
169
138
|
if (params.address !== undefined) {
|
|
139
|
+
// @ts-ignore
|
|
170
140
|
vaultAccount = await this.program.account.vault.fetch(params.address);
|
|
171
141
|
}
|
|
172
142
|
else if (params.vault !== undefined) {
|
|
@@ -177,97 +147,13 @@ class VaultClient {
|
|
|
177
147
|
}
|
|
178
148
|
const vaultEquity = await this.calculateVaultEquity({
|
|
179
149
|
vault: vaultAccount,
|
|
180
|
-
factorUnrealizedPNL: params.factorUnrealizedPNL,
|
|
181
150
|
});
|
|
182
151
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
183
152
|
const spotOracle = this.driftClient.getOracleDataForSpotMarket(vaultAccount.spotMarketIndex);
|
|
184
153
|
const spotPrecision = sdk_1.TEN.pow(new sdk_1.BN(spotMarket.decimals));
|
|
185
154
|
return vaultEquity.mul(spotPrecision).div(spotOracle.price);
|
|
186
155
|
}
|
|
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
|
-
}
|
|
262
156
|
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
|
-
};
|
|
271
157
|
const vault = (0, addresses_1.getVaultAddressSync)(this.program.programId, params.name);
|
|
272
158
|
const tokenAccount = (0, addresses_1.getTokenVaultAddressSync)(this.program.programId, vault);
|
|
273
159
|
const driftState = await this.driftClient.getStatePublicKey();
|
|
@@ -287,43 +173,18 @@ class VaultClient {
|
|
|
287
173
|
tokenAccount,
|
|
288
174
|
driftProgram: this.driftClient.program.programId,
|
|
289
175
|
};
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
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
|
-
}
|
|
176
|
+
return await this.program.methods
|
|
177
|
+
.initializeVault(params)
|
|
178
|
+
.preInstructions([
|
|
179
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
180
|
+
units: 400000,
|
|
181
|
+
}),
|
|
182
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
183
|
+
microLamports: 300000,
|
|
184
|
+
}),
|
|
185
|
+
])
|
|
186
|
+
.accounts(accounts)
|
|
187
|
+
.rpc();
|
|
327
188
|
}
|
|
328
189
|
/**
|
|
329
190
|
* Updates the delegate address for a vault. The delegate address will be allowed to trade
|
|
@@ -354,7 +215,7 @@ class VaultClient {
|
|
|
354
215
|
/**
|
|
355
216
|
* Updates the vault margin trading status.
|
|
356
217
|
* @param vault vault address to update
|
|
357
|
-
* @param
|
|
218
|
+
* @param enabeld whether to enable margin trading
|
|
358
219
|
* @returns
|
|
359
220
|
*/
|
|
360
221
|
async updateMarginTradingEnabled(vault, enabled) {
|
|
@@ -385,14 +246,6 @@ class VaultClient {
|
|
|
385
246
|
userAccounts: [user.getUserAccount()],
|
|
386
247
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
387
248
|
});
|
|
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
|
-
}
|
|
396
249
|
return await this.program.methods
|
|
397
250
|
.managerDeposit(amount)
|
|
398
251
|
.accounts({
|
|
@@ -421,14 +274,6 @@ class VaultClient {
|
|
|
421
274
|
userAccounts: [user.getUserAccount()],
|
|
422
275
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
423
276
|
});
|
|
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
|
-
}
|
|
432
277
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vault);
|
|
433
278
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
434
279
|
const accounts = {
|
|
@@ -473,14 +318,6 @@ class VaultClient {
|
|
|
473
318
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
474
319
|
userAccounts: [user.getUserAccount()],
|
|
475
320
|
});
|
|
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
|
-
}
|
|
484
321
|
if (this.cliMode) {
|
|
485
322
|
return await this.program.methods
|
|
486
323
|
.mangerCancelWithdrawRequest()
|
|
@@ -509,14 +346,6 @@ class VaultClient {
|
|
|
509
346
|
userAccounts: [user.getUserAccount()],
|
|
510
347
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
511
348
|
});
|
|
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
|
-
}
|
|
520
349
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
521
350
|
if (!spotMarket) {
|
|
522
351
|
throw new Error(`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`);
|
|
@@ -637,14 +466,6 @@ class VaultClient {
|
|
|
637
466
|
userAccounts: [user.getUserAccount()],
|
|
638
467
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
639
468
|
});
|
|
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
|
-
}
|
|
648
469
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultPubKey);
|
|
649
470
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
650
471
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
@@ -669,38 +490,28 @@ class VaultClient {
|
|
|
669
490
|
remainingAccounts,
|
|
670
491
|
};
|
|
671
492
|
}
|
|
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
|
-
*/
|
|
681
493
|
async createDepositTx(vaultDepositor, amount, initVaultDepositor, txParams) {
|
|
682
494
|
const { vaultAccount, accounts, remainingAccounts } = await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
|
|
683
|
-
const
|
|
495
|
+
const depositIx = this.program.instruction.deposit(amount, {
|
|
496
|
+
accounts: {
|
|
497
|
+
authority: this.driftClient.wallet.publicKey,
|
|
498
|
+
...accounts,
|
|
499
|
+
},
|
|
500
|
+
remainingAccounts,
|
|
501
|
+
});
|
|
684
502
|
if (initVaultDepositor) {
|
|
685
|
-
|
|
503
|
+
const initIx = this.createInitVaultDepositorIx(vaultAccount.pubkey, initVaultDepositor.authority);
|
|
504
|
+
return await this.createTxn([initIx, depositIx], txParams);
|
|
505
|
+
}
|
|
506
|
+
else {
|
|
507
|
+
return await this.createTxn([depositIx], txParams);
|
|
686
508
|
}
|
|
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);
|
|
697
509
|
}
|
|
698
510
|
/**
|
|
699
511
|
* Depositor funds into the specified vault.
|
|
700
512
|
* @param vaultDepositor
|
|
701
513
|
* @param amount
|
|
702
514
|
* @param initVaultDepositor If true, will initialize the vault depositor account
|
|
703
|
-
* @param txParams
|
|
704
515
|
* @returns
|
|
705
516
|
*/
|
|
706
517
|
async deposit(vaultDepositor, amount, initVaultDepositor, txParams) {
|
|
@@ -727,14 +538,6 @@ class VaultClient {
|
|
|
727
538
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
728
539
|
userAccounts: [user.getUserAccount()],
|
|
729
540
|
});
|
|
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
|
-
}
|
|
738
541
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
|
|
739
542
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
740
543
|
const accounts = {
|
|
@@ -774,14 +577,6 @@ class VaultClient {
|
|
|
774
577
|
userAccounts: [user.getUserAccount()],
|
|
775
578
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
776
579
|
});
|
|
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
|
-
}
|
|
785
580
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
|
|
786
581
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
787
582
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
@@ -808,32 +603,21 @@ class VaultClient {
|
|
|
808
603
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
809
604
|
};
|
|
810
605
|
if (this.cliMode) {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
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
|
-
}
|
|
606
|
+
return await this.program.methods
|
|
607
|
+
.withdraw()
|
|
608
|
+
.accounts(accounts)
|
|
609
|
+
.remainingAccounts(remainingAccounts)
|
|
610
|
+
.rpc();
|
|
826
611
|
}
|
|
827
612
|
else {
|
|
828
613
|
const ixs = [
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
.instruction(),
|
|
614
|
+
this.program.instruction.withdraw({
|
|
615
|
+
accounts: {
|
|
616
|
+
authority: this.driftClient.wallet.publicKey,
|
|
617
|
+
...accounts,
|
|
618
|
+
},
|
|
619
|
+
remainingAccounts,
|
|
620
|
+
}),
|
|
837
621
|
];
|
|
838
622
|
if (createAtaIx) {
|
|
839
623
|
ixs.unshift(createAtaIx);
|
|
@@ -852,14 +636,6 @@ class VaultClient {
|
|
|
852
636
|
userAccounts: [user.getUserAccount()],
|
|
853
637
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
854
638
|
});
|
|
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
|
-
}
|
|
863
639
|
const userStatsKey = (0, sdk_1.getUserStatsAccountPublicKey)(this.driftClient.program.programId, vaultDepositorAccount.vault);
|
|
864
640
|
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
865
641
|
const spotMarket = this.driftClient.getSpotMarketAccount(vaultAccount.spotMarketIndex);
|
|
@@ -921,14 +697,6 @@ class VaultClient {
|
|
|
921
697
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
922
698
|
userAccounts: [user.getUserAccount()],
|
|
923
699
|
});
|
|
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
|
-
}
|
|
932
700
|
if (this.cliMode) {
|
|
933
701
|
return await this.program.methods
|
|
934
702
|
.cancelRequestWithdraw()
|
|
@@ -1092,138 +860,5 @@ class VaultClient {
|
|
|
1092
860
|
})
|
|
1093
861
|
.rpc();
|
|
1094
862
|
}
|
|
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
|
-
}
|
|
1228
863
|
}
|
|
1229
864
|
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.424",
|
|
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.94.0-beta.
|
|
12
|
+
"@drift-labs/sdk": "2.94.0-beta.1",
|
|
13
13
|
"@solana/web3.js": "1.92.3",
|
|
14
14
|
"commander": "^11.0.0",
|
|
15
15
|
"dotenv": "^16.3.1",
|