@lavarage/sdk 7.5.7 → 7.5.9

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 (84) hide show
  1. package/.DS_Store +0 -0
  2. package/dist/index.d.mts +148 -2
  3. package/dist/index.d.ts +148 -2
  4. package/dist/index.js +355 -6
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +355 -6
  7. package/dist/index.mjs.map +1 -1
  8. package/docs/.nojekyll +1 -0
  9. package/docs/assets/hierarchy.js +1 -0
  10. package/docs/assets/highlight.css +92 -0
  11. package/docs/assets/icons.js +18 -0
  12. package/docs/assets/icons.svg +1 -0
  13. package/docs/assets/main.js +60 -0
  14. package/docs/assets/navigation.js +1 -0
  15. package/docs/assets/search.js +1 -0
  16. package/docs/assets/style.css +1633 -0
  17. package/docs/functions/borrowV1.html +1 -0
  18. package/docs/functions/borrowV2.html +1 -0
  19. package/docs/functions/closePositionEvm.html +9 -0
  20. package/docs/functions/closeTradeV1.html +17 -0
  21. package/docs/functions/closeTradeV2.html +18 -0
  22. package/docs/functions/createTpDelegate.html +17 -0
  23. package/docs/functions/getActiveLoanCountEvm.html +8 -0
  24. package/docs/functions/getActiveLoansBatchEvm.html +10 -0
  25. package/docs/functions/getAllPositions.html +17 -0
  26. package/docs/functions/getAvailableExposureEvm.html +9 -0
  27. package/docs/functions/getClosedPositions.html +7 -0
  28. package/docs/functions/getClosedPositionsEvm.html +9 -0
  29. package/docs/functions/getCollateralExposureEvm.html +9 -0
  30. package/docs/functions/getCollateralInfoEvm.html +9 -0
  31. package/docs/functions/getDelegateAccounts.html +13 -0
  32. package/docs/functions/getLiquidatedPositions.html +13 -0
  33. package/docs/functions/getLiquidatedPositionsEvm.html +9 -0
  34. package/docs/functions/getLoanEvm.html +9 -0
  35. package/docs/functions/getLoansByBorrowerEvm.html +9 -0
  36. package/docs/functions/getOffers.html +7 -0
  37. package/docs/functions/getOffersEvm.html +9 -0
  38. package/docs/functions/getOpenPositions.html +11 -0
  39. package/docs/functions/getOpeningFeeEvm.html +8 -0
  40. package/docs/functions/getPda.html +8 -0
  41. package/docs/functions/getPositionAccountPDA.html +9 -0
  42. package/docs/functions/getPositionsEvm.html +8 -0
  43. package/docs/functions/getProfitFeeEvm.html +8 -0
  44. package/docs/functions/getTokenBalanceEvm.html +8 -0
  45. package/docs/functions/getUserLoansEvm.html +9 -0
  46. package/docs/functions/lending.addToWithdrawalAccessList.html +1 -0
  47. package/docs/functions/lending.createOffer.html +15 -0
  48. package/docs/functions/lending.depositFunds.html +17 -0
  49. package/docs/functions/lending.getNodeWalletPDA.html +9 -0
  50. package/docs/functions/lending.getTradingPoolPDA.html +9 -0
  51. package/docs/functions/lending.getWithdrawalAccessList.html +1 -0
  52. package/docs/functions/lending.getWithdrawalAccessListPDA.html +1 -0
  53. package/docs/functions/lending.removeFromWithdrawalAccessList.html +1 -0
  54. package/docs/functions/lending.updateInterestRate.html +13 -0
  55. package/docs/functions/lending.updateMaxBorrow.html +13 -0
  56. package/docs/functions/lending.updateMaxExposure.html +13 -0
  57. package/docs/functions/lending.updateOffer.html +19 -0
  58. package/docs/functions/lending.withdrawFunds.html +20 -0
  59. package/docs/functions/lending.withdrawFundsV1.html +16 -0
  60. package/docs/functions/lending.withdrawFundsV2.html +19 -0
  61. package/docs/functions/liquidatePositionEvm.html +6 -0
  62. package/docs/functions/mergePositionV2.html +13 -0
  63. package/docs/functions/modifyTpDelegate.html +17 -0
  64. package/docs/functions/openPositionEvm.html +9 -0
  65. package/docs/functions/openTradeV1.html +26 -0
  66. package/docs/functions/openTradeV2.html +29 -0
  67. package/docs/functions/partialRepayV1.html +10 -0
  68. package/docs/functions/partialRepayV2.html +10 -0
  69. package/docs/functions/removeTpDelegate.html +13 -0
  70. package/docs/functions/splitPositionV2.html +13 -0
  71. package/docs/functions/updateMaxLendPerTokenBatchEvm.html +9 -0
  72. package/docs/hierarchy.html +1 -0
  73. package/docs/index.html +3 -0
  74. package/docs/modules/IDLV2.html +2 -0
  75. package/docs/modules/lending.html +1 -0
  76. package/docs/modules.html +4 -0
  77. package/docs/types/IDLV2.Lavarage.html +8 -0
  78. package/docs/types/Lavarage.html +8 -0
  79. package/docs/variables/IDL.html +1 -0
  80. package/docs/variables/IDLV2.IDL.html +1 -0
  81. package/idl/lavarage.ts +126 -0
  82. package/idl/lavaragev2.ts +166 -0
  83. package/index.ts +437 -176
  84. package/package.json +2 -2
