@meteora-ag/dynamic-bonding-curve-sdk 1.4.9 → 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
  );
@@ -1004,6 +1013,21 @@ var getPercentageSupplyOnMigration = (initialMarketCap, migrationMarketCap, lock
1004
1013
  const denominator = new Decimal2(1).add(sqrtRatio);
1005
1014
  return numerator.div(denominator).toNumber();
1006
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
+ }
1007
1031
  var getMigrationQuoteAmount = (migrationMarketCap, percentageSupplyOnMigration) => {
1008
1032
  return migrationMarketCap.mul(percentageSupplyOnMigration).div(new Decimal2(100));
1009
1033
  };
@@ -1205,19 +1229,19 @@ function getLockedVestingParams(totalLockedVestingAmount, numberOfVestingPeriod,
1205
1229
  };
1206
1230
  }
1207
1231
  var getTwoCurve = (migrationSqrtPrice, midSqrtPrice, initialSqrtPrice, swapAmount, migrationQuoteThreshold) => {
1208
- let p0 = new Decimal2(initialSqrtPrice.toString());
1209
- let p1 = new Decimal2(midSqrtPrice.toString());
1210
- let p2 = new Decimal2(migrationSqrtPrice.toString());
1211
- let a1 = new Decimal2(1).div(p0).sub(new Decimal2(1).div(p1));
1212
- let b1 = new Decimal2(1).div(p1).sub(new Decimal2(1).div(p2));
1213
- let c1 = new Decimal2(swapAmount.toString());
1214
- let a2 = p1.sub(p0);
1215
- let b2 = p2.sub(p1);
1216
- 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(
1217
1241
  Decimal2.pow(2, 128)
1218
1242
  );
1219
- let l0 = c1.mul(b2).sub(c2.mul(b1)).div(a1.mul(b2).sub(a2.mul(b1)));
1220
- 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)));
1221
1245
  if (l0.isNeg() || l1.isNeg()) {
1222
1246
  return {
1223
1247
  isOk: false,
@@ -3291,7 +3315,7 @@ function validateMigrationFee(migrationFee) {
3291
3315
  import Decimal4 from "decimal.js";
3292
3316
  import BN14 from "bn.js";
3293
3317
  function buildCurve(buildCurveParam) {
3294
- let {
3318
+ const {
3295
3319
  totalTokenSupply,
3296
3320
  percentageSupplyOnMigration,
3297
3321
  migrationQuoteThreshold,
@@ -3348,7 +3372,7 @@ function buildCurve(buildCurveParam) {
3348
3372
  const migrationPrice = new Decimal4(migrationQuoteAmount.toString()).div(
3349
3373
  new Decimal4(migrationBaseSupply.toString())
3350
3374
  );
3351
- let migrationQuoteThresholdInLamport = convertToLamports(
3375
+ const migrationQuoteThresholdInLamport = convertToLamports(
3352
3376
  migrationQuoteThreshold,
3353
3377
  tokenQuoteDecimal
3354
3378
  );
@@ -3358,7 +3382,7 @@ function buildCurve(buildCurveParam) {
3358
3382
  tokenBaseDecimal,
3359
3383
  tokenQuoteDecimal
3360
3384
  );
3361
- let migrationQuoteAmountInLamport = fromDecimalToBN(
3385
+ const migrationQuoteAmountInLamport = fromDecimalToBN(
3362
3386
  migrationQuoteAmount.mul(new Decimal4(10 ** tokenQuoteDecimal))
3363
3387
  );
3364
3388
  const migrationBaseAmount = getMigrationBaseToken(
@@ -3461,7 +3485,14 @@ function buildCurveWithMarketCap(buildCurveWithMarketCapParam) {
3461
3485
  );
3462
3486
  const totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3463
3487
  const totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3464
- const percentageSupplyOnMigration = getPercentageSupplyOnMigration(
3488
+ const percentageSupplyOnMigration = migrationFee.feePercentage > 0 ? calculateAdjustedPercentageSupplyOnMigration(
3489
+ initialMarketCap,
3490
+ migrationMarketCap,
3491
+ migrationFee,
3492
+ lockedVesting,
3493
+ totalLeftover,
3494
+ totalSupply
3495
+ ) : getPercentageSupplyOnMigration(
3465
3496
  new Decimal4(initialMarketCap),
3466
3497
  new Decimal4(migrationMarketCap),
3467
3498
  lockedVesting,
@@ -3532,57 +3563,57 @@ function buildCurveWithTwoSegments(buildCurveWithTwoSegmentsParam) {
3532
3563
  migrationFeeOption,
3533
3564
  migratedPoolFee
3534
3565
  );
3535
- let migrationBaseSupply = new BN14(totalTokenSupply).mul(new BN14(percentageSupplyOnMigration)).div(new BN14(100));
3536
- let totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3537
- 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(
3538
3569
  new Decimal4(migrationMarketCap),
3539
3570
  new Decimal4(percentageSupplyOnMigration)
3540
3571
  );
3541
- let migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3572
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3542
3573
  migrationQuoteAmount,
3543
3574
  new Decimal4(migrationFee.feePercentage)
3544
3575
  );
3545
- let migrationPrice = migrationQuoteAmount.div(
3576
+ const migrationPrice = migrationQuoteAmount.div(
3546
3577
  new Decimal4(migrationBaseSupply.toString())
3547
3578
  );
3548
- let migrationQuoteThresholdInLamport = fromDecimalToBN(
3579
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
3549
3580
  migrationQuoteThreshold.mul(new Decimal4(10 ** tokenQuoteDecimal))
3550
3581
  );
3551
- let migrationQuoteAmountInLamport = fromDecimalToBN(
3582
+ const migrationQuoteAmountInLamport = fromDecimalToBN(
3552
3583
  migrationQuoteAmount.mul(new Decimal4(10 ** tokenQuoteDecimal))
3553
3584
  );
3554
- let migrateSqrtPrice = getSqrtPriceFromPrice(
3585
+ const migrateSqrtPrice = getSqrtPriceFromPrice(
3555
3586
  migrationPrice.toString(),
3556
3587
  tokenBaseDecimal,
3557
3588
  tokenQuoteDecimal
3558
3589
  );
3559
- let migrationBaseAmount = getMigrationBaseToken(
3590
+ const migrationBaseAmount = getMigrationBaseToken(
3560
3591
  migrationQuoteAmountInLamport,
3561
3592
  migrateSqrtPrice,
3562
3593
  migrationOption
3563
3594
  );
3564
- let totalVestingAmount = getTotalVestingAmount(lockedVesting);
3565
- let totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3566
- let swapAmount = totalSupply.sub(migrationBaseAmount).sub(totalVestingAmount).sub(totalLeftover);
3567
- 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(
3568
3599
  initialMarketCap,
3569
3600
  totalTokenSupply,
3570
3601
  tokenBaseDecimal,
3571
3602
  tokenQuoteDecimal
3572
3603
  );
3573
- let midSqrtPriceDecimal1 = new Decimal4(migrateSqrtPrice.toString()).mul(new Decimal4(initialSqrtPrice.toString())).sqrt();
3574
- let midSqrtPrice1 = new BN14(midSqrtPriceDecimal1.floor().toFixed());
3575
- let numerator1 = new Decimal4(initialSqrtPrice.toString());
3576
- let numerator2 = Decimal4.pow(migrateSqrtPrice.toString(), 3);
3577
- let product1 = numerator1.mul(numerator2);
3578
- let midSqrtPriceDecimal2 = Decimal4.pow(product1, 0.25);
3579
- let midSqrtPrice2 = new BN14(midSqrtPriceDecimal2.floor().toFixed());
3580
- let numerator3 = Decimal4.pow(initialSqrtPrice.toString(), 3);
3581
- let numerator4 = new Decimal4(migrateSqrtPrice.toString());
3582
- let product2 = numerator3.mul(numerator4);
3583
- let midSqrtPriceDecimal3 = Decimal4.pow(product2, 0.25);
3584
- let midSqrtPrice3 = new BN14(midSqrtPriceDecimal3.floor().toFixed());
3585
- let midPrices = [midSqrtPrice3, midSqrtPrice2, midSqrtPrice1];
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];
3586
3617
  let sqrtStartPrice = new BN14(0);
3587
3618
  let curve = [];
3588
3619
  for (let i = 0; i < midPrices.length; i++) {
@@ -3599,7 +3630,7 @@ function buildCurveWithTwoSegments(buildCurveWithTwoSegmentsParam) {
3599
3630
  break;
3600
3631
  }
3601
3632
  }
3602
- let totalDynamicSupply = getTotalSupplyFromCurve(
3633
+ const totalDynamicSupply = getTotalSupplyFromCurve(
3603
3634
  migrationQuoteThresholdInLamport,
3604
3635
  sqrtStartPrice,
3605
3636
  curve,
@@ -3609,7 +3640,7 @@ function buildCurveWithTwoSegments(buildCurveWithTwoSegmentsParam) {
3609
3640
  migrationFee.feePercentage
3610
3641
  );
3611
3642
  if (totalDynamicSupply.gt(totalSupply)) {
3612
- let leftOverDelta = totalDynamicSupply.sub(totalSupply);
3643
+ const leftOverDelta = totalDynamicSupply.sub(totalSupply);
3613
3644
  if (!leftOverDelta.lt(totalLeftover)) {
3614
3645
  throw new Error("leftOverDelta must be less than totalLeftover");
3615
3646
  }
@@ -3704,39 +3735,39 @@ function buildCurveWithMidPrice(buildCurveWithMidPriceParam) {
3704
3735
  migrationFeeOption,
3705
3736
  migratedPoolFee
3706
3737
  );
3707
- let migrationBaseSupply = new BN14(totalTokenSupply).mul(new BN14(percentageSupplyOnMigration)).div(new BN14(100));
3708
- let totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3709
- let migrationQuoteAmount = getMigrationQuoteAmount(
3738
+ const migrationBaseSupply = new BN14(totalTokenSupply).mul(new BN14(percentageSupplyOnMigration)).div(new BN14(100));
3739
+ const totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3740
+ const migrationQuoteAmount = getMigrationQuoteAmount(
3710
3741
  new Decimal4(migrationMarketCap),
3711
3742
  new Decimal4(percentageSupplyOnMigration)
3712
3743
  );
3713
- let migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3744
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3714
3745
  migrationQuoteAmount,
3715
3746
  new Decimal4(migrationFee.feePercentage)
3716
3747
  );
3717
- let migrationPrice = migrationQuoteAmount.div(
3748
+ const migrationPrice = migrationQuoteAmount.div(
3718
3749
  new Decimal4(migrationBaseSupply.toString())
3719
3750
  );
3720
- let migrationQuoteThresholdInLamport = fromDecimalToBN(
3751
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
3721
3752
  migrationQuoteThreshold.mul(new Decimal4(10 ** tokenQuoteDecimal))
3722
3753
  );
3723
- let migrationQuoteAmountInLamport = fromDecimalToBN(
3754
+ const migrationQuoteAmountInLamport = fromDecimalToBN(
3724
3755
  migrationQuoteAmount.mul(new Decimal4(10 ** tokenQuoteDecimal))
3725
3756
  );
3726
- let migrateSqrtPrice = getSqrtPriceFromPrice(
3757
+ const migrateSqrtPrice = getSqrtPriceFromPrice(
3727
3758
  migrationPrice.toString(),
3728
3759
  tokenBaseDecimal,
3729
3760
  tokenQuoteDecimal
3730
3761
  );
3731
- let migrationBaseAmount = getMigrationBaseToken(
3762
+ const migrationBaseAmount = getMigrationBaseToken(
3732
3763
  migrationQuoteAmountInLamport,
3733
3764
  migrateSqrtPrice,
3734
3765
  migrationOption
3735
3766
  );
3736
- let totalVestingAmount = getTotalVestingAmount(lockedVesting);
3737
- let totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3738
- let swapAmount = totalSupply.sub(migrationBaseAmount).sub(totalVestingAmount).sub(totalLeftover);
3739
- let initialSqrtPrice = getSqrtPriceFromMarketCap(
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(
3740
3771
  initialMarketCap,
3741
3772
  totalTokenSupply,
3742
3773
  tokenBaseDecimal,
@@ -3758,7 +3789,7 @@ function buildCurveWithMidPrice(buildCurveWithMidPriceParam) {
3758
3789
  );
3759
3790
  curve = result.curve;
3760
3791
  sqrtStartPrice = result.sqrtStartPrice;
3761
- let totalDynamicSupply = getTotalSupplyFromCurve(
3792
+ const totalDynamicSupply = getTotalSupplyFromCurve(
3762
3793
  migrationQuoteThresholdInLamport,
3763
3794
  sqrtStartPrice,
3764
3795
  curve,
@@ -3768,7 +3799,7 @@ function buildCurveWithMidPrice(buildCurveWithMidPriceParam) {
3768
3799
  migrationFee.feePercentage
3769
3800
  );
3770
3801
  if (totalDynamicSupply.gt(totalSupply)) {
3771
- let leftOverDelta = totalDynamicSupply.sub(totalSupply);
3802
+ const leftOverDelta = totalDynamicSupply.sub(totalSupply);
3772
3803
  if (!leftOverDelta.lt(totalLeftover)) {
3773
3804
  throw new Error("leftOverDelta must be less than totalLeftover");
3774
3805
  }
@@ -3813,7 +3844,7 @@ function buildCurveWithMidPrice(buildCurveWithMidPriceParam) {
3813
3844
  return instructionParams;
3814
3845
  }
3815
3846
  function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3816
- let {
3847
+ const {
3817
3848
  totalTokenSupply,
3818
3849
  migrationOption,
3819
3850
  tokenBaseDecimal,
@@ -3862,23 +3893,23 @@ function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3862
3893
  migrationFeeOption,
3863
3894
  migratedPoolFee
3864
3895
  );
3865
- let pMin = getSqrtPriceFromMarketCap(
3896
+ const pMin = getSqrtPriceFromMarketCap(
3866
3897
  initialMarketCap,
3867
3898
  totalTokenSupply,
3868
3899
  tokenBaseDecimal,
3869
3900
  tokenQuoteDecimal
3870
3901
  );
3871
- let pMax = getSqrtPriceFromMarketCap(
3902
+ const pMax = getSqrtPriceFromMarketCap(
3872
3903
  migrationMarketCap,
3873
3904
  totalTokenSupply,
3874
3905
  tokenBaseDecimal,
3875
3906
  tokenQuoteDecimal
3876
3907
  );
3877
- let priceRatio = new Decimal4(pMax.toString()).div(
3908
+ const priceRatio = new Decimal4(pMax.toString()).div(
3878
3909
  new Decimal4(pMin.toString())
3879
3910
  );
3880
- let qDecimal = priceRatio.pow(new Decimal4(1).div(new Decimal4(16)));
3881
- let sqrtPrices = [];
3911
+ const qDecimal = priceRatio.pow(new Decimal4(1).div(new Decimal4(16)));
3912
+ const sqrtPrices = [];
3882
3913
  let currentPrice = pMin;
3883
3914
  for (let i = 0; i < 17; i++) {
3884
3915
  sqrtPrices.push(currentPrice);
@@ -3886,49 +3917,217 @@ function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3886
3917
  qDecimal.mul(new Decimal4(currentPrice.toString()))
3887
3918
  );
3888
3919
  }
3889
- let totalSupply = convertToLamports(totalTokenSupply, tokenBaseDecimal);
3890
- let totalLeftover = convertToLamports(leftover, tokenBaseDecimal);
3891
- let totalVestingAmount = getTotalVestingAmount(lockedVesting);
3892
- 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);
3893
3924
  let sumFactor = new Decimal4(0);
3894
- let pmaxWeight = new Decimal4(pMax.toString());
3895
- 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));
3896
3927
  for (let i = 1; i < 17; i++) {
3897
- let pi = new Decimal4(sqrtPrices[i].toString());
3898
- let piMinus = new Decimal4(sqrtPrices[i - 1].toString());
3899
- let k = new Decimal4(liquidityWeights[i - 1]);
3900
- let w1 = pi.sub(piMinus).div(pi.mul(piMinus));
3901
- let w2 = pi.sub(piMinus).mul(migrationFeeFactor).div(pmaxWeight.mul(pmaxWeight));
3902
- 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));
3903
3934
  sumFactor = sumFactor.add(weight);
3904
3935
  }
3905
- let l1 = new Decimal4(totalSwapAndMigrationAmount.toString()).div(sumFactor);
3906
- let curve = [];
3936
+ const l1 = new Decimal4(totalSwapAndMigrationAmount.toString()).div(
3937
+ sumFactor
3938
+ );
3939
+ const curve = [];
3907
3940
  for (let i = 0; i < 16; i++) {
3908
- let k = new Decimal4(liquidityWeights[i]);
3909
- let liquidity = convertDecimalToBN(l1.mul(k));
3910
- 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;
3911
3944
  curve.push({
3912
3945
  sqrtPrice,
3913
3946
  liquidity
3914
3947
  });
3915
3948
  }
3916
- let swapBaseAmount = getBaseTokenForSwap(pMin, pMax, curve);
3917
- let swapBaseAmountBuffer = getSwapAmountWithBuffer(
3949
+ const swapBaseAmount = getBaseTokenForSwap(pMin, pMax, curve);
3950
+ const swapBaseAmountBuffer = getSwapAmountWithBuffer(
3918
3951
  swapBaseAmount,
3919
3952
  pMin,
3920
3953
  curve
3921
3954
  );
3922
- let migrationAmount = totalSwapAndMigrationAmount.sub(swapBaseAmountBuffer);
3923
- let migrationQuoteAmount = migrationAmount.mul(pMax).mul(pMax).shrn(128);
3924
- let migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3955
+ const migrationAmount = totalSwapAndMigrationAmount.sub(swapBaseAmountBuffer);
3956
+ const migrationQuoteAmount = migrationAmount.mul(pMax).mul(pMax).shrn(128);
3957
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
3925
3958
  new Decimal4(migrationQuoteAmount.toString()),
3926
3959
  new Decimal4(migrationFee.feePercentage)
3927
3960
  );
3928
- let migrationQuoteThresholdInLamport = fromDecimalToBN(
3961
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
3929
3962
  migrationQuoteThreshold
3930
3963
  );
3931
- let totalDynamicSupply = getTotalSupplyFromCurve(
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];
4110
+ curve.push({
4111
+ sqrtPrice,
4112
+ liquidity
4113
+ });
4114
+ }
4115
+ const swapBaseAmount = getBaseTokenForSwap(pMin, pMax, curve);
4116
+ const swapBaseAmountBuffer = getSwapAmountWithBuffer(
4117
+ swapBaseAmount,
4118
+ pMin,
4119
+ curve
4120
+ );
4121
+ const migrationAmount = totalSwapAndMigrationAmount.sub(swapBaseAmountBuffer);
4122
+ const migrationQuoteAmount = migrationAmount.mul(pMax).mul(pMax).shrn(128);
4123
+ const migrationQuoteThreshold = getMigrationQuoteThresholdFromMigrationQuoteAmount(
4124
+ new Decimal4(migrationQuoteAmount.toString()),
4125
+ new Decimal4(migrationFee.feePercentage)
4126
+ );
4127
+ const migrationQuoteThresholdInLamport = fromDecimalToBN(
4128
+ migrationQuoteThreshold
4129
+ );
4130
+ const totalDynamicSupply = getTotalSupplyFromCurve(
3932
4131
  migrationQuoteThresholdInLamport,
3933
4132
  pMin,
3934
4133
  curve,
@@ -3938,7 +4137,7 @@ function buildCurveWithLiquidityWeights(buildCurveWithLiquidityWeightsParam) {
3938
4137
  migrationFee.feePercentage
3939
4138
  );
3940
4139
  if (totalDynamicSupply.gt(totalSupply)) {
3941
- let leftOverDelta = totalDynamicSupply.sub(totalSupply);
4140
+ const leftOverDelta = totalDynamicSupply.sub(totalSupply);
3942
4141
  if (!leftOverDelta.lt(totalLeftover)) {
3943
4142
  throw new Error("leftOverDelta must be less than totalLeftover");
3944
4143
  }
@@ -26557,10 +26756,12 @@ export {
26557
26756
  VAULT_PROGRAM_ID,
26558
26757
  bpsToFeeNumerator,
26559
26758
  buildCurve,
26759
+ buildCurveWithCustomSqrtPrices,
26560
26760
  buildCurveWithLiquidityWeights,
26561
26761
  buildCurveWithMarketCap,
26562
26762
  buildCurveWithMidPrice,
26563
26763
  buildCurveWithTwoSegments,
26764
+ calculateAdjustedPercentageSupplyOnMigration,
26564
26765
  calculateBaseToQuoteFromAmountIn,
26565
26766
  calculateBaseToQuoteFromAmountOut,
26566
26767
  calculateFeeSchedulerEndingBaseFeeBps,
@@ -26576,6 +26777,7 @@ export {
26576
26777
  createInitializePermissionlessDynamicVaultIx,
26577
26778
  createLockEscrowIx,
26578
26779
  createProgramAccountFilter,
26780
+ createSqrtPrices,
26579
26781
  createVaultProgram,
26580
26782
  deriveBaseKeyForLocker,
26581
26783
  deriveDammV1EventAuthority,