@lavarage/sdk 6.7.5 → 6.8.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.
package/evm.ts CHANGED
@@ -155,7 +155,7 @@ export async function getPositionsEvm(
155
155
  trader: loan.borrower,
156
156
  tokenCollateral: loan.collateral.collateralAddress,
157
157
  loanId: loan.id,
158
- openingPositionSize: loan.amount,
158
+ openingPositionSize: loan.amount + loan.userPaid,
159
159
  collateralAmount: loan.collateralAmount,
160
160
  initialMargin: loan.userPaid,
161
161
  transactionHash: "", // Not available from loan data
package/index.ts CHANGED
@@ -27,6 +27,9 @@ import {
27
27
  TokenInvalidAccountOwnerError,
28
28
  } from "@solana/spl-token";
29
29
 
30
+ export * from "./evm";
31
+ export * as lending from "./lending";
32
+
30
33
  export function getPda(seed: Buffer | Buffer[], programId: PublicKey) {
31
34
  const seedsBuffer = Array.isArray(seed) ? seed : [seed];
32
35
 
@@ -1646,5 +1649,3 @@ export const mergePositionV2 = async (
1646
1649
 
1647
1650
  return tx;
1648
1651
  };
1649
-
1650
- export * from "./evm";
@@ -0,0 +1,9 @@
1
+ import { Keypair } from "@solana/web3.js";
2
+
3
+ import { PublicKey } from "@solana/web3.js";
4
+
5
+ export interface TradingPoolPDA {
6
+ tradingPool: PublicKey;
7
+ poolOwnerPublicKey: PublicKey;
8
+ tokenPublicKey: PublicKey;
9
+ }
package/lending.ts ADDED
@@ -0,0 +1,456 @@
1
+ import { BN, Instruction, Program } from "@coral-xyz/anchor";
2
+ import { Lavarage } from "./idl/lavarage";
3
+ import { Lavarage as LavarageV2 } from "./idl/lavaragev2";
4
+ import {
5
+ ComputeBudgetProgram,
6
+ Keypair,
7
+ PublicKey,
8
+ SystemProgram,
9
+ TransactionInstruction,
10
+ TransactionMessage,
11
+ VersionedTransaction,
12
+ } from "@solana/web3.js";
13
+ import {
14
+ TOKEN_PROGRAM_ID,
15
+ createTransferCheckedInstruction,
16
+ getAssociatedTokenAddressSync,
17
+ getMint,
18
+ } from "@solana/spl-token";
19
+ import { getPda } from "./index";
20
+
21
+ export function getNodeWalletPDA(
22
+ operatorPublicKey: PublicKey,
23
+ mintPublicKey: PublicKey,
24
+ programId: PublicKey
25
+ ): PublicKey {
26
+ return getPda(
27
+ [
28
+ Buffer.from("node_wallet"),
29
+ operatorPublicKey.toBuffer(),
30
+ mintPublicKey.toBuffer(),
31
+ ],
32
+ programId
33
+ );
34
+ }
35
+
36
+ export function getTradingPoolPDA(
37
+ poolOwnerPublicKey: PublicKey,
38
+ tokenPublicKey: PublicKey,
39
+ programId: PublicKey
40
+ ): PublicKey {
41
+ return getPda(
42
+ [
43
+ Buffer.from("trading_pool"),
44
+ poolOwnerPublicKey.toBuffer(),
45
+ tokenPublicKey.toBuffer(),
46
+ ],
47
+ programId
48
+ );
49
+ }
50
+
51
+ const computeFeeIx = ComputeBudgetProgram.setComputeUnitPrice({
52
+ microLamports: 150000,
53
+ });
54
+
55
+ async function createNodeWallet(
56
+ lavarageProgram: Program<Lavarage> | Program<LavarageV2>,
57
+ params: {
58
+ operator: PublicKey;
59
+ mint?: string; // Required for V2, optional for V1
60
+ liquidationLtv?: number; // Required for V2, optional for V1
61
+ }
62
+ ): Promise<{
63
+ instruction: TransactionInstruction;
64
+ nodeWallet: Keypair | undefined;
65
+ }> {
66
+ const { blockhash } =
67
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
68
+
69
+ let instruction, nodeWallet;
70
+
71
+ // Check if this is V2 program (has liquidationLtv parameter)
72
+ if (params.liquidationLtv !== undefined && params.mint !== undefined) {
73
+ nodeWallet = getNodeWalletPDA(
74
+ new PublicKey(params.operator),
75
+ new PublicKey(params.mint),
76
+ lavarageProgram.programId
77
+ );
78
+ // V2 version
79
+ instruction = await (lavarageProgram as Program<LavarageV2>).methods
80
+ .lpOperatorCreateNodeWallet(new BN(params.liquidationLtv))
81
+ .accounts({
82
+ nodeWallet: nodeWallet,
83
+ operator: new PublicKey(params.operator),
84
+ systemProgram: SystemProgram.programId,
85
+ mint: new PublicKey(params.mint),
86
+ })
87
+ .instruction();
88
+ } else {
89
+ nodeWallet = Keypair.generate();
90
+ // V1 version
91
+ instruction = await (lavarageProgram as Program<Lavarage>).methods
92
+ .lpOperatorCreateNodeWallet()
93
+ .accounts({
94
+ nodeWallet: nodeWallet.publicKey,
95
+ operator: new PublicKey(params.operator),
96
+ systemProgram: SystemProgram.programId,
97
+ })
98
+ .instruction();
99
+ }
100
+
101
+ return {
102
+ instruction,
103
+ nodeWallet: nodeWallet instanceof Keypair ? nodeWallet : undefined,
104
+ };
105
+ }
106
+
107
+ export async function depositFunds(
108
+ lavarageProgram: Program<Lavarage>,
109
+ params: {
110
+ nodeWallet: PublicKey;
111
+ mint?: string; // Required for V2, optional for V1
112
+ funder: PublicKey;
113
+ amount: number;
114
+ }
115
+ ): Promise<VersionedTransaction> {
116
+ const { blockhash } =
117
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
118
+
119
+ let instruction;
120
+ if (params.mint === undefined) {
121
+ instruction = await lavarageProgram.methods
122
+ .lpOperatorFundNodeWallet(new BN(params.amount))
123
+ .accounts({
124
+ nodeWallet: params.nodeWallet,
125
+ funder: params.funder,
126
+ systemProgram: SystemProgram.programId,
127
+ })
128
+ .instruction();
129
+ } else {
130
+ const mintPubkey = new PublicKey(params.mint);
131
+ const mintOwner = await lavarageProgram.provider.connection.getAccountInfo(mintPubkey);
132
+ const mintAccount = await getMint(lavarageProgram.provider.connection, mintPubkey, 'confirmed', mintOwner?.owner);
133
+ instruction = createTransferCheckedInstruction(
134
+ getAssociatedTokenAddressSync(mintPubkey, new PublicKey(params.funder), true, mintOwner?.owner),
135
+ new PublicKey(params.mint),
136
+ getAssociatedTokenAddressSync(mintPubkey, new PublicKey(params.nodeWallet), true, mintOwner?.owner),
137
+ lavarageProgram.provider.publicKey!,
138
+ params.amount,
139
+ mintAccount.decimals,
140
+ [],
141
+ mintOwner?.owner
142
+ );
143
+ }
144
+
145
+ const messageV0 = new TransactionMessage({
146
+ payerKey: lavarageProgram.provider.publicKey!,
147
+ recentBlockhash: blockhash,
148
+ instructions: [instruction, computeFeeIx],
149
+ }).compileToV0Message();
150
+
151
+ return new VersionedTransaction(messageV0);
152
+ }
153
+
154
+ export async function withdrawFundsV1(
155
+ lavarageProgram: Program<Lavarage>,
156
+ params: {
157
+ nodeWallet: PublicKey;
158
+ funder: PublicKey;
159
+ amount: number;
160
+ }
161
+ ): Promise<VersionedTransaction> {
162
+ const { blockhash } =
163
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
164
+
165
+ const instruction = await lavarageProgram.methods
166
+ .lpOperatorWithdrawFromNodeWallet(new BN(params.amount))
167
+ .accounts({
168
+ nodeWallet: params.nodeWallet,
169
+ funder: params.funder,
170
+ systemProgram: SystemProgram.programId,
171
+ })
172
+ .instruction();
173
+
174
+ const messageV0 = new TransactionMessage({
175
+ payerKey: lavarageProgram.provider.publicKey!,
176
+ recentBlockhash: blockhash,
177
+ instructions: [instruction, computeFeeIx],
178
+ }).compileToV0Message();
179
+
180
+ return new VersionedTransaction(messageV0);
181
+ }
182
+
183
+ export async function withdrawFundsV2(
184
+ lavarageProgram: Program<LavarageV2>,
185
+ params: {
186
+ nodeWallet: PublicKey;
187
+ funder: PublicKey;
188
+ mint: string;
189
+ amount: number;
190
+ fromTokenAccount?: PublicKey; // Optional, will be derived if not provided
191
+ toTokenAccount?: PublicKey; // Optional, will be derived if not provided
192
+ }
193
+ ): Promise<VersionedTransaction> {
194
+ const { blockhash } =
195
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
196
+
197
+ const mintPubkey = new PublicKey(params.mint);
198
+
199
+ // Derive token accounts if not provided
200
+ const fromTokenAccount =
201
+ params.fromTokenAccount ||
202
+ getAssociatedTokenAddressSync(mintPubkey, params.nodeWallet, true);
203
+ const toTokenAccount =
204
+ params.toTokenAccount ||
205
+ getAssociatedTokenAddressSync(mintPubkey, params.funder);
206
+
207
+ const instruction = await lavarageProgram.methods
208
+ .lpOperatorWithdrawFromNodeWallet(new BN(params.amount))
209
+ .accounts({
210
+ nodeWallet: params.nodeWallet,
211
+ funder: params.funder,
212
+ systemProgram: SystemProgram.programId,
213
+ mint: mintPubkey,
214
+ fromTokenAccount,
215
+ toTokenAccount,
216
+ tokenProgram: TOKEN_PROGRAM_ID,
217
+ })
218
+ .instruction();
219
+
220
+ const messageV0 = new TransactionMessage({
221
+ payerKey: lavarageProgram.provider.publicKey!,
222
+ recentBlockhash: blockhash,
223
+ instructions: [instruction, computeFeeIx],
224
+ }).compileToV0Message();
225
+
226
+ return new VersionedTransaction(messageV0);
227
+ }
228
+
229
+ // Unified withdraw function that works with both V1 and V2
230
+ export async function withdrawFunds(
231
+ lavarageProgram: Program<Lavarage> | Program<LavarageV2>,
232
+ params: {
233
+ nodeWallet: PublicKey;
234
+ funder: PublicKey;
235
+ amount: number;
236
+ mint?: string; // Required for V2, optional for V1
237
+ fromTokenAccount?: PublicKey; // Only used for V2
238
+ toTokenAccount?: PublicKey; // Only used for V2
239
+ }
240
+ ): Promise<VersionedTransaction> {
241
+ // Check if mint is provided to determine if this is V2
242
+ if (params.mint) {
243
+ return withdrawFundsV2(lavarageProgram as Program<LavarageV2>, {
244
+ nodeWallet: params.nodeWallet,
245
+ funder: params.funder,
246
+ mint: params.mint,
247
+ amount: params.amount,
248
+ fromTokenAccount: params.fromTokenAccount,
249
+ toTokenAccount: params.toTokenAccount,
250
+ });
251
+ } else {
252
+ return withdrawFundsV1(lavarageProgram as Program<Lavarage>, {
253
+ nodeWallet: params.nodeWallet,
254
+ funder: params.funder,
255
+ amount: params.amount,
256
+ });
257
+ }
258
+ }
259
+
260
+ export async function createOffer(
261
+ lavarageProgram: Program<Lavarage> | Program<LavarageV2>,
262
+ params: {
263
+ tradingPool: PublicKey;
264
+ poolOwner: PublicKey;
265
+ nodeWallet: string;
266
+ mint?: string;
267
+ interestRate: number;
268
+ maxExposure: number;
269
+ }
270
+ ): Promise<VersionedTransaction> {
271
+
272
+ const nodeWalletAccount = await lavarageProgram.provider.connection.getAccountInfo(new PublicKey(params.nodeWallet));
273
+ let nodeWalletSigner, createNodeWalletInstruction;
274
+ if (!nodeWalletAccount) {
275
+ // create node wallet
276
+ const { instruction, nodeWallet } = await createNodeWallet(lavarageProgram, {
277
+ operator: new PublicKey(params.poolOwner.toBase58()),
278
+ mint: params.mint,
279
+ liquidationLtv: 90,
280
+ });
281
+ nodeWalletSigner = nodeWallet;
282
+ createNodeWalletInstruction = instruction;
283
+ }
284
+
285
+ const { blockhash } =
286
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
287
+
288
+ const instruction = await lavarageProgram.methods
289
+ .lpOperatorCreateTradingPool(new BN(params.interestRate))
290
+ .accounts({
291
+ tradingPool: params.tradingPool,
292
+ operator: params.poolOwner,
293
+ nodeWallet: new PublicKey(params.nodeWallet),
294
+ mint: params.mint ? new PublicKey(params.mint) : undefined,
295
+ systemProgram: SystemProgram.programId,
296
+ })
297
+ .instruction();
298
+
299
+ const updateMaxExposureInstruction = await lavarageProgram.methods
300
+ .lpOperatorUpdateMaxExposure(new BN(params.maxExposure))
301
+ .accounts({
302
+ tradingPool: params.tradingPool,
303
+ nodeWallet: new PublicKey(params.nodeWallet),
304
+ operator: params.poolOwner,
305
+ systemProgram: SystemProgram.programId,
306
+ })
307
+ .instruction();
308
+
309
+ const messageV0 = new TransactionMessage({
310
+ payerKey: lavarageProgram.provider.publicKey!,
311
+ recentBlockhash: blockhash,
312
+ instructions: [createNodeWalletInstruction === undefined ? null : createNodeWalletInstruction, instruction, updateMaxExposureInstruction, computeFeeIx].filter(Boolean) as TransactionInstruction[],
313
+ }).compileToV0Message();
314
+
315
+ if (nodeWalletSigner) {
316
+ const transaction = new VersionedTransaction(messageV0);
317
+ transaction.addSignature(
318
+ nodeWalletSigner.publicKey,
319
+ nodeWalletSigner.secretKey
320
+ );
321
+ return transaction;
322
+ }
323
+
324
+ return new VersionedTransaction(messageV0);
325
+ }
326
+
327
+ export async function updateMaxExposure(
328
+ lavarageProgram: Program<Lavarage> | Program<LavarageV2>,
329
+ params: {
330
+ tradingPool: PublicKey;
331
+ nodeWallet: string;
332
+ poolOwner: PublicKey;
333
+ maxExposure: number;
334
+ }
335
+ ): Promise<VersionedTransaction> {
336
+ const { blockhash } =
337
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
338
+
339
+ const instruction = await lavarageProgram.methods
340
+ .lpOperatorUpdateMaxExposure(new BN(params.maxExposure))
341
+ .accounts({
342
+ tradingPool: params.tradingPool,
343
+ nodeWallet: new PublicKey(params.nodeWallet),
344
+ operator: params.poolOwner,
345
+ systemProgram: SystemProgram.programId,
346
+ })
347
+ .instruction();
348
+
349
+ const messageV0 = new TransactionMessage({
350
+ payerKey: lavarageProgram.provider.publicKey!,
351
+ recentBlockhash: blockhash,
352
+ instructions: [instruction],
353
+ }).compileToV0Message();
354
+
355
+ return new VersionedTransaction(messageV0);
356
+ }
357
+
358
+ export async function updateInterestRate(
359
+ lavarageProgram: Program<Lavarage> | Program<LavarageV2>,
360
+ params: {
361
+ tradingPool: PublicKey;
362
+ nodeWallet: string;
363
+ poolOwner: PublicKey;
364
+ interestRate: number;
365
+ }
366
+ ): Promise<VersionedTransaction> {
367
+ const { blockhash } =
368
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
369
+
370
+ const instruction = await lavarageProgram.methods
371
+ .lpOperatorUpdateInterestRate(new BN(params.interestRate))
372
+ .accounts({
373
+ tradingPool: params.tradingPool,
374
+ nodeWallet: new PublicKey(params.nodeWallet),
375
+ operator: params.poolOwner,
376
+ systemProgram: SystemProgram.programId,
377
+ })
378
+ .instruction();
379
+
380
+ const messageV0 = new TransactionMessage({
381
+ payerKey: lavarageProgram.provider.publicKey!,
382
+ recentBlockhash: blockhash,
383
+ instructions: [instruction],
384
+ }).compileToV0Message();
385
+
386
+ return new VersionedTransaction(messageV0);
387
+ }
388
+
389
+ export async function updateOffer(
390
+ lavarageProgram: Program<Lavarage> | Program<LavarageV2>,
391
+ params: {
392
+ tradingPool: PublicKey;
393
+ poolOwner: PublicKey;
394
+ nodeWallet: string;
395
+ mint: string;
396
+ interestRate: number;
397
+ maxExposure: number;
398
+ //includeCreatePool?: boolean; // Optional flag to include pool creation
399
+ }
400
+ ): Promise<VersionedTransaction> {
401
+ const { blockhash } =
402
+ await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
403
+
404
+ const instructions = [];
405
+
406
+ // // Optionally include pool creation instruction
407
+ // if (params.includeCreatePool) {
408
+ // const createPoolInstruction = await lavarageProgram.methods
409
+ // .lpOperatorCreateTradingPool(new BN(params.interestRate))
410
+ // .accounts({
411
+ // tradingPool: params.tradingPool,
412
+ // operator: params.poolOwner,
413
+ // nodeWallet: new PublicKey(params.nodeWallet),
414
+ // mint: new PublicKey(params.mint),
415
+ // systemProgram: SystemProgram.programId,
416
+ // })
417
+ // .instruction();
418
+
419
+ // instructions.push(createPoolInstruction);
420
+ // }
421
+
422
+ // Update max exposure instruction
423
+ const updateMaxExposureInstruction = await lavarageProgram.methods
424
+ .lpOperatorUpdateMaxExposure(new BN(params.maxExposure))
425
+ .accounts({
426
+ tradingPool: params.tradingPool,
427
+ nodeWallet: new PublicKey(params.nodeWallet),
428
+ operator: params.poolOwner,
429
+ systemProgram: SystemProgram.programId,
430
+ })
431
+ .instruction();
432
+
433
+ // Update interest rate instruction
434
+ const updateInterestRateInstruction = await lavarageProgram.methods
435
+ .lpOperatorUpdateInterestRate(new BN(params.interestRate))
436
+ .accounts({
437
+ tradingPool: params.tradingPool,
438
+ nodeWallet: new PublicKey(params.nodeWallet),
439
+ operator: params.poolOwner,
440
+ systemProgram: SystemProgram.programId,
441
+ })
442
+ .instruction();
443
+
444
+ instructions.push(
445
+ updateMaxExposureInstruction,
446
+ updateInterestRateInstruction
447
+ );
448
+
449
+ const messageV0 = new TransactionMessage({
450
+ payerKey: lavarageProgram.provider.publicKey!,
451
+ recentBlockhash: blockhash,
452
+ instructions,
453
+ }).compileToV0Message();
454
+
455
+ return new VersionedTransaction(messageV0);
456
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lavarage/sdk",
3
- "version": "6.7.5",
3
+ "version": "6.8.0",
4
4
  "description": "Lavarage SDK",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",