@velocity-exchange/vaults-sdk 0.0.1

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.
Files changed (65) hide show
  1. package/.env.example +3 -0
  2. package/README.md +152 -0
  3. package/cli/cli.ts +751 -0
  4. package/cli/commands/adminDeleteFeeUpdate.ts +73 -0
  5. package/cli/commands/adminInitFeeUpdate.ts +73 -0
  6. package/cli/commands/adminUpdateVaultClass.ts +49 -0
  7. package/cli/commands/applyProfitShare.ts +139 -0
  8. package/cli/commands/decodeLogs.ts +98 -0
  9. package/cli/commands/deposit.ts +98 -0
  10. package/cli/commands/deriveVaultAddress.ts +14 -0
  11. package/cli/commands/forceWithdraw.ts +56 -0
  12. package/cli/commands/forceWithdrawAll.ts +142 -0
  13. package/cli/commands/index.ts +28 -0
  14. package/cli/commands/initVault.ts +227 -0
  15. package/cli/commands/initVaultDepositor.ts +42 -0
  16. package/cli/commands/listDepositorsForVault.ts +32 -0
  17. package/cli/commands/managerApplyProfitShare.ts +32 -0
  18. package/cli/commands/managerBorrow.ts +77 -0
  19. package/cli/commands/managerCancelWithdraw.ts +30 -0
  20. package/cli/commands/managerDeposit.ts +45 -0
  21. package/cli/commands/managerRepay.ts +94 -0
  22. package/cli/commands/managerRequestWithdraw.ts +86 -0
  23. package/cli/commands/managerUpdateBorrow.ts +56 -0
  24. package/cli/commands/managerUpdateFees.ts +156 -0
  25. package/cli/commands/managerUpdateMarginTradingEnabled.ts +32 -0
  26. package/cli/commands/managerUpdatePoolId.ts +36 -0
  27. package/cli/commands/managerUpdateVault.ts +210 -0
  28. package/cli/commands/managerUpdateVaultDelegate.ts +43 -0
  29. package/cli/commands/managerUpdateVaultManager.ts +77 -0
  30. package/cli/commands/managerWithdraw.ts +30 -0
  31. package/cli/commands/requestWithdraw.ts +58 -0
  32. package/cli/commands/vaultDeposit.ts +42 -0
  33. package/cli/commands/vaultInvariantChecks.ts +407 -0
  34. package/cli/commands/vaultWithdraw.ts +42 -0
  35. package/cli/commands/viewVault.ts +50 -0
  36. package/cli/commands/viewVaultDepositor.ts +36 -0
  37. package/cli/commands/withdraw.ts +40 -0
  38. package/cli/ledgerWallet.test.ts +49 -0
  39. package/cli/ledgerWallet.ts +111 -0
  40. package/cli/utils.ts +389 -0
  41. package/package.json +48 -0
  42. package/src/accountSubscribers/index.ts +2 -0
  43. package/src/accountSubscribers/pollingVaultDepositorSubscriber.ts +69 -0
  44. package/src/accountSubscribers/pollingVaultSubscriber.ts +63 -0
  45. package/src/accountSubscribers/pollingVaultsProgramAccountSubscriber.ts +114 -0
  46. package/src/accounts/index.ts +2 -0
  47. package/src/accounts/vaultAccount.ts +255 -0
  48. package/src/accounts/vaultDepositorAccount.ts +77 -0
  49. package/src/accounts/vaultsProgramAccount.ts +38 -0
  50. package/src/addresses.ts +114 -0
  51. package/src/constants/index.ts +15 -0
  52. package/src/idl/drift_vaults.json +5698 -0
  53. package/src/index.ts +11 -0
  54. package/src/math/index.ts +2 -0
  55. package/src/math/vault.ts +71 -0
  56. package/src/math/vaultDepositor.ts +90 -0
  57. package/src/name.ts +18 -0
  58. package/src/parsers/index.ts +1 -0
  59. package/src/parsers/logParser.ts +28 -0
  60. package/src/types/drift_vaults.ts +6211 -0
  61. package/src/types/types.ts +336 -0
  62. package/src/utils.ts +74 -0
  63. package/src/vaultClient.ts +3666 -0
  64. package/tsconfig.json +24 -0
  65. package/velocity-exchange-vaults-sdk-0.0.1.tgz +0 -0
