@drift-labs/vaults-sdk 0.1.423 → 0.1.425
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/src/vaultClient.ts
CHANGED
|
@@ -8,7 +8,6 @@ import {
|
|
|
8
8
|
getUserStatsAccountPublicKey,
|
|
9
9
|
TEN,
|
|
10
10
|
UserMap,
|
|
11
|
-
unstakeSharesToAmount as depositSharesToVaultAmount,
|
|
12
11
|
ZERO,
|
|
13
12
|
} from '@drift-labs/sdk';
|
|
14
13
|
import { BorshAccountsCoder, Program, ProgramAccount } from '@coral-xyz/anchor';
|
|
@@ -22,10 +21,8 @@ import {
|
|
|
22
21
|
getTokenVaultAddressSync,
|
|
23
22
|
getVaultAddressSync,
|
|
24
23
|
getVaultDepositorAddressSync,
|
|
25
|
-
getVaultProtocolAddressSync,
|
|
26
24
|
} from './addresses';
|
|
27
25
|
import {
|
|
28
|
-
AccountMeta,
|
|
29
26
|
AddressLookupTableAccount,
|
|
30
27
|
ComputeBudgetProgram,
|
|
31
28
|
PublicKey,
|
|
@@ -40,17 +37,9 @@ import {
|
|
|
40
37
|
getAssociatedTokenAddressSync,
|
|
41
38
|
TOKEN_PROGRAM_ID,
|
|
42
39
|
} from '@solana/spl-token';
|
|
43
|
-
import {
|
|
44
|
-
Vault,
|
|
45
|
-
VaultDepositor,
|
|
46
|
-
VaultParams,
|
|
47
|
-
VaultProtocol,
|
|
48
|
-
VaultProtocolParams,
|
|
49
|
-
WithdrawUnit,
|
|
50
|
-
} from './types/types';
|
|
40
|
+
import { Vault, VaultDepositor, WithdrawUnit } from './types/types';
|
|
51
41
|
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
|
|
52
42
|
import { UserMapConfig } from '@drift-labs/sdk/lib/userMap/userMapConfig';
|
|
53
|
-
import { calculateRealizedVaultDepositorEquity } from './math';
|
|
54
43
|
|
|
55
44
|
export type TxParams = {
|
|
56
45
|
cuLimit?: number;
|
|
@@ -98,14 +87,8 @@ export class VaultClient {
|
|
|
98
87
|
}
|
|
99
88
|
}
|
|
100
89
|
|
|
101
|
-
/**
|
|
102
|
-
* Unsubscribes from the vault users map. Call this to clean up any dangling promises.
|
|
103
|
-
*/
|
|
104
|
-
public async unsubscribe() {
|
|
105
|
-
await this.vaultUsers.unsubscribe();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
90
|
public async getVault(vault: PublicKey): Promise<Vault> {
|
|
91
|
+
// @ts-ignore
|
|
109
92
|
return await this.program.account.vault.fetch(vault);
|
|
110
93
|
}
|
|
111
94
|
|
|
@@ -136,27 +119,6 @@ export class VaultClient {
|
|
|
136
119
|
};
|
|
137
120
|
}
|
|
138
121
|
|
|
139
|
-
public getVaultProtocolAddress(vault: PublicKey): PublicKey {
|
|
140
|
-
return getVaultProtocolAddressSync(this.program.programId, vault);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
public async getVaultProtocol(
|
|
144
|
-
vaultProtocol: PublicKey
|
|
145
|
-
): Promise<VaultProtocol> {
|
|
146
|
-
return await this.program.account.vaultProtocol.fetch(vaultProtocol);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
public async getVaultProtocolAndSlot(
|
|
150
|
-
vaultProtocol: PublicKey
|
|
151
|
-
): Promise<{ vaultProtocol: VaultProtocol; slot: number }> {
|
|
152
|
-
const vaultProtocolAndSlot =
|
|
153
|
-
await this.program.account.vaultProtocol.fetchAndContext(vaultProtocol);
|
|
154
|
-
return {
|
|
155
|
-
vaultProtocol: vaultProtocolAndSlot.data as VaultProtocol,
|
|
156
|
-
slot: vaultProtocolAndSlot.context.slot,
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
|
|
160
122
|
public async getAllVaultDepositorsWithNoWithdrawRequest(
|
|
161
123
|
vault: PublicKey
|
|
162
124
|
): Promise<ProgramAccount<VaultDepositor>[]> {
|
|
@@ -227,21 +189,13 @@ export class VaultClient {
|
|
|
227
189
|
/**
|
|
228
190
|
*
|
|
229
191
|
* @param vault pubkey
|
|
230
|
-
* @param factorUnrealizedPNL add unrealized pnl to net balance
|
|
231
192
|
* @returns vault equity, in USDC
|
|
232
193
|
*/
|
|
233
194
|
public async calculateVaultEquity(params: {
|
|
234
195
|
address?: PublicKey;
|
|
235
196
|
vault?: Vault;
|
|
236
|
-
factorUnrealizedPNL?: boolean;
|
|
237
197
|
}): Promise<BN> {
|
|
238
198
|
try {
|
|
239
|
-
// defaults to true if undefined
|
|
240
|
-
let factorUnrealizedPNL = true;
|
|
241
|
-
if (params.factorUnrealizedPNL !== undefined) {
|
|
242
|
-
factorUnrealizedPNL = params.factorUnrealizedPNL;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
199
|
let vaultAccount: Vault;
|
|
246
200
|
if (params.address !== undefined) {
|
|
247
201
|
// @ts-ignore
|
|
@@ -255,13 +209,9 @@ export class VaultClient {
|
|
|
255
209
|
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
256
210
|
|
|
257
211
|
const netSpotValue = user.getNetSpotMarketValue();
|
|
212
|
+
const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
|
|
258
213
|
|
|
259
|
-
|
|
260
|
-
const unrealizedPnl = user.getUnrealizedPNL(true, undefined, undefined);
|
|
261
|
-
return netSpotValue.add(unrealizedPnl);
|
|
262
|
-
} else {
|
|
263
|
-
return netSpotValue;
|
|
264
|
-
}
|
|
214
|
+
return netSpotValue.add(unrealizedPnl);
|
|
265
215
|
} catch (err) {
|
|
266
216
|
console.error('VaultClient ~ err:', err);
|
|
267
217
|
return ZERO;
|
|
@@ -271,16 +221,15 @@ export class VaultClient {
|
|
|
271
221
|
/**
|
|
272
222
|
*
|
|
273
223
|
* @param vault pubkey
|
|
274
|
-
* @
|
|
275
|
-
* @returns total vault equity, in spot deposit asset
|
|
224
|
+
* @returns vault equity, in spot deposit asset
|
|
276
225
|
*/
|
|
277
226
|
public async calculateVaultEquityInDepositAsset(params: {
|
|
278
227
|
address?: PublicKey;
|
|
279
228
|
vault?: Vault;
|
|
280
|
-
factorUnrealizedPNL?: boolean;
|
|
281
229
|
}): Promise<BN> {
|
|
282
230
|
let vaultAccount: Vault;
|
|
283
231
|
if (params.address !== undefined) {
|
|
232
|
+
// @ts-ignore
|
|
284
233
|
vaultAccount = await this.program.account.vault.fetch(params.address);
|
|
285
234
|
} else if (params.vault !== undefined) {
|
|
286
235
|
vaultAccount = params.vault;
|
|
@@ -289,7 +238,6 @@ export class VaultClient {
|
|
|
289
238
|
}
|
|
290
239
|
const vaultEquity = await this.calculateVaultEquity({
|
|
291
240
|
vault: vaultAccount,
|
|
292
|
-
factorUnrealizedPNL: params.factorUnrealizedPNL,
|
|
293
241
|
});
|
|
294
242
|
const spotMarket = this.driftClient.getSpotMarketAccount(
|
|
295
243
|
vaultAccount.spotMarketIndex
|
|
@@ -302,124 +250,6 @@ export class VaultClient {
|
|
|
302
250
|
return vaultEquity.mul(spotPrecision).div(spotOracle.price);
|
|
303
251
|
}
|
|
304
252
|
|
|
305
|
-
/**
|
|
306
|
-
* @param params
|
|
307
|
-
* @returns vault depositor equity, in spot market value (which is usually USDC)
|
|
308
|
-
*/
|
|
309
|
-
public async calculateWithdrawableVaultDepositorEquity(params: {
|
|
310
|
-
vaultDepositorAddress?: PublicKey;
|
|
311
|
-
vaultDepositor?: VaultDepositor;
|
|
312
|
-
vaultAddress?: PublicKey;
|
|
313
|
-
vault?: Vault;
|
|
314
|
-
}): Promise<BN> {
|
|
315
|
-
let vaultAccount: Vault;
|
|
316
|
-
if (params.vaultAddress !== undefined) {
|
|
317
|
-
vaultAccount = await this.program.account.vault.fetch(
|
|
318
|
-
params.vaultAddress
|
|
319
|
-
);
|
|
320
|
-
} else if (params.vault !== undefined) {
|
|
321
|
-
vaultAccount = params.vault;
|
|
322
|
-
} else {
|
|
323
|
-
throw new Error('Must supply vaultAddress or vault');
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
let vaultDepositorAccount: VaultDepositor;
|
|
327
|
-
if (params.vaultDepositorAddress !== undefined) {
|
|
328
|
-
vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(
|
|
329
|
-
params.vaultDepositorAddress
|
|
330
|
-
);
|
|
331
|
-
} else if (params.vaultDepositor !== undefined) {
|
|
332
|
-
vaultDepositorAccount = params.vaultDepositor;
|
|
333
|
-
} else {
|
|
334
|
-
throw new Error('Must supply vaultDepositorAddress or vaultDepositor');
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
const vaultEquity = await this.calculateVaultEquity({
|
|
338
|
-
vault: vaultAccount,
|
|
339
|
-
factorUnrealizedPNL: false,
|
|
340
|
-
});
|
|
341
|
-
return calculateRealizedVaultDepositorEquity(
|
|
342
|
-
vaultDepositorAccount,
|
|
343
|
-
vaultEquity,
|
|
344
|
-
vaultAccount
|
|
345
|
-
);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
public async calculateWithdrawableVaultDepositorEquityInDepositAsset(params: {
|
|
349
|
-
vaultDepositorAddress?: PublicKey;
|
|
350
|
-
vaultDepositor?: VaultDepositor;
|
|
351
|
-
vaultAddress?: PublicKey;
|
|
352
|
-
vault?: Vault;
|
|
353
|
-
}): Promise<BN> {
|
|
354
|
-
let vaultAccount: Vault;
|
|
355
|
-
if (params.vaultAddress !== undefined) {
|
|
356
|
-
vaultAccount = await this.program.account.vault.fetch(
|
|
357
|
-
params.vaultAddress
|
|
358
|
-
);
|
|
359
|
-
} else if (params.vault !== undefined) {
|
|
360
|
-
vaultAccount = params.vault;
|
|
361
|
-
} else {
|
|
362
|
-
throw new Error('Must supply vaultAddress or vault');
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
let vaultDepositorAccount: VaultDepositor;
|
|
366
|
-
if (params.vaultDepositorAddress !== undefined) {
|
|
367
|
-
vaultDepositorAccount = await this.program.account.vaultDepositor.fetch(
|
|
368
|
-
params.vaultDepositorAddress
|
|
369
|
-
);
|
|
370
|
-
} else if (params.vaultDepositor !== undefined) {
|
|
371
|
-
vaultDepositorAccount = params.vaultDepositor;
|
|
372
|
-
} else {
|
|
373
|
-
throw new Error('Must supply vaultDepositorAddress or vaultDepositor');
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
let vaultProtocol: VaultProtocol | undefined = undefined;
|
|
377
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
378
|
-
vaultProtocol = await this.program.account.vaultProtocol.fetch(
|
|
379
|
-
vaultAccount.vaultProtocol
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
const vaultEquity = await this.calculateVaultEquity({
|
|
384
|
-
vault: vaultAccount,
|
|
385
|
-
factorUnrealizedPNL: false,
|
|
386
|
-
});
|
|
387
|
-
const vdEquity = calculateRealizedVaultDepositorEquity(
|
|
388
|
-
vaultDepositorAccount,
|
|
389
|
-
vaultEquity,
|
|
390
|
-
vaultAccount,
|
|
391
|
-
vaultProtocol
|
|
392
|
-
);
|
|
393
|
-
|
|
394
|
-
const spotMarket = this.driftClient.getSpotMarketAccount(
|
|
395
|
-
vaultAccount.spotMarketIndex
|
|
396
|
-
);
|
|
397
|
-
const spotOracle = this.driftClient.getOracleDataForSpotMarket(
|
|
398
|
-
vaultAccount.spotMarketIndex
|
|
399
|
-
);
|
|
400
|
-
const spotPrecision = TEN.pow(new BN(spotMarket!.decimals));
|
|
401
|
-
|
|
402
|
-
return vdEquity.mul(spotPrecision).div(spotOracle.price);
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
public async calculateVaultProtocolEquity(params: {
|
|
406
|
-
vault: PublicKey;
|
|
407
|
-
}): Promise<BN> {
|
|
408
|
-
const vaultAccount = await this.program.account.vault.fetch(params.vault);
|
|
409
|
-
const vaultTotalEquity = await this.calculateVaultEquity({
|
|
410
|
-
vault: vaultAccount,
|
|
411
|
-
});
|
|
412
|
-
const vaultProtocol = this.getVaultProtocolAddress(params.vault);
|
|
413
|
-
const vpAccount = await this.program.account.vaultProtocol.fetch(
|
|
414
|
-
vaultProtocol
|
|
415
|
-
);
|
|
416
|
-
return depositSharesToVaultAmount(
|
|
417
|
-
vpAccount.protocolProfitAndFeeShares,
|
|
418
|
-
vaultAccount.totalShares,
|
|
419
|
-
vaultTotalEquity
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
253
|
public async initializeVault(params: {
|
|
424
254
|
name: number[];
|
|
425
255
|
spotMarketIndex: number;
|
|
@@ -430,17 +260,7 @@ export class VaultClient {
|
|
|
430
260
|
profitShare: number;
|
|
431
261
|
hurdleRate: number;
|
|
432
262
|
permissioned: boolean;
|
|
433
|
-
vaultProtocol?: VaultProtocolParams;
|
|
434
263
|
}): Promise<TransactionSignature> {
|
|
435
|
-
// This is a workaround to make client backwards compatible.
|
|
436
|
-
// VaultProtocol is optionally undefined, but the anchor type is optionally null.
|
|
437
|
-
// Old clients will default to undefined which prevents old clients from having to pass in a null value.
|
|
438
|
-
// Instead, we can cast to null internally.
|
|
439
|
-
const _params: VaultParams = {
|
|
440
|
-
...params,
|
|
441
|
-
vaultProtocol: params.vaultProtocol ? params.vaultProtocol : null,
|
|
442
|
-
};
|
|
443
|
-
|
|
444
264
|
const vault = getVaultAddressSync(this.program.programId, params.name);
|
|
445
265
|
const tokenAccount = getTokenVaultAddressSync(
|
|
446
266
|
this.program.programId,
|
|
@@ -477,44 +297,18 @@ export class VaultClient {
|
|
|
477
297
|
driftProgram: this.driftClient.program.programId,
|
|
478
298
|
};
|
|
479
299
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
.initializeVault(_params)
|
|
493
|
-
.preInstructions([
|
|
494
|
-
ComputeBudgetProgram.setComputeUnitLimit({
|
|
495
|
-
units: 400_000,
|
|
496
|
-
}),
|
|
497
|
-
ComputeBudgetProgram.setComputeUnitPrice({
|
|
498
|
-
microLamports: 300_000,
|
|
499
|
-
}),
|
|
500
|
-
])
|
|
501
|
-
.accounts(accounts)
|
|
502
|
-
.remainingAccounts(remainingAccounts)
|
|
503
|
-
.rpc();
|
|
504
|
-
} else {
|
|
505
|
-
return await this.program.methods
|
|
506
|
-
.initializeVault(_params)
|
|
507
|
-
.preInstructions([
|
|
508
|
-
ComputeBudgetProgram.setComputeUnitLimit({
|
|
509
|
-
units: 400_000,
|
|
510
|
-
}),
|
|
511
|
-
ComputeBudgetProgram.setComputeUnitPrice({
|
|
512
|
-
microLamports: 300_000,
|
|
513
|
-
}),
|
|
514
|
-
])
|
|
515
|
-
.accounts(accounts)
|
|
516
|
-
.rpc();
|
|
517
|
-
}
|
|
300
|
+
return await this.program.methods
|
|
301
|
+
.initializeVault(params)
|
|
302
|
+
.preInstructions([
|
|
303
|
+
ComputeBudgetProgram.setComputeUnitLimit({
|
|
304
|
+
units: 400_000,
|
|
305
|
+
}),
|
|
306
|
+
ComputeBudgetProgram.setComputeUnitPrice({
|
|
307
|
+
microLamports: 300_000,
|
|
308
|
+
}),
|
|
309
|
+
])
|
|
310
|
+
.accounts(accounts)
|
|
311
|
+
.rpc();
|
|
518
312
|
}
|
|
519
313
|
|
|
520
314
|
/**
|
|
@@ -550,7 +344,7 @@ export class VaultClient {
|
|
|
550
344
|
/**
|
|
551
345
|
* Updates the vault margin trading status.
|
|
552
346
|
* @param vault vault address to update
|
|
553
|
-
* @param
|
|
347
|
+
* @param enabeld whether to enable margin trading
|
|
554
348
|
* @returns
|
|
555
349
|
*/
|
|
556
350
|
public async updateMarginTradingEnabled(
|
|
@@ -594,14 +388,6 @@ export class VaultClient {
|
|
|
594
388
|
userAccounts: [user.getUserAccount()],
|
|
595
389
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
596
390
|
});
|
|
597
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
598
|
-
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
599
|
-
remainingAccounts.push({
|
|
600
|
-
pubkey: vaultProtocol,
|
|
601
|
-
isSigner: false,
|
|
602
|
-
isWritable: true,
|
|
603
|
-
});
|
|
604
|
-
}
|
|
605
391
|
|
|
606
392
|
return await this.program.methods
|
|
607
393
|
.managerDeposit(amount)
|
|
@@ -649,14 +435,6 @@ export class VaultClient {
|
|
|
649
435
|
userAccounts: [user.getUserAccount()],
|
|
650
436
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
651
437
|
});
|
|
652
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
653
|
-
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
654
|
-
remainingAccounts.push({
|
|
655
|
-
pubkey: vaultProtocol,
|
|
656
|
-
isSigner: false,
|
|
657
|
-
isWritable: true,
|
|
658
|
-
});
|
|
659
|
-
}
|
|
660
438
|
|
|
661
439
|
const userStatsKey = getUserStatsAccountPublicKey(
|
|
662
440
|
this.driftClient.program.programId,
|
|
@@ -721,14 +499,6 @@ export class VaultClient {
|
|
|
721
499
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
722
500
|
userAccounts: [user.getUserAccount()],
|
|
723
501
|
});
|
|
724
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
725
|
-
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
726
|
-
remainingAccounts.push({
|
|
727
|
-
pubkey: vaultProtocol,
|
|
728
|
-
isSigner: false,
|
|
729
|
-
isWritable: true,
|
|
730
|
-
});
|
|
731
|
-
}
|
|
732
502
|
|
|
733
503
|
if (this.cliMode) {
|
|
734
504
|
return await this.program.methods
|
|
@@ -765,14 +535,6 @@ export class VaultClient {
|
|
|
765
535
|
userAccounts: [user.getUserAccount()],
|
|
766
536
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
767
537
|
});
|
|
768
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
769
|
-
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
770
|
-
remainingAccounts.push({
|
|
771
|
-
pubkey: vaultProtocol,
|
|
772
|
-
isSigner: false,
|
|
773
|
-
isWritable: true,
|
|
774
|
-
});
|
|
775
|
-
}
|
|
776
538
|
|
|
777
539
|
const spotMarket = this.driftClient.getSpotMarketAccount(
|
|
778
540
|
vaultAccount.spotMarketIndex
|
|
@@ -966,14 +728,6 @@ export class VaultClient {
|
|
|
966
728
|
userAccounts: [user.getUserAccount()],
|
|
967
729
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
968
730
|
});
|
|
969
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
970
|
-
const vaultProtocol = this.getVaultProtocolAddress(vaultPubKey);
|
|
971
|
-
remainingAccounts.push({
|
|
972
|
-
pubkey: vaultProtocol,
|
|
973
|
-
isSigner: false,
|
|
974
|
-
isWritable: true,
|
|
975
|
-
});
|
|
976
|
-
}
|
|
977
731
|
|
|
978
732
|
const userStatsKey = getUserStatsAccountPublicKey(
|
|
979
733
|
this.driftClient.program.programId,
|
|
@@ -1015,15 +769,6 @@ export class VaultClient {
|
|
|
1015
769
|
};
|
|
1016
770
|
}
|
|
1017
771
|
|
|
1018
|
-
/**
|
|
1019
|
-
* Creates a transaction to deposit funds into the specified vault.
|
|
1020
|
-
* Uses the associated token account of the vault depositor authority and spot market mint,
|
|
1021
|
-
* and assumes it exists before calling this function.
|
|
1022
|
-
* @param vaultDepositor
|
|
1023
|
-
* @param amount
|
|
1024
|
-
* @param initVaultDepositor If true, will initialize the vault depositor account
|
|
1025
|
-
* @returns transaction
|
|
1026
|
-
*/
|
|
1027
772
|
public async createDepositTx(
|
|
1028
773
|
vaultDepositor: PublicKey,
|
|
1029
774
|
amount: BN,
|
|
@@ -1036,28 +781,23 @@ export class VaultClient {
|
|
|
1036
781
|
const { vaultAccount, accounts, remainingAccounts } =
|
|
1037
782
|
await this.prepDepositTx(vaultDepositor, amount, initVaultDepositor);
|
|
1038
783
|
|
|
1039
|
-
const
|
|
784
|
+
const depositIx = this.program.instruction.deposit(amount, {
|
|
785
|
+
accounts: {
|
|
786
|
+
authority: this.driftClient.wallet.publicKey,
|
|
787
|
+
...accounts,
|
|
788
|
+
},
|
|
789
|
+
remainingAccounts,
|
|
790
|
+
});
|
|
1040
791
|
|
|
1041
792
|
if (initVaultDepositor) {
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
initVaultDepositor.authority
|
|
1046
|
-
)
|
|
793
|
+
const initIx = this.createInitVaultDepositorIx(
|
|
794
|
+
vaultAccount.pubkey,
|
|
795
|
+
initVaultDepositor.authority
|
|
1047
796
|
);
|
|
797
|
+
return await this.createTxn([initIx, depositIx], txParams);
|
|
798
|
+
} else {
|
|
799
|
+
return await this.createTxn([depositIx], txParams);
|
|
1048
800
|
}
|
|
1049
|
-
|
|
1050
|
-
const depositIx = await this.program.methods
|
|
1051
|
-
.deposit(amount)
|
|
1052
|
-
.accounts({
|
|
1053
|
-
authority: this.driftClient.wallet.publicKey,
|
|
1054
|
-
...accounts,
|
|
1055
|
-
})
|
|
1056
|
-
.remainingAccounts(remainingAccounts)
|
|
1057
|
-
.instruction();
|
|
1058
|
-
ixs.push(depositIx);
|
|
1059
|
-
|
|
1060
|
-
return await this.createTxn(ixs, txParams);
|
|
1061
801
|
}
|
|
1062
802
|
|
|
1063
803
|
/**
|
|
@@ -1065,7 +805,6 @@ export class VaultClient {
|
|
|
1065
805
|
* @param vaultDepositor
|
|
1066
806
|
* @param amount
|
|
1067
807
|
* @param initVaultDepositor If true, will initialize the vault depositor account
|
|
1068
|
-
* @param txParams
|
|
1069
808
|
* @returns
|
|
1070
809
|
*/
|
|
1071
810
|
public async deposit(
|
|
@@ -1120,16 +859,6 @@ export class VaultClient {
|
|
|
1120
859
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1121
860
|
userAccounts: [user.getUserAccount()],
|
|
1122
861
|
});
|
|
1123
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
1124
|
-
const vaultProtocol = this.getVaultProtocolAddress(
|
|
1125
|
-
vaultDepositorAccount.vault
|
|
1126
|
-
);
|
|
1127
|
-
remainingAccounts.push({
|
|
1128
|
-
pubkey: vaultProtocol,
|
|
1129
|
-
isSigner: false,
|
|
1130
|
-
isWritable: true,
|
|
1131
|
-
});
|
|
1132
|
-
}
|
|
1133
862
|
|
|
1134
863
|
const userStatsKey = getUserStatsAccountPublicKey(
|
|
1135
864
|
this.driftClient.program.programId,
|
|
@@ -1186,16 +915,6 @@ export class VaultClient {
|
|
|
1186
915
|
userAccounts: [user.getUserAccount()],
|
|
1187
916
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
1188
917
|
});
|
|
1189
|
-
const vaultProtocol = this.getVaultProtocolAddress(
|
|
1190
|
-
vaultDepositorAccount.vault
|
|
1191
|
-
);
|
|
1192
|
-
if (!vaultProtocol.equals(SystemProgram.programId)) {
|
|
1193
|
-
remainingAccounts.push({
|
|
1194
|
-
pubkey: vaultProtocol,
|
|
1195
|
-
isSigner: false,
|
|
1196
|
-
isWritable: true,
|
|
1197
|
-
});
|
|
1198
|
-
}
|
|
1199
918
|
|
|
1200
919
|
const userStatsKey = getUserStatsAccountPublicKey(
|
|
1201
920
|
this.driftClient.program.programId,
|
|
@@ -1247,30 +966,20 @@ export class VaultClient {
|
|
|
1247
966
|
};
|
|
1248
967
|
|
|
1249
968
|
if (this.cliMode) {
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
.preInstructions([createAtaIx])
|
|
1256
|
-
.rpc();
|
|
1257
|
-
} else {
|
|
1258
|
-
return await this.program.methods
|
|
1259
|
-
.withdraw()
|
|
1260
|
-
.accounts(accounts)
|
|
1261
|
-
.remainingAccounts(remainingAccounts)
|
|
1262
|
-
.rpc();
|
|
1263
|
-
}
|
|
969
|
+
return await this.program.methods
|
|
970
|
+
.withdraw()
|
|
971
|
+
.accounts(accounts)
|
|
972
|
+
.remainingAccounts(remainingAccounts)
|
|
973
|
+
.rpc();
|
|
1264
974
|
} else {
|
|
1265
975
|
const ixs = [
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
.accounts({
|
|
976
|
+
this.program.instruction.withdraw({
|
|
977
|
+
accounts: {
|
|
1269
978
|
authority: this.driftClient.wallet.publicKey,
|
|
1270
979
|
...accounts,
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
|
-
|
|
980
|
+
},
|
|
981
|
+
remainingAccounts,
|
|
982
|
+
}),
|
|
1274
983
|
];
|
|
1275
984
|
if (createAtaIx) {
|
|
1276
985
|
ixs.unshift(createAtaIx);
|
|
@@ -1297,16 +1006,6 @@ export class VaultClient {
|
|
|
1297
1006
|
userAccounts: [user.getUserAccount()],
|
|
1298
1007
|
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
1299
1008
|
});
|
|
1300
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
1301
|
-
const vaultProtocol = this.getVaultProtocolAddress(
|
|
1302
|
-
vaultDepositorAccount.vault
|
|
1303
|
-
);
|
|
1304
|
-
remainingAccounts.push({
|
|
1305
|
-
pubkey: vaultProtocol,
|
|
1306
|
-
isSigner: false,
|
|
1307
|
-
isWritable: true,
|
|
1308
|
-
});
|
|
1309
|
-
}
|
|
1310
1009
|
|
|
1311
1010
|
const userStatsKey = getUserStatsAccountPublicKey(
|
|
1312
1011
|
this.driftClient.program.programId,
|
|
@@ -1397,16 +1096,6 @@ export class VaultClient {
|
|
|
1397
1096
|
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1398
1097
|
userAccounts: [user.getUserAccount()],
|
|
1399
1098
|
});
|
|
1400
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
1401
|
-
const vaultProtocol = this.getVaultProtocolAddress(
|
|
1402
|
-
vaultDepositorAccount.vault
|
|
1403
|
-
);
|
|
1404
|
-
remainingAccounts.push({
|
|
1405
|
-
pubkey: vaultProtocol,
|
|
1406
|
-
isSigner: false,
|
|
1407
|
-
isWritable: true,
|
|
1408
|
-
});
|
|
1409
|
-
}
|
|
1410
1099
|
|
|
1411
1100
|
if (this.cliMode) {
|
|
1412
1101
|
return await this.program.methods
|
|
@@ -1637,191 +1326,4 @@ export class VaultClient {
|
|
|
1637
1326
|
})
|
|
1638
1327
|
.rpc();
|
|
1639
1328
|
}
|
|
1640
|
-
|
|
1641
|
-
public async protocolRequestWithdraw(
|
|
1642
|
-
vault: PublicKey,
|
|
1643
|
-
amount: BN,
|
|
1644
|
-
withdrawUnit: WithdrawUnit
|
|
1645
|
-
): Promise<TransactionSignature> {
|
|
1646
|
-
// @ts-ignore
|
|
1647
|
-
const vaultAccount = (await this.program.account.vault.fetch(
|
|
1648
|
-
vault
|
|
1649
|
-
)) as Vault;
|
|
1650
|
-
const vp = this.getVaultProtocolAddress(vault);
|
|
1651
|
-
const vpAccount = (await this.program.account.vaultProtocol.fetch(
|
|
1652
|
-
vp
|
|
1653
|
-
)) as VaultProtocol;
|
|
1654
|
-
|
|
1655
|
-
if (!this.driftClient.wallet.publicKey.equals(vpAccount.protocol)) {
|
|
1656
|
-
throw new Error(`Only the protocol of the vault can request a withdraw.`);
|
|
1657
|
-
}
|
|
1658
|
-
|
|
1659
|
-
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
1660
|
-
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1661
|
-
userAccounts: [user.getUserAccount()],
|
|
1662
|
-
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
1663
|
-
});
|
|
1664
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
1665
|
-
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
1666
|
-
remainingAccounts.push({
|
|
1667
|
-
pubkey: vaultProtocol,
|
|
1668
|
-
isSigner: false,
|
|
1669
|
-
isWritable: true,
|
|
1670
|
-
});
|
|
1671
|
-
}
|
|
1672
|
-
|
|
1673
|
-
const userStatsKey = getUserStatsAccountPublicKey(
|
|
1674
|
-
this.driftClient.program.programId,
|
|
1675
|
-
vault
|
|
1676
|
-
);
|
|
1677
|
-
|
|
1678
|
-
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
1679
|
-
|
|
1680
|
-
const accounts = {
|
|
1681
|
-
vault,
|
|
1682
|
-
driftUserStats: userStatsKey,
|
|
1683
|
-
driftUser: vaultAccount.user,
|
|
1684
|
-
driftState: driftStateKey,
|
|
1685
|
-
};
|
|
1686
|
-
|
|
1687
|
-
if (this.cliMode) {
|
|
1688
|
-
return await this.program.methods
|
|
1689
|
-
// @ts-ignore, 0.29.0 anchor issues..
|
|
1690
|
-
.managerRequestWithdraw(amount, withdrawUnit)
|
|
1691
|
-
.accounts(accounts)
|
|
1692
|
-
.remainingAccounts(remainingAccounts)
|
|
1693
|
-
.rpc();
|
|
1694
|
-
} else {
|
|
1695
|
-
const requestWithdrawIx = this.program.instruction.managerRequestWithdraw(
|
|
1696
|
-
// @ts-ignore
|
|
1697
|
-
amount,
|
|
1698
|
-
withdrawUnit,
|
|
1699
|
-
{
|
|
1700
|
-
accounts: {
|
|
1701
|
-
manager: this.driftClient.wallet.publicKey,
|
|
1702
|
-
...accounts,
|
|
1703
|
-
},
|
|
1704
|
-
remainingAccounts,
|
|
1705
|
-
}
|
|
1706
|
-
);
|
|
1707
|
-
|
|
1708
|
-
return await this.createAndSendTxn([requestWithdrawIx]);
|
|
1709
|
-
}
|
|
1710
|
-
}
|
|
1711
|
-
|
|
1712
|
-
public async protocolCancelWithdrawRequest(
|
|
1713
|
-
vault: PublicKey
|
|
1714
|
-
): Promise<TransactionSignature> {
|
|
1715
|
-
const vaultAccount = await this.program.account.vault.fetch(vault);
|
|
1716
|
-
|
|
1717
|
-
const userStatsKey = getUserStatsAccountPublicKey(
|
|
1718
|
-
this.driftClient.program.programId,
|
|
1719
|
-
vault
|
|
1720
|
-
);
|
|
1721
|
-
|
|
1722
|
-
const driftStateKey = await this.driftClient.getStatePublicKey();
|
|
1723
|
-
|
|
1724
|
-
const accounts = {
|
|
1725
|
-
manager: this.driftClient.wallet.publicKey,
|
|
1726
|
-
vault,
|
|
1727
|
-
driftUserStats: userStatsKey,
|
|
1728
|
-
driftUser: vaultAccount.user,
|
|
1729
|
-
driftState: driftStateKey,
|
|
1730
|
-
};
|
|
1731
|
-
|
|
1732
|
-
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
1733
|
-
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1734
|
-
userAccounts: [user.getUserAccount()],
|
|
1735
|
-
});
|
|
1736
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
1737
|
-
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
1738
|
-
remainingAccounts.push({
|
|
1739
|
-
pubkey: vaultProtocol,
|
|
1740
|
-
isSigner: false,
|
|
1741
|
-
isWritable: true,
|
|
1742
|
-
});
|
|
1743
|
-
}
|
|
1744
|
-
|
|
1745
|
-
if (this.cliMode) {
|
|
1746
|
-
return await this.program.methods
|
|
1747
|
-
.mangerCancelWithdrawRequest()
|
|
1748
|
-
.accounts(accounts)
|
|
1749
|
-
.remainingAccounts(remainingAccounts)
|
|
1750
|
-
.rpc();
|
|
1751
|
-
} else {
|
|
1752
|
-
const cancelRequestWithdrawIx =
|
|
1753
|
-
this.program.instruction.mangerCancelWithdrawRequest({
|
|
1754
|
-
accounts: {
|
|
1755
|
-
...accounts,
|
|
1756
|
-
manager: this.driftClient.wallet.publicKey,
|
|
1757
|
-
},
|
|
1758
|
-
remainingAccounts,
|
|
1759
|
-
});
|
|
1760
|
-
|
|
1761
|
-
return await this.createAndSendTxn([cancelRequestWithdrawIx]);
|
|
1762
|
-
}
|
|
1763
|
-
}
|
|
1764
|
-
|
|
1765
|
-
public async protocolWithdraw(
|
|
1766
|
-
vault: PublicKey
|
|
1767
|
-
): Promise<TransactionSignature> {
|
|
1768
|
-
const vaultAccount = await this.program.account.vault.fetch(vault);
|
|
1769
|
-
|
|
1770
|
-
if (!this.driftClient.wallet.publicKey.equals(vaultAccount.manager)) {
|
|
1771
|
-
throw new Error(`Only the manager of the vault can request a withdraw.`);
|
|
1772
|
-
}
|
|
1773
|
-
|
|
1774
|
-
const user = await this.getSubscribedVaultUser(vaultAccount.user);
|
|
1775
|
-
|
|
1776
|
-
const remainingAccounts = this.driftClient.getRemainingAccounts({
|
|
1777
|
-
userAccounts: [user.getUserAccount()],
|
|
1778
|
-
writableSpotMarketIndexes: [vaultAccount.spotMarketIndex],
|
|
1779
|
-
});
|
|
1780
|
-
if (!vaultAccount.vaultProtocol.equals(SystemProgram.programId)) {
|
|
1781
|
-
const vaultProtocol = this.getVaultProtocolAddress(vault);
|
|
1782
|
-
remainingAccounts.push({
|
|
1783
|
-
pubkey: vaultProtocol,
|
|
1784
|
-
isSigner: false,
|
|
1785
|
-
isWritable: true,
|
|
1786
|
-
});
|
|
1787
|
-
}
|
|
1788
|
-
|
|
1789
|
-
const spotMarket = this.driftClient.getSpotMarketAccount(
|
|
1790
|
-
vaultAccount.spotMarketIndex
|
|
1791
|
-
);
|
|
1792
|
-
if (!spotMarket) {
|
|
1793
|
-
throw new Error(
|
|
1794
|
-
`Spot market ${vaultAccount.spotMarketIndex} not found on driftClient`
|
|
1795
|
-
);
|
|
1796
|
-
}
|
|
1797
|
-
|
|
1798
|
-
const ix = this.program.instruction.managerWithdraw({
|
|
1799
|
-
accounts: {
|
|
1800
|
-
vault,
|
|
1801
|
-
manager: this.driftClient.wallet.publicKey,
|
|
1802
|
-
vaultTokenAccount: vaultAccount.tokenAccount,
|
|
1803
|
-
driftUser: await getUserAccountPublicKey(
|
|
1804
|
-
this.driftClient.program.programId,
|
|
1805
|
-
vault
|
|
1806
|
-
),
|
|
1807
|
-
driftProgram: this.driftClient.program.programId,
|
|
1808
|
-
driftUserStats: getUserStatsAccountPublicKey(
|
|
1809
|
-
this.driftClient.program.programId,
|
|
1810
|
-
vault
|
|
1811
|
-
),
|
|
1812
|
-
driftState: await this.driftClient.getStatePublicKey(),
|
|
1813
|
-
driftSpotMarketVault: spotMarket.vault,
|
|
1814
|
-
userTokenAccount: getAssociatedTokenAddressSync(
|
|
1815
|
-
spotMarket.mint,
|
|
1816
|
-
this.driftClient.wallet.publicKey
|
|
1817
|
-
),
|
|
1818
|
-
driftSigner: this.driftClient.getStateAccount().signer,
|
|
1819
|
-
tokenProgram: TOKEN_PROGRAM_ID,
|
|
1820
|
-
},
|
|
1821
|
-
remainingAccounts,
|
|
1822
|
-
});
|
|
1823
|
-
return this.createAndSendTxn([ix], {
|
|
1824
|
-
cuLimit: 1_000_000,
|
|
1825
|
-
});
|
|
1826
|
-
}
|
|
1827
1329
|
}
|