@magmaprotocol/magma-clmm-sdk 0.4.3 → 0.5.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/index.mjs CHANGED
@@ -983,11 +983,7 @@ var ClmmPoolUtil = class {
983
983
  coinB = new decimal_default(0);
984
984
  } else if (curSqrtPrice.lt(upperSqrtPrice)) {
985
985
  coinA = MathUtil.toX64_Decimal(liq).mul(upperPriceStr.sub(curSqrtPriceStr)).div(curSqrtPriceStr.mul(upperPriceStr));
986
- const a = curSqrtPriceStr.sub(lowerPriceStr);
987
- const b = liq.mul(a);
988
- const c = MathUtil.fromX64_Decimal(b);
989
- coinB = c;
990
- console.log("########## a, b, c", a, b, c);
986
+ coinB = MathUtil.fromX64_Decimal(liq.mul(curSqrtPriceStr.sub(lowerPriceStr)));
991
987
  } else {
992
988
  coinA = new decimal_default(0);
993
989
  coinB = MathUtil.fromX64_Decimal(liq.mul(upperPriceStr.sub(lowerPriceStr)));
@@ -1036,8 +1032,8 @@ var ClmmPoolUtil = class {
1036
1032
  liquidity = estimateLiquidityForCoinB(curSqrtPrice, lowerSqrtPrice, coinAmount);
1037
1033
  }
1038
1034
  const coinAmounts = ClmmPoolUtil.getCoinAmountFromLiquidity(liquidity, curSqrtPrice, lowerSqrtPrice, upperSqrtPrice, roundUp);
1039
- const tokenLimitA = roundUp ? d(coinAmounts.coinA.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinA.toString()).mul(1 - slippage).toString();
1040
- const tokenLimitB = roundUp ? d(coinAmounts.coinB.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinB.toString()).mul(1 - slippage).toString();
1035
+ const tokenLimitA = iscoinA ? d(coinAmounts.coinA.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinA.toString()).mul(1 - slippage).toString();
1036
+ const tokenLimitB = iscoinA ? d(coinAmounts.coinB.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinB.toString()).mul(1 - slippage).toString();
1041
1037
  return {
1042
1038
  coinAmountA: coinAmounts.coinA,
1043
1039
  coinAmountB: coinAmounts.coinB,
@@ -2618,6 +2614,62 @@ var _TransactionUtil = class {
2618
2614
  );
2619
2615
  return tx;
2620
2616
  }
2617
+ /**
2618
+ * build add liquidity with protection transaction
2619
+ * @param params
2620
+ * @param packageId
2621
+ * @returns
2622
+ */
2623
+ static async buildAddLiquidityWithProtectionFixToken(sdk, allCoinAsset, params, tx, inputCoinA, inputCoinB) {
2624
+ if (sdk.senderAddress.length === 0) {
2625
+ throw Error("this config sdk senderAddress is empty");
2626
+ }
2627
+ tx = tx || new Transaction();
2628
+ let primaryCoinAInputs;
2629
+ let primaryCoinBInputs;
2630
+ if (inputCoinA == null || inputCoinB == null) {
2631
+ primaryCoinAInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2632
+ tx,
2633
+ !params.fix_amount_a,
2634
+ params.amount_a,
2635
+ params.slippage,
2636
+ params.coinTypeA,
2637
+ allCoinAsset,
2638
+ false
2639
+ );
2640
+ primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2641
+ tx,
2642
+ params.fix_amount_a,
2643
+ params.amount_b,
2644
+ params.slippage,
2645
+ params.coinTypeB,
2646
+ allCoinAsset,
2647
+ false
2648
+ );
2649
+ } else {
2650
+ primaryCoinAInputs = {
2651
+ targetCoin: inputCoinA,
2652
+ remainCoins: [],
2653
+ isMintZeroCoin: false,
2654
+ tragetCoinAmount: "0"
2655
+ };
2656
+ primaryCoinBInputs = {
2657
+ targetCoin: inputCoinB,
2658
+ remainCoins: [],
2659
+ isMintZeroCoin: false,
2660
+ tragetCoinAmount: "0"
2661
+ };
2662
+ }
2663
+ tx = _TransactionUtil.buildAddLiquidityWithProtectionFixTokenArgs(
2664
+ tx,
2665
+ sdk,
2666
+ allCoinAsset,
2667
+ params,
2668
+ primaryCoinAInputs,
2669
+ primaryCoinBInputs
2670
+ );
2671
+ return tx;
2672
+ }
2621
2673
  static buildAddLiquidityFixTokenCoinInput(tx, need_interval_amount, amount, slippage, coinType, allCoinAsset, buildVector = true) {
2622
2674
  return need_interval_amount ? _TransactionUtil.buildCoinForAmountInterval(
2623
2675
  tx,
@@ -2693,6 +2745,67 @@ var _TransactionUtil = class {
2693
2745
  });
2694
2746
  return tx;
2695
2747
  }