@@ -0,0 +1,336 @@
1
+ import { BN, DataAndSlot, Event } from '@velocity-exchange/sdk';
2
+ import { PublicKey } from '@solana/web3.js';
3
+ import { EventEmitter } from 'events';
4
+ import StrictEventEmitter from 'strict-event-emitter-types';
5
+
6
+ export const VAULT_PROGRAM_ID = new PublicKey(
7
+ 'vAuLTsyrvSfZRuRB3XgvkPwNGgYSs9YRYymVebLKoxR'
8
+ );
9
+
10
+ export class WithdrawUnit {
11
+ static readonly SHARES = { shares: {} };
12
+ static readonly TOKEN = { token: {} };
13
+ static readonly SHARES_PERCENT = { sharesPercent: {} };
14
+ }
15
+
16
+ export type WithdrawRequest = {
17
+ shares: BN;
18
+ value: BN;
19
+ ts: BN;
20
+ };
21
+
22
+ export type VaultParams = {
23
+ name: number[];
24
+ spotMarketIndex: number;
25
+ redeemPeriod: BN;
26
+ maxTokens: BN;
27
+ minDepositAmount: BN;
28
+ managementFee: BN;
29
+ profitShare: number;
30
+ hurdleRate: number;
31
+ permissioned: boolean;
32
+ };
33
+
34
+ export type VaultWithProtocolParams = {
35
+ name: number[];
36
+ spotMarketIndex: number;
37
+ redeemPeriod: BN;
38
+ maxTokens: BN;
39
+ minDepositAmount: BN;
40
+ managementFee: BN;
41
+ profitShare: number;
42
+ hurdleRate: number;
43
+ permissioned: boolean;
44
+ vaultProtocol: VaultProtocolParams;
45
+ };
46
+
47
+ export type VaultProtocolParams = {
48
+ protocol: PublicKey;
49
+ protocolFee: BN;
50
+ protocolProfitShare: number;
51
+ };
52
+
53
+ export type UpdateVaultParams = {
54
+ redeemPeriod: BN | null;
55
+ maxTokens: BN | null;
56
+ minDepositAmount: BN | null;
57
+ managementFee: BN | null;
58
+ profitShare: number | null;
59
+ hurdleRate: number | null;
60
+ permissioned: boolean | null;
61
+ };
62
+
63
+ export type UpdateVaultProtocolParams = {
64
+ protocolFee: BN | null;
65
+ protocolProfitShare: number | null;
66
+ };
67
+
68
+ // Vault program accounts
69
+
70
+ export type Vault = {
71
+ name: number[];
72
+ pubkey: PublicKey;
73
+ manager: PublicKey;
74
+ tokenAccount: PublicKey;
75
+ userStats: PublicKey;
76
+ user: PublicKey;
77
+ delegate: PublicKey;
78
+ liquidationDelegate: PublicKey;
79
+ userShares: BN;
80
+ totalShares: BN;
81
+ lastFeeUpdateTs: BN;
82
+ liquidationStartTs: BN;
83
+ redeemPeriod: BN;
84
+ totalWithdrawRequested: BN;
85
+ maxTokens: BN;
86
+ sharesBase: number;
87
+ managementFee: BN;
88
+ initTs: BN;
89
+ netDeposits: BN;
90
+ managerNetDeposits: BN;
91
+ totalDeposits: BN;
92
+ totalWithdraws: BN;
93
+ managerTotalDeposits: BN;
94
+ managerTotalWithdraws: BN;
95
+ managerTotalFee: BN;
96
+ managerTotalProfitShare: BN;
97
+ minDepositAmount: BN;
98
+ profitShare: number;
99
+ hurdleRate: number;
100
+ spotMarketIndex: number;
101
+ bump: number;
102
+ permissioned: boolean;
103
+ lastManagerWithdrawRequest: WithdrawRequest;
104
+ vaultProtocol: boolean;
105
+ feeUpdateStatus: FeeUpdateStatus;
106
+ vaultClass: VaultClass;
107
+ managerBorrowedValue: BN;
108
+ padding: number[];
109
+ };
110
+
111
+ export enum VaultClass {
112
+ NORMAL = 0,
113
+ TRUSTED = 1,
114
+ }
115
+
116
+ export function isNormalVaultClass(vaultClass: number | VaultClass): boolean {
117
+ return (vaultClass & VaultClass.NORMAL) === VaultClass.NORMAL;
118
+ }
119
+
120
+ export function isTrustedVaultClass(vaultClass: number | VaultClass): boolean {
121
+ return (vaultClass & VaultClass.TRUSTED) === VaultClass.TRUSTED;
122
+ }
123
+
124
+ export enum FeeUpdateStatus {
125
+ None = 0,
126
+ PendingFeeUpdate = 1,
127
+ }
128
+
129
+ export function hasPendingFeeUpdate(status: number | FeeUpdateStatus): boolean {
130
+ return (
131
+ (status & FeeUpdateStatus.PendingFeeUpdate) ===
132
+ FeeUpdateStatus.PendingFeeUpdate
133
+ );
134
+ }
135
+
136
+ export type FeeUpdate = {
137
+ incomingUpdateTs: BN;
138
+ incomingManagementFee: BN;
139
+ incomingProfitShare: number;
140
+ incomingHurdleRate: number;
141
+ padding: BN[];
142
+ padding2: number[];
143
+ };
144
+
145
+ export type VaultDepositor = {
146
+ vault: PublicKey;
147
+ pubkey: PublicKey;
148
+ authority: PublicKey;
149
+ vaultShares: BN;
150
+ lastWithdrawRequest: WithdrawRequest;
151
+ lastValidTs: BN;
152
+ netDeposits: BN;
153
+ totalDeposits: BN;
154
+ totalWithdraws: BN;
155
+ cumulativeProfitShareAmount: BN;
156
+ vaultSharesBase: number;
157
+ profitShareFeePaid: BN;
158
+ paddingAlign: number;
159
+ padding: BN[];
160
+ };
161
+
162
+ export type VaultProtocol = {
163
+ protocol: PublicKey;
164
+ protocolProfitAndFeeShares: BN;
165
+ protocolFee: BN;
166
+ protocolTotalWithdraws: BN;
167
+ protocolTotalFee: BN;
168
+ protocolTotalProfitShare: BN;
169
+ lastProtocolWithdrawRequest: WithdrawRequest;
170
+ protocolProfitShare: number;
171
+ bump: number;
172
+ version: number;
173
+ };
174
+
175
+ export type VaultsProgramAccountBaseEvents = {
176
+ update: void;
177
+ error: (e: Error) => void;
178
+ };
179
+
180
+ export type VaultDepositorAccountEvents = {
181
+ vaultDepositorUpdate: (payload: VaultDepositor) => void;
182
+ } & VaultsProgramAccountBaseEvents;
183
+
184
+ export type VaultAccountEvents = {
185
+ vaultUpdate: (payload: Vault) => void;
186
+ } & VaultsProgramAccountBaseEvents;
187
+
188
+ export interface VaultsProgramAccountSubscriber<
189
+ Account,
190
+ AccountEvents extends VaultsProgramAccountBaseEvents
191
+ > {
192
+ eventEmitter: StrictEventEmitter<EventEmitter, AccountEvents>;
193
+ isSubscribed: boolean;
194
+
195
+ subscribe(): Promise<boolean>;
196
+
197
+ fetch(): Promise<void>;
198
+
199
+ updateData(account: Account, slot: number): void;
200
+
201
+ unsubscribe(): Promise<void>;
202
+
203
+ getAccountAndSlot(): DataAndSlot<Account>;
204
+ }
205
+
206
+ export type VaultAccountSubscriber = VaultsProgramAccountSubscriber<
207
+ Vault,
208
+ VaultAccountEvents
209
+ >;
210
+
211
+ export type VaultDepositorAccountSubscriber = VaultsProgramAccountSubscriber<
212
+ VaultDepositor,
213
+ VaultDepositorAccountEvents
214
+ >;
215
+
216
+ // Logs/Records
217
+
218
+ export class VaultDepositorAction {
219
+ static readonly DEPOSIT = { deposit: {} };
220
+ static readonly WITHDRAW = { withdraw: {} };
221
+ static readonly WITHDRAW_REQUEST = { withdrawRequest: {} };
222
+ static readonly CANCEL_WITHDRAW_REQUEST = { cancelWithdrawRequest: {} };
223
+ static readonly FEE_PAYMENT = { feePayment: {} };
224
+ }
225
+
226
+ export type VaultDepositorRecord = {
227
+ ts: BN;
228
+
229
+ vault: PublicKey;
230
+ depositorAuthority: PublicKey;
231
+ action: VaultDepositorAction;
232
+ amount: BN;
233
+
234
+ spotMarketIndex: number;
235
+ vaultSharesBefore: BN;
236
+ vaultSharesAfter: BN;
237
+ vaultEquityBefore: BN;
238
+
239
+ userVaultSharesBefore: BN;
240
+ totalVaultSharesBefore: BN;
241
+
242
+ userVaultSharesAfter: BN;
243
+ totalVaultSharesAfter: BN;
244
+
245
+ profitShare: BN;
246
+ managementFee: BN;
247
+ managementFeeShares: BN;
248
+
249
+ depositOraclePrice: BN;
250
+ };
251
+
252
+ export type VaultDepositorV1Record = {
253
+ ts: BN;
254
+
255
+ vault: PublicKey;
256
+ depositorAuthority: PublicKey;
257
+ action: VaultDepositorAction;
258
+ amount: BN;
259
+
260
+ spotMarketIndex: number;
261
+ vaultSharesBefore: BN;
262
+ vaultSharesAfter: BN;
263
+ vaultEquityBefore: BN;
264
+
265
+ userVaultSharesBefore: BN;
266
+ totalVaultSharesBefore: BN;
267
+
268
+ userVaultSharesAfter: BN;
269
+ totalVaultSharesAfter: BN;
270
+
271
+ protocolProfitShare: BN;
272
+ protocolFee: BN;
273
+ protocolFeeShares: BN;
274
+
275
+ managerProfitShare: BN;
276
+ managementFee: BN;
277
+ managementFeeShares: BN;
278
+
279
+ depositOraclePrice: BN;
280
+ };
281
+
282
+ export type VaultsEventMap = {
283
+ VaultDepositorRecord: Event<VaultDepositorRecord>;
284
+ VaultDepositorV1Record: Event<VaultDepositorV1Record>;
285
+ };
286
+
287
+ export type EventType = keyof VaultsEventMap;
288
+ export type WrappedEvent<Type extends EventType> = VaultsEventMap[Type] & {
289
+ eventType: Type;
290
+ };
291
+ export type WrappedEvents = WrappedEvent<EventType>[];
292
+
293
+ export class FeeUpdateAction {
294
+ static readonly PENDING = { pending: {} };
295
+ static readonly APPLIED = { applied: {} };
296
+ }
297
+
298
+ export type FeeUpdateRecord = {
299
+ ts: BN;
300
+ action: FeeUpdateAction;
301
+ timelockEndTs: BN;
302
+ vault: PublicKey;
303
+ oldManagementFee: BN;
304
+ oldProfitShare: number;
305
+ oldHurdleRate: number;
306
+ newManagementFee: BN;
307
+ newProfitShare: number;
308
+ newHurdleRate: number;
309
+ };
310
+
311
+ export type ManagerBorrowRecord = {
312
+ ts: BN;
313
+ vault: PublicKey;
314
+ manager: PublicKey;
315
+ borrowAmount: BN;
316
+ borrowValue: BN;
317
+ borrowSpotMarketIndex: number;
318
+ borrowOraclePrice: BN;
319
+ depositSpotMarketIndex: number;
320
+ depositOraclePrice: BN;
321
+ vaultEquity: BN;
322
+ };
323
+
324
+ export type ManagerRepayRecord = {
325
+ ts: BN;
326
+ vault: PublicKey;
327
+ manager: PublicKey;
328
+ repayAmount: BN;
329
+ repayValue: BN;
330
+ repaySpotMarketIndex: number;
331
+ repayOraclePrice: BN;
332
+ depositSpotMarketIndex: number;
333
+ depositOraclePrice: BN;
334
+ vaultEquityBefore: BN;
335
+ vaultEquityAfter: BN;
336
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,74 @@
1
+ import { AnchorProvider } from '@coral-xyz/anchor';
2
+ import { DriftClient, IWallet } from '@velocity-exchange/sdk';
3
+ import { Connection, PublicKey, TransactionInstruction } from '@solana/web3.js';
4
+ import { DriftVaults } from './types/drift_vaults';
5
+ import driftVaultsIDL from './idl/drift_vaults.json';
6
+
7
+ export const IDL = driftVaultsIDL as DriftVaults;
8
+ import { VaultClient } from './vaultClient';
9
+ import * as anchor from '@coral-xyz/anchor';
10
+ import {
11
+ createAssociatedTokenAccountInstruction,
12
+ getAssociatedTokenAddress,
13
+ } from '@solana/spl-token';
14
+
15
+ export const getDriftVaultProgram = (
16
+ connection: Connection,
17
+ wallet: IWallet
18
+ ): anchor.Program<DriftVaults> => {
19
+ const provider = new AnchorProvider(connection, wallet as anchor.Wallet, {});
20
+ anchor.setProvider(provider);
21
+ const vaultProgram = new anchor.Program<DriftVaults>(
22
+ driftVaultsIDL as DriftVaults,
23
+ provider
24
+ );
25
+
26
+ return vaultProgram;
27
+ };
28
+
29
+ export const getVaultClient = (
30
+ connection: Connection,
31
+ wallet: IWallet,
32
+ driftClient: DriftClient
33
+ ): VaultClient => {
34
+ const vaultProgram = getDriftVaultProgram(connection, wallet);
35
+
36
+ const vaultClient = new VaultClient({
37
+ driftClient,
38
+ program: vaultProgram,
39
+ });
40
+
41
+ return vaultClient;
42
+ };
43
+
44
+ export const getOrCreateATAInstruction = async (
45
+ tokenMint: PublicKey,
46
+ owner: PublicKey,
47
+ connection: Connection,
48
+ allowOwnerOffCurve = true,
49
+ payer = owner
50
+ ): Promise<[PublicKey, TransactionInstruction?]> => {
51
+ let toAccount;
52
+ try {
53
+ toAccount = await getAssociatedTokenAddress(
54
+ tokenMint,
55
+ owner,
56
+ allowOwnerOffCurve
57
+ );
58
+ const account = await connection.getAccountInfo(toAccount);
59
+ if (!account) {
60
+ const ix = createAssociatedTokenAccountInstruction(
61
+ payer,
62
+ toAccount,
63
+ owner,
64
+ tokenMint
65
+ );
66
+ return [toAccount, ix];
67
+ }
68
+ return [toAccount, undefined];
69
+ } catch (e) {
70
+ /* handle error */
71
+ console.error('Error::getOrCreateATAInstruction', e);
72
+ throw e;
73
+ }
74
+ };