@magmaprotocol/magma-clmm-sdk 0.5.0 → 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
@@ -1032,8 +1032,8 @@ var ClmmPoolUtil = class {
1032
1032
  liquidity = estimateLiquidityForCoinB(curSqrtPrice, lowerSqrtPrice, coinAmount);
1033
1033
  }
1034
1034
  const coinAmounts = ClmmPoolUtil.getCoinAmountFromLiquidity(liquidity, curSqrtPrice, lowerSqrtPrice, upperSqrtPrice, roundUp);
1035
- const tokenLimitA = roundUp ? d(coinAmounts.coinA.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinA.toString()).mul(1 - slippage).toString();
1036
- 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();
1037
1037
  return {
1038
1038
  coinAmountA: coinAmounts.coinA,
1039
1039
  coinAmountB: coinAmounts.coinB,
@@ -2614,6 +2614,62 @@ var _TransactionUtil = class {
2614
2614
  );
2615
2615
  return tx;
2616
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
+ }
2617
2673
  static buildAddLiquidityFixTokenCoinInput(tx, need_interval_amount, amount, slippage, coinType, allCoinAsset, buildVector = true) {
2618
2674
  return need_interval_amount ? _TransactionUtil.buildCoinForAmountInterval(
2619
2675
  tx,
@@ -2689,6 +2745,67 @@ var _TransactionUtil = class {
2689
2745
  });
2690
2746
  return tx;
2691
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
+ }
2692
2809
  // -------------------------------------------swap--------------------------------------------------//
2693
2810
  /**
2694
2811
  * build add liquidity transaction
@@ -2974,65 +3091,6 @@ var _TransactionUtil = class {
2974
3091
  });
2975
3092
  return tx;
2976
3093
  }
2977
- static buildOpenPositionWithLiquidityByFixCoinWithProtection(sdk, params, allCoinAsset) {
2978
- const tx = new Transaction();
2979
- tx.setSender(sdk.senderAddress);
2980
- const { clmm_pool, integrate } = sdk.sdkOptions;
2981
- const globalConfigID = getPackagerConfigs(clmm_pool).global_config_id;
2982
- const typeArguments = [params.coinAType, params.coinBType];
2983
- const functionName = "open_position_with_liquidity_by_fix_coin_with_protection";
2984
- const coinA = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_a), params.coinAType, false, true);
2985
- const coinB = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_b), params.coinBType, false, true);
2986
- const args = [
2987
- tx.object(globalConfigID),
2988
- tx.object(params.pool),
2989
- tx.pure.u32(params.tick_lower),
2990
- tx.pure.u32(params.tick_upper),
2991
- coinA.targetCoin,
2992
- coinB.targetCoin,
2993
- tx.pure.u64(params.max_amount_a),
2994
- tx.pure.u64(params.max_amount_b),
2995
- tx.pure.u64(params.min_amount_a),
2996
- tx.pure.u64(params.min_amount_b),
2997
- tx.pure.bool(params.by_amount_a),
2998
- tx.object(CLOCK_ADDRESS)
2999
- ];
3000
- tx.moveCall({
3001
- target: `${integrate.published_at}::${Voter}::${functionName}`,
3002
- typeArguments,
3003
- arguments: args
3004
- });
3005
- return tx;
3006
- }
3007
- static buildAddLiquidityByFixCoinWithProtection(sdk, params, allCoinAsset) {
3008
- const tx = new Transaction();
3009
- tx.setSender(sdk.senderAddress);
3010
- const { clmm_pool, integrate } = sdk.sdkOptions;
3011
- const globalConfigID = getPackagerConfigs(clmm_pool).global_config_id;
3012
- const typeArguments = [params.coinAType, params.coinBType];
3013
- const functionName = "add_liquidity_by_fix_coin_with_protection";
3014
- const coinA = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_a), params.coinAType, false, true);
3015
- const coinB = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_b), params.coinBType, false, true);
3016
- const args = [
3017
- tx.object(globalConfigID),
3018
- tx.object(params.pool),
3019
- tx.object(params.position),
3020
- coinA.targetCoin,
3021
- coinB.targetCoin,
3022
- tx.pure.u64(params.max_amount_a),
3023
- tx.pure.u64(params.max_amount_b),
3024
- tx.pure.u64(params.min_amount_a),
3025
- tx.pure.u64(params.min_amount_b),
3026
- tx.pure.bool(params.fix_amount_a),
3027
- tx.object(CLOCK_ADDRESS)
3028
- ];
3029
- tx.moveCall({
3030
- target: `${integrate.published_at}::${Voter}::${functionName}`,
3031
- typeArguments,
3032
- arguments: args
3033
- });
3034
- return tx;
3035
- }
3036
3094
  static buildClaimVotingBribe(sdk, locks, incentive_tokens) {
3037
3095
  const tx = new Transaction();
3038
3096
  tx.setGasBudget(5e8);
@@ -5306,6 +5364,36 @@ var PositionModule = class {
5306
5364
  }
5307
5365
  return TransactionUtil.buildAddLiquidityFixToken(this._sdk, allCoinAsset, params, tx, inputCoinA, inputCoinB);
5308
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
+ }
5309
5397
  /**
5310
5398
  * create add liquidity transaction payload
5311
5399
  * @param {AddLiquidityParams} params
@@ -5552,20 +5640,6 @@ var PositionModule = class {
5552
5640
  feeOwedB: "0"
5553
5641
  };
5554
5642
  }
5555
- async openPositionWithLiquidityByFixCoinWithProtection(params) {
5556
- if (this._sdk.senderAddress.length === 0) {
5557
- throw Error("this config sdk senderAddress is empty");
5558
- }
5559
- const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5560
- return TransactionUtil.buildOpenPositionWithLiquidityByFixCoinWithProtection(this.sdk, params, allCoinAsset);
5561
- }
5562
- async addLiquidityByFixCoinWithProtection(params) {
5563
- if (this._sdk.senderAddress.length === 0) {
5564
- throw Error("this config sdk senderAddress is empty");
5565
- }
5566
- const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5567
- return TransactionUtil.buildAddLiquidityByFixCoinWithProtection(this.sdk, params, allCoinAsset);
5568
- }
5569
5643
  /**
5570
5644
  * Updates the cache for the given key.
5571
5645
  * @param {string} key The key of the cache entry to update.