@liquid-af/sdk 0.3.0 → 0.4.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 (74) hide show
  1. package/dist/accounts/liquid.d.ts +2 -3
  2. package/dist/accounts/liquid.d.ts.map +1 -1
  3. package/dist/accounts/liquid.js +3 -4
  4. package/dist/accounts/liquid.js.map +1 -1
  5. package/dist/client.d.ts +4 -4
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/client.js +6 -6
  8. package/dist/client.js.map +1 -1
  9. package/dist/helpers/preview.d.ts.map +1 -1
  10. package/dist/helpers/preview.js +1 -1
  11. package/dist/helpers/preview.js.map +1 -1
  12. package/dist/idl/index.d.ts +1 -0
  13. package/dist/idl/index.d.ts.map +1 -1
  14. package/dist/idl/index.js +15 -4
  15. package/dist/idl/index.js.map +1 -1
  16. package/dist/idl/liquid.d.ts +0 -20
  17. package/dist/idl/liquid.d.ts.map +1 -1
  18. package/dist/idl/liquid.json +0 -20
  19. package/dist/idl/patch-idl.d.ts +10 -0
  20. package/dist/idl/patch-idl.d.ts.map +1 -0
  21. package/dist/idl/patch-idl.js +62 -0
  22. package/dist/idl/patch-idl.js.map +1 -0
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +1 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/instructions/liquid.d.ts.map +1 -1
  28. package/dist/instructions/liquid.js +3 -3
  29. package/dist/instructions/liquid.js.map +1 -1
  30. package/dist/instructions/program-cache.d.ts.map +1 -1
  31. package/dist/instructions/program-cache.js +10 -6
  32. package/dist/instructions/program-cache.js.map +1 -1
  33. package/dist/pda/index.d.ts.map +1 -1
  34. package/dist/pda/index.js +1 -1
  35. package/dist/pda/index.js.map +1 -1
  36. package/dist/pda/liquid.d.ts +2 -3
  37. package/dist/pda/liquid.d.ts.map +1 -1
  38. package/dist/pda/liquid.js +3 -4
  39. package/dist/pda/liquid.js.map +1 -1
  40. package/package.json +4 -4
  41. package/src/accounts/liquid-fees.ts +2 -2
  42. package/src/accounts/liquid-state.ts +7 -7
  43. package/src/accounts/liquid-swap.ts +4 -4
  44. package/src/accounts/liquid.ts +7 -10
  45. package/src/client.ts +33 -36
  46. package/src/config.ts +10 -10
  47. package/src/errors.ts +6 -6
  48. package/src/events/parser.ts +5 -5
  49. package/src/helpers/preview.ts +31 -32
  50. package/src/helpers/user.ts +1 -1
  51. package/src/idl/index.ts +31 -8
  52. package/src/idl/liquid.json +0 -20
  53. package/src/idl/liquid.ts +0 -20
  54. package/src/idl/patch-idl.ts +80 -0
  55. package/src/index.ts +1 -0
  56. package/src/instructions/liquid-fees.ts +14 -14
  57. package/src/instructions/liquid-state.ts +4 -4
  58. package/src/instructions/liquid-swap.ts +18 -18
  59. package/src/instructions/liquid.ts +47 -50
  60. package/src/instructions/program-cache.ts +18 -9
  61. package/src/math/amm.ts +3 -3
  62. package/src/math/bonding-curve.ts +6 -6
  63. package/src/math/fees.ts +9 -9
  64. package/src/math/tiered-fees.ts +5 -5
  65. package/src/oracle.ts +3 -3
  66. package/src/pda/index.ts +27 -28
  67. package/src/pda/liquid-fees.ts +6 -6
  68. package/src/pda/liquid-state.ts +13 -13
  69. package/src/pda/liquid-swap.ts +16 -16
  70. package/src/pda/liquid.ts +17 -19
  71. package/src/provider.ts +2 -2
  72. package/src/transaction/builder.ts +4 -4
  73. package/src/transaction/send.ts +4 -4
  74. package/src/types.ts +2 -2