package/index.ts CHANGED
@@ -2128,8 +2128,12 @@ export const closeTradeV1 = async (
2128
2128
  computeBudgetMicroLamports?: number,
2129
2129
  platformFeeRecipient?: PublicKey,
2130
2130
  splitTransactions?: boolean,
2131
+ discountBps?: number,
2132
+ referralBps?: number,
2131
2133
  ) => {
2132
2134
  let partnerFeeMarkupAsPkey;
2135
+ const referralVaultProgram = new Program<UserVault>(userVaultIDL, REFFERAL_VAULT_PROGRAM_ID, lavarageProgram.provider);
2136
+
2133
2137
  if (partnerFeeMarkup) {
2134
2138
  const feeBuffer = Buffer.alloc(8);
2135
2139
  feeBuffer.writeBigUInt64LE(BigInt(partnerFeeMarkup));
@@ -2216,6 +2220,31 @@ export const closeTradeV1 = async (
2216
2220
  const { blockhash } =
2217
2221
  await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
2218
2222
 
2223
+ const useReferral = discountBps !== undefined && referralBps !== undefined;
2224
+
2225
+ // Check if partner fee recipient vault needs to be initialized via referralVaultProgram
2226
+ let partnerFeeRecipientCreateIx: TransactionInstruction | undefined;
2227
+ let userVaultPda: PublicKey | undefined;
2228
+ if (partnerFeeRecipient && referralBps !== undefined && referralVaultProgram) {
2229
+ [userVaultPda] = PublicKey.findProgramAddressSync(
2230
+ [Buffer.from("user_vault"), new PublicKey(partnerFeeRecipient).toBuffer()],
2231
+ referralVaultProgram.programId
2232
+ );
2233
+ const vaultAccountInfo = await lavarageProgram.provider.connection.getAccountInfo(userVaultPda);
2234
+ if (!vaultAccountInfo) {
2235
+ // Initialize the vault using referralVaultProgram
2236
+ partnerFeeRecipientCreateIx = await referralVaultProgram.methods
2237
+ .initializeVault()
2238
+ .accountsStrict({
2239
+ userVault: userVaultPda,
2240
+ user: partnerFeeRecipient!,
2241
+ funder: lavarageProgram.provider.publicKey!,
2242
+ systemProgram: SystemProgram.programId,
2243
+ })
2244
+ .instruction();
2245
+ }
2246
+ }
2247
+
2219
2248
  const closePositionIx = await lavarageProgram.methods
2220
2249
  .tradingCloseBorrowCollateral()
2221
2250
  .accountsStrict({
@@ -2236,71 +2265,131 @@ export const closeTradeV1 = async (
2236
2265
  let repaySolIx: TransactionInstruction | null = null;
2237
2266
  let jupiterIxs: TransactionInstruction[] = [];
2238
2267
  if (jupInstruction.instructions == undefined) {
2239
- repaySolIx = await lavarageProgram.methods
2240
- .tradingCloseRepaySol(
2241
- new BN(jupInstruction.quoteResponse.outAmount),
2242
- new BN(9997)
2243
- )
2244
- .accountsStrict({
2245
- nodeWallet: pool.account.nodeWallet,
2246
- positionAccount: positionAccountPDA,
2247
- tradingPool: poolPubKey,
2248
- trader: lavarageProgram.provider.publicKey!,
2249
- systemProgram: SystemProgram.programId,
2250
- clock: SYSVAR_CLOCK_PUBKEY,
2251
- randomAccountAsId: position.account.seed,
2252
- feeReceipient: "6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF",
2253
- })
2254
- .remainingAccounts(
2255
- partnerFeeRecipient && partnerFeeMarkupAsPkey
2256
- ? [
2257
- {
2258
- pubkey: partnerFeeRecipient,
2259
- isSigner: false,
2260
- isWritable: true,
2261
- },
2262
- {
2263
- pubkey: partnerFeeMarkupAsPkey,
2264
- isSigner: false,
2265
- isWritable: false,
2266
- },
2267
- ]
2268
- : []
2269
- )
2270
- .instruction();
2268
+ repaySolIx = useReferral
2269
+ ? await lavarageProgram.methods
2270
+ .tradingCloseRepaySolWithReferral(
2271
+ new BN(jupInstruction.quoteResponse.outAmount),
2272
+ new BN(9997),
2273
+ new BN(discountBps),
2274
+ new BN(referralBps)
2275
+ )
2276
+ .accountsStrict({
2277
+ nodeWallet: pool.account.nodeWallet,
2278
+ positionAccount: positionAccountPDA,
2279
+ tradingPool: poolPubKey,
2280
+ trader: lavarageProgram.provider.publicKey!,
2281
+ systemProgram: SystemProgram.programId,
2282
+ clock: SYSVAR_CLOCK_PUBKEY,
2283
+ randomAccountAsId: position.account.seed,
2284
+ feeReceipient: "6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF",
2285
+ })
2286
+ .remainingAccounts(
2287
+ partnerFeeRecipient && partnerFeeMarkupAsPkey && userVaultPda
2288
+ ? [
2289
+ {
2290
+ pubkey: userVaultPda,
2291
+ isSigner: false,
2292
+ isWritable: true,
2293
+ }
2294
+ ]
2295
+ : []
2296
+ )
2297
+ .instruction()
2298
+ : await lavarageProgram.methods
2299
+ .tradingCloseRepaySol(
2300
+ new BN(jupInstruction.quoteResponse.outAmount),
2301
+ new BN(9997)
2302
+ )
2303
+ .accountsStrict({
2304
+ nodeWallet: pool.account.nodeWallet,
2305
+ positionAccount: positionAccountPDA,
2306
+ tradingPool: poolPubKey,
2307
+ trader: lavarageProgram.provider.publicKey!,
2308
+ systemProgram: SystemProgram.programId,
2309
+ clock: SYSVAR_CLOCK_PUBKEY,
2310
+ randomAccountAsId: position.account.seed,
2311
+ feeReceipient: "6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF",
2312
+ })
2313
+ .remainingAccounts(
2314
+ partnerFeeRecipient && partnerFeeMarkupAsPkey
2315
+ ? [
2316
+ {
2317
+ pubkey: partnerFeeRecipient,
2318
+ isSigner: false,
2319
+ isWritable: true,
2320
+ },
2321
+ {
2322
+ pubkey: partnerFeeMarkupAsPkey,
2323
+ isSigner: false,
2324
+ isWritable: false,
2325
+ },
2326
+ ]
2327
+ : []
2328
+ )
2329
+ .instruction();
2271
2330
  } else {
2272
- repaySolIx = await lavarageProgram.methods
2273
- .tradingCloseRepaySol(
2274
- new BN(jupInstruction.quoteResponse.outAmount),
2275
- new BN(9998)
2276
- )
2277
- .accountsStrict({
2278
- nodeWallet: pool.account.nodeWallet,
2279
- positionAccount: positionAccountPDA,
2280
- tradingPool: poolPubKey,
2281
- trader: lavarageProgram.provider.publicKey!,
2282
- systemProgram: SystemProgram.programId,
2283
- clock: SYSVAR_CLOCK_PUBKEY,
2284
- randomAccountAsId: position.account.seed,
2285
- feeReceipient: "6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF",
2286
- })
2287
- .remainingAccounts(
2288
- partnerFeeRecipient && partnerFeeMarkupAsPkey
2289
- ? [
2290
- {
2291
- pubkey: partnerFeeRecipient,
2292
- isSigner: false,
2293
- isWritable: true,
2294
- },
2295
- {
2296
- pubkey: partnerFeeMarkupAsPkey,
2297
- isSigner: false,
2298
- isWritable: false,
2299
- },
2300
- ]
2301
- : []
2302
- )
2303
- .instruction();
2331
+ repaySolIx = useReferral
2332
+ ? await lavarageProgram.methods
2333
+ .tradingCloseRepaySolWithReferral(
2334
+ new BN(jupInstruction.quoteResponse.outAmount),
2335
+ new BN(9998),
2336
+ new BN(discountBps),
2337
+ new BN(referralBps)
2338
+ )
2339
+ .accountsStrict({
2340
+ nodeWallet: pool.account.nodeWallet,
2341
+ positionAccount: positionAccountPDA,
2342
+ tradingPool: poolPubKey,
2343
+ trader: lavarageProgram.provider.publicKey!,
2344
+ systemProgram: SystemProgram.programId,
2345
+ clock: SYSVAR_CLOCK_PUBKEY,
2346
+ randomAccountAsId: position.account.seed,
2347
+ feeReceipient: "6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF",
2348
+ })
2349
+ .remainingAccounts(
2350
+ partnerFeeRecipient && partnerFeeMarkupAsPkey && userVaultPda
2351
+ ? [
2352
+ {
2353
+ pubkey: userVaultPda,
2354
+ isSigner: false,
2355
+ isWritable: true,
2356
+ }
2357
+ ]
2358
+ : []
2359
+ )
2360
+ .instruction()
2361
+ : await lavarageProgram.methods
2362
+ .tradingCloseRepaySol(
2363
+ new BN(jupInstruction.quoteResponse.outAmount),
2364
+ new BN(9998)
2365
+ )
2366
+ .accountsStrict({
2367
+ nodeWallet: pool.account.nodeWallet,
2368
+ positionAccount: positionAccountPDA,
2369
+ tradingPool: poolPubKey,
2370
+ trader: lavarageProgram.provider.publicKey!,
2371
+ systemProgram: SystemProgram.programId,
2372
+ clock: SYSVAR_CLOCK_PUBKEY,
2373
+ randomAccountAsId: position.account.seed,
2374
+ feeReceipient: "6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF",
2375
+ })
2376
+ .remainingAccounts(
2377
+ partnerFeeRecipient && partnerFeeMarkupAsPkey
2378
+ ? [
2379
+ {
2380
+ pubkey: partnerFeeRecipient,
2381
+ isSigner: false,
2382
+ isWritable: true,
2383
+ },
2384
+ {
2385
+ pubkey: partnerFeeMarkupAsPkey,
2386
+ isSigner: false,
2387
+ isWritable: false,
2388
+ },
2389
+ ]
2390
+ : []
2391
+ )
2392
+ .instruction();
2304
2393
 
2305
2394
  const {
2306
2395
  setupInstructions,
@@ -2339,6 +2428,7 @@ export const closeTradeV1 = async (
2339
2428
 
2340
2429
  if (splitTransactions) {
2341
2430
  const setUpInstructions = [
2431
+ partnerFeeRecipientCreateIx,
2342
2432
  jupInstruction.instructions && platformFeeRecipientAccount?.instruction ? platformFeeRecipientAccount.instruction : null,
2343
2433
  createAssociatedTokenAccountInstruction
2344
2434
  ].filter((i) => !!i);
@@ -2375,6 +2465,7 @@ export const closeTradeV1 = async (
2375
2465
  }
2376
2466
 
2377
2467
  const allInstructions = [
2468
+ partnerFeeRecipientCreateIx,
2378
2469
  jupInstruction.instructions && platformFeeRecipientAccount?.instruction ? platformFeeRecipientAccount.instruction : null,
2379
2470
  createAssociatedTokenAccountInstruction,
2380
2471
  jupInstruction.instructions?.tokenLedgerInstruction
@@ -2464,8 +2555,12 @@ export const closeTradeV2 = async (
2464
2555
  computeBudgetMicroLamports?: number,
2465
2556
  platformFeeRecipient?: PublicKey,
2466
2557
  splitTransactions?: boolean,
2558
+ discountBps?: number,
2559
+ referralBps?: number,
2467
2560
  ) => {
2468
2561
  let partnerFeeMarkupAsPkey;
2562
+ const referralVaultProgram = new Program<UserVault>(userVaultIDL, REFFERAL_VAULT_PROGRAM_ID, lavarageProgram.provider);
2563
+
2469
2564
  if (partnerFeeMarkup) {
2470
2565
  const feeBuffer = Buffer.alloc(8);
2471
2566
  feeBuffer.writeBigUInt64LE(BigInt(partnerFeeMarkup));
@@ -2556,6 +2651,60 @@ export const closeTradeV2 = async (
2556
2651
  const { blockhash } =
2557
2652
  await lavarageProgram.provider.connection.getLatestBlockhash("finalized");
2558
2653
 
2654
+ const useReferral = discountBps !== undefined && referralBps !== undefined;
2655
+
2656
+ // Check if partner fee recipient vault and token account need to be created
2657
+ let partnerFeeRecipientVaultCreateIx: TransactionInstruction | undefined;
2658
+ let partnerFeeRecipientTokenAccountCreateIx: TransactionInstruction | undefined;
2659
+ let userVaultPda: PublicKey | undefined;
2660
+
2661
+ if (partnerFeeRecipient && partnerFeeMarkupAsPkey && referralVaultProgram) {
2662
+ // Derive the userVault PDA
2663
+ [userVaultPda] = PublicKey.findProgramAddressSync(
2664
+ [Buffer.from("user_vault"), new PublicKey(partnerFeeRecipient).toBuffer()],
2665
+ referralVaultProgram.programId
2666
+ );
2667
+
2668
+ // Get the vault's associated token account
2669
+ const vaultTokenAccount = getAssociatedTokenAddressSync(
2670
+ quoteToken,
2671
+ userVaultPda,
2672
+ true, // allowOwnerOffCurve for PDA
2673
+ quoteTokenProgram
2674
+ );
2675
+
2676
+ // Check both accounts at the same time
2677
+ const [vaultAccountInfo, vaultTokenAccountInfo] = await lavarageProgram.provider.connection.getMultipleAccountsInfo([
2678
+ userVaultPda,
2679
+ vaultTokenAccount
2680
+ ]);
2681
+
2682
+ // Create vault if it doesn't exist
2683
+ if (!vaultAccountInfo) {
2684
+ partnerFeeRecipientVaultCreateIx = await referralVaultProgram.methods
2685
+ .initializeVault()
2686
+ .accountsStrict({
2687
+ userVault: userVaultPda,
2688
+ user: partnerFeeRecipient!,
2689
+ funder: lavarageProgram.provider.publicKey!,
2690
+ systemProgram: SystemProgram.programId,
2691
+ })
2692
+ .instruction();
2693
+ }
2694
+
2695
+ // Create token account if it doesn't exist
2696
+ if (!vaultTokenAccountInfo) {
2697
+ partnerFeeRecipientTokenAccountCreateIx = createAssociatedTokenAccountIdempotentInstruction(
2698
+ lavarageProgram.provider.publicKey!,
2699
+ vaultTokenAccount,
2700
+ userVaultPda,
2701
+ quoteToken,
2702
+ quoteTokenProgram,
2703
+ ASSOCIATED_TOKEN_PROGRAM_ID
2704
+ );
2705
+ }
2706
+ }
2707
+
2559
2708
  const closePositionIx = await lavarageProgram.methods
2560
2709
  .tradingCloseBorrowCollateral()
2561
2710
  .accountsStrict({
@@ -2576,119 +2725,227 @@ export const closeTradeV2 = async (
2576
2725
  let repaySolIx: TransactionInstruction | null = null;
2577
2726
  let jupiterIxs: TransactionInstruction[] = [];
2578
2727
  if (jupInstruction.instructions == undefined) {
2579
- repaySolIx = await lavarageProgram.methods
2580
- .tradingCloseRepaySol(
2581
- new BN(jupInstruction.quoteResponse.outAmount),
2582
- new BN(9997)
2583
- )
2584
- .accountsStrict({
2585
- nodeWallet: pool.account.nodeWallet,
2586
- positionAccount: positionAccountPDA,
2587
- tradingPool: poolPubKey,
2588
- trader: lavarageProgram.provider.publicKey!,
2589
- systemProgram: SystemProgram.programId,
2590
- clock: SYSVAR_CLOCK_PUBKEY,
2591
- randomAccountAsId: position.account.seed,
2592
- feeTokenAccount: getAssociatedTokenAddressSync(
2593
- quoteToken,
2594
- new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
2595
- false,
2596
- quoteTokenProgram
2597
- ),
2598
- fromTokenAccount: getAssociatedTokenAddressSync(
2599
- quoteToken,
2600
- lavarageProgram.provider.publicKey!,
2601
- false,
2602
- quoteTokenProgram
2603
- ),
2604
- tokenProgram: quoteTokenProgram!,
2605
- toTokenAccount: getAssociatedTokenAddressSync(
2606
- quoteToken,
2607
- pool.account.nodeWallet,
2608
- true,
2609
- quoteTokenProgram
2610
- ),
2611
- mint: quoteToken,
2612
- })
2613
- .remainingAccounts(
2614
- partnerFeeRecipient && partnerFeeMarkupAsPkey
2615
- ? [
2616
- {
2617
- pubkey: getAssociatedTokenAddressSync(
2618
- quoteToken,
2619
- partnerFeeRecipient,
2620
- false,
2621
- quoteTokenProgram
2622
- ),
2623
- isSigner: false,
2624
- isWritable: true,
2625
- },
2626
- {
2627
- pubkey: partnerFeeMarkupAsPkey,
2628
- isSigner: false,
2629
- isWritable: false,
2630
- },
2631
- ]
2632
- : []
2633
- )
2634
- .instruction();
2728
+ repaySolIx = useReferral
2729
+ ? await lavarageProgram.methods
2730
+ .tradingCloseRepaySolWithReferral(
2731
+ new BN(jupInstruction.quoteResponse.outAmount),
2732
+ new BN(9997),
2733
+ new BN(discountBps),
2734
+ new BN(referralBps)
2735
+ )
2736
+ .accountsStrict({
2737
+ nodeWallet: pool.account.nodeWallet,
2738
+ positionAccount: positionAccountPDA,
2739
+ tradingPool: poolPubKey,
2740
+ trader: lavarageProgram.provider.publicKey!,
2741
+ systemProgram: SystemProgram.programId,
2742
+ clock: SYSVAR_CLOCK_PUBKEY,
2743
+ randomAccountAsId: position.account.seed,
2744
+ feeTokenAccount: getAssociatedTokenAddressSync(
2745
+ quoteToken,
2746
+ new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
2747
+ false,
2748
+ quoteTokenProgram
2749
+ ),
2750
+ fromTokenAccount: getAssociatedTokenAddressSync(
2751
+ quoteToken,
2752
+ lavarageProgram.provider.publicKey!,
2753
+ false,
2754
+ quoteTokenProgram
2755
+ ),
2756
+ tokenProgram: quoteTokenProgram!,
2757
+ toTokenAccount: getAssociatedTokenAddressSync(
2758
+ quoteToken,
2759
+ pool.account.nodeWallet,
2760
+ true,
2761
+ quoteTokenProgram
2762
+ ),
2763
+ mint: quoteToken,
2764
+ })
2765
+ .remainingAccounts(
2766
+ partnerFeeRecipient && partnerFeeMarkupAsPkey && userVaultPda
2767
+ ? [
2768
+ {
2769
+ pubkey: getAssociatedTokenAddressSync(
2770
+ quoteToken,
2771
+ userVaultPda,
2772
+ true, // allowOwnerOffCurve for PDA
2773
+ quoteTokenProgram
2774
+ ),
2775
+ isSigner: false,
2776
+ isWritable: true,
2777
+ }
2778
+ ]
2779
+ : []
2780
+ )
2781
+ .instruction()
2782
+ : await lavarageProgram.methods
2783
+ .tradingCloseRepaySol(
2784
+ new BN(jupInstruction.quoteResponse.outAmount),
2785
+ new BN(9997)
2786
+ )
2787
+ .accountsStrict({
2788
+ nodeWallet: pool.account.nodeWallet,
2789
+ positionAccount: positionAccountPDA,
2790
+ tradingPool: poolPubKey,
2791
+ trader: lavarageProgram.provider.publicKey!,
2792
+ systemProgram: SystemProgram.programId,
2793
+ clock: SYSVAR_CLOCK_PUBKEY,
2794
+ randomAccountAsId: position.account.seed,
2795
+ feeTokenAccount: getAssociatedTokenAddressSync(
2796
+ quoteToken,
2797
+ new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
2798
+ false,
2799
+ quoteTokenProgram
2800
+ ),
2801
+ fromTokenAccount: getAssociatedTokenAddressSync(
2802
+ quoteToken,
2803
+ lavarageProgram.provider.publicKey!,
2804
+ false,
2805
+ quoteTokenProgram
2806
+ ),
2807
+ tokenProgram: quoteTokenProgram!,
2808
+ toTokenAccount: getAssociatedTokenAddressSync(
2809
+ quoteToken,
2810
+ pool.account.nodeWallet,
2811
+ true,
2812
+ quoteTokenProgram
2813
+ ),
2814
+ mint: quoteToken,
2815
+ })
2816
+ .remainingAccounts(
2817
+ partnerFeeRecipient && partnerFeeMarkupAsPkey
2818
+ ? [
2819
+ {
2820
+ pubkey: getAssociatedTokenAddressSync(
2821
+ quoteToken,
2822
+ partnerFeeRecipient,
2823
+ false,
2824
+ quoteTokenProgram
2825
+ ),
2826
+ isSigner: false,
2827
+ isWritable: true,
2828
+ },
2829
+ {
2830
+ pubkey: partnerFeeMarkupAsPkey,
2831
+ isSigner: false,
2832
+ isWritable: false,
2833
+ },
2834
+ ]
2835
+ : []
2836
+ )
2837
+ .instruction();
2635
2838
  } else {
2636
- repaySolIx = await lavarageProgram.methods
2637
- .tradingCloseRepaySol(
2638
- new BN(jupInstruction.quoteResponse.outAmount),
2639
- new BN(9998)
2640
- )
2641
- .accountsStrict({
2642
- nodeWallet: pool.account.nodeWallet,
2643
- positionAccount: positionAccountPDA,
2644
- tradingPool: poolPubKey,
2645
- trader: lavarageProgram.provider.publicKey!,
2646
- systemProgram: SystemProgram.programId,
2647
- clock: SYSVAR_CLOCK_PUBKEY,
2648
- randomAccountAsId: position.account.seed,
2649
- feeTokenAccount: getAssociatedTokenAddressSync(
2650
- quoteToken,
2651
- new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
2652
- false,
2653
- quoteTokenProgram
2654
- ),
2655
- fromTokenAccount: getAssociatedTokenAddressSync(
2656
- quoteToken,
2657
- lavarageProgram.provider.publicKey!,
2658
- false,
2659
- quoteTokenProgram
2660
- ),
2661
- tokenProgram: quoteTokenProgram!,
2662
- toTokenAccount: getAssociatedTokenAddressSync(
2663
- quoteToken,
2664
- pool.account.nodeWallet,
2665
- true,
2666
- quoteTokenProgram
2667
- ),
2668
- mint: quoteToken,
2669
- })
2670
- .remainingAccounts(
2671
- partnerFeeRecipient && partnerFeeMarkupAsPkey
2672
- ? [
2673
- {
2674
- pubkey: getAssociatedTokenAddressSync(
2675
- quoteToken,
2676
- partnerFeeRecipient,
2677
- false,
2678
- quoteTokenProgram
2679
- ),
2680
- isSigner: false,
2681
- isWritable: true,
2682
- },
2683
- {
2684
- pubkey: partnerFeeMarkupAsPkey,
2685
- isSigner: false,
2686
- isWritable: false,
2687
- },
2688
- ]
2689
- : []
2690
- )
2691
- .instruction();
2839
+ repaySolIx = useReferral
2840
+ ? await lavarageProgram.methods
2841
+ .tradingCloseRepaySolWithReferral(
2842
+ new BN(jupInstruction.quoteResponse.outAmount),
2843
+ new BN(9998),
2844
+ new BN(discountBps),
2845
+ new BN(referralBps)
2846
+ )
2847
+ .accountsStrict({
2848
+ nodeWallet: pool.account.nodeWallet,
2849
+ positionAccount: positionAccountPDA,
2850
+ tradingPool: poolPubKey,
2851
+ trader: lavarageProgram.provider.publicKey!,
2852
+ systemProgram: SystemProgram.programId,
2853
+ clock: SYSVAR_CLOCK_PUBKEY,
2854
+ randomAccountAsId: position.account.seed,
2855
+ feeTokenAccount: getAssociatedTokenAddressSync(
2856
+ quoteToken,
2857
+ new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
2858
+ false,
2859
+ quoteTokenProgram
2860
+ ),
2861
+ fromTokenAccount: getAssociatedTokenAddressSync(
2862
+ quoteToken,
2863
+ lavarageProgram.provider.publicKey!,
2864
+ false,
2865
+ quoteTokenProgram
2866
+ ),
2867
+ tokenProgram: quoteTokenProgram!,
2868
+ toTokenAccount: getAssociatedTokenAddressSync(
2869
+ quoteToken,
2870
+ pool.account.nodeWallet,
2871
+ true,
2872
+ quoteTokenProgram
2873
+ ),
2874
+ mint: quoteToken,
2875
+ })
2876
+ .remainingAccounts(
2877
+ partnerFeeRecipient && partnerFeeMarkupAsPkey && userVaultPda
2878
+ ? [
2879
+ {
2880
+ pubkey: getAssociatedTokenAddressSync(
2881
+ quoteToken,
2882
+ userVaultPda,
2883
+ true, // allowOwnerOffCurve for PDA
2884
+ quoteTokenProgram
2885
+ ),
2886
+ isSigner: false,
2887
+ isWritable: true,
2888
+ }
2889
+ ]
2890
+ : []
2891
+ )
2892
+ .instruction()
2893
+ : await lavarageProgram.methods
2894
+ .tradingCloseRepaySol(
2895
+ new BN(jupInstruction.quoteResponse.outAmount),
2896
+ new BN(9998)
2897
+ )
2898
+ .accountsStrict({
2899
+ nodeWallet: pool.account.nodeWallet,
2900
+ positionAccount: positionAccountPDA,
2901
+ tradingPool: poolPubKey,
2902
+ trader: lavarageProgram.provider.publicKey!,
2903
+ systemProgram: SystemProgram.programId,
2904
+ clock: SYSVAR_CLOCK_PUBKEY,
2905
+ randomAccountAsId: position.account.seed,
2906
+ feeTokenAccount: getAssociatedTokenAddressSync(
2907
+ quoteToken,
2908
+ new PublicKey("6JfTobDvwuwZxZP6FR5JPmjdvQ4h4MovkEVH2FPsMSrF"),
2909
+ false,
2910
+ quoteTokenProgram
2911
+ ),
2912
+ fromTokenAccount: getAssociatedTokenAddressSync(
2913
+ quoteToken,
2914
+ lavarageProgram.provider.publicKey!,
2915
+ false,
2916
+ quoteTokenProgram
2917
+ ),
2918
+ tokenProgram: quoteTokenProgram!,
2919
+ toTokenAccount: getAssociatedTokenAddressSync(
2920
+ quoteToken,
2921
+ pool.account.nodeWallet,
2922
+ true,
2923
+ quoteTokenProgram
2924
+ ),
2925
+ mint: quoteToken,
2926
+ })
2927
+ .remainingAccounts(
2928
+ partnerFeeRecipient && partnerFeeMarkupAsPkey
2929
+ ? [
2930
+ {
2931
+ pubkey: getAssociatedTokenAddressSync(
2932
+ quoteToken,
2933
+ partnerFeeRecipient,
2934
+ false,
2935
+ quoteTokenProgram
2936
+ ),
2937
+ isSigner: false,
2938
+ isWritable: true,
2939
+ },
2940
+ {
2941
+ pubkey: partnerFeeMarkupAsPkey,
2942
+ isSigner: false,
2943
+ isWritable: false,
2944
+ },
2945
+ ]
2946
+ : []
2947
+ )
2948
+ .instruction();
2692
2949
  const {
2693
2950
  setupInstructions,
2694
2951
  swapInstruction: swapInstructionPayload,
@@ -2729,6 +2986,8 @@ export const closeTradeV2 = async (
2729
2986
 
2730
2987
  if (splitTransactions) {
2731
2988
  const setUpInstructions = [
2989
+ partnerFeeRecipientVaultCreateIx,
2990
+ partnerFeeRecipientTokenAccountCreateIx,
2732
2991
  jupInstruction.instructions && platformFeeRecipientAccount?.instruction ? platformFeeRecipientAccount.instruction : null,
2733
2992
  createAssociatedTokenAccountInstruction,
2734
2993
  ].filter((i) => !!i);
@@ -2765,6 +3024,8 @@ export const closeTradeV2 = async (
2765
3024
  }
2766
3025
 
2767
3026
  const allInstructions = [
3027
+ partnerFeeRecipientVaultCreateIx,
3028
+ partnerFeeRecipientTokenAccountCreateIx,
2768
3029
  jupInstruction.instructions && platformFeeRecipientAccount?.instruction ? platformFeeRecipientAccount.instruction : null,
2769
3030
  createAssociatedTokenAccountInstruction,
2770
3031
  jupInstruction.instructions?.tokenLedgerInstruction