@voltr/vault-sdk 0.1.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.
- package/README.md +178 -0
- package/dist/client.d.ts +563 -0
- package/dist/client.js +729 -0
- package/dist/constants.d.ts +11 -0
- package/dist/constants.js +14 -0
- package/dist/idl/voltr_vault.json +3101 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +18 -0
- package/dist/types/vault.d.ts +24 -0
- package/dist/types/vault.js +2 -0
- package/dist/types/voltr_vault.d.ts +3107 -0
- package/dist/types/voltr_vault.js +2 -0
- package/package.json +40 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.VoltrClient = void 0;
|
|
37
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
38
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
39
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
40
|
+
const constants_1 = require("./constants");
|
|
41
|
+
// Import IDL files
|
|
42
|
+
const vaultIdl = __importStar(require("./idl/voltr_vault.json"));
|
|
43
|
+
class CustomWallet {
|
|
44
|
+
constructor(payer) {
|
|
45
|
+
this.payer = payer;
|
|
46
|
+
}
|
|
47
|
+
async signTransaction(tx) {
|
|
48
|
+
tx.partialSign(this.payer);
|
|
49
|
+
return tx;
|
|
50
|
+
}
|
|
51
|
+
async signAllTransactions(txs) {
|
|
52
|
+
return txs.map((t) => {
|
|
53
|
+
t.partialSign(this.payer);
|
|
54
|
+
return t;
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
get publicKey() {
|
|
58
|
+
return this.payer.publicKey;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
class AccountUtils {
|
|
62
|
+
constructor(conn) {
|
|
63
|
+
this.conn = conn;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Gets the balance of a Solana account
|
|
67
|
+
* @param publicKey - Public key to check balance for
|
|
68
|
+
* @returns Promise resolving to the account balance in lamports
|
|
69
|
+
* @throws {Error} If fetching balance fails
|
|
70
|
+
*/
|
|
71
|
+
async getBalance(publicKey) {
|
|
72
|
+
try {
|
|
73
|
+
return await this.conn.getBalance(publicKey);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new Error(`Failed to get account balance: ${error}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Main client for interacting with the Voltr protocol
|
|
82
|
+
*
|
|
83
|
+
* @remarks
|
|
84
|
+
* The VoltrClient provides methods for initializing and managing vaults,
|
|
85
|
+
* handling strategies, and performing deposits/withdrawals. It requires
|
|
86
|
+
* a Solana connection and optionally accepts a wallet for signing transactions.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* import { VoltrClient } from '@voltr/sdk';
|
|
91
|
+
* import { Connection } from '@solana/web3.js';
|
|
92
|
+
*
|
|
93
|
+
* const connection = new Connection('https://api.mainnet-beta.solana.com');
|
|
94
|
+
* const client = new VoltrClient(connection);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
class VoltrClient extends AccountUtils {
|
|
98
|
+
/**
|
|
99
|
+
* Creates a new VoltrClient instance
|
|
100
|
+
* @param conn - Solana connection instance
|
|
101
|
+
* @param wallet - Optional keypair for signing transactions
|
|
102
|
+
*/
|
|
103
|
+
constructor(conn, wallet) {
|
|
104
|
+
super(conn);
|
|
105
|
+
// Initialize programs
|
|
106
|
+
this.setProvider(wallet);
|
|
107
|
+
this.setPrograms(vaultIdl);
|
|
108
|
+
}
|
|
109
|
+
setProvider(wallet) {
|
|
110
|
+
/// we are creating instructions with this client without signing
|
|
111
|
+
let kp;
|
|
112
|
+
if (!wallet) {
|
|
113
|
+
const leakedKp = web3_js_1.Keypair.fromSecretKey(Uint8Array.from([
|
|
114
|
+
208, 175, 150, 242, 88, 34, 108, 88, 177, 16, 168, 75, 115, 181, 199,
|
|
115
|
+
242, 120, 4, 78, 75, 19, 227, 13, 215, 184, 108, 226, 53, 111, 149,
|
|
116
|
+
179, 84, 137, 121, 79, 1, 160, 223, 124, 241, 202, 203, 220, 237, 50,
|
|
117
|
+
242, 57, 158, 226, 207, 203, 188, 43, 28, 70, 110, 214, 234, 251, 15,
|
|
118
|
+
249, 157, 62, 80,
|
|
119
|
+
]));
|
|
120
|
+
kp = leakedKp;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
kp = wallet;
|
|
124
|
+
}
|
|
125
|
+
this.provider = new anchor_1.AnchorProvider(this.conn, new CustomWallet(kp), anchor_1.AnchorProvider.defaultOptions());
|
|
126
|
+
(0, anchor_1.setProvider)(this.provider);
|
|
127
|
+
}
|
|
128
|
+
setPrograms(vaultIdl) {
|
|
129
|
+
this.vaultProgram = new anchor_1.Program(vaultIdl, this.provider);
|
|
130
|
+
}
|
|
131
|
+
// --------------------------------------- Find PDA addresses
|
|
132
|
+
/**
|
|
133
|
+
* Finds the vault LP mint address for a given vault
|
|
134
|
+
* @param vault - Public key of the vault
|
|
135
|
+
* @returns The PDA for the vault's LP mint
|
|
136
|
+
*/
|
|
137
|
+
findVaultLpMint(vault) {
|
|
138
|
+
const [vaultLpMint] = web3_js_1.PublicKey.findProgramAddressSync([constants_1.SEEDS.VAULT_LP_MINT, vault.toBuffer()], this.vaultProgram.programId);
|
|
139
|
+
return vaultLpMint;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Finds the vault's asset idle authority address
|
|
143
|
+
* @param vault - Public key of the vault
|
|
144
|
+
* @returns The PDA for the vault's asset idle authority
|
|
145
|
+
*/
|
|
146
|
+
findVaultAssetIdleAuth(vault) {
|
|
147
|
+
const [vaultAssetIdleAuth] = web3_js_1.PublicKey.findProgramAddressSync([constants_1.SEEDS.VAULT_ASSET_IDLE_AUTH, vault.toBuffer()], this.vaultProgram.programId);
|
|
148
|
+
return vaultAssetIdleAuth;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Finds the vault's LP fee authority address
|
|
152
|
+
* @param vault - Public key of the vault
|
|
153
|
+
* @returns The PDA for the vault's LP fee authority
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const feeAuth = client.findVaultLpFeeAuth(vaultPubkey);
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
findVaultLpFeeAuth(vault) {
|
|
161
|
+
const [vaultLpFeeAuth] = web3_js_1.PublicKey.findProgramAddressSync([constants_1.SEEDS.VAULT_LP_FEE_AUTH, vault.toBuffer()], this.vaultProgram.programId);
|
|
162
|
+
return vaultLpFeeAuth;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Finds all vault-related addresses
|
|
166
|
+
* @param vault - Public key of the vault
|
|
167
|
+
* @returns Object containing all vault-related PDAs
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const addresses = client.findVaultAddresses(vaultPubkey);
|
|
172
|
+
* console.log(addresses.vaultLpMint.toBase58());
|
|
173
|
+
* console.log(addresses.vaultAssetIdleAuth.toBase58());
|
|
174
|
+
* console.log(addresses.vaultLpFeeAuth.toBase58());
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
findVaultAddresses(vault) {
|
|
178
|
+
const vaultLpMint = this.findVaultLpMint(vault);
|
|
179
|
+
const vaultAssetIdleAuth = this.findVaultAssetIdleAuth(vault);
|
|
180
|
+
const vaultLpFeeAuth = this.findVaultLpFeeAuth(vault);
|
|
181
|
+
return {
|
|
182
|
+
vaultLpMint,
|
|
183
|
+
vaultAssetIdleAuth,
|
|
184
|
+
vaultLpFeeAuth,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Finds the vault strategy auth address
|
|
189
|
+
* @param vault - Public key of the vault
|
|
190
|
+
* @param strategy - Public key of the strategy
|
|
191
|
+
* @returns The PDA for the vault strategy auth
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const vaultStrategyAuth = client.findVaultStrategyAuth(vaultPubkey, strategyPubkey);
|
|
196
|
+
* ```
|
|
197
|
+
*/
|
|
198
|
+
findVaultStrategyAuth(vault, strategy) {
|
|
199
|
+
const [vaultStrategyAuth] = web3_js_1.PublicKey.findProgramAddressSync([constants_1.SEEDS.VAULT_STRATEGY_AUTH, vault.toBuffer(), strategy.toBuffer()], this.vaultProgram.programId);
|
|
200
|
+
return vaultStrategyAuth;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Finds the strategy init receipt address
|
|
204
|
+
* @param vault - Public key of the vault
|
|
205
|
+
* @param strategy - Public key of the strategy
|
|
206
|
+
* @returns The PDA for the strategy init receipt
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* const strategyInitReceipt = client.findStrategyInitReceipt(vaultPubkey, strategyPubkey);
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
findStrategyInitReceipt(vault, strategy) {
|
|
214
|
+
const [strategyInitReceipt] = web3_js_1.PublicKey.findProgramAddressSync([constants_1.SEEDS.STRATEGY_INIT_RECEIPT, vault.toBuffer(), strategy.toBuffer()], this.vaultProgram.programId);
|
|
215
|
+
return strategyInitReceipt;
|
|
216
|
+
}
|
|
217
|
+
// --------------------------------------- Vault Instructions
|
|
218
|
+
/**
|
|
219
|
+
* Creates an instruction to initialize a new vault
|
|
220
|
+
*
|
|
221
|
+
* @param {VaultParams} vaultParams - Configuration parameters for the vault
|
|
222
|
+
* @param {VaultConfig} vaultParams.config - Vault configuration settings
|
|
223
|
+
* @param {BN} vaultParams.config.maxCap - Maximum capacity of the vault
|
|
224
|
+
* @param {BN} vaultParams.config.startAtTs - Vault start timestamp in seconds
|
|
225
|
+
* @param {number} vaultParams.config.managerManagementFee - Manager's management fee in basis points (e.g., 50 = 0.5%)
|
|
226
|
+
* @param {number} vaultParams.config.managerPerformanceFee - Manager's performance fee in basis points (e.g., 1000 = 10%)
|
|
227
|
+
* @param {number} vaultParams.config.adminManagementFee - Admin's management fee in basis points (e.g., 50 = 0.5%)
|
|
228
|
+
* @param {number} vaultParams.config.adminPerformanceFee - Admin's performance fee in basis points (e.g., 1000 = 10%)
|
|
229
|
+
* @param {string} vaultParams.name - Name of the vault
|
|
230
|
+
* @param {string} vaultParams.description - Description of the vault
|
|
231
|
+
* @param {Object} params - Additional parameters for initializing the vault
|
|
232
|
+
* @param {Keypair} params.vault - Keypair for the new vault
|
|
233
|
+
* @param {PublicKey} params.vaultAssetMint - Public key of the vault's asset mint
|
|
234
|
+
* @param {PublicKey} params.admin - Public key of the vault admin
|
|
235
|
+
* @param {PublicKey} params.manager - Public key of the vault manager
|
|
236
|
+
* @param {PublicKey} params.payer - Public key of the fee payer
|
|
237
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for initializing the vault
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* const ix = await client.createInitializeVaultIx(
|
|
242
|
+
* {
|
|
243
|
+
* config: {
|
|
244
|
+
* maxCap: new BN('1000000000'),
|
|
245
|
+
* startAtTs: new BN(Math.floor(Date.now() / 1000)),
|
|
246
|
+
* managerManagementFee: 50, // 0.5%
|
|
247
|
+
* managerPerformanceFee: 1000, // 10%
|
|
248
|
+
* adminManagementFee: 50, // 0.5%
|
|
249
|
+
* adminPerformanceFee: 1000, // 10%
|
|
250
|
+
* },
|
|
251
|
+
* name: "My Vault",
|
|
252
|
+
* description: "Example vault"
|
|
253
|
+
* },
|
|
254
|
+
* {
|
|
255
|
+
* vault: vaultKeypair,
|
|
256
|
+
* vaultAssetMint: new PublicKey('...'),
|
|
257
|
+
* admin: adminPubkey,
|
|
258
|
+
* manager: managerPubkey,
|
|
259
|
+
* payer: payerPubkey
|
|
260
|
+
* }
|
|
261
|
+
* );
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
async createInitializeVaultIx(vaultParams, { vault, vaultAssetMint, admin, manager, payer, }) {
|
|
265
|
+
const addresses = this.findVaultAddresses(vault.publicKey);
|
|
266
|
+
const vaultAssetIdleAta = (0, spl_token_1.getAssociatedTokenAddressSync)(vaultAssetMint, addresses.vaultAssetIdleAuth, true);
|
|
267
|
+
const vaultLpFeeAta = (0, spl_token_1.getAssociatedTokenAddressSync)(addresses.vaultLpMint, addresses.vaultLpFeeAuth, true);
|
|
268
|
+
return await this.vaultProgram.methods
|
|
269
|
+
.initialize(vaultParams.config, vaultParams.name, vaultParams.description)
|
|
270
|
+
.accounts({
|
|
271
|
+
payer,
|
|
272
|
+
admin,
|
|
273
|
+
manager,
|
|
274
|
+
vault: vault.publicKey,
|
|
275
|
+
vaultAssetMint,
|
|
276
|
+
vaultAssetIdleAta,
|
|
277
|
+
vaultLpFeeAta,
|
|
278
|
+
assetTokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
279
|
+
})
|
|
280
|
+
.instruction();
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Creates a deposit instruction
|
|
284
|
+
*
|
|
285
|
+
* @param {BN} amount - Amount of tokens to deposit
|
|
286
|
+
* @param {Object} params - Deposit parameters
|
|
287
|
+
* @param {PublicKey} params.userAuthority - Public key of the user's transfer authority
|
|
288
|
+
* @param {PublicKey} params.vault - Public key of the vault
|
|
289
|
+
* @param {PublicKey} params.vaultAssetMint - Public key of the vault asset mint
|
|
290
|
+
* @param {PublicKey} params.assetTokenProgram - Public key of the asset token program
|
|
291
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for depositing tokens
|
|
292
|
+
* @throws {Error} If instruction creation fails
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* const ix = await client.createDepositIx(
|
|
297
|
+
* new BN('1000000000'),
|
|
298
|
+
* {
|
|
299
|
+
* userAuthority: userPubkey,
|
|
300
|
+
* vault: vaultPubkey,
|
|
301
|
+
* vaultAssetMint: mintPubkey,
|
|
302
|
+
* assetTokenProgram: tokenProgramPubkey
|
|
303
|
+
* }
|
|
304
|
+
* );
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
async createDepositIx(amount, { userAuthority, vault, vaultAssetMint, assetTokenProgram, }) {
|
|
308
|
+
return await this.vaultProgram.methods
|
|
309
|
+
.deposit(amount)
|
|
310
|
+
.accounts({
|
|
311
|
+
userTransferAuthority: userAuthority,
|
|
312
|
+
vault,
|
|
313
|
+
vaultAssetMint,
|
|
314
|
+
assetTokenProgram,
|
|
315
|
+
})
|
|
316
|
+
.instruction();
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Creates a withdraw instruction
|
|
320
|
+
*
|
|
321
|
+
* @param amount - Amount of LP tokens to withdraw
|
|
322
|
+
* @param {Object} params - Withdraw parameters
|
|
323
|
+
* @param {PublicKey} params.userAuthority - Public key of the user authority
|
|
324
|
+
* @param {PublicKey} params.vault - Public key of the vault
|
|
325
|
+
* @param {PublicKey} params.vaultAssetMint - Public key of the vault asset mint
|
|
326
|
+
* @param {PublicKey} params.assetTokenProgram - Public key of the asset token program
|
|
327
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for withdrawal
|
|
328
|
+
*
|
|
329
|
+
* @throws {Error} If the instruction creation fails
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* const ix = await client.createWithdrawIx(
|
|
333
|
+
* new BN('1000000000'),
|
|
334
|
+
* {
|
|
335
|
+
* userAuthority: userPubkey,
|
|
336
|
+
* vault: vaultPubkey,
|
|
337
|
+
* vaultAssetMint: mintPubkey,
|
|
338
|
+
* assetTokenProgram: tokenProgramPubkey
|
|
339
|
+
* }
|
|
340
|
+
* );
|
|
341
|
+
*/
|
|
342
|
+
async createWithdrawIx(amount, { userAuthority, vault, vaultAssetMint, assetTokenProgram, }) {
|
|
343
|
+
return await this.vaultProgram.methods
|
|
344
|
+
.withdraw(amount)
|
|
345
|
+
.accounts({
|
|
346
|
+
userTransferAuthority: userAuthority,
|
|
347
|
+
vault,
|
|
348
|
+
vaultAssetMint,
|
|
349
|
+
assetTokenProgram,
|
|
350
|
+
})
|
|
351
|
+
.instruction();
|
|
352
|
+
}
|
|
353
|
+
// --------------------------------------- Strategy Instructions
|
|
354
|
+
/**
|
|
355
|
+
* Creates an instruction to add an adaptor to a vault
|
|
356
|
+
* @param {Object} params - Parameters for adding adaptor to vault
|
|
357
|
+
* @param {PublicKey} params.vault - Public key of the vault
|
|
358
|
+
* @param {PublicKey} params.payer - Public key of the payer
|
|
359
|
+
* @param {PublicKey} params.adaptorProgram - Public key of the adaptor program
|
|
360
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for adding adaptor to vault
|
|
361
|
+
*
|
|
362
|
+
* @throws {Error} If the instruction creation fails
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```typescript
|
|
366
|
+
* const ix = await client.createAddAdaptorIx({
|
|
367
|
+
* vault: vaultPubkey,
|
|
368
|
+
* payer: payerPubkey,
|
|
369
|
+
* adaptorProgram: adaptorProgramPubkey
|
|
370
|
+
* });
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
async createAddAdaptorIx({ vault, payer, adaptorProgram = constants_1.DEFAULT_ADAPTOR_PROGRAM_ID, }) {
|
|
374
|
+
return await this.vaultProgram.methods
|
|
375
|
+
.addAdaptor()
|
|
376
|
+
.accounts({
|
|
377
|
+
payer,
|
|
378
|
+
vault,
|
|
379
|
+
adaptorProgram,
|
|
380
|
+
})
|
|
381
|
+
.instruction();
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Creates an instruction to initialize a strategy to a vault
|
|
385
|
+
* @param {InitializeStrategyArgs} initArgs - Arguments for strategy initialization
|
|
386
|
+
* @param {Buffer | null} [initArgs.instructionDiscriminator] - Optional discriminator for the instruction
|
|
387
|
+
* @param {Buffer | null} [initArgs.additionalArgs] - Optional additional arguments for the instruction
|
|
388
|
+
* @param {Object} params - Parameters for initializing strategy to vault
|
|
389
|
+
* @param {PublicKey} params.payer - Public key of the payer
|
|
390
|
+
* @param {PublicKey} params.vault - Public key of the vault
|
|
391
|
+
* @param {PublicKey} params.manager - Public key of the manager
|
|
392
|
+
* @param {PublicKey} params.strategy - Public key of the strategy
|
|
393
|
+
* @param {PublicKey} params.adaptorProgram - Public key of the adaptor program
|
|
394
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for initializing strategy to vault
|
|
395
|
+
* @throws {Error} If the instruction creation fails
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* ```typescript
|
|
399
|
+
* const ix = await client.createInitializeStrategyIx(
|
|
400
|
+
* {
|
|
401
|
+
* instructionDiscriminator: Buffer.from('...'), // optional
|
|
402
|
+
* additionalArgs: Buffer.from('...') // optional
|
|
403
|
+
* },
|
|
404
|
+
* {
|
|
405
|
+
* payer: payerPubkey,
|
|
406
|
+
* vault: vaultPubkey,
|
|
407
|
+
* manager: managerPubkey,
|
|
408
|
+
* strategy: strategyPubkey,
|
|
409
|
+
* adaptorProgram: adaptorProgramPubkey
|
|
410
|
+
* }
|
|
411
|
+
* );
|
|
412
|
+
* ```
|
|
413
|
+
*/
|
|
414
|
+
async createInitializeStrategyIx({ instructionDiscriminator = null, additionalArgs = null, }, { payer, vault, manager, strategy, adaptorProgram = constants_1.DEFAULT_ADAPTOR_PROGRAM_ID, }) {
|
|
415
|
+
return await this.vaultProgram.methods
|
|
416
|
+
.initializeStrategy(instructionDiscriminator ?? null, additionalArgs ?? null)
|
|
417
|
+
.accounts({
|
|
418
|
+
payer,
|
|
419
|
+
vault,
|
|
420
|
+
manager,
|
|
421
|
+
strategy,
|
|
422
|
+
adaptorProgram,
|
|
423
|
+
})
|
|
424
|
+
.instruction();
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Creates an instruction to deposit assets into a strategy
|
|
428
|
+
*
|
|
429
|
+
* @param {Object} depositArgs - Deposit arguments
|
|
430
|
+
* @param {BN} depositArgs.depositAmount - Amount of assets to deposit
|
|
431
|
+
* @param {Buffer | null} [depositArgs.instructionDiscriminator] - Optional discriminator for the instruction
|
|
432
|
+
* @param {Buffer | null} [depositArgs.additionalArgs] - Optional additional arguments for the instruction
|
|
433
|
+
* @param {Object} params - Strategy deposit parameters
|
|
434
|
+
* @param {PublicKey} params.vault - Public key of the vault
|
|
435
|
+
* @param {PublicKey} params.vaultAssetMint - Public key of the vault asset mint
|
|
436
|
+
* @param {PublicKey} params.strategy - Public key of the strategy
|
|
437
|
+
* @param {PublicKey} params.assetTokenProgram - Public key of the asset token program
|
|
438
|
+
* @param {PublicKey} params.adaptorProgram - Public key of the adaptor program
|
|
439
|
+
* @param {Array<{ pubkey: PublicKey, isSigner: boolean, isWritable: boolean }>} params.remainingAccounts - Remaining accounts for the instruction
|
|
440
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for depositing assets into strategy
|
|
441
|
+
* @throws {Error} If the instruction creation fails
|
|
442
|
+
*
|
|
443
|
+
* @example
|
|
444
|
+
* ```typescript
|
|
445
|
+
* const ix = await client.createDepositStrategyIx(
|
|
446
|
+
* {
|
|
447
|
+
* depositAmount: new BN('1000000000'),
|
|
448
|
+
* instructionDiscriminator: Buffer.from('...'),
|
|
449
|
+
* additionalArgs: Buffer.from('...')
|
|
450
|
+
* },
|
|
451
|
+
* {
|
|
452
|
+
* vault: vaultPubkey,
|
|
453
|
+
* vaultAssetMint: mintPubkey,
|
|
454
|
+
* strategy: strategyPubkey,
|
|
455
|
+
* assetTokenProgram: tokenProgramPubkey,
|
|
456
|
+
* adaptorProgram: adaptorProgramPubkey,
|
|
457
|
+
* remainingAccounts: []
|
|
458
|
+
* }
|
|
459
|
+
* );
|
|
460
|
+
* ```
|
|
461
|
+
*/
|
|
462
|
+
async createDepositStrategyIx({ depositAmount, instructionDiscriminator = null, additionalArgs = null, }, { vault, vaultAssetMint, strategy, assetTokenProgram, adaptorProgram = constants_1.DEFAULT_ADAPTOR_PROGRAM_ID, remainingAccounts, }) {
|
|
463
|
+
return await this.vaultProgram.methods
|
|
464
|
+
.depositStrategy(depositAmount, instructionDiscriminator, additionalArgs)
|
|
465
|
+
.accounts({
|
|
466
|
+
vault,
|
|
467
|
+
vaultAssetMint,
|
|
468
|
+
adaptorProgram,
|
|
469
|
+
strategy,
|
|
470
|
+
assetTokenProgram,
|
|
471
|
+
})
|
|
472
|
+
.remainingAccounts(remainingAccounts)
|
|
473
|
+
.instruction();
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Creates an instruction to withdraw assets from a strategy
|
|
477
|
+
*
|
|
478
|
+
* @param {Object} withdrawArgs - Withdrawal arguments
|
|
479
|
+
* @param {BN} withdrawArgs.withdrawAmount - Amount of assets to withdraw
|
|
480
|
+
* @param {Buffer | null} [withdrawArgs.instructionDiscriminator] - Optional discriminator for the instruction
|
|
481
|
+
* @param {Buffer | null} [withdrawArgs.additionalArgs] - Optional additional arguments for the instruction
|
|
482
|
+
* @param {Object} params - Strategy withdrawal parameters
|
|
483
|
+
* @param {PublicKey} params.vault - Public key of the vault
|
|
484
|
+
* @param {PublicKey} params.vaultAssetMint - Public key of the vault asset mint
|
|
485
|
+
* @param {PublicKey} params.strategy - Public key of the strategy
|
|
486
|
+
* @param {PublicKey} params.assetTokenProgram - Public key of the asset token program
|
|
487
|
+
* @param {PublicKey} params.adaptorProgram - Public key of the adaptor program
|
|
488
|
+
* @param {Array<{ pubkey: PublicKey, isSigner: boolean, isWritable: boolean }>} params.remainingAccounts - Remaining accounts for the instruction
|
|
489
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for withdrawing assets from strategy
|
|
490
|
+
* @throws {Error} If the instruction creation fails
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* ```typescript
|
|
494
|
+
* const ix = await client.createWithdrawStrategyIx(
|
|
495
|
+
* {
|
|
496
|
+
* withdrawAmount: new BN('1000000000'),
|
|
497
|
+
* instructionDiscriminator: Buffer.from('...'),
|
|
498
|
+
* additionalArgs: Buffer.from('...')
|
|
499
|
+
* },
|
|
500
|
+
* {
|
|
501
|
+
* vault: vaultPubkey,
|
|
502
|
+
* vaultAssetMint: mintPubkey,
|
|
503
|
+
* strategy: strategyPubkey,
|
|
504
|
+
* assetTokenProgram: tokenProgramPubkey,
|
|
505
|
+
* adaptorProgram: adaptorProgramPubkey,
|
|
506
|
+
* remainingAccounts: []
|
|
507
|
+
* }
|
|
508
|
+
* );
|
|
509
|
+
* ```
|
|
510
|
+
*/
|
|
511
|
+
async createWithdrawStrategyIx({ withdrawAmount, instructionDiscriminator = null, additionalArgs = null, }, { vault, vaultAssetMint, strategy, assetTokenProgram, adaptorProgram = constants_1.DEFAULT_ADAPTOR_PROGRAM_ID, remainingAccounts, }) {
|
|
512
|
+
return await this.vaultProgram.methods
|
|
513
|
+
.withdrawStrategy(withdrawAmount, instructionDiscriminator, additionalArgs)
|
|
514
|
+
.accounts({
|
|
515
|
+
vault,
|
|
516
|
+
vaultAssetMint,
|
|
517
|
+
adaptorProgram,
|
|
518
|
+
strategy,
|
|
519
|
+
assetTokenProgram,
|
|
520
|
+
})
|
|
521
|
+
.remainingAccounts(remainingAccounts)
|
|
522
|
+
.instruction();
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Creates an instruction to remove a strategy from a vault
|
|
526
|
+
* @param {Object} params - Parameters for removing strategy
|
|
527
|
+
* @param {PublicKey} params.vault - Public key of the vault
|
|
528
|
+
* @param {PublicKey} params.adaptorProgram - Public key of the adaptor program
|
|
529
|
+
* @returns {Promise<TransactionInstruction>} Transaction instruction for removing strategy from vault
|
|
530
|
+
* @throws {Error} If instruction creation fails
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* ```typescript
|
|
534
|
+
* const ix = await client.createRemoveStrategyIx({
|
|
535
|
+
* vault: vaultPubkey,
|
|
536
|
+
* adaptorProgram: adaptorProgramPubkey
|
|
537
|
+
* });
|
|
538
|
+
* ```
|
|
539
|
+
*/
|
|
540
|
+
async createRemoveStrategyIx({ vault, adaptorProgram = constants_1.DEFAULT_ADAPTOR_PROGRAM_ID, }) {
|
|
541
|
+
return await this.vaultProgram.methods
|
|
542
|
+
.removeAdaptor()
|
|
543
|
+
.accounts({
|
|
544
|
+
vault,
|
|
545
|
+
adaptorProgram,
|
|
546
|
+
})
|
|
547
|
+
.instruction();
|
|
548
|
+
}
|
|
549
|
+
// --------------------------------------- Account Fetching All
|
|
550
|
+
/**
|
|
551
|
+
* Fetches all strategy init receipt accounts
|
|
552
|
+
* @returns Promise resolving to an array of strategy init receipt accounts
|
|
553
|
+
*
|
|
554
|
+
* @example
|
|
555
|
+
* ```typescript
|
|
556
|
+
* const strategyInitReceiptAccounts = await client.fetchAllStrategyInitReceiptAccounts();
|
|
557
|
+
* ```
|
|
558
|
+
*/
|
|
559
|
+
async fetchAllStrategyInitReceiptAccounts() {
|
|
560
|
+
return await this.vaultProgram.account.strategyInitReceipt.all();
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Fetches all strategy init receipt accounts of a vault
|
|
564
|
+
* @param vault - Public key of the vault
|
|
565
|
+
* @returns Promise resolving to an array of strategy init receipt accounts
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* ```typescript
|
|
569
|
+
* const strategyInitReceiptAccounts = await client.fetchAllStrategyInitReceiptAccountsOfVault(vaultPubkey);
|
|
570
|
+
* ```
|
|
571
|
+
*/
|
|
572
|
+
async fetchAllStrategyInitReceiptAccountsOfVault(vault) {
|
|
573
|
+
return await this.vaultProgram.account.strategyInitReceipt.all([
|
|
574
|
+
{
|
|
575
|
+
memcmp: {
|
|
576
|
+
offset: 8, // 8 for discriminator
|
|
577
|
+
bytes: vault.toBase58(),
|
|
578
|
+
},
|
|
579
|
+
},
|
|
580
|
+
]);
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Fetches all adaptor add receipt accounts of a vault
|
|
584
|
+
* @param vault - Public key of the vault
|
|
585
|
+
* @returns Promise resolving to an array of adaptor add receipt accounts
|
|
586
|
+
*
|
|
587
|
+
* @example
|
|
588
|
+
* ```typescript
|
|
589
|
+
* const adaptorAddReceiptAccounts = await client.fetchAllAdaptorAddReceiptAccountsOfVault(vaultPubkey);
|
|
590
|
+
* ```
|
|
591
|
+
*/
|
|
592
|
+
async fetchAllAdaptorAddReceiptAccountsOfVault(vault) {
|
|
593
|
+
return await this.vaultProgram.account.adaptorAddReceipt.all([
|
|
594
|
+
{
|
|
595
|
+
memcmp: {
|
|
596
|
+
offset: 8, // 8 for discriminator
|
|
597
|
+
bytes: vault.toBase58(),
|
|
598
|
+
},
|
|
599
|
+
},
|
|
600
|
+
]);
|
|
601
|
+
}
|
|
602
|
+
async getPositionAndTotalValuesForVault(vault) {
|
|
603
|
+
const vaultAccount = await this.fetchVaultAccount(vault);
|
|
604
|
+
const totalAssetValue = vaultAccount.asset.totalValue;
|
|
605
|
+
const strategyInitReceiptAccounts = await this.fetchAllStrategyInitReceiptAccountsOfVault(vault);
|
|
606
|
+
const strategyInfo = strategyInitReceiptAccounts.map((vaultStrategyAccount) => ({
|
|
607
|
+
strategyId: vaultStrategyAccount.account.strategy.toBase58(),
|
|
608
|
+
amount: vaultStrategyAccount.account.positionValue.toNumber(),
|
|
609
|
+
}));
|
|
610
|
+
return {
|
|
611
|
+
totalValue: totalAssetValue.toNumber(),
|
|
612
|
+
strategies: strategyInfo,
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
// --------------------------------------- Account Fetching
|
|
616
|
+
/**
|
|
617
|
+
* Fetches a vault account's data
|
|
618
|
+
* @param vault - Public key of the vault
|
|
619
|
+
* @returns Promise resolving to the vault account data
|
|
620
|
+
*/
|
|
621
|
+
async fetchVaultAccount(vault) {
|
|
622
|
+
return await this.vaultProgram.account.vault.fetch(vault);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Fetches a strategy init receipt account's data
|
|
626
|
+
* @param strategyInitReceipt - Public key of the strategy init receipt account
|
|
627
|
+
* @returns Promise resolving to the strategy init receipt account data
|
|
628
|
+
*
|
|
629
|
+
* @example
|
|
630
|
+
* ```typescript
|
|
631
|
+
* const strategyInitReceiptAccount = await client.fetchStrategyInitReceiptAccount(strategyInitReceiptPubkey);
|
|
632
|
+
* ```
|
|
633
|
+
*/
|
|
634
|
+
async fetchStrategyInitReceiptAccount(strategyInitReceipt) {
|
|
635
|
+
return await this.vaultProgram.account.strategyInitReceipt.fetch(strategyInitReceipt);
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* Fetches an adaptor add receipt account's data
|
|
639
|
+
* @param adaptorAddReceipt - Public key of the adaptor add receipt account
|
|
640
|
+
* @returns Promise resolving to the adaptor add receipt account data
|
|
641
|
+
*
|
|
642
|
+
* @example
|
|
643
|
+
* ```typescript
|
|
644
|
+
* const adaptorAddReceiptAccount = await client.fetchAdaptorAddReceiptAccount(adaptorAddReceiptPubkey);
|
|
645
|
+
* ```
|
|
646
|
+
*/
|
|
647
|
+
async fetchAdaptorAddReceiptAccount(adaptorAddReceipt) {
|
|
648
|
+
return await this.vaultProgram.account.adaptorAddReceipt.fetch(adaptorAddReceipt);
|
|
649
|
+
}
|
|
650
|
+
// --------------------------------------- Helpers
|
|
651
|
+
/**
|
|
652
|
+
* Calculates the amount of assets that would be received for a given LP token amount
|
|
653
|
+
*
|
|
654
|
+
* @param vaultPk - Public key of the vault
|
|
655
|
+
* @param lpAmount - Amount of LP tokens to calculate for
|
|
656
|
+
* @returns Promise resolving to the amount of assets that would be received
|
|
657
|
+
*
|
|
658
|
+
* @throws {Error} If LP supply or total assets are invalid
|
|
659
|
+
* @throws {Error} If math overflow occurs during calculation
|
|
660
|
+
*
|
|
661
|
+
* @example
|
|
662
|
+
* ```typescript
|
|
663
|
+
* const assetsToReceive = await client.calculateAssetsForWithdraw(
|
|
664
|
+
* vaultPubkey,
|
|
665
|
+
* new BN('1000000000')
|
|
666
|
+
* );
|
|
667
|
+
* ```
|
|
668
|
+
*/
|
|
669
|
+
async calculateAssetsForWithdraw(vaultPk, lpAmount) {
|
|
670
|
+
const vault = await this.fetchVaultAccount(vaultPk);
|
|
671
|
+
const totalValue = vault.asset.totalValue;
|
|
672
|
+
const lpMint = this.findVaultLpMint(vaultPk);
|
|
673
|
+
const lp = await (0, spl_token_1.getMint)(this.conn, lpMint);
|
|
674
|
+
const lpSupply = new anchor_1.BN(lp.supply.toString());
|
|
675
|
+
// Validate inputs
|
|
676
|
+
if (lpSupply <= new anchor_1.BN(0))
|
|
677
|
+
throw new Error("Invalid LP supply");
|
|
678
|
+
if (totalValue <= new anchor_1.BN(0))
|
|
679
|
+
throw new Error("Invalid total assets");
|
|
680
|
+
// Calculate: (lpAmount * totalValue) / totalLpSupply
|
|
681
|
+
try {
|
|
682
|
+
return lpAmount.mul(totalValue).div(lpSupply);
|
|
683
|
+
}
|
|
684
|
+
catch (e) {
|
|
685
|
+
throw new Error("Math overflow in asset calculation");
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Calculates the amount of LP tokens that would be received for a given asset deposit
|
|
690
|
+
*
|
|
691
|
+
* @param depositAmount - Amount of assets to deposit
|
|
692
|
+
* @param vaultPk - Public key of the vault
|
|
693
|
+
* @returns Promise resolving to the amount of LP tokens that would be received
|
|
694
|
+
*
|
|
695
|
+
* @throws {Error} If math overflow occurs during calculation
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
698
|
+
* ```typescript
|
|
699
|
+
* const lpTokens = await client.calculateLpTokensForDeposit(
|
|
700
|
+
* new BN('1000000000'),
|
|
701
|
+
* vaultPubkey
|
|
702
|
+
* );
|
|
703
|
+
* ```
|
|
704
|
+
*/
|
|
705
|
+
async calculateLpTokensForDeposit(depositAmount, vaultPk) {
|
|
706
|
+
const vault = await this.fetchVaultAccount(vaultPk);
|
|
707
|
+
const totalValue = vault.asset.totalValue;
|
|
708
|
+
const lpMint = this.findVaultLpMint(vaultPk);
|
|
709
|
+
const lp = await (0, spl_token_1.getMint)(this.conn, lpMint);
|
|
710
|
+
const lpSupply = new anchor_1.BN(lp.supply.toString());
|
|
711
|
+
// If the pool is empty, mint LP tokens 1:1 with deposit
|
|
712
|
+
if (totalValue <= new anchor_1.BN(0) && lpSupply <= new anchor_1.BN(0)) {
|
|
713
|
+
const assetMint = await (0, spl_token_1.getMint)(this.conn, vault.asset.mint);
|
|
714
|
+
const assetDecimals = assetMint.decimals;
|
|
715
|
+
const lpDecimals = lp.decimals;
|
|
716
|
+
return depositAmount
|
|
717
|
+
.mul(new anchor_1.BN(10 ** lpDecimals))
|
|
718
|
+
.div(new anchor_1.BN(10 ** assetDecimals));
|
|
719
|
+
}
|
|
720
|
+
// Calculate: (depositAmount * totalLpSupply) / totalValue
|
|
721
|
+
try {
|
|
722
|
+
return depositAmount.mul(lpSupply).div(totalValue);
|
|
723
|
+
}
|
|
724
|
+
catch (e) {
|
|
725
|
+
throw new Error("Math overflow in LP token calculation");
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
exports.VoltrClient = VoltrClient;
|