@meteora-ag/dynamic-bonding-curve-sdk 1.4.8 → 1.4.10

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/index.js CHANGED
@@ -737,8 +737,17 @@ var getSqrtPriceFromPrice = (price, tokenADecimal, tokenBDecimal) => {
737
737
  const sqrtValueQ64 = sqrtValue.mul(Decimal2.pow(2, 64));
738
738
  return new BN6(sqrtValueQ64.floor().toFixed());
739
739
  };
740
+ var createSqrtPrices = (prices, tokenBaseDecimal, tokenQuoteDecimal) => {
741
+ return prices.map(
742
+ (price) => getSqrtPriceFromPrice(
743
+ price.toString(),
744
+ tokenBaseDecimal,
745
+ tokenQuoteDecimal
746
+ )
747
+ );
748
+ };
740
749
  var getSqrtPriceFromMarketCap = (marketCap, totalSupply, tokenBaseDecimal, tokenQuoteDecimal) => {
741
- let price = new Decimal2(marketCap).div(new Decimal2(totalSupply));
750
+ const price = new Decimal2(marketCap).div(new Decimal2(totalSupply));
742
751
  return getSqrtPriceFromPrice(
743
752
  price.toString(),
744
753
  tokenBaseDecimal,
@@ -924,8 +933,8 @@ var getMigrationThresholdPrice = (migrationThreshold, sqrtStartPrice, curve) =>
924
933
  }
925
934
  }
926
935
  if (!amountLeft.isZero()) {
927
- let migrationThresholdStr = migrationThreshold.toString();
928
- let amountLeftStr = amountLeft.toString();
936
+ const migrationThresholdStr = migrationThreshold.toString();
937
+ const amountLeftStr = amountLeft.toString();
929
938
  throw Error(
930
939
  `Not enough liquidity, migrationThreshold: ${migrationThresholdStr} amountLeft: ${amountLeftStr}`
931
940
  );
@@ -933,6 +942,56 @@ var getMigrationThresholdPrice = (migrationThreshold, sqrtStartPrice, curve) =>
933
942
  }
934
943
  return nextSqrtPrice;
935
944
  };
