@drift-labs/vaults-sdk 0.1.0

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 (78) hide show
  1. package/.env.example +2 -0
  2. package/README.md +58 -0
  3. package/cli/cli.ts +105 -0
  4. package/cli/commands/applyProfitShare.ts +48 -0
  5. package/cli/commands/deposit.ts +35 -0
  6. package/cli/commands/index.ts +13 -0
  7. package/cli/commands/initVault.ts +61 -0
  8. package/cli/commands/initVaultDepositor.ts +43 -0
  9. package/cli/commands/listDepositorsForVault.ts +33 -0
  10. package/cli/commands/managerCancelWithdraw.ts +25 -0
  11. package/cli/commands/managerDeposit.ts +34 -0
  12. package/cli/commands/managerRequestWithdraw.ts +27 -0
  13. package/cli/commands/managerUpdateVault.ts +36 -0
  14. package/cli/commands/managerWithdraw.ts +25 -0
  15. package/cli/commands/requestWithdraw.ts +29 -0
  16. package/cli/commands/vaultDeposit.ts +43 -0
  17. package/cli/commands/vaultWithdraw.ts +43 -0
  18. package/cli/commands/viewVault.ts +30 -0
  19. package/cli/commands/viewVaultDepositor.ts +37 -0
  20. package/cli/commands/withdraw.ts +25 -0
  21. package/cli/utils.ts +119 -0
  22. package/lib/accountSubscribers/index.d.ts +2 -0
  23. package/lib/accountSubscribers/index.js +14 -0
  24. package/lib/accountSubscribers/pollingVaultDepositorSubscriber.d.ts +7 -0
  25. package/lib/accountSubscribers/pollingVaultDepositorSubscriber.js +44 -0
  26. package/lib/accountSubscribers/pollingVaultSubscriber.d.ts +7 -0
  27. package/lib/accountSubscribers/pollingVaultSubscriber.js +44 -0
  28. package/lib/accountSubscribers/pollingVaultsProgramAccountSubscriber.d.ts +28 -0
  29. package/lib/accountSubscribers/pollingVaultsProgramAccountSubscriber.js +68 -0
  30. package/lib/accounts/index.d.ts +2 -0
  31. package/lib/accounts/index.js +14 -0
  32. package/lib/accounts/vaultAccount.d.ts +15 -0
  33. package/lib/accounts/vaultAccount.js +53 -0
  34. package/lib/accounts/vaultDepositorAccount.d.ts +18 -0
  35. package/lib/accounts/vaultDepositorAccount.js +45 -0
  36. package/lib/accounts/vaultsProgramAccount.d.ts +13 -0
  37. package/lib/accounts/vaultsProgramAccount.js +25 -0
  38. package/lib/addresses.d.ts +4 -0
  39. package/lib/addresses.js +46 -0
  40. package/lib/constants/index.d.ts +3 -0
  41. package/lib/constants/index.js +5 -0
  42. package/lib/index.d.ts +9 -0
  43. package/lib/index.js +21 -0
  44. package/lib/name.d.ts +3 -0
  45. package/lib/name.js +19 -0
  46. package/lib/parsers/index.d.ts +1 -0
  47. package/lib/parsers/index.js +13 -0
  48. package/lib/parsers/logParser.d.ts +14 -0
  49. package/lib/parsers/logParser.js +21 -0
  50. package/lib/types/drift_vaults.d.ts +1475 -0
  51. package/lib/types/drift_vaults.js +1477 -0
  52. package/lib/types/types.d.ts +137 -0
  53. package/lib/types/types.js +20 -0
  54. package/lib/utils.d.ts +7 -0
  55. package/lib/utils.js +43 -0
  56. package/lib/vaultClient.d.ts +100 -0
  57. package/lib/vaultClient.js +596 -0
  58. package/package.json +29 -0
  59. package/src/accountSubscribers/index.ts +2 -0
  60. package/src/accountSubscribers/pollingVaultDepositorSubscriber.ts +68 -0
  61. package/src/accountSubscribers/pollingVaultSubscriber.ts +62 -0
  62. package/src/accountSubscribers/pollingVaultsProgramAccountSubscriber.ts +111 -0
  63. package/src/accounts/index.ts +2 -0
  64. package/src/accounts/vaultAccount.ts +86 -0
  65. package/src/accounts/vaultDepositorAccount.ts +67 -0
  66. package/src/accounts/vaultsProgramAccount.ts +37 -0
  67. package/src/addresses.ts +43 -0
  68. package/src/constants/index.ts +3 -0
  69. package/src/idl/drift_vaults.json +1547 -0
  70. package/src/index.ts +9 -0
  71. package/src/name.ts +18 -0
  72. package/src/parsers/index.ts +1 -0
  73. package/src/parsers/logParser.ts +28 -0
  74. package/src/types/drift_vaults.ts +2949 -0
  75. package/src/types/types.ts +158 -0
  76. package/src/utils.ts +33 -0
  77. package/src/vaultClient.ts +904 -0
  78. package/tsconfig.json +13 -0
