@liquid-af/sdk 0.2.0 → 0.3.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/dist/accounts/liquid-swap.d.ts +2 -0
- package/dist/accounts/liquid-swap.d.ts.map +1 -1
- package/dist/client.d.ts +2 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/helpers/preview.js.map +1 -1
- package/dist/idl/index.d.ts +1 -0
- package/dist/idl/index.d.ts.map +1 -1
- package/dist/idl/index.js +15 -4
- package/dist/idl/index.js.map +1 -1
- package/dist/idl/liquid.d.ts +1359 -1359
- package/dist/idl/liquid.d.ts.map +1 -1
- package/dist/idl/liquid.json +1359 -1359
- package/dist/idl/liquid_fees.d.ts +1 -1
- package/dist/idl/liquid_fees.json +1 -1
- package/dist/idl/liquid_state.d.ts +62 -62
- package/dist/idl/liquid_state.d.ts.map +1 -1
- package/dist/idl/liquid_state.json +62 -62
- package/dist/idl/liquid_swap.d.ts +597 -494
- package/dist/idl/liquid_swap.d.ts.map +1 -1
- package/dist/idl/liquid_swap.json +597 -494
- package/dist/idl/patch-idl.d.ts +10 -0
- package/dist/idl/patch-idl.d.ts.map +1 -0
- package/dist/idl/patch-idl.js +62 -0
- package/dist/idl/patch-idl.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/instructions/program-cache.d.ts.map +1 -1
- package/dist/instructions/program-cache.js +10 -6
- package/dist/instructions/program-cache.js.map +1 -1
- package/package.json +4 -4
- package/src/accounts/liquid-fees.ts +2 -2
- package/src/accounts/liquid-state.ts +7 -7
- package/src/accounts/liquid-swap.ts +4 -4
- package/src/accounts/liquid.ts +6 -6
- package/src/client.ts +31 -31
- package/src/config.ts +10 -10
- package/src/errors.ts +6 -6
- package/src/events/parser.ts +5 -5
- package/src/helpers/preview.ts +31 -31
- package/src/helpers/user.ts +1 -1
- package/src/idl/index.ts +31 -8
- package/src/idl/liquid.json +1359 -1359
- package/src/idl/liquid.ts +1359 -1359
- package/src/idl/liquid_fees.json +1 -1
- package/src/idl/liquid_fees.ts +1 -1
- package/src/idl/liquid_state.json +62 -62
- package/src/idl/liquid_state.ts +62 -62
- package/src/idl/liquid_swap.json +597 -494
- package/src/idl/liquid_swap.ts +597 -494
- package/src/idl/patch-idl.ts +80 -0
- package/src/index.ts +1 -0
- package/src/instructions/liquid-fees.ts +14 -14
- package/src/instructions/liquid-state.ts +4 -4
- package/src/instructions/liquid-swap.ts +18 -18
- package/src/instructions/liquid.ts +47 -47
- package/src/instructions/program-cache.ts +18 -9
- package/src/math/amm.ts +3 -3
- package/src/math/bonding-curve.ts +6 -6
- package/src/math/fees.ts +9 -9
- package/src/math/tiered-fees.ts +5 -5
- package/src/oracle.ts +3 -3
- package/src/pda/index.ts +27 -27
- package/src/pda/liquid-fees.ts +6 -6
- package/src/pda/liquid-state.ts +13 -13
- package/src/pda/liquid-swap.ts +16 -16
- package/src/pda/liquid.ts +15 -15
- package/src/provider.ts +2 -2
- package/src/transaction/builder.ts +4 -4
- package/src/transaction/send.ts +4 -4
- package/src/types.ts +2 -2
|
@@ -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,13 @@ 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
593
|
params.quoteMint,
|
|
594
|
-
params.config
|
|
594
|
+
params.config,
|
|
595
595
|
);
|
|
596
596
|
return buildBuyStable({ ...params, creator: curve.creator });
|
|
597
597
|
}
|
|
@@ -617,7 +617,7 @@ export interface BuildSellStableParams {
|
|
|
617
617
|
* @returns Transaction instruction
|
|
618
618
|
*/
|
|
619
619
|
export function buildSellStable(
|
|
620
|
-
params: BuildSellStableParams
|
|
620
|
+
params: BuildSellStableParams,
|
|
621
621
|
): Promise<TransactionInstruction> {
|
|
622
622
|
const {
|
|
623
623
|
user,
|
|
@@ -639,13 +639,13 @@ export function buildSellStable(
|
|
|
639
639
|
quoteMint,
|
|
640
640
|
feeRecipient,
|
|
641
641
|
true,
|
|
642
|
-
TOKEN_PROGRAM_ID
|
|
642
|
+
TOKEN_PROGRAM_ID,
|
|
643
643
|
);
|
|
644
644
|
|
|
645
645
|
// Derive creator-related accounts that Anchor can't auto-resolve
|
|
646
646
|
const [creatorUserProperties] = getUserPropertiesPDA(
|
|
647
647
|
creator,
|
|
648
|
-
config.liquidStateProgramId
|
|
648
|
+
config.liquidStateProgramId,
|
|
649
649
|
);
|
|
650
650
|
|
|
651
651
|
return program.methods
|
|
@@ -676,13 +676,13 @@ export function buildSellStable(
|
|
|
676
676
|
*/
|
|
677
677
|
export async function buildSellStableAutoResolve(
|
|
678
678
|
connection: Connection,
|
|
679
|
-
params: Omit<BuildSellStableParams, "creator"
|
|
679
|
+
params: Omit<BuildSellStableParams, "creator">,
|
|
680
680
|
): Promise<TransactionInstruction> {
|
|
681
681
|
const curve = await fetchStableBondingCurve(
|
|
682
682
|
connection,
|
|
683
683
|
params.mint,
|
|
684
684
|
params.quoteMint,
|
|
685
|
-
params.config
|
|
685
|
+
params.config,
|
|
686
686
|
);
|
|
687
687
|
return buildSellStable({ ...params, creator: curve.creator });
|
|
688
688
|
}
|
|
@@ -702,7 +702,7 @@ export interface BuildMigrateStableParams {
|
|
|
702
702
|
* @returns Transaction instruction
|
|
703
703
|
*/
|
|
704
704
|
export function buildMigrateStable(
|
|
705
|
-
params: BuildMigrateStableParams
|
|
705
|
+
params: BuildMigrateStableParams,
|
|
706
706
|
): Promise<TransactionInstruction> {
|
|
707
707
|
const { migrator, mint, quoteMint, config } = params;
|
|
708
708
|
const program = getCachedLiquidProgram(config);
|
|
@@ -711,33 +711,33 @@ export function buildMigrateStable(
|
|
|
711
711
|
const [bondingCurve] = getStableBondingCurvePDA(
|
|
712
712
|
mint,
|
|
713
713
|
quoteMint,
|
|
714
|
-
config.liquidProgramId
|
|
714
|
+
config.liquidProgramId,
|
|
715
715
|
);
|
|
716
716
|
const [ammPoolState] = getPoolPDA(
|
|
717
717
|
mint,
|
|
718
718
|
quoteMint,
|
|
719
|
-
config.liquidSwapProgramId
|
|
719
|
+
config.liquidSwapProgramId,
|
|
720
720
|
);
|
|
721
721
|
const [ammLpMint] = getPoolLpMintPDA(
|
|
722
722
|
ammPoolState,
|
|
723
|
-
config.liquidSwapProgramId
|
|
723
|
+
config.liquidSwapProgramId,
|
|
724
724
|
);
|
|
725
725
|
const [ammBaseVault] = getPoolVaultPDA(
|
|
726
726
|
ammPoolState,
|
|
727
727
|
mint,
|
|
728
|
-
config.liquidSwapProgramId
|
|
728
|
+
config.liquidSwapProgramId,
|
|
729
729
|
);
|
|
730
730
|
const [ammQuoteVault] = getPoolVaultPDA(
|
|
731
731
|
ammPoolState,
|
|
732
732
|
quoteMint,
|
|
733
|
-
config.liquidSwapProgramId
|
|
733
|
+
config.liquidSwapProgramId,
|
|
734
734
|
);
|
|
735
735
|
const [ammObservationState] = getObservationPDA(
|
|
736
736
|
ammPoolState,
|
|
737
|
-
config.liquidSwapProgramId
|
|
737
|
+
config.liquidSwapProgramId,
|
|
738
738
|
);
|
|
739
739
|
const [ammGlobalConfig] = getSwapGlobalConfigPDA(
|
|
740
|
-
config.liquidSwapProgramId
|
|
740
|
+
config.liquidSwapProgramId,
|
|
741
741
|
);
|
|
742
742
|
const [ammAuthority] = getSwapAuthorityPDA(config.liquidSwapProgramId);
|
|
743
743
|
const [tokenVolume] = getTokenVolumePDA(mint, config.liquidStateProgramId);
|
|
@@ -745,12 +745,12 @@ export function buildMigrateStable(
|
|
|
745
745
|
ammLpMint,
|
|
746
746
|
bondingCurve,
|
|
747
747
|
true,
|
|
748
|
-
TOKEN_PROGRAM_ID
|
|
748
|
+
TOKEN_PROGRAM_ID,
|
|
749
749
|
);
|
|
750
750
|
const [ammBuybackVault] = getSwapBuybackVaultPDA(
|
|
751
751
|
ammPoolState,
|
|
752
752
|
quoteMint,
|
|
753
|
-
config.liquidSwapProgramId
|
|
753
|
+
config.liquidSwapProgramId,
|
|
754
754
|
);
|
|
755
755
|
return program.methods
|
|
756
756
|
.migrateStable()
|
|
@@ -788,7 +788,7 @@ export interface BuildInitializeFeeRecipientVaultsParams {
|
|
|
788
788
|
* @returns Transaction instruction
|
|
789
789
|
*/
|
|
790
790
|
export function buildInitializeFeeRecipientVaults(
|
|
791
|
-
params: BuildInitializeFeeRecipientVaultsParams
|
|
791
|
+
params: BuildInitializeFeeRecipientVaultsParams,
|
|
792
792
|
): Promise<TransactionInstruction> {
|
|
793
793
|
const { payer, quoteMint, feeRecipients, config } = params;
|
|
794
794
|
|
|
@@ -831,7 +831,7 @@ export interface BuildInitializeReferralTokenVaultParams {
|
|
|
831
831
|
* @returns Transaction instruction
|
|
832
832
|
*/
|
|
833
833
|
export function buildInitializeReferralTokenVault(
|
|
834
|
-
params: BuildInitializeReferralTokenVaultParams
|
|
834
|
+
params: BuildInitializeReferralTokenVaultParams,
|
|
835
835
|
): Promise<TransactionInstruction> {
|
|
836
836
|
const { user, quoteMint, config } = params;
|
|
837
837
|
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
|
package/src/math/fees.ts
CHANGED
|
@@ -42,7 +42,7 @@ export const calculateFees = (
|
|
|
42
42
|
amount: BN,
|
|
43
43
|
config: FeeConfig,
|
|
44
44
|
hasCreatorRef: boolean,
|
|
45
|
-
hasTraderRef: boolean
|
|
45
|
+
hasTraderRef: boolean,
|
|
46
46
|
): FeeDistribution => {
|
|
47
47
|
const baseProtocolFee = calcBps(amount, config.protocolFeeBps);
|
|
48
48
|
const creatorFee = calcBps(amount, config.creatorFeeBps);
|
|
@@ -80,7 +80,7 @@ export const calculateFees = (
|
|
|
80
80
|
*/
|
|
81
81
|
export const calculateDistribution = (
|
|
82
82
|
vaultBalance: number,
|
|
83
|
-
recipients: FeeRecipient[]
|
|
83
|
+
recipients: FeeRecipient[],
|
|
84
84
|
): DistributionResult[] => {
|
|
85
85
|
const distributable = Math.max(0, vaultBalance - MIN_VAULT_BALANCE);
|
|
86
86
|
return recipients.map((r) => ({
|
|
@@ -98,11 +98,11 @@ export const calculateDistribution = (
|
|
|
98
98
|
*/
|
|
99
99
|
export const calculateTotalDistribution = (
|
|
100
100
|
vaultBalance: number,
|
|
101
|
-
recipients: FeeRecipient[]
|
|
101
|
+
recipients: FeeRecipient[],
|
|
102
102
|
): number => {
|
|
103
103
|
return calculateDistribution(vaultBalance, recipients).reduce(
|
|
104
104
|
(sum, r) => sum + r.amount,
|
|
105
|
-
0
|
|
105
|
+
0,
|
|
106
106
|
);
|
|
107
107
|
};
|
|
108
108
|
|
|
@@ -115,12 +115,12 @@ export const calculateTotalDistribution = (
|
|
|
115
115
|
*/
|
|
116
116
|
export const calculateDistributionDust = (
|
|
117
117
|
vaultBalance: number,
|
|
118
|
-
recipients: FeeRecipient[]
|
|
118
|
+
recipients: FeeRecipient[],
|
|
119
119
|
): number => {
|
|
120
120
|
const distributable = Math.max(0, vaultBalance - MIN_VAULT_BALANCE);
|
|
121
121
|
const totalDistributed = calculateTotalDistribution(
|
|
122
122
|
vaultBalance,
|
|
123
|
-
recipients
|
|
123
|
+
recipients,
|
|
124
124
|
);
|
|
125
125
|
return distributable - totalDistributed;
|
|
126
126
|
};
|
|
@@ -134,7 +134,7 @@ export const calculateDistributionDust = (
|
|
|
134
134
|
*/
|
|
135
135
|
export const createRecipients = (
|
|
136
136
|
pubkeys: PublicKey[],
|
|
137
|
-
bpsArray: number[]
|
|
137
|
+
bpsArray: number[],
|
|
138
138
|
): FeeRecipient[] => {
|
|
139
139
|
if (pubkeys.length !== bpsArray.length) {
|
|
140
140
|
throw new Error("pubkeys and bpsArray must have the same length");
|
|
@@ -154,11 +154,11 @@ export const createRecipients = (
|
|
|
154
154
|
*/
|
|
155
155
|
export const createEqualRecipients = (
|
|
156
156
|
count: number,
|
|
157
|
-
pubkeys: PublicKey[]
|
|
157
|
+
pubkeys: PublicKey[],
|
|
158
158
|
): FeeRecipient[] => {
|
|
159
159
|
if (pubkeys.length < count) {
|
|
160
160
|
throw new Error(
|
|
161
|
-
`Need at least ${count} pubkeys, got ${pubkeys.length}
|
|
161
|
+
`Need at least ${count} pubkeys, got ${pubkeys.length}`,
|
|
162
162
|
);
|
|
163
163
|
}
|
|
164
164
|
const bpsPerRecipient = Math.floor(BPS_DENOMINATOR / count);
|
package/src/math/tiered-fees.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { FeeTier } from "../types.js";
|
|
|
4
4
|
|
|
5
5
|
/** WSOL mint address (native SOL wrapped as SPL token) */
|
|
6
6
|
export const WSOL_MINT = new PublicKey(
|
|
7
|
-
"So11111111111111111111111111111111111111112"
|
|
7
|
+
"So11111111111111111111111111111111111111112",
|
|
8
8
|
);
|
|
9
9
|
|
|
10
10
|
/** Lamports per SOL constant */
|
|
@@ -47,7 +47,7 @@ export function calculatePoolMarketCap(
|
|
|
47
47
|
baseVaultAmount: BN,
|
|
48
48
|
baseTotalSupply: BN,
|
|
49
49
|
solPriceUsd: BN,
|
|
50
|
-
quoteMint: PublicKey
|
|
50
|
+
quoteMint: PublicKey,
|
|
51
51
|
): BN {
|
|
52
52
|
// Prevent division by zero
|
|
53
53
|
if (baseVaultAmount.isZero()) {
|
|
@@ -108,7 +108,7 @@ export function calculatePoolMarketCap(
|
|
|
108
108
|
*/
|
|
109
109
|
export function lookupFeeTier(
|
|
110
110
|
marketCapUsd: BN,
|
|
111
|
-
tiers: FeeTier[]
|
|
111
|
+
tiers: FeeTier[],
|
|
112
112
|
): [number, number, number] {
|
|
113
113
|
if (tiers.length === 0) {
|
|
114
114
|
throw new Error("No fee tiers configured");
|
|
@@ -151,14 +151,14 @@ export function calculateFeesForPool(
|
|
|
151
151
|
baseTotalSupply: BN,
|
|
152
152
|
solPriceUsd: BN,
|
|
153
153
|
quoteMint: PublicKey,
|
|
154
|
-
tiers: FeeTier[]
|
|
154
|
+
tiers: FeeTier[],
|
|
155
155
|
): [number, number, number] {
|
|
156
156
|
const marketCap = calculatePoolMarketCap(
|
|
157
157
|
quoteVaultAmount,
|
|
158
158
|
baseVaultAmount,
|
|
159
159
|
baseTotalSupply,
|
|
160
160
|
solPriceUsd,
|
|
161
|
-
quoteMint
|
|
161
|
+
quoteMint,
|
|
162
162
|
);
|
|
163
163
|
|
|
164
164
|
return lookupFeeTier(marketCap, tiers);
|
package/src/oracle.ts
CHANGED
|
@@ -31,7 +31,7 @@ export const USD_DECIMALS = 6;
|
|
|
31
31
|
*/
|
|
32
32
|
export function calculateVirtualSolReserves(
|
|
33
33
|
initialVirtualReserveUsd: bigint,
|
|
34
|
-
solPriceUsd: bigint
|
|
34
|
+
solPriceUsd: bigint,
|
|
35
35
|
): bigint {
|
|
36
36
|
// virtual_sol (lamports) = initial_virtual_reserve_usd / sol_price_usd * 10^9
|
|
37
37
|
return (initialVirtualReserveUsd * BigInt(1_000_000_000)) / solPriceUsd;
|
|
@@ -56,7 +56,7 @@ export function calculateNativeMarketCapUsd(
|
|
|
56
56
|
virtualSolReserves: bigint,
|
|
57
57
|
virtualTokenReserves: bigint,
|
|
58
58
|
tokenTotalSupply: bigint,
|
|
59
|
-
solPriceUsd: bigint
|
|
59
|
+
solPriceUsd: bigint,
|
|
60
60
|
): bigint {
|
|
61
61
|
// Convert virtual SOL reserves to USD value, then compute FDV
|
|
62
62
|
const virtualSolUsd =
|
|
@@ -83,7 +83,7 @@ export function calculateStableMarketCapUsd(
|
|
|
83
83
|
virtualQuoteReserves: bigint,
|
|
84
84
|
virtualTokenReserves: bigint,
|
|
85
85
|
tokenTotalSupply: bigint,
|
|
86
|
-
quoteDecimals: number = 6
|
|
86
|
+
quoteDecimals: number = 6,
|
|
87
87
|
): bigint {
|
|
88
88
|
const raw =
|
|
89
89
|
(virtualQuoteReserves * tokenTotalSupply) / virtualTokenReserves;
|