945
+ var getCurveBreakdown = (migrationQuoteThreshold, sqrtStartPrice, curve) => {
946
+ if (curve.length === 0) {
947
+ throw Error("Curve is empty");
948
+ }
949
+ const segmentAmounts = [];
950
+ let totalAllocated = new BN6(0);
951
+ let currentSqrtPrice = sqrtStartPrice;
952
+ let finalSqrtPrice = sqrtStartPrice;
953
+ for (let i = 0; i < curve.length; i++) {
954
+ const lowerSqrtPrice = currentSqrtPrice;
955
+ const upperSqrtPrice = curve[i].sqrtPrice;
956
+ const liquidity = curve[i].liquidity;
957
+ const maxSegmentAmount = getDeltaAmountQuoteUnsigned(
958
+ lowerSqrtPrice,
959
+ upperSqrtPrice,
960
+ liquidity,
961
+ 0 /* Up */
962
+ );
963
+ if (maxSegmentAmount.gte(migrationQuoteThreshold)) {
964
+ segmentAmounts.push(migrationQuoteThreshold);
965
+ totalAllocated = totalAllocated.add(migrationQuoteThreshold);
966
+ finalSqrtPrice = getNextSqrtPriceFromInput(
967
+ lowerSqrtPrice,
968
+ liquidity,
969
+ migrationQuoteThreshold,
970
+ false
971
+ );
972
+ for (let j = i + 1; j < curve.length; j++) {
973
+ segmentAmounts.push(new BN6(0));
974
+ }
975
+ break;
976
+ } else {
977
+ segmentAmounts.push(maxSegmentAmount);
978
+ totalAllocated = totalAllocated.add(maxSegmentAmount);
979
+ currentSqrtPrice = upperSqrtPrice;
980
+ finalSqrtPrice = upperSqrtPrice;
981
+ if (i === curve.length - 1 && totalAllocated.lt(migrationQuoteThreshold)) {
982
+ const shortfall = migrationQuoteThreshold.sub(totalAllocated);
983
+ throw Error(
984
+ `Not enough liquidity in curve. Total allocated: ${totalAllocated.toString()}, Required: ${migrationQuoteThreshold.toString()}, Shortfall: ${shortfall.toString()}`
985
+ );
986
+ }
987
+ }
988
+ }
989
+ return {
990
+ segmentAmounts,
991
+ finalSqrtPrice,
992
+ totalAmount: totalAllocated
993
+ };
994
+ };
936
995
  var getSwapAmountWithBuffer = (swapBaseAmount, sqrtStartPrice, curve) => {
937
996
  const swapAmountBuffer = swapBaseAmount.add(
938
997
  swapBaseAmount.mul(new BN6(SWAP_BUFFER_PERCENTAGE)).div(new BN6(100))
@@ -954,6 +1013,21 @@ var getPercentageSupplyOnMigration = (initialMarketCap, migrationMarketCap, lock
954
1013
  const denominator = new Decimal2(1).add(sqrtRatio);
955
1014
  return numerator.div(denominator).toNumber();
956
1015
  };
1016
+ function calculateAdjustedPercentageSupplyOnMigration(initialMarketCap, migrationMarketCap, migrationFee, lockedVesting, totalLeftover, totalTokenSupply) {
1017
+ const D = new Decimal2(initialMarketCap);
1018
+ const M = new Decimal2(migrationMarketCap);
1019
+ const f = new Decimal2(migrationFee.feePercentage).div(100);
1020
+ const totalVestingAmount = getTotalVestingAmount(lockedVesting);
1021
+ const V = new Decimal2(totalVestingAmount.toString()).mul(100).div(new Decimal2(totalTokenSupply.toString()));
1022
+ const L = new Decimal2(totalLeftover.toString()).mul(100).div(new Decimal2(totalTokenSupply.toString()));
1023
+ const requiredRatio = Decimal2.sqrt(D.div(M));
1024
+ const oneMinusF = new Decimal2(1).sub(f);
1025
+ const availablePercentage = new Decimal2(100).sub(V).sub(L);
1026
+ const numerator = requiredRatio.mul(oneMinusF).mul(availablePercentage);
1027
+ const denominator = new Decimal2(1).add(requiredRatio.mul(oneMinusF));
1028
+ const percentageSupplyOnMigration = numerator.div(denominator).toNumber();
1029
+ return percentageSupplyOnMigration;
1030
+ }
957
1031
  var getMigrationQuoteAmount = (migrationMarketCap, percentageSupplyOnMigration) => {
958
1032
  return migrationMarketCap.mul(percentageSupplyOnMigration).div(new Decimal2(100));
959
1033
  };
@@ -1155,19 +1229,19 @@ function getLockedVestingParams(totalLockedVestingAmount, numberOfVestingPeriod,
1155
1229
  };
1156
1230
  }
1157
1231
  var getTwoCurve = (migrationSqrtPrice, midSqrtPrice, initialSqrtPrice, swapAmount, migrationQuoteThreshold) => {
1158
- let p0 = new Decimal2(initialSqrtPrice.toString());
1159
- let p1 = new Decimal2(midSqrtPrice.toString());
1160
- let p2 = new Decimal2(migrationSqrtPrice.toString());
1161
- let a1 = new Decimal2(1).div(p0).sub(new Decimal2(1).div(p1));
1162
- let b1 = new Decimal2(1).div(p1).sub(new Decimal2(1).div(p2));
1163
- let c1 = new Decimal2(swapAmount.toString());
1164
- let a2 = p1.sub(p0);
1165
- let b2 = p2.sub(p1);
1166
- let c2 = new Decimal2(migrationQuoteThreshold.toString()).mul(
1232
+ const p0 = new Decimal2(initialSqrtPrice.toString());
1233
+ const p1 = new Decimal2(midSqrtPrice.toString());
1234
+ const p2 = new Decimal2(migrationSqrtPrice.toString());
1235
+ const a1 = new Decimal2(1).div(p0).sub(new Decimal2(1).div(p1));
1236
+ const b1 = new Decimal2(1).div(p1).sub(new Decimal2(1).div(p2));
1237
+ const c1 = new Decimal2(swapAmount.toString());
1238
+ const a2 = p1.sub(p0);
1239
+ const b2 = p2.sub(p1);
1240
+ const c2 = new Decimal2(migrationQuoteThreshold.toString()).mul(
1167
1241
  Decimal2.pow(2, 128)
1168
1242
  );
1169
- let l0 = c1.mul(b2).sub(c2.mul(b1)).div(a1.mul(b2).sub(a2.mul(b1)));
1170
- let l1 = c1.mul(a2).sub(c2.mul(a1)).div(b1.mul(a2).sub(b2.mul(a1)));
1243
+ const l0 = c1.mul(b2).sub(c2.mul(b1)).div(a1.mul(b2).sub(a2.mul(b1)));
1244
+ const l1 = c1.mul(a2).sub(c2.mul(a1)).div(b1.mul(a2).sub(b2.mul(a1)));
1171
1245
  if (l0.isNeg() || l1.isNeg()) {
1172
1246
  return {
1173
1247
  isOk: false,
@@ -3241,7 +3315,7 @@ function validateMigrationFee(migrationFee) {
3241
3315
  import Decimal4 from "decimal.js";
3242
3316
  import BN14 from "bn.js";
3243
3317
  function buildCurve(buildCurveParam) {
3244
- let {
3318
+ const {
3245
3319
  totalTokenSupply,
3246
3320
  percentageSupplyOnMigration,
3247
3321
  migrationQuoteThreshold,
@@ -3298,7 +3372,7 @@ function buildCurve(buildCurveParam) {
3298
3372
  const migrationPrice = new Decimal4(migrationQuoteAmount.toString()).div(
3299
3373
  new Decimal4(migrationBaseSupply.toString())
3300
3374
  );
3301
- let migrationQuoteThresholdInLamport = convertToLamports(
3375
+ const migrationQuoteThresholdInLamport = convertToLamports(
3302
3376
  migrationQuoteThreshold,
3303
3377
  tokenQuoteDecimal
3304
3378
  );
@@ -3308,7 +3382,7 @@ function buildCurve(buildCurveParam) {
3308
3382
  tokenBaseDecimal,
3309
3383
  tokenQuoteDecimal
3310
3384
  );
3311
- let migrationQuoteAmountInLamport = fromDecimalToBN(
3385
+ const migrationQuoteAmountInLamport = fromDecimalToBN(
3312
3386
  migrationQuoteAmount.mul(new Decimal4(10 ** tokenQuoteDecimal))
3313
3387
  );
3314
3388
  const migrationBaseAmount = getMigrationBaseToken(
@@ -3411,7 +3485,14 @@ function buildCurveWithMarketCap(buildCurveWithMarketCapParam) {
3411
3485
  );
3412
3486
  const totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3413
3487
  const totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3414
- const percentageSupplyOnMigration = getPercentageSupplyOnMigration(
3488
+ const percentageSupplyOnMigration = migrationFee.feePercentage > 0 ? calculateAdjustedPercentageSupplyOnMigration(
3489
+ initialMarketCap,
3490
+ migrationMarketCap,
3491
+ migrationFee,
3492
+ lockedVesting,
3493
+ totalLeftover,
3494
+ totalSupply
3495
+ ) : getPercentageSupplyOnMigration(
3415
3496
  new Decimal4(initialMarketCap),
3416
3497
  new Decimal4(migrationMarketCap),
3417
3498
  lockedVesting,
@@ -3482,57 +3563,57 @@ function buildCurveWithTwoSegments(buildCurveWithTwoSegmentsParam) {
3482
3563
  migrationFeeOption,
3483
3564
  migratedPoolFee
3484
3565
  );
3485
- let migrationBaseSupply = new BN14(totalTokenSupply).mul(new BN14(percentageSupplyOnMigration)).div(new BN14(100));
3486
- let totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3487
- let migrationQuoteAmount = getMigrationQuoteAmount(
3566
+ const migrationBaseSupply = new BN14(totalTokenSupply).mul(new BN14(percentageSupplyOnMigration)).div(new BN14(100));
3567
+ const totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3568
+ const migrationQuoteAmount = getMigrationQuoteAmount(
3488
3569
  new Decimal4(migrationMarketCap),
3489
3570
  new Decimal4(percentageSupplyOnMigration)
3490
3571
  );
3491
- let migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3572
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3492
3573
  migrationQuoteAmount,
3493
3574
  new Decimal4(migrationFee.feePercentage)
3494
3575
  );
3495
- let migrationPrice = migrationQuoteAmount.div(
3576
+ const migrationPrice = migrationQuoteAmount.div(
3496
3577
  new Decimal4(migrationBaseSupply.toString())
3497
3578
  );
3498
- let migrationQuoteThresholdInLamport = fromDecimalToBN(
3579
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
3499
3580
  migrationQuoteThreshold.mul(new Decimal4(10 ** tokenQuoteDecimal))
3500
3581
  );
3501
- let migrationQuoteAmountInLamport = fromDecimalToBN(
3582
+ const migrationQuoteAmountInLamport = fromDecimalToBN(
3502
3583
  migrationQuoteAmount.mul(new Decimal4(10 ** tokenQuoteDecimal))
3503
3584
  );
3504
- let migrateSqrtPrice = getSqrtPriceFromPrice(
3585
+ const migrateSqrtPrice = getSqrtPriceFromPrice(
3505
3586
  migrationPrice.toString(),
3506
3587
  tokenBaseDecimal,
3507
3588
  tokenQuoteDecimal
3508
3589
  );
3509
- let migrationBaseAmount = getMigrationBaseToken(
3590
+ const migrationBaseAmount = getMigrationBaseToken(
3510
3591
  migrationQuoteAmountInLamport,
3511
3592
  migrateSqrtPrice,
3512
3593
  migrationOption
3513
3594
  );
3514
- let totalVestingAmount = getTotalVestingAmount(lockedVesting);
3515
- let totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3516
- let swapAmount = totalSupply.sub(migrationBaseAmount).sub(totalVestingAmount).sub(totalLeftover);
3517
- let initialSqrtPrice = getSqrtPriceFromMarketCap(
3595
+ const totalVestingAmount = getTotalVestingAmount(lockedVesting);
3596
+ const totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3597
+ const swapAmount = totalSupply.sub(migrationBaseAmount).sub(totalVestingAmount).sub(totalLeftover);
3598
+ const initialSqrtPrice = getSqrtPriceFromMarketCap(
3518
3599
  initialMarketCap,
3519
3600
  totalTokenSupply,
3520
3601
  tokenBaseDecimal,
3521
3602
  tokenQuoteDecimal
3522
3603
  );
3523
- let midSqrtPriceDecimal1 = new Decimal4(migrateSqrtPrice.toString()).mul(new Decimal4(initialSqrtPrice.toString())).sqrt();
3524
- let midSqrtPrice1 = new BN14(midSqrtPriceDecimal1.floor().toFixed());
3525
- let numerator1 = new Decimal4(initialSqrtPrice.toString());
3526
- let numerator2 = Decimal4.pow(migrateSqrtPrice.toString(), 3);
3527
- let product1 = numerator1.mul(numerator2);
3528
- let midSqrtPriceDecimal2 = Decimal4.pow(product1, 0.25);
3529
- let midSqrtPrice2 = new BN14(midSqrtPriceDecimal2.floor().toFixed());
3530
- let numerator3 = Decimal4.pow(initialSqrtPrice.toString(), 3);
3531
- let numerator4 = new Decimal4(migrateSqrtPrice.toString());
3532
- let product2 = numerator3.mul(numerator4);
3533
- let midSqrtPriceDecimal3 = Decimal4.pow(product2, 0.25);
3534
- let midSqrtPrice3 = new BN14(midSqrtPriceDecimal3.floor().toFixed());
3535
- let midPrices = [midSqrtPrice1, midSqrtPrice2, midSqrtPrice3];
3604
+ const midSqrtPriceDecimal1 = new Decimal4(migrateSqrtPrice.toString()).mul(new Decimal4(initialSqrtPrice.toString())).sqrt();
3605
+ const midSqrtPrice1 = new BN14(midSqrtPriceDecimal1.floor().toFixed());
3606
+ const numerator1 = new Decimal4(initialSqrtPrice.toString());
3607
+ const numerator2 = Decimal4.pow(migrateSqrtPrice.toString(), 3);
3608
+ const product1 = numerator1.mul(numerator2);
3609
+ const midSqrtPriceDecimal2 = Decimal4.pow(product1, 0.25);
3610
+ const midSqrtPrice2 = new BN14(midSqrtPriceDecimal2.floor().toFixed());
3611
+ const numerator3 = Decimal4.pow(initialSqrtPrice.toString(), 3);
3612
+ const numerator4 = new Decimal4(migrateSqrtPrice.toString());
3613
+ const product2 = numerator3.mul(numerator4);
3614
+ const midSqrtPriceDecimal3 = Decimal4.pow(product2, 0.25);
3615
+ const midSqrtPrice3 = new BN14(midSqrtPriceDecimal3.floor().toFixed());
3616
+ const midPrices = [midSqrtPrice3, midSqrtPrice2, midSqrtPrice1];
3536
3617
  let sqrtStartPrice = new BN14(0);
3537
3618
  let curve = [];
3538
3619
  for (let i = 0; i < midPrices.length; i++) {
@@ -3549,7 +3630,166 @@ function buildCurveWithTwoSegments(buildCurveWithTwoSegmentsParam) {
3549
3630
  break;
3550
3631
  }
3551
3632
  }
3552
- let totalDynamicSupply = getTotalSupplyFromCurve(
3633
+ const totalDynamicSupply = getTotalSupplyFromCurve(
3634
+ migrationQuoteThresholdInLamport,
3635
+ sqrtStartPrice,
3636
+ curve,
3637
+ lockedVesting,
3638
+ migrationOption,
3639
+ totalLeftover,
3640
+ migrationFee.feePercentage
3641
+ );
3642
+ if (totalDynamicSupply.gt(totalSupply)) {
3643
+ const leftOverDelta = totalDynamicSupply.sub(totalSupply);
3644
+ if (!leftOverDelta.lt(totalLeftover)) {
3645
+ throw new Error("leftOverDelta must be less than totalLeftover");
3646
+ }
3647
+ }
3648
+ const instructionParams = {
3649
+ poolFees: {
3650
+ baseFee: {
3651
+ ...baseFee
3652
+ },
3653
+ dynamicFee: dynamicFeeEnabled ? getDynamicFeeParams(
3654
+ baseFeeParams.baseFeeMode === 2 /* RateLimiter */ ? baseFeeParams.rateLimiterParam.baseFeeBps : baseFeeParams.feeSchedulerParam.endingFeeBps
3655
+ ) : null
3656
+ },
3657
+ activationType,
3658
+ collectFeeMode,
3659
+ migrationOption,
3660
+ tokenType,
3661
+ tokenDecimal: tokenBaseDecimal,
3662
+ migrationQuoteThreshold: migrationQuoteThresholdInLamport,
3663
+ partnerLpPercentage,
3664
+ creatorLpPercentage,
3665
+ partnerLockedLpPercentage,
3666
+ creatorLockedLpPercentage,
3667
+ sqrtStartPrice,
3668
+ lockedVesting,
3669
+ migrationFeeOption,
3670
+ tokenSupply: {
3671
+ preMigrationTokenSupply: totalSupply,
3672
+ postMigrationTokenSupply: totalSupply
3673
+ },
3674
+ creatorTradingFeePercentage,
3675
+ migratedPoolFee: {
3676
+ collectFeeMode: migratedPoolFeeParams.collectFeeMode,
3677
+ dynamicFee: migratedPoolFeeParams.dynamicFee,
3678
+ poolFeeBps: migratedPoolFeeParams.poolFeeBps
3679
+ },
3680
+ padding: [],
3681
+ curve,
3682
+ tokenUpdateAuthority,
3683
+ migrationFee
3684
+ };
3685
+ return instructionParams;
3686
+ }
3687
+ function buildCurveWithMidPrice(buildCurveWithMidPriceParam) {
3688
+ const {
3689
+ totalTokenSupply,
3690
+ initialMarketCap,
3691
+ migrationMarketCap,
3692
+ midPrice,
3693
+ percentageSupplyOnMigration,
3694
+ migrationOption,
3695
+ tokenBaseDecimal,
3696
+ tokenQuoteDecimal,
3697
+ creatorTradingFeePercentage,
3698
+ collectFeeMode,
3699
+ leftover,
3700
+ tokenType,
3701
+ partnerLpPercentage,
3702
+ creatorLpPercentage,
3703
+ partnerLockedLpPercentage,
3704
+ creatorLockedLpPercentage,
3705
+ activationType,
3706
+ dynamicFeeEnabled,
3707
+ migrationFeeOption,
3708
+ migrationFee,
3709
+ tokenUpdateAuthority,
3710
+ baseFeeParams,
3711
+ migratedPoolFee
3712
+ } = buildCurveWithMidPriceParam;
3713
+ const baseFee = getBaseFeeParams(
3714
+ baseFeeParams,
3715
+ tokenQuoteDecimal,
3716
+ activationType
3717
+ );
3718
+ const {
3719
+ totalLockedVestingAmount,
3720
+ numberOfVestingPeriod,
3721
+ cliffUnlockAmount,
3722
+ totalVestingDuration,
3723
+ cliffDurationFromMigrationTime
3724
+ } = buildCurveWithMidPriceParam.lockedVestingParam;
3725
+ const lockedVesting = getLockedVestingParams(
3726
+ totalLockedVestingAmount,
3727
+ numberOfVestingPeriod,
3728
+ cliffUnlockAmount,
3729
+ totalVestingDuration,
3730
+ cliffDurationFromMigrationTime,
3731
+ tokenBaseDecimal
3732
+ );
3733
+ const migratedPoolFeeParams = getMigratedPoolFeeParams(
3734
+ migrationOption,
3735
+ migrationFeeOption,
3736
+ migratedPoolFee
3737
+ );
3738
+ const migrationBaseSupply = new BN14(totalTokenSupply).mul(new BN14(percentageSupplyOnMigration)).div(new BN14(100));
3739
+ const totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3740
+ const migrationQuoteAmount = getMigrationQuoteAmount(
3741
+ new Decimal4(migrationMarketCap),
3742
+ new Decimal4(percentageSupplyOnMigration)
3743
+ );
3744
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3745
+ migrationQuoteAmount,
3746
+ new Decimal4(migrationFee.feePercentage)
3747
+ );
3748
+ const migrationPrice = migrationQuoteAmount.div(
3749
+ new Decimal4(migrationBaseSupply.toString())
3750
+ );
3751
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
3752
+ migrationQuoteThreshold.mul(new Decimal4(10 ** tokenQuoteDecimal))
3753
+ );
3754
+ const migrationQuoteAmountInLamport = fromDecimalToBN(
3755
+ migrationQuoteAmount.mul(new Decimal4(10 ** tokenQuoteDecimal))
3756
+ );
3757
+ const migrateSqrtPrice = getSqrtPriceFromPrice(
3758
+ migrationPrice.toString(),
3759
+ tokenBaseDecimal,
3760
+ tokenQuoteDecimal
3761
+ );
3762
+ const migrationBaseAmount = getMigrationBaseToken(
3763
+ migrationQuoteAmountInLamport,
3764
+ migrateSqrtPrice,
3765
+ migrationOption
3766
+ );
3767
+ const totalVestingAmount = getTotalVestingAmount(lockedVesting);
3768
+ const totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3769
+ const swapAmount = totalSupply.sub(migrationBaseAmount).sub(totalVestingAmount).sub(totalLeftover);
3770
+ const initialSqrtPrice = getSqrtPriceFromMarketCap(
3771
+ initialMarketCap,
3772
+ totalTokenSupply,
3773
+ tokenBaseDecimal,
3774
+ tokenQuoteDecimal
3775
+ );
3776
+ const midSqrtPrice = getSqrtPriceFromPrice(
3777
+ midPrice.toString(),
3778
+ tokenBaseDecimal,
3779
+ tokenQuoteDecimal
3780
+ );
3781
+ let sqrtStartPrice = new BN14(0);
3782
+ let curve = [];
3783
+ const result = getTwoCurve(
3784
+ migrateSqrtPrice,
3785
+ midSqrtPrice,
3786
+ initialSqrtPrice,
3787
+ swapAmount,
3788
+ migrationQuoteThresholdInLamport
3789
+ );
3790
+ curve = result.curve;
3791
+ sqrtStartPrice = result.sqrtStartPrice;
3792
+ const totalDynamicSupply = getTotalSupplyFromCurve(
3553
3793
  migrationQuoteThresholdInLamport,
3554
3794
  sqrtStartPrice,
3555
3795
  curve,
@@ -3559,7 +3799,7 @@ function buildCurveWithTwoSegments(buildCurveWithTwoSegmentsParam) {
3559
3799
  migrationFee.feePercentage
3560
3800
  );
3561
3801
  if (totalDynamicSupply.gt(totalSupply)) {
3562
- let leftOverDelta = totalDynamicSupply.sub(totalSupply);
3802
+ const leftOverDelta = totalDynamicSupply.sub(totalSupply);
3563
3803
  if (!leftOverDelta.lt(totalLeftover)) {
3564
3804
  throw new Error("leftOverDelta must be less than totalLeftover");
3565
3805
  }
@@ -3604,7 +3844,7 @@ function buildCurveWithTwoSegments(buildCurveWithTwoSegmentsParam) {
3604
3844
  return instructionParams;
3605
3845
  }
3606
3846
  function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3607
- let {
3847
+ const {
3608
3848
  totalTokenSupply,
3609
3849
  migrationOption,
3610
3850
  tokenBaseDecimal,
@@ -3653,23 +3893,23 @@ function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3653
3893
  migrationFeeOption,
3654
3894
  migratedPoolFee
3655
3895
  );
3656
- let pMin = getSqrtPriceFromMarketCap(
3896
+ const pMin = getSqrtPriceFromMarketCap(
3657
3897
  initialMarketCap,
3658
3898
  totalTokenSupply,
3659
3899
  tokenBaseDecimal,
3660
3900
  tokenQuoteDecimal
3661
3901
  );
3662
- let pMax = getSqrtPriceFromMarketCap(
3902
+ const pMax = getSqrtPriceFromMarketCap(
3663
3903
  migrationMarketCap,
3664
3904
  totalTokenSupply,
3665
3905
  tokenBaseDecimal,
3666
3906
  tokenQuoteDecimal
3667
3907
  );
3668
- let priceRatio = new Decimal4(pMax.toString()).div(
3908
+ const priceRatio = new Decimal4(pMax.toString()).div(
3669
3909
  new Decimal4(pMin.toString())
3670
3910
  );
3671
- let qDecimal = priceRatio.pow(new Decimal4(1).div(new Decimal4(16)));
3672
- let sqrtPrices = [];
3911
+ const qDecimal = priceRatio.pow(new Decimal4(1).div(new Decimal4(16)));
3912
+ const sqrtPrices = [];
3673
3913
  let currentPrice = pMin;
3674
3914
  for (let i = 0; i < 17; i++) {
3675
3915
  sqrtPrices.push(currentPrice);
@@ -3677,49 +3917,217 @@ function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3677
3917
  qDecimal.mul(new Decimal4(currentPrice.toString()))
3678
3918
  );
3679
3919
  }
3680
- let totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3681
- let totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3682
- let totalVestingAmount = getTotalVestingAmount(lockedVesting);
3683
- let totalSwapAndMigrationAmount = totalSupply.sub(totalVestingAmount).sub(totalLeftover);
3920
+ const totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3921
+ const totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3922
+ const totalVestingAmount = getTotalVestingAmount(lockedVesting);
3923
+ const totalSwapAndMigrationAmount = totalSupply.sub(totalVestingAmount).sub(totalLeftover);
3684
3924
  let sumFactor = new Decimal4(0);
3685
- let pmaxWeight = new Decimal4(pMax.toString());
3686
- let migrationFeeFactor = new Decimal4(100).sub(new Decimal4(migrationFee.feePercentage)).div(new Decimal4(100));
3925
+ const pmaxWeight = new Decimal4(pMax.toString());
3926
+ const migrationFeeFactor = new Decimal4(100).sub(new Decimal4(migrationFee.feePercentage)).div(new Decimal4(100));
3687
3927
  for (let i = 1; i < 17; i++) {
3688
- let pi = new Decimal4(sqrtPrices[i].toString());
3689
- let piMinus = new Decimal4(sqrtPrices[i - 1].toString());
3690
- let k = new Decimal4(liquidityWeights[i - 1]);
3691
- let w1 = pi.sub(piMinus).div(pi.mul(piMinus));
3692
- let w2 = pi.sub(piMinus).mul(migrationFeeFactor).div(pmaxWeight.mul(pmaxWeight));
3693
- let weight = k.mul(w1.add(w2));
3928
+ const pi = new Decimal4(sqrtPrices[i].toString());
3929
+ const piMinus = new Decimal4(sqrtPrices[i - 1].toString());
3930
+ const k = new Decimal4(liquidityWeights[i - 1]);
3931
+ const w1 = pi.sub(piMinus).div(pi.mul(piMinus));
3932
+ const w2 = pi.sub(piMinus).mul(migrationFeeFactor).div(pmaxWeight.mul(pmaxWeight));
3933
+ const weight = k.mul(w1.add(w2));
3694
3934
  sumFactor = sumFactor.add(weight);
3695
3935
  }
3696
- let l1 = new Decimal4(totalSwapAndMigrationAmount.toString()).div(sumFactor);
3697
- let curve = [];
3936
+ const l1 = new Decimal4(totalSwapAndMigrationAmount.toString()).div(
3937
+ sumFactor
3938
+ );
3939
+ const curve = [];
3698
3940
  for (let i = 0; i < 16; i++) {
3699
- let k = new Decimal4(liquidityWeights[i]);
3700
- let liquidity = convertDecimalToBN(l1.mul(k));
3701
- let sqrtPrice = i < 15 ? sqrtPrices[i + 1] : pMax;
3941
+ const k = new Decimal4(liquidityWeights[i]);
3942
+ const liquidity = convertDecimalToBN(l1.mul(k));
3943
+ const sqrtPrice = i < 15 ? sqrtPrices[i + 1] : pMax;
3944
+ curve.push({
3945
+ sqrtPrice,
3946
+ liquidity
3947
+ });
3948
+ }
3949
+ const swapBaseAmount = getBaseTokenForSwap(pMin, pMax, curve);
3950
+ const swapBaseAmountBuffer = getSwapAmountWithBuffer(
3951
+ swapBaseAmount,
3952
+ pMin,
3953
+ curve
3954
+ );
3955
+ const migrationAmount = totalSwapAndMigrationAmount.sub(swapBaseAmountBuffer);
3956
+ const migrationQuoteAmount = migrationAmount.mul(pMax).mul(pMax).shrn(128);
3957
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3958
+ new Decimal4(migrationQuoteAmount.toString()),
3959
+ new Decimal4(migrationFee.feePercentage)
3960
+ );
3961
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
3962
+ migrationQuoteThreshold
3963
+ );
3964
+ const totalDynamicSupply = getTotalSupplyFromCurve(
3965
+ migrationQuoteThresholdInLamport,
3966
+ pMin,
3967
+ curve,
3968
+ lockedVesting,
3969
+ migrationOption,
3970
+ totalLeftover,
3971
+ migrationFee.feePercentage
3972
+ );
3973
+ if (totalDynamicSupply.gt(totalSupply)) {
3974
+ const leftOverDelta = totalDynamicSupply.sub(totalSupply);
3975
+ if (!leftOverDelta.lt(totalLeftover)) {
3976
+ throw new Error("leftOverDelta must be less than totalLeftover");
3977
+ }
3978
+ }
3979
+ const instructionParams = {
3980
+ poolFees: {
3981
+ baseFee: {
3982
+ ...baseFee
3983
+ },
3984
+ dynamicFee: dynamicFeeEnabled ? getDynamicFeeParams(
3985
+ baseFeeParams.baseFeeMode === 2 /* RateLimiter */ ? baseFeeParams.rateLimiterParam.baseFeeBps : baseFeeParams.feeSchedulerParam.endingFeeBps
3986
+ ) : null
3987
+ },
3988
+ activationType,
3989
+ collectFeeMode,
3990
+ migrationOption,
3991
+ tokenType,
3992
+ tokenDecimal: tokenBaseDecimal,
3993
+ migrationQuoteThreshold: migrationQuoteThresholdInLamport,
3994
+ partnerLpPercentage,
3995
+ creatorLpPercentage,
3996
+ partnerLockedLpPercentage,
3997
+ creatorLockedLpPercentage,
3998
+ sqrtStartPrice: pMin,
3999
+ lockedVesting,
4000
+ migrationFeeOption,
4001
+ tokenSupply: {
4002
+ preMigrationTokenSupply: totalSupply,
4003
+ postMigrationTokenSupply: totalSupply
4004
+ },
4005
+ creatorTradingFeePercentage,
4006
+ migratedPoolFee: {
4007
+ collectFeeMode: migratedPoolFeeParams.collectFeeMode,
4008
+ dynamicFee: migratedPoolFeeParams.dynamicFee,
4009
+ poolFeeBps: migratedPoolFeeParams.poolFeeBps
4010
+ },
4011
+ padding: [],
4012
+ curve,
4013
+ migrationFee,
4014
+ tokenUpdateAuthority
4015
+ };
4016
+ return instructionParams;
4017
+ }
4018
+ function buildCurveWithCustomSqrtPrices(buildCurveWithCustomSqrtPricesParam) {
4019
+ const {
4020
+ totalTokenSupply,
4021
+ migrationOption,
4022
+ tokenBaseDecimal,
4023
+ tokenQuoteDecimal,
4024
+ dynamicFeeEnabled,
4025
+ activationType,
4026
+ collectFeeMode,
4027
+ migrationFeeOption,
4028
+ tokenType,
4029
+ partnerLpPercentage,
4030
+ creatorLpPercentage,
4031
+ partnerLockedLpPercentage,
4032
+ creatorLockedLpPercentage,
4033
+ creatorTradingFeePercentage,
4034
+ leftover,
4035
+ sqrtPrices,
4036
+ migrationFee,
4037
+ tokenUpdateAuthority,
4038
+ baseFeeParams,
4039
+ migratedPoolFee
4040
+ } = buildCurveWithCustomSqrtPricesParam;
4041
+ let { liquidityWeights } = buildCurveWithCustomSqrtPricesParam;
4042
+ if (sqrtPrices.length < 2) {
4043
+ throw new Error("sqrtPrices array must have at least 2 elements");
4044
+ }
4045
+ for (let i = 1; i < sqrtPrices.length; i++) {
4046
+ if (sqrtPrices[i].lte(sqrtPrices[i - 1])) {
4047
+ throw new Error("sqrtPrices must be in ascending order");
4048
+ }
4049
+ }
4050
+ if (!liquidityWeights) {
4051
+ const numSegments2 = sqrtPrices.length - 1;
4052
+ liquidityWeights = Array(numSegments2).fill(1);
4053
+ } else if (liquidityWeights.length !== sqrtPrices.length - 1) {
4054
+ throw new Error(
4055
+ "liquidityWeights length must equal sqrtPrices.length - 1"
4056
+ );
4057
+ }
4058
+ const baseFee = getBaseFeeParams(
4059
+ baseFeeParams,
4060
+ tokenQuoteDecimal,
4061
+ activationType
4062
+ );
4063
+ const {
4064
+ totalLockedVestingAmount,
4065
+ numberOfVestingPeriod,
4066
+ cliffUnlockAmount,
4067
+ totalVestingDuration,
4068
+ cliffDurationFromMigrationTime
4069
+ } = buildCurveWithCustomSqrtPricesParam.lockedVestingParam;
4070
+ const lockedVesting = getLockedVestingParams(
4071
+ totalLockedVestingAmount,
4072
+ numberOfVestingPeriod,
4073
+ cliffUnlockAmount,
4074
+ totalVestingDuration,
4075
+ cliffDurationFromMigrationTime,
4076
+ tokenBaseDecimal
4077
+ );
4078
+ const migratedPoolFeeParams = getMigratedPoolFeeParams(
4079
+ migrationOption,
4080
+ migrationFeeOption,
4081
+ migratedPoolFee
4082
+ );
4083
+ const pMin = sqrtPrices[0];
4084
+ const pMax = sqrtPrices[sqrtPrices.length - 1];
4085
+ const totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
4086
+ const totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
4087
+ const totalVestingAmount = getTotalVestingAmount(lockedVesting);
4088
+ const totalSwapAndMigrationAmount = totalSupply.sub(totalVestingAmount).sub(totalLeftover);
4089
+ let sumFactor = new Decimal4(0);
4090
+ const pmaxWeight = new Decimal4(pMax.toString());
4091
+ const migrationFeeFactor = new Decimal4(100).sub(new Decimal4(migrationFee.feePercentage)).div(new Decimal4(100));
4092
+ const numSegments = sqrtPrices.length - 1;
4093
+ for (let i = 0; i < numSegments; i++) {
4094
+ const pi = new Decimal4(sqrtPrices[i + 1].toString());
4095
+ const piMinus = new Decimal4(sqrtPrices[i].toString());
4096
+ const k = new Decimal4(liquidityWeights[i]);
4097
+ const w1 = pi.sub(piMinus).div(pi.mul(piMinus));
4098
+ const w2 = pi.sub(piMinus).mul(migrationFeeFactor).div(pmaxWeight.mul(pmaxWeight));
4099
+ const weight = k.mul(w1.add(w2));
4100
+ sumFactor = sumFactor.add(weight);
4101
+ }
4102
+ const l1 = new Decimal4(totalSwapAndMigrationAmount.toString()).div(
4103
+ sumFactor
4104
+ );
4105
+ const curve = [];
4106
+ for (let i = 0; i < numSegments; i++) {
4107
+ const k = new Decimal4(liquidityWeights[i]);
4108
+ const liquidity = convertDecimalToBN(l1.mul(k));
4109
+ const sqrtPrice = sqrtPrices[i + 1];
3702
4110
  curve.push({
3703
4111
  sqrtPrice,
3704
4112
  liquidity
3705
4113
  });
3706
4114
  }
3707
- let swapBaseAmount = getBaseTokenForSwap(pMin, pMax, curve);
3708
- let swapBaseAmountBuffer = getSwapAmountWithBuffer(
4115
+ const swapBaseAmount = getBaseTokenForSwap(pMin, pMax, curve);
4116
+ const swapBaseAmountBuffer = getSwapAmountWithBuffer(
3709
4117
  swapBaseAmount,
3710
4118
  pMin,
3711
4119
  curve
3712
4120
  );
3713
- let migrationAmount = totalSwapAndMigrationAmount.sub(swapBaseAmountBuffer);
3714
- let migrationQuoteAmount = migrationAmount.mul(pMax).mul(pMax).shrn(128);
3715
- let migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
4121
+ const migrationAmount = totalSwapAndMigrationAmount.sub(swapBaseAmountBuffer);
4122
+ const migrationQuoteAmount = migrationAmount.mul(pMax).mul(pMax).shrn(128);
4123
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3716
4124
  new Decimal4(migrationQuoteAmount.toString()),
3717
4125
  new Decimal4(migrationFee.feePercentage)
3718
4126
  );
3719
- let migrationQuoteThresholdInLamport = fromDecimalToBN(
4127
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
3720
4128
  migrationQuoteThreshold
3721
4129
  );
3722
- let totalDynamicSupply = getTotalSupplyFromCurve(
4130
+ const totalDynamicSupply = getTotalSupplyFromCurve(
3723
4131
  migrationQuoteThresholdInLamport,
3724
4132
  pMin,
3725
4133
  curve,
@@ -3729,7 +4137,7 @@ function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3729
4137
  migrationFee.feePercentage
3730
4138
  );
3731
4139
  if (totalDynamicSupply.gt(totalSupply)) {
3732
- let leftOverDelta = totalDynamicSupply.sub(totalSupply);
4140
+ const leftOverDelta = totalDynamicSupply.sub(totalSupply);
3733
4141
  if (!leftOverDelta.lt(totalLeftover)) {
3734
4142
  throw new Error("leftOverDelta must be less than totalLeftover");
3735
4143
  }
@@ -26348,9 +26756,12 @@ export {
26348
26756
  VAULT_PROGRAM_ID,
26349
26757
  bpsToFeeNumerator,
26350
26758
  buildCurve,
26759
+ buildCurveWithCustomSqrtPrices,
26351
26760
  buildCurveWithLiquidityWeights,
26352
26761
  buildCurveWithMarketCap,
26762
+ buildCurveWithMidPrice,
26353
26763
  buildCurveWithTwoSegments,
26764
+ calculateAdjustedPercentageSupplyOnMigration,
26354
26765
  calculateBaseToQuoteFromAmountIn,
26355
26766
  calculateBaseToQuoteFromAmountOut,
26356
26767
  calculateFeeSchedulerEndingBaseFeeBps,
@@ -26366,6 +26777,7 @@ export {
26366
26777
  createInitializePermissionlessDynamicVaultIx,
26367
26778
  createLockEscrowIx,
26368
26779
  createProgramAccountFilter,
26780
+ createSqrtPrices,
26369
26781
  createVaultProgram,
26370
26782
  deriveBaseKeyForLocker,
26371
26783
  deriveDammV1EventAuthority,
@@ -26410,6 +26822,7 @@ export {
26410
26822
  getBaseTokenForSwap,
26411
26823
  getCheckedAmounts,
26412
26824
  getCurrentPoint,
26825
+ getCurveBreakdown,
26413
26826
  getDeltaAmountBaseUnsigned,
26414
26827
  getDeltaAmountBaseUnsigned256,
26415
26828
  getDeltaAmountBaseUnsignedUnchecked,