@@ -0,0 +1,137 @@
1
+ /// <reference types="bn.js" />
2
+ /// <reference types="node" />
3
+ import { BN, DataAndSlot, Event } from '@drift-labs/sdk';
4
+ import { PublicKey } from '@solana/web3.js';
5
+ import { EventEmitter } from 'events';
6
+ import StrictEventEmitter from 'strict-event-emitter-types';
7
+ export declare const VAULT_PROGRAM_ID: PublicKey;
8
+ export declare class WithdrawUnit {
9
+ static readonly SHARES: {
10
+ shares: {};
11
+ };
12
+ static readonly TOKEN: {
13
+ token: {};
14
+ };
15
+ static readonly SHARES_PERCENT: {
16
+ sharesPercent: {};
17
+ };
18
+ }
19
+ export declare type WithdrawRequest = {
20
+ shares: BN;
21
+ value: BN;
22
+ ts: BN;
23
+ };
24
+ export declare type Vault = {
25
+ name: number[];
26
+ pubkey: PublicKey;
27
+ manager: PublicKey;
28
+ tokenAccount: PublicKey;
29
+ userStats: PublicKey;
30
+ user: PublicKey;
31
+ delegate: PublicKey;
32
+ liquidationDelegate: PublicKey;
33
+ userShares: BN;
34
+ totalShares: BN;
35
+ lastFeeUpdateTs: BN;
36
+ liquidationStartTs: BN;
37
+ redeemPeriod: BN;
38
+ totalWithdrawRequested: BN;
39
+ maxTokens: BN;
40
+ sharesBase: number;
41
+ managementFee: BN;
42
+ initTs: BN;
43
+ netDeposits: BN;
44
+ managerNetDeposits: BN;
45
+ totalDeposits: BN;
46
+ totalWithdraws: BN;
47
+ managerTotalDeposits: BN;
48
+ managerTotalWithdraws: BN;
49
+ managerTotalFee: BN;
50
+ managerTotalProfitShare: BN;
51
+ minDepositAmount: BN;
52
+ profitShare: number;
53
+ hurdleRate: number;
54
+ spotMarketIndex: number;
55
+ bump: number;
56
+ permissioned: boolean;
57
+ lastManagerWithdrawRequest: WithdrawRequest;
58
+ };
59
+ export declare type VaultDepositor = {
60
+ vault: PublicKey;
61
+ pubkey: PublicKey;
62
+ authority: PublicKey;
63
+ vaultShares: BN;
64
+ lastWithdrawRequest: WithdrawRequest;
65
+ lastValidTs: BN;
66
+ netDeposits: BN;
67
+ totalDeposits: BN;
68
+ totalWithdraws: BN;
69
+ cumulativeProfitShareAmount: BN;
70
+ vaultSharesBase: number;
71
+ profitShareFeePaid: BN;
72
+ padding: number[];
73
+ };
74
+ export declare type VaultsProgramAccountBaseEvents = {
75
+ update: void;
76
+ error: (e: Error) => void;
77
+ };
78
+ export declare type VaultDepositorAccountEvents = {
79
+ vaultDepositorUpdate: (payload: VaultDepositor) => void;
80
+ } & VaultsProgramAccountBaseEvents;
81
+ export declare type VaultAccountEvents = {
82
+ vaultUpdate: (payload: Vault) => void;
83
+ } & VaultsProgramAccountBaseEvents;
84
+ export interface VaultsProgramAccountSubscriber<Account, AccountEvents extends VaultsProgramAccountBaseEvents> {
85
+ eventEmitter: StrictEventEmitter<EventEmitter, AccountEvents>;
86
+ isSubscribed: boolean;
87
+ subscribe(): Promise<boolean>;
88
+ fetch(): Promise<void>;
89
+ updateData(account: Account, slot: number): void;
90
+ unsubscribe(): Promise<void>;
91
+ getAccountAndSlot(): DataAndSlot<Account>;
92
+ }
93
+ export declare type VaultAccountSubscriber = VaultsProgramAccountSubscriber<Vault, VaultAccountEvents>;
94
+ export declare type VaultDepositorAccountSubscriber = VaultsProgramAccountSubscriber<VaultDepositor, VaultDepositorAccountEvents>;
95
+ export declare class VaultDepositorAction {
96
+ static readonly DEPOSIT: {
97
+ deposit: {};
98
+ };
99
+ static readonly WITHDRAW: {
100
+ withdraw: {};
101
+ };
102
+ static readonly WITHDRAW_REQUEST: {
103
+ withdrawRequest: {};
104
+ };
105
+ static readonly CANCEL_WITHDRAW_REQUEST: {
106
+ cancelWithdrawRequest: {};
107
+ };
108
+ static readonly FEE_PAYMENT: {
109
+ feePayment: {};
110
+ };
111
+ }
112
+ export declare type VaultDepositorRecord = {
113
+ ts: BN;
114
+ vault: PublicKey;
115
+ depositorAuthority: PublicKey;
116
+ action: VaultDepositorAction;
117
+ amount: BN;
118
+ spotMarketIndex: number;
119
+ vaultSharesBefore: BN;
120
+ vaultSharesAfter: BN;
121
+ vaultEquityBefore: BN;
122
+ userVaultSharesBefore: BN;
123
+ totalVaultSharesBefore: BN;
124
+ userVaultSharesAfter: BN;
125
+ totalVaultSharesAfter: BN;
126
+ profitShare: BN;
127
+ managementFee: BN;
128
+ managementFeeShares: BN;
129
+ };
130
+ export declare type VaultsEventMap = {
131
+ VaultDepositorRecord: Event<VaultDepositorRecord>;
132
+ };
133
+ export declare type EventType = keyof VaultsEventMap;
134
+ export declare type WrappedEvent<Type extends EventType> = VaultsEventMap[Type] & {
135
+ eventType: Type;
136
+ };
137
+ export declare type WrappedEvents = WrappedEvent<EventType>[];
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VaultDepositorAction = exports.WithdrawUnit = exports.VAULT_PROGRAM_ID = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ exports.VAULT_PROGRAM_ID = new web3_js_1.PublicKey('vAuLTsyrvSfZRuRB3XgvkPwNGgYSs9YRYymVebLKoxR');
6
+ class WithdrawUnit {
7
+ }
8
+ exports.WithdrawUnit = WithdrawUnit;
9
+ WithdrawUnit.SHARES = { shares: {} };
10
+ WithdrawUnit.TOKEN = { token: {} };
11
+ WithdrawUnit.SHARES_PERCENT = { sharesPercent: {} };
12
+ // Logs/Records
13
+ class VaultDepositorAction {
14
+ }
15
+ exports.VaultDepositorAction = VaultDepositorAction;
16
+ VaultDepositorAction.DEPOSIT = { deposit: {} };
17
+ VaultDepositorAction.WITHDRAW = { withdraw: {} };
18
+ VaultDepositorAction.WITHDRAW_REQUEST = { withdrawRequest: {} };
19
+ VaultDepositorAction.CANCEL_WITHDRAW_REQUEST = { cancelWithdrawRequest: {} };
20
+ VaultDepositorAction.FEE_PAYMENT = { feePayment: {} };
package/lib/utils.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { DriftClient, IWallet } from '@drift-labs/sdk';
2
+ import { Connection } from '@solana/web3.js';
3
+ import { DriftVaults } from './types/drift_vaults';
4
+ import { VaultClient } from './vaultClient';
5
+ import * as anchor from '@coral-xyz/anchor';
6
+ export declare const getDriftVaultProgram: (connection: Connection, wallet: IWallet) => anchor.Program<DriftVaults>;
7
+ export declare const getVaultClient: (connection: Connection, wallet: IWallet, driftClient: DriftClient) => VaultClient;
package/lib/utils.js ADDED
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.getVaultClient = exports.getDriftVaultProgram = void 0;
23
+ const anchor_1 = require("@coral-xyz/anchor");
24
+ const drift_vaults_1 = require("./types/drift_vaults");
25
+ const vaultClient_1 = require("./vaultClient");
26
+ const anchor = __importStar(require("@coral-xyz/anchor"));
27
+ const types_1 = require("./types/types");
28
+ const getDriftVaultProgram = (connection, wallet) => {
29
+ const provider = new anchor_1.AnchorProvider(connection, wallet, {});
30
+ anchor.setProvider(provider);
31
+ const vaultProgram = new anchor.Program(drift_vaults_1.IDL, types_1.VAULT_PROGRAM_ID, provider);
32
+ return vaultProgram;
33
+ };
34
+ exports.getDriftVaultProgram = getDriftVaultProgram;
35
+ const getVaultClient = (connection, wallet, driftClient) => {
36
+ const vaultProgram = exports.getDriftVaultProgram(connection, wallet);
37
+ const vaultClient = new vaultClient_1.VaultClient({
38
+ driftClient,
39
+ program: vaultProgram,
40
+ });
41
+ return vaultClient;
42
+ };
43
+ exports.getVaultClient = getVaultClient;
@@ -0,0 +1,100 @@
1
+ /// <reference types="bn.js" />
2
+ import { BN, DriftClient } from '@drift-labs/sdk';
3
+ import { Program, ProgramAccount } from '@coral-xyz/anchor';
4
+ import { DriftVaults } from './types/drift_vaults';
5
+ import { PublicKey, SetComputeUnitLimitParams, TransactionInstruction, TransactionSignature } from '@solana/web3.js';
6
+ import { Vault, VaultDepositor, WithdrawUnit } from './types/types';
7
+ export declare class VaultClient {
8
+ driftClient: DriftClient;
9
+ program: Program<DriftVaults>;
10
+ cliMode: boolean;
11
+ constructor({ driftClient, program, cliMode, }: {
12
+ driftClient: DriftClient;
13
+ program: Program<DriftVaults>;
14
+ cliMode?: boolean;
15
+ });
16
+ getVault(vault: PublicKey): Promise<Vault>;
17
+ getVaultDepositor(vaultDepositor: PublicKey): Promise<any>;
18
+ getAllVaultDepositors(vault: PublicKey): Promise<ProgramAccount<VaultDepositor>[]>;
19
+ /**
20
+ *
21
+ * @param vault pubkey
22
+ * @returns vault equity, in QUOTE_PRECISION
23
+ */
24
+ calculateVaultEquity(params: {
25
+ address?: PublicKey;
26
+ vault?: Vault;
27
+ }): Promise<BN>;
28
+ initializeVault(params: {
29
+ name: number[];
30
+ spotMarketIndex: number;
31
+ redeemPeriod: BN;
32
+ maxTokens: BN;
33
+ minDepositAmount: BN;
34
+ managementFee: BN;
35
+ profitShare: number;
36
+ hurdleRate: number;
37
+ permissioned: boolean;
38
+ }): Promise<TransactionSignature>;
39
+ /**
40
+ * Updates the delegate address for a vault. The delegate address will be allowed to trade
41
+ * on behalf of the vault.
42
+ * @param vault vault address to update
43
+ * @param delegate delegate address to update to
44
+ * @returns
45
+ */
46
+ updateDelegate(vault: PublicKey, delegate: PublicKey): Promise<TransactionSignature>;
47
+ /**
48
+ *
49
+ * @param vault vault address to deposit to
50
+ * @param amount amount to deposit
51
+ * @returns
52
+ */
53
+ managerDeposit(vault: PublicKey, amount: BN): Promise<TransactionSignature>;
54
+ managerRequestWithdraw(vault: PublicKey, amount: BN, withdrawUnit: WithdrawUnit): Promise<TransactionSignature>;
55
+ managerCancelWithdrawRequest(vault: PublicKey): Promise<TransactionSignature>;
56
+ managerWithdraw(vault: PublicKey): Promise<TransactionSignature>;
57
+ managerUpdateVault(vault: PublicKey, params: {
58
+ redeemPeriod: BN | null;
59
+ maxTokens: BN | null;
60
+ managementFee: BN | null;
61
+ minDepositAmount: BN | null;
62
+ profitShare: number | null;
63
+ hurdleRate: number | null;
64
+ permissioned: boolean | null;
65
+ }): Promise<TransactionSignature>;
66
+ getApplyProfitShareIx(vault: PublicKey, vaultDepositor: PublicKey): Promise<TransactionInstruction>;
67
+ private createInitVaultDepositorIx;
68
+ /**
69
+ * Initializes the vault depositor account. This account is used to deposit funds into a vault.
70
+ * @param vault the vault address to deposit into
71
+ * @param authority the authority allowed to make deposits into the vault
72
+ * @returns
73
+ */
74
+ initializeVaultDepositor(vault: PublicKey, authority?: PublicKey): Promise<TransactionSignature>;
75
+ /**
76
+ * Depositor funds into the specified vault.
77
+ * @param vaultDepositor
78
+ * @param amount
79
+ * @param initVaultDepositor If true, will initialize the vault depositor account
80
+ * @returns
81
+ */
82
+ deposit(vaultDepositor: PublicKey, amount: BN, initVaultDepositor?: {
83
+ authority: PublicKey;
84
+ vault: PublicKey;
85
+ }): Promise<TransactionSignature>;
86
+ requestWithdraw(vaultDepositor: PublicKey, amount: BN, withdrawUnit: WithdrawUnit): Promise<TransactionSignature>;
87
+ withdraw(vaultDepositor: PublicKey): Promise<TransactionSignature>;
88
+ cancelRequestWithdraw(vaultDepositor: PublicKey): Promise<TransactionSignature>;
89
+ /**
90
+ * Liquidates (become delegate for) a vault.
91
+ * @param
92
+ * @param
93
+ * @returns
94
+ */
95
+ liquidate(vaultDepositor: PublicKey): Promise<TransactionSignature>;
96
+ /**
97
+ * Used for UI wallet adapters compatibility
98
+ */
99
+ createAndSendTxn(ixs: TransactionInstruction[], computeUnitParams?: SetComputeUnitLimitParams): Promise<TransactionSignature>;
100
+ }