2748
+ static buildAddLiquidityWithProtectionFixTokenArgs(tx, sdk, allCoinAsset, params, primaryCoinAInputs, primaryCoinBInputs) {
2749
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
2750
+ const functionName = params.is_open ? "open_position_with_liquidity_by_fix_coin_with_protection" : "add_liquidity_by_fix_coin_with_protection";
2751
+ const { clmm_pool, integrate } = sdk.sdkOptions;
2752
+ if (!params.is_open) {
2753
+ tx = _TransactionUtil.createCollectRewarderAndFeeParams(
2754
+ sdk,
2755
+ tx,
2756
+ params,
2757
+ allCoinAsset,
2758
+ primaryCoinAInputs.remainCoins,
2759
+ primaryCoinBInputs.remainCoins
2760
+ );
2761
+ }
2762
+ const clmmConfig = getPackagerConfigs(clmm_pool);
2763
+ let min_amount_a, max_amount_a, min_amount_b, max_amount_b;
2764
+ if (params.fix_amount_a) {
2765
+ max_amount_a = params.amount_a;
2766
+ min_amount_a = params.amount_a;
2767
+ max_amount_b = params.amount_b;
2768
+ min_amount_b = new Decimal4(params.amount_b).div(new Decimal4(1).plus(new Decimal4(params.slippage))).mul(new Decimal4(1).minus(new Decimal4(params.slippage))).toNumber();
2769
+ } else {
2770
+ max_amount_b = params.amount_b;
2771
+ min_amount_b = params.amount_b;
2772
+ max_amount_a = params.amount_a;
2773
+ min_amount_a = new Decimal4(params.amount_a).div(new Decimal4(1).plus(new Decimal4(params.slippage))).mul(new Decimal4(1).minus(new Decimal4(params.slippage))).toNumber();
2774
+ }
2775
+ const args = params.is_open ? [
2776
+ tx.object(clmmConfig.global_config_id),
2777
+ tx.object(params.pool_id),
2778
+ tx.pure.u32(Number(asUintN(BigInt(params.tick_lower)).toString())),
2779
+ tx.pure.u32(Number(asUintN(BigInt(params.tick_upper)).toString())),
2780
+ primaryCoinAInputs.targetCoin,
2781
+ primaryCoinBInputs.targetCoin,
2782
+ tx.pure.u64(max_amount_a),
2783
+ tx.pure.u64(max_amount_b),
2784
+ tx.pure.u64(min_amount_a),
2785
+ tx.pure.u64(min_amount_b),
2786
+ tx.pure.bool(params.fix_amount_a),
2787
+ tx.object(CLOCK_ADDRESS)
2788
+ ] : [
2789
+ tx.object(clmmConfig.global_config_id),
2790
+ tx.object(params.pool_id),
2791
+ tx.object(params.pos_id),
2792
+ primaryCoinAInputs.targetCoin,
2793
+ primaryCoinBInputs.targetCoin,
2794
+ tx.pure.u64(max_amount_a),
2795
+ tx.pure.u64(max_amount_b),
2796
+ tx.pure.u64(min_amount_a),
2797
+ tx.pure.u64(min_amount_b),
2798
+ tx.pure.bool(params.fix_amount_a),
2799
+ tx.object(CLOCK_ADDRESS),
2800
+ tx.object(CLOCK_ADDRESS)
2801
+ ];
2802
+ tx.moveCall({
2803
+ target: `${integrate.published_at}::${ClmmIntegratePoolV2Module}::${functionName}`,
2804
+ typeArguments,
2805
+ arguments: args
2806
+ });
2807
+ return tx;
2808
+ }
2696
2809
  // -------------------------------------------swap--------------------------------------------------//
2697
2810
  /**
2698
2811
  * build add liquidity transaction
@@ -5251,6 +5364,36 @@ var PositionModule = class {
5251
5364
  }
5252
5365
  return TransactionUtil.buildAddLiquidityFixToken(this._sdk, allCoinAsset, params, tx, inputCoinA, inputCoinB);
5253
5366
  }
5367
+ /**
5368
+ * create add liquidity transaction payload with fix token
5369
+ * @param {AddLiquidityFixTokenParams} params
5370
+ * @param gasEstimateArg : When the fix input amount is SUI, gasEstimateArg can control whether to recalculate the number of SUI to prevent insufficient gas.
5371
+ * If this parameter is not passed, gas estimation is not performed
5372
+ * @returns {Promise<TransactionBlock>}
5373
+ */
5374
+ async createAddLiquidityFixTokenWithProtectionPayload(params, gasEstimateArg, tx, inputCoinA, inputCoinB) {
5375
+ if (!checkInvalidSuiAddress(this._sdk.senderAddress)) {
5376
+ throw new ClmmpoolsError("this config sdk senderAddress is not set right", "InvalidSendAddress" /* InvalidSendAddress */);
5377
+ }
5378
+ const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5379
+ if (gasEstimateArg) {
5380
+ const { isAdjustCoinA, isAdjustCoinB } = findAdjustCoin(params);
5381
+ params = params;
5382
+ if (params.fix_amount_a && isAdjustCoinA || !params.fix_amount_a && isAdjustCoinB) {
5383
+ tx = await TransactionUtil.buildAddLiquidityFixTokenForGas(
5384
+ this._sdk,
5385
+ allCoinAsset,
5386
+ params,
5387
+ gasEstimateArg,
5388
+ tx,
5389
+ inputCoinA,
5390
+ inputCoinB
5391
+ );
5392
+ return tx;
5393
+ }
5394
+ }
5395
+ return TransactionUtil.buildAddLiquidityWithProtectionFixToken(this._sdk, allCoinAsset, params, tx, inputCoinA, inputCoinB);
5396
+ }
5254
5397
  /**
5255
5398
  * create add liquidity transaction payload
5256
5399
  * @param {AddLiquidityParams} params