@@ -26,7 +26,7 @@ export interface BuildCreatePoolParams {
26
26
  * @returns Transaction instruction
27
27
  */
28
28
  export function buildCreatePool(
29
- params: BuildCreatePoolParams
29
+ params: BuildCreatePoolParams,
30
30
  ): Promise<TransactionInstruction> {
31
31
  const {
32
32
  signer,
@@ -46,13 +46,13 @@ export function buildCreatePool(
46
46
  baseMint,
47
47
  signer,
48
48
  false,
49
- baseTokenProgram
49
+ baseTokenProgram,
50
50
  );
51
51
  const creatorQuote = getAssociatedTokenAddressSync(
52
52
  quoteMint,
53
53
  signer,
54
54
  false,
55
- quoteTokenProgram
55
+ quoteTokenProgram,
56
56
  );
57
57
 
58
58
  return program.methods
@@ -91,7 +91,7 @@ export interface BuildSwapBuyParams {
91
91
  * @returns Transaction instruction
92
92
  */
93
93
  export function buildSwapBuy(
94
- params: BuildSwapBuyParams
94
+ params: BuildSwapBuyParams,
95
95
  ): Promise<TransactionInstruction> {
96
96
  const {
97
97
  payer,
@@ -113,13 +113,13 @@ export function buildSwapBuy(
113
113
  baseMint,
114
114
  payer,
115
115
  false,
116
- baseTokenProgram
116
+ baseTokenProgram,
117
117
  );
118
118
  const quoteTokenAccount = getAssociatedTokenAddressSync(
119
119
  quoteMint,
120
120
  payer,
121
121
  false,
122
- quoteTokenProgram
122
+ quoteTokenProgram,
123
123
  );
124
124
  return program.methods
125
125
  .buy(maxAmountIn, amountOut)
@@ -163,7 +163,7 @@ export interface BuildSwapSellParams {
163
163
  * @returns Transaction instruction
164
164
  */
165
165
  export function buildSwapSell(
166
- params: BuildSwapSellParams
166
+ params: BuildSwapSellParams,
167
167
  ): Promise<TransactionInstruction> {
168
168
  const {
169
169
  payer,
@@ -185,13 +185,13 @@ export function buildSwapSell(
185
185
  baseMint,
186
186
  payer,
187
187
  false,
188
- baseTokenProgram
188
+ baseTokenProgram,
189
189
  );
190
190
  const quoteTokenAccount = getAssociatedTokenAddressSync(
191
191
  quoteMint,
192
192
  payer,
193
193
  false,
194
- quoteTokenProgram
194
+ quoteTokenProgram,
195
195
  );
196
196
  return program.methods
197
197
  .sell(amountIn, minimumAmountOut)
@@ -237,7 +237,7 @@ export interface BuildDepositParams {
237
237
  * @returns Transaction instruction
238
238
  */
239
239
  export function buildDeposit(
240
- params: BuildDepositParams
240
+ params: BuildDepositParams,
241
241
  ): Promise<TransactionInstruction> {
242
242
  const {
243
243
  owner,
@@ -261,19 +261,19 @@ export function buildDeposit(
261
261
  lpMint,
262
262
  owner,
263
263
  false,
264
- TOKEN_PROGRAM_ID
264
+ TOKEN_PROGRAM_ID,
265
265
  );
266
266
  const baseAccount = getAssociatedTokenAddressSync(
267
267
  baseMint,
268
268
  owner,
269
269
  false,
270
- baseTokenProgram
270
+ baseTokenProgram,
271
271
  );
272
272
  const quoteAccount = getAssociatedTokenAddressSync(
273
273
  quoteMint,
274
274
  owner,
275
275
  false,
276
- quoteTokenProgram
276
+ quoteTokenProgram,
277
277
  );
278
278
 
279
279
  return program.methods
@@ -316,7 +316,7 @@ export interface BuildWithdrawParams {
316
316
  * @returns Transaction instruction
317
317
  */
318
318
  export function buildWithdraw(
319
- params: BuildWithdrawParams
319
+ params: BuildWithdrawParams,
320
320
  ): Promise<TransactionInstruction> {
321
321
  const {
322
322
  owner,
@@ -340,19 +340,19 @@ export function buildWithdraw(
340
340
  lpMint,
341
341
  owner,
342
342
  false,
343
- TOKEN_PROGRAM_ID
343
+ TOKEN_PROGRAM_ID,
344
344
  );
345
345
  const baseAccount = getAssociatedTokenAddressSync(
346
346
  baseMint,
347
347
  owner,
348
348
  false,
349
- baseTokenProgram
349
+ baseTokenProgram,
350
350
  );
351
351
  const quoteAccount = getAssociatedTokenAddressSync(
352
352
  quoteMint,
353
353
  owner,
354
354
  false,
355
- quoteTokenProgram
355
+ quoteTokenProgram,
356
356
  );
357
357
 
358
358
  return program.methods
@@ -389,7 +389,7 @@ export interface BuildSwapExecuteBuybackParams {
389
389
  * @returns Transaction instruction
390
390
  */
391
391
  export function buildSwapExecuteBuyback(
392
- params: BuildSwapExecuteBuybackParams
392
+ params: BuildSwapExecuteBuybackParams,
393
393
  ): Promise<TransactionInstruction> {
394
394
  const {
395
395
  payer,
@@ -54,14 +54,14 @@ export interface BuildCreateNativeCurveParams {
54
54
  * @returns Transaction instruction
55
55
  */
56
56
  export function buildCreateNativeCurve(
57
- params: BuildCreateNativeCurveParams
57
+ params: BuildCreateNativeCurveParams,
58
58
  ): Promise<TransactionInstruction> {
59
59
  const { creator, mint, pythPriceFeed, name, symbol, uri, config } = params;
60
60
  const program = getCachedLiquidProgram(config);
61
61
 
62
62
  const bondingCurveTokenAccount = getBondingCurveTokenAccount(
63
63
  mint,
64
- config.liquidProgramId
64
+ config.liquidProgramId,
65
65
  );
66
66
  return program.methods
67
67
  .createNativeCurve({
@@ -93,7 +93,7 @@ export interface BuildBumpNativeParams {
93
93
  * @returns Transaction instruction
94
94
  */
95
95
  export function buildBumpNative(
96
- params: BuildBumpNativeParams
96
+ params: BuildBumpNativeParams,
97
97
  ): Promise<TransactionInstruction> {
98
98
  const { mint, pythPriceFeed, config } = params;
99
99
  const program = getCachedLiquidProgram(config);
@@ -128,7 +128,7 @@ export interface BuildBuyNativeParams {
128
128
  * @returns Transaction instruction
129
129
  */
130
130
  export function buildBuyNative(
131
- params: BuildBuyNativeParams
131
+ params: BuildBuyNativeParams,
132
132
  ): Promise<TransactionInstruction> {
133
133
  const {
134
134
  user,
@@ -148,7 +148,7 @@ export function buildBuyNative(
148
148
  // Derive creator-related accounts that Anchor can't auto-resolve
149
149
  const [creatorUserProperties] = getUserPropertiesPDA(
150
150
  creator,
151
- config.liquidStateProgramId
151
+ config.liquidStateProgramId,
152
152
  );
153
153
 
154
154
  return program.methods
@@ -179,12 +179,12 @@ export function buildBuyNative(
179
179
  */
180
180
  export async function buildBuyNativeAutoResolve(
181
181
  connection: Connection,
182
- params: Omit<BuildBuyNativeParams, "creator">
182
+ params: Omit<BuildBuyNativeParams, "creator">,
183
183
  ): Promise<TransactionInstruction> {
184
184
  const curve = await fetchNativeBondingCurve(
185
185
  connection,
186
186
  params.mint,
187
- params.config
187
+ params.config,
188
188
  );
189
189
  return buildBuyNative({
190
190
  ...params,
@@ -214,7 +214,7 @@ export interface BuildSellNativeParams {
214
214
  * @returns Transaction instruction
215
215
  */
216
216
  export function buildSellNative(
217
- params: BuildSellNativeParams
217
+ params: BuildSellNativeParams,
218
218
  ): Promise<TransactionInstruction> {
219
219
  const {
220
220
  user,
@@ -234,7 +234,7 @@ export function buildSellNative(
234
234
  // Derive creator-related accounts that Anchor can't auto-resolve
235
235
  const [creatorUserProperties] = getUserPropertiesPDA(
236
236
  creator,
237
- config.liquidStateProgramId
237
+ config.liquidStateProgramId,
238
238
  );
239
239
 
240
240
  return program.methods
@@ -265,12 +265,12 @@ export function buildSellNative(
265
265
  */
266
266
  export async function buildSellNativeAutoResolve(
267
267
  connection: Connection,
268
- params: Omit<BuildSellNativeParams, "creator">
268
+ params: Omit<BuildSellNativeParams, "creator">,
269
269
  ): Promise<TransactionInstruction> {
270
270
  const curve = await fetchNativeBondingCurve(
271
271
  connection,
272
272
  params.mint,
273
- params.config
273
+ params.config,
274
274
  );
275
275
  return buildSellNative({
276
276
  ...params,
@@ -293,7 +293,7 @@ export interface BuildMigrateNativeParams {
293
293
  * @returns Transaction instruction
294
294
  */
295
295
  export function buildMigrateNative(
296
- params: BuildMigrateNativeParams
296
+ params: BuildMigrateNativeParams,
297
297
  ): Promise<TransactionInstruction> {
298
298
  const { migrator, mint, config } = params;
299
299
  const program = getCachedLiquidProgram(config);
@@ -303,28 +303,28 @@ export function buildMigrateNative(
303
303
  const [ammPoolState] = getPoolPDA(
304
304
  mint,
305
305
  WSOL_MINT,
306
- config.liquidSwapProgramId
306
+ config.liquidSwapProgramId,
307
307
  );
308
308
  const [ammLpMint] = getPoolLpMintPDA(
309
309
  ammPoolState,
310
- config.liquidSwapProgramId
310
+ config.liquidSwapProgramId,
311
311
  );
312
312
  const [ammBaseVault] = getPoolVaultPDA(
313
313
  ammPoolState,
314
314
  mint,
315
- config.liquidSwapProgramId
315
+ config.liquidSwapProgramId,
316
316
  );
317
317
  const [ammQuoteVault] = getPoolVaultPDA(
318
318
  ammPoolState,
319
319
  WSOL_MINT,
320
- config.liquidSwapProgramId
320
+ config.liquidSwapProgramId,
321
321
  );
322
322
  const [ammObservationState] = getObservationPDA(
323
323
  ammPoolState,
324
- config.liquidSwapProgramId
324
+ config.liquidSwapProgramId,
325
325
  );
326
326
  const [ammGlobalConfig] = getSwapGlobalConfigPDA(
327
- config.liquidSwapProgramId
327
+ config.liquidSwapProgramId,
328
328
  );
329
329
  const [ammAuthority] = getSwapAuthorityPDA(config.liquidSwapProgramId);
330
330
  const [tokenVolume] = getTokenVolumePDA(mint, config.liquidStateProgramId);
@@ -332,12 +332,12 @@ export function buildMigrateNative(
332
332
  ammLpMint,
333
333
  bondingCurve,
334
334
  true,
335
- TOKEN_PROGRAM_ID
335
+ TOKEN_PROGRAM_ID,
336
336
  );
337
337
  const [ammBuybackVault] = getSwapBuybackVaultPDA(
338
338
  ammPoolState,
339
339
  WSOL_MINT,
340
- config.liquidSwapProgramId
340
+ config.liquidSwapProgramId,
341
341
  );
342
342
  return program.methods
343
343
  .migrate()
@@ -370,7 +370,7 @@ export interface BuildInitializeNativeReferralVaultParams {
370
370
  * @returns Transaction instruction
371
371
  */
372
372
  export function buildInitializeNativeReferralVault(
373
- params: BuildInitializeNativeReferralVaultParams
373
+ params: BuildInitializeNativeReferralVaultParams,
374
374
  ): Promise<TransactionInstruction> {
375
375
  const { user, config } = params;
376
376
  const program = getCachedLiquidProgram(config);
@@ -393,7 +393,7 @@ export interface BuildWithdrawReferralRewardsParams {
393
393
  * @returns Transaction instruction
394
394
  */
395
395
  export function buildWithdrawReferralRewards(
396
- params: BuildWithdrawReferralRewardsParams
396
+ params: BuildWithdrawReferralRewardsParams,
397
397
  ): Promise<TransactionInstruction> {
398
398
  const { user, config } = params;
399
399
  const program = getCachedLiquidProgram(config);
@@ -419,7 +419,7 @@ export interface BuildExecuteBuybackNativeParams {
419
419
  * @returns Transaction instruction
420
420
  */
421
421
  export function buildExecuteBuybackNative(
422
- params: BuildExecuteBuybackNativeParams
422
+ params: BuildExecuteBuybackNativeParams,
423
423
  ): Promise<TransactionInstruction> {
424
424
  const { payer, mint, pythPriceFeed, config } = params;
425
425
  const program = getCachedLiquidProgram(config);
@@ -449,7 +449,7 @@ export interface BuildExecuteBuybackTokenParams {
449
449
  * @returns Transaction instruction
450
450
  */
451
451
  export function buildExecuteBuybackToken(
452
- params: BuildExecuteBuybackTokenParams
452
+ params: BuildExecuteBuybackTokenParams,
453
453
  ): Promise<TransactionInstruction> {
454
454
  const { payer, mint, quoteMint, config } = params;
455
455
  const program = getCachedLiquidProgram(config);
@@ -486,7 +486,7 @@ export interface BuildCreateStableCurveParams {
486
486
  * @returns Transaction instruction
487
487
  */
488
488
  export function buildCreateStableCurve(
489
- params: BuildCreateStableCurveParams
489
+ params: BuildCreateStableCurveParams,
490
490
  ): Promise<TransactionInstruction> {
491
491
  const { creator, mint, quoteMint, name, symbol, uri, config } = params;
492
492
  const program = getCachedLiquidProgram(config);
@@ -526,7 +526,7 @@ export interface BuildBuyStableParams {
526
526
  * @returns Transaction instruction
527
527
  */
528
528
  export function buildBuyStable(
529
- params: BuildBuyStableParams
529
+ params: BuildBuyStableParams,
530
530
  ): Promise<TransactionInstruction> {
531
531
  const {
532
532
  user,
@@ -548,13 +548,13 @@ export function buildBuyStable(
548
548
  quoteMint,
549
549
  feeRecipient,
550
550
  true,
551
- TOKEN_PROGRAM_ID
551
+ TOKEN_PROGRAM_ID,
552
552
  );
553
553
 
554
554
  // Derive creator-related accounts that Anchor can't auto-resolve
555
555
  const [creatorUserProperties] = getUserPropertiesPDA(
556
556
  creator,
557
- config.liquidStateProgramId
557
+ config.liquidStateProgramId,
558
558
  );
559
559
 
560
560
  return program.methods
@@ -585,13 +585,12 @@ export function buildBuyStable(
585
585
  */
586
586
  export async function buildBuyStableAutoResolve(
587
587
  connection: Connection,
588
- params: Omit<BuildBuyStableParams, "creator">
588
+ params: Omit<BuildBuyStableParams, "creator">,
589
589
  ): Promise<TransactionInstruction> {
590
590
  const curve = await fetchStableBondingCurve(
591
591
  connection,
592
592
  params.mint,
593
- params.quoteMint,
594
- params.config
593
+ params.config,
595
594
  );
596
595
  return buildBuyStable({ ...params, creator: curve.creator });
597
596
  }
@@ -617,7 +616,7 @@ export interface BuildSellStableParams {
617
616
  * @returns Transaction instruction
618
617
  */
619
618
  export function buildSellStable(
620
- params: BuildSellStableParams
619
+ params: BuildSellStableParams,
621
620
  ): Promise<TransactionInstruction> {
622
621
  const {
623
622
  user,
@@ -639,13 +638,13 @@ export function buildSellStable(
639
638
  quoteMint,
640
639
  feeRecipient,
641
640
  true,
642
- TOKEN_PROGRAM_ID
641
+ TOKEN_PROGRAM_ID,
643
642
  );
644
643
 
645
644
  // Derive creator-related accounts that Anchor can't auto-resolve
646
645
  const [creatorUserProperties] = getUserPropertiesPDA(
647
646
  creator,
648
- config.liquidStateProgramId
647
+ config.liquidStateProgramId,
649
648
  );
650
649
 
651
650
  return program.methods
@@ -676,13 +675,12 @@ export function buildSellStable(
676
675
  */
677
676
  export async function buildSellStableAutoResolve(
678
677
  connection: Connection,
679
- params: Omit<BuildSellStableParams, "creator">
678
+ params: Omit<BuildSellStableParams, "creator">,
680
679
  ): Promise<TransactionInstruction> {
681
680
  const curve = await fetchStableBondingCurve(
682
681
  connection,
683
682
  params.mint,
684
- params.quoteMint,
685
- params.config
683
+ params.config,
686
684
  );
687
685
  return buildSellStable({ ...params, creator: curve.creator });
688
686
  }
@@ -702,7 +700,7 @@ export interface BuildMigrateStableParams {
702
700
  * @returns Transaction instruction
703
701
  */
704
702
  export function buildMigrateStable(
705
- params: BuildMigrateStableParams
703
+ params: BuildMigrateStableParams,
706
704
  ): Promise<TransactionInstruction> {
707
705
  const { migrator, mint, quoteMint, config } = params;
708
706
  const program = getCachedLiquidProgram(config);
@@ -710,34 +708,33 @@ export function buildMigrateStable(
710
708
  // Derive AMM PDAs
711
709
  const [bondingCurve] = getStableBondingCurvePDA(
712
710
  mint,
713
- quoteMint,
714
- config.liquidProgramId
711
+ config.liquidProgramId,
715
712
  );
716
713
  const [ammPoolState] = getPoolPDA(
717
714
  mint,
718
715
  quoteMint,
719
- config.liquidSwapProgramId
716
+ config.liquidSwapProgramId,
720
717
  );
721
718
  const [ammLpMint] = getPoolLpMintPDA(
722
719
  ammPoolState,
723
- config.liquidSwapProgramId
720
+ config.liquidSwapProgramId,
724
721
  );
725
722
  const [ammBaseVault] = getPoolVaultPDA(
726
723
  ammPoolState,
727
724
  mint,
728
- config.liquidSwapProgramId
725
+ config.liquidSwapProgramId,
729
726
  );
730
727
  const [ammQuoteVault] = getPoolVaultPDA(
731
728
  ammPoolState,
732
729
  quoteMint,
733
- config.liquidSwapProgramId
730
+ config.liquidSwapProgramId,
734
731
  );
735
732
  const [ammObservationState] = getObservationPDA(
736
733
  ammPoolState,
737
- config.liquidSwapProgramId
734
+ config.liquidSwapProgramId,
738
735
  );
739
736
  const [ammGlobalConfig] = getSwapGlobalConfigPDA(
740
- config.liquidSwapProgramId
737
+ config.liquidSwapProgramId,
741
738
  );
742
739
  const [ammAuthority] = getSwapAuthorityPDA(config.liquidSwapProgramId);
743
740
  const [tokenVolume] = getTokenVolumePDA(mint, config.liquidStateProgramId);
@@ -745,12 +742,12 @@ export function buildMigrateStable(
745
742
  ammLpMint,
746
743
  bondingCurve,
747
744
  true,
748
- TOKEN_PROGRAM_ID
745
+ TOKEN_PROGRAM_ID,
749
746
  );
750
747
  const [ammBuybackVault] = getSwapBuybackVaultPDA(
751
748
  ammPoolState,
752
749
  quoteMint,
753
- config.liquidSwapProgramId
750
+ config.liquidSwapProgramId,
754
751
  );
755
752
  return program.methods
756
753
  .migrateStable()
@@ -788,7 +785,7 @@ export interface BuildInitializeFeeRecipientVaultsParams {
788
785
  * @returns Transaction instruction
789
786
  */
790
787
  export function buildInitializeFeeRecipientVaults(
791
- params: BuildInitializeFeeRecipientVaultsParams
788
+ params: BuildInitializeFeeRecipientVaultsParams,
792
789
  ): Promise<TransactionInstruction> {
793
790
  const { payer, quoteMint, feeRecipients, config } = params;
794
791
 
@@ -831,7 +828,7 @@ export interface BuildInitializeReferralTokenVaultParams {
831
828
  * @returns Transaction instruction
832
829
  */
833
830
  export function buildInitializeReferralTokenVault(
834
- params: BuildInitializeReferralTokenVaultParams
831
+ params: BuildInitializeReferralTokenVaultParams,
835
832
  ): Promise<TransactionInstruction> {
836
833
  const { user, quoteMint, config } = params;
837
834
  const program = getCachedLiquidProgram(config);
@@ -1,6 +1,7 @@
1
1
  import { Program } from "@coral-xyz/anchor";
2
2
  import type { LiquidConfig } from "../config.js";
3
3
  import { createStubProvider } from "../provider.js";
4
+ import { patchIdl } from "../idl/patch-idl.js";
4
5
 
5
6
  import liquidIdl from "../idl/liquid.json" with { type: "json" };
6
7
  import liquidSwapIdl from "../idl/liquid_swap.json" with { type: "json" };
@@ -22,21 +23,25 @@ const cache = new Map<string, Program<any>>();
22
23
 
23
24
  /**
24
25
  * Returns a cached Program instance or creates one if it doesn't exist.
26
+ * Deep-patches the IDL to replace all embedded mainnet program addresses
27
+ * with the addresses from the provided config.
25
28
  *
26
29
  * @param key - Cache key prefix (e.g., "liquid", "swap")
27
30
  * @param idl - Raw IDL JSON object
28
31
  * @param address - Program address as a base58 string
32
+ * @param config - Liquid protocol configuration
29
33
  * @returns Cached or newly created Program instance
30
34
  */
31
35
  function getOrCreate<T extends Liquid | LiquidSwap | LiquidFees | LiquidState>(
32
36
  key: string,
33
37
  idl: Record<string, unknown>,
34
- address: string
38
+ address: string,
39
+ config: LiquidConfig,
35
40
  ): Program<T> {
36
41
  const cacheKey = `${key}:${address}`;
37
42
  let prog = cache.get(cacheKey) as Program<T> | undefined;
38
43
  if (!prog) {
39
- const patchedIdl = { ...idl, address };
44
+ const patchedIdl = patchIdl({ ...idl, address }, config);
40
45
  prog = new Program(patchedIdl as T, createStubProvider());
41
46
  cache.set(cacheKey, prog);
42
47
  }
@@ -53,7 +58,8 @@ export function getCachedLiquidProgram(config: LiquidConfig): Program<Liquid> {
53
58
  return getOrCreate<Liquid>(
54
59
  "liquid",
55
60
  liquidIdl,
56
- config.liquidProgramId.toBase58()
61
+ config.liquidProgramId.toBase58(),
62
+ config,
57
63
  );
58
64
  }
59
65
 
@@ -64,12 +70,13 @@ export function getCachedLiquidProgram(config: LiquidConfig): Program<Liquid> {
64
70
  * @returns Cached Program instance typed as LiquidSwap
65
71
  */
66
72
  export function getCachedSwapProgram(
67
- config: LiquidConfig
73
+ config: LiquidConfig,
68
74
  ): Program<LiquidSwap> {
69
75
  return getOrCreate<LiquidSwap>(
70
76
  "swap",
71
77
  liquidSwapIdl,
72
- config.liquidSwapProgramId.toBase58()
78
+ config.liquidSwapProgramId.toBase58(),
79
+ config,
73
80
  );
74
81
  }
75
82
 
@@ -80,12 +87,13 @@ export function getCachedSwapProgram(
80
87
  * @returns Cached Program instance typed as LiquidFees
81
88
  */
82
89
  export function getCachedFeesProgram(
83
- config: LiquidConfig
90
+ config: LiquidConfig,
84
91
  ): Program<LiquidFees> {
85
92
  return getOrCreate<LiquidFees>(
86
93
  "fees",
87
94
  liquidFeesIdl,
88
- config.liquidFeesProgramId.toBase58()
95
+ config.liquidFeesProgramId.toBase58(),
96
+ config,
89
97
  );
90
98
  }
91
99
 
@@ -96,11 +104,12 @@ export function getCachedFeesProgram(
96
104
  * @returns Cached Program instance typed as LiquidState
97
105
  */
98
106
  export function getCachedStateProgram(
99
- config: LiquidConfig
107
+ config: LiquidConfig,
100
108
  ): Program<LiquidState> {
101
109
  return getOrCreate<LiquidState>(
102
110
  "state",
103
111
  liquidStateIdl,
104
- config.liquidStateProgramId.toBase58()
112
+ config.liquidStateProgramId.toBase58(),
113
+ config,
105
114
  );
106
115
  }
package/src/math/amm.ts CHANGED
@@ -23,7 +23,7 @@ export const calculateAmmSellOutput = (
23
23
  amountIn: BN,
24
24
  baseVault: BN,
25
25
  quoteVault: BN,
26
- fees: AmmFeeRates
26
+ fees: AmmFeeRates,
27
27
  ): AmmSellOutput => {
28
28
  // Gross output: (amountIn * quoteVault) / (baseVault + amountIn)
29
29
  const grossQuoteOut = amountIn.mul(quoteVault).div(baseVault.add(amountIn));
@@ -64,7 +64,7 @@ export const calculateAmmBuyInput = (
64
64
  amountOut: BN,
65
65
  baseVault: BN,
66
66
  quoteVault: BN,
67
- fees: AmmFeeRates
67
+ fees: AmmFeeRates,
68
68
  ): AmmBuyInput => {
69
69
  const denom = new BN(FEE_RATE_DENOMINATOR);
70
70
 
@@ -119,7 +119,7 @@ export const calculateLpToTokens = (
119
119
  lpSupply: BN,
120
120
  baseVault: BN,
121
121
  quoteVault: BN,
122
- roundUp: boolean = false
122
+ roundUp: boolean = false,
123
123
  ): LpTokenAmounts => {
124
124
  let baseAmount: BN;
125
125
  let quoteAmount: BN;
@@ -21,13 +21,13 @@ export const calculateBuyExpectation = (
21
21
  curveState: { virtualQuoteReserves: BN; virtualTokenReserves: BN },
22
22
  config: FeeConfig,
23
23
  hasCreatorRef: boolean,
24
- hasTraderRef: boolean
24
+ hasTraderRef: boolean,
25
25
  ): BondingCurveBuyResult => {
26
26
  const fees = calculateFees(
27
27
  amountInGross,
28
28
  config,
29
29
  hasCreatorRef,
30
- hasTraderRef
30
+ hasTraderRef,
31
31
  );
32
32
 
33
33
  // Net quote amount that goes into the curve after fees
@@ -35,7 +35,7 @@ export const calculateBuyExpectation = (
35
35
 
36
36
  // Constant product: k = x * y
37
37
  const k = curveState.virtualQuoteReserves.mul(
38
- curveState.virtualTokenReserves
38
+ curveState.virtualTokenReserves,
39
39
  );
40
40
 
41
41
  // New quote reserves after adding net amount
@@ -80,11 +80,11 @@ export const calculateSellExpectation = (
80
80
  curveState: { virtualQuoteReserves: BN; virtualTokenReserves: BN },
81
81
  config: FeeConfig,
82
82
  hasCreatorRef: boolean,
83
- hasTraderRef: boolean
83
+ hasTraderRef: boolean,
84
84
  ): BondingCurveSellResult => {
85
85
  // Constant product: k = x * y
86
86
  const k = curveState.virtualQuoteReserves.mul(
87
- curveState.virtualTokenReserves
87
+ curveState.virtualTokenReserves,
88
88
  );
89
89
 
90
90
  // New token reserves after adding sold tokens
@@ -101,7 +101,7 @@ export const calculateSellExpectation = (
101
101
  quoteOutGross,
102
102
  config,
103
103
  hasCreatorRef,
104
- hasTraderRef
104
+ hasTraderRef,
105
105
  );
106
106
 
107
107
  // Net quote amount user receives