@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.d.ts CHANGED
@@ -985,25 +985,7 @@ type OpenPositionAddLiquidityWithProtectionParams = {
985
985
  min_amount_b: number;
986
986
  by_amount_a: boolean;
987
987
  };
988
- type AddLiquidityWithProtectionParams = {
989
- pool: SuiAddressType;
990
- position: SuiObjectIdType;
991
- coin_a: number | string;
992
- coin_b: number | string;
993
- /**
994
- * The address type of the coin A.
995
- */
996
- coinAType: SuiAddressType;
997
- /**
998
- * The address type of the coin A.
999
- */
1000
- coinBType: SuiAddressType;
1001
- max_amount_a: number;
1002
- max_amount_b: number;
1003
- min_amount_a: number;
1004
- min_amount_b: number;
1005
- fix_amount_a: boolean;
1006
- };
988
+ type AddLiquidityWithProtectionParams = AddLiquidityFixTokenParams & {};
1007
989
 
1008
990
  /**
1009
991
  * Represents tick data for a liquidity pool.
@@ -2632,6 +2614,17 @@ declare class PositionModule implements IModule {
2632
2614
  slippage: number;
2633
2615
  curSqrtPrice: BN;
2634
2616
  }, tx?: Transaction, inputCoinA?: TransactionObjectArgument, inputCoinB?: TransactionObjectArgument): Promise<Transaction>;
2617
+ /**
2618
+ * create add liquidity transaction payload with fix token
2619
+ * @param {AddLiquidityFixTokenParams} params
2620
+ * @param gasEstimateArg : When the fix input amount is SUI, gasEstimateArg can control whether to recalculate the number of SUI to prevent insufficient gas.
2621
+ * If this parameter is not passed, gas estimation is not performed
2622
+ * @returns {Promise<TransactionBlock>}
2623
+ */
2624
+ createAddLiquidityFixTokenWithProtectionPayload(params: AddLiquidityWithProtectionParams, gasEstimateArg?: {
2625
+ slippage: number;
2626
+ curSqrtPrice: BN;
2627
+ }, tx?: Transaction, inputCoinA?: TransactionObjectArgument, inputCoinB?: TransactionObjectArgument): Promise<Transaction>;
2635
2628
  /**
2636
2629
  * create add liquidity transaction payload
2637
2630
  * @param {AddLiquidityParams} params
@@ -2674,8 +2667,6 @@ declare class PositionModule implements IModule {
2674
2667
  feeOwedA: any;
2675
2668
  feeOwedB: any;
2676
2669
  }>;
2677
- openPositionWithLiquidityByFixCoinWithProtection(params: OpenPositionAddLiquidityWithProtectionParams): Promise<Transaction>;
2678
- addLiquidityByFixCoinWithProtection(params: AddLiquidityWithProtectionParams): Promise<Transaction>;
2679
2670
  /**
2680
2671
  * Updates the cache for the given key.
2681
2672
  * @param {string} key The key of the cache entry to update.
@@ -3724,6 +3715,13 @@ declare class TransactionUtil {
3724
3715
  * @returns
3725
3716
  */
3726
3717
  static buildAddLiquidityFixToken(sdk: MagmaClmmSDK, allCoinAsset: CoinAsset[], params: AddLiquidityFixTokenParams, tx?: Transaction, inputCoinA?: TransactionObjectArgument, inputCoinB?: TransactionObjectArgument): Promise<Transaction>;
3718
+ /**
3719
+ * build add liquidity with protection transaction
3720
+ * @param params
3721
+ * @param packageId
3722
+ * @returns
3723
+ */
3724
+ static buildAddLiquidityWithProtectionFixToken(sdk: MagmaClmmSDK, allCoinAsset: CoinAsset[], params: AddLiquidityFixTokenParams, tx?: Transaction, inputCoinA?: TransactionObjectArgument, inputCoinB?: TransactionObjectArgument): Promise<Transaction>;
3727
3725
  static buildAddLiquidityFixTokenCoinInput(tx: Transaction, need_interval_amount: boolean, amount: number | string, slippage: number, coinType: string, allCoinAsset: CoinAsset[], buildVector?: boolean): BuildCoinResult;
3728
3726
  /**
3729
3727
  * fix add liquidity fix token for coin amount
@@ -3734,6 +3732,7 @@ declare class TransactionUtil {
3734
3732
  */
3735
3733
  static fixAddLiquidityFixTokenParams(params: AddLiquidityFixTokenParams, slippage: number, curSqrtPrice: BN): AddLiquidityFixTokenParams;
3736
3734
  private static buildAddLiquidityFixTokenArgs;
3735
+ private static buildAddLiquidityWithProtectionFixTokenArgs;
3737
3736
  /**
3738
3737
  * build add liquidity transaction
3739
3738
  * @param params
@@ -3770,8 +3769,6 @@ declare class TransactionUtil {
3770
3769
  static buildClaimVotingRewardsPoolsTransaction(sdk: MagmaClmmSDK, params: ClaimFeesPoolsParams): Transaction;
3771
3770
  static buildClaimAndLockRebases(sdk: MagmaClmmSDK, params: ClaimAndLockParams): Transaction;
3772
3771
  static buildPoke(sdk: MagmaClmmSDK, params: PokeParams): Transaction;
3773
- static buildOpenPositionWithLiquidityByFixCoinWithProtection(sdk: MagmaClmmSDK, params: OpenPositionAddLiquidityWithProtectionParams, allCoinAsset: CoinAsset[]): Transaction;
3774
- static buildAddLiquidityByFixCoinWithProtection(sdk: MagmaClmmSDK, params: AddLiquidityWithProtectionParams, allCoinAsset: CoinAsset[]): Transaction;
3775
3772
  static buildClaimVotingBribe(sdk: MagmaClmmSDK, locks: string[], incentive_tokens: string[]): Transaction;
3776
3773
  /**
3777
3774
  * build swap transaction
package/dist/index.js CHANGED
@@ -1235,8 +1235,8 @@ var ClmmPoolUtil = class {
1235
1235
  liquidity = estimateLiquidityForCoinB(curSqrtPrice, lowerSqrtPrice, coinAmount);
1236
1236
  }
1237
1237
  const coinAmounts = ClmmPoolUtil.getCoinAmountFromLiquidity(liquidity, curSqrtPrice, lowerSqrtPrice, upperSqrtPrice, roundUp);
1238
- const tokenLimitA = roundUp ? d(coinAmounts.coinA.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinA.toString()).mul(1 - slippage).toString();
1239
- const tokenLimitB = roundUp ? d(coinAmounts.coinB.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinB.toString()).mul(1 - slippage).toString();
1238
+ const tokenLimitA = iscoinA ? d(coinAmounts.coinA.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinA.toString()).mul(1 - slippage).toString();
1239
+ const tokenLimitB = iscoinA ? d(coinAmounts.coinB.toString()).mul(1 + slippage).toString() : d(coinAmounts.coinB.toString()).mul(1 - slippage).toString();
1240
1240
  return {
1241
1241
  coinAmountA: coinAmounts.coinA,
1242
1242
  coinAmountB: coinAmounts.coinB,
@@ -2817,6 +2817,62 @@ var _TransactionUtil = class {
2817
2817
  );
2818
2818
  return tx;
2819
2819
  }
2820
+ /**
2821
+ * build add liquidity with protection transaction
2822
+ * @param params
2823
+ * @param packageId
2824
+ * @returns
2825
+ */
2826
+ static async buildAddLiquidityWithProtectionFixToken(sdk, allCoinAsset, params, tx, inputCoinA, inputCoinB) {
2827
+ if (sdk.senderAddress.length === 0) {
2828
+ throw Error("this config sdk senderAddress is empty");
2829
+ }
2830
+ tx = tx || new import_transactions.Transaction();
2831
+ let primaryCoinAInputs;
2832
+ let primaryCoinBInputs;
2833
+ if (inputCoinA == null || inputCoinB == null) {
2834
+ primaryCoinAInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2835
+ tx,
2836
+ !params.fix_amount_a,
2837
+ params.amount_a,
2838
+ params.slippage,
2839
+ params.coinTypeA,
2840
+ allCoinAsset,
2841
+ false
2842
+ );
2843
+ primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2844
+ tx,
2845
+ params.fix_amount_a,
2846
+ params.amount_b,
2847
+ params.slippage,
2848
+ params.coinTypeB,
2849
+ allCoinAsset,
2850
+ false
2851
+ );
2852
+ } else {
2853
+ primaryCoinAInputs = {
2854
+ targetCoin: inputCoinA,
2855
+ remainCoins: [],
2856
+ isMintZeroCoin: false,
2857
+ tragetCoinAmount: "0"
2858
+ };
2859
+ primaryCoinBInputs = {
2860
+ targetCoin: inputCoinB,
2861
+ remainCoins: [],
2862
+ isMintZeroCoin: false,
2863
+ tragetCoinAmount: "0"
2864
+ };
2865
+ }
2866
+ tx = _TransactionUtil.buildAddLiquidityWithProtectionFixTokenArgs(
2867
+ tx,
2868
+ sdk,
2869
+ allCoinAsset,
2870
+ params,
2871
+ primaryCoinAInputs,
2872
+ primaryCoinBInputs
2873
+ );
2874
+ return tx;
2875
+ }
2820
2876
  static buildAddLiquidityFixTokenCoinInput(tx, need_interval_amount, amount, slippage, coinType, allCoinAsset, buildVector = true) {
2821
2877
  return need_interval_amount ? _TransactionUtil.buildCoinForAmountInterval(
2822
2878
  tx,
@@ -2892,6 +2948,67 @@ var _TransactionUtil = class {
2892
2948
  });
2893
2949
  return tx;
2894
2950
  }
2951
+ static buildAddLiquidityWithProtectionFixTokenArgs(tx, sdk, allCoinAsset, params, primaryCoinAInputs, primaryCoinBInputs) {
2952
+ const typeArguments = [params.coinTypeA, params.coinTypeB];
2953
+ const functionName = params.is_open ? "open_position_with_liquidity_by_fix_coin_with_protection" : "add_liquidity_by_fix_coin_with_protection";
2954
+ const { clmm_pool, integrate } = sdk.sdkOptions;
2955
+ if (!params.is_open) {
2956
+ tx = _TransactionUtil.createCollectRewarderAndFeeParams(
2957
+ sdk,
2958
+ tx,
2959
+ params,
2960
+ allCoinAsset,
2961
+ primaryCoinAInputs.remainCoins,
2962
+ primaryCoinBInputs.remainCoins
2963
+ );
2964
+ }
2965
+ const clmmConfig = getPackagerConfigs(clmm_pool);
2966
+ let min_amount_a, max_amount_a, min_amount_b, max_amount_b;
2967
+ if (params.fix_amount_a) {
2968
+ max_amount_a = params.amount_a;
2969
+ min_amount_a = params.amount_a;
2970
+ max_amount_b = params.amount_b;
2971
+ min_amount_b = new import_decimal7.default(params.amount_b).div(new import_decimal7.default(1).plus(new import_decimal7.default(params.slippage))).mul(new import_decimal7.default(1).minus(new import_decimal7.default(params.slippage))).toNumber();
2972
+ } else {
2973
+ max_amount_b = params.amount_b;
2974
+ min_amount_b = params.amount_b;
2975
+ max_amount_a = params.amount_a;
2976
+ min_amount_a = new import_decimal7.default(params.amount_a).div(new import_decimal7.default(1).plus(new import_decimal7.default(params.slippage))).mul(new import_decimal7.default(1).minus(new import_decimal7.default(params.slippage))).toNumber();
2977
+ }
2978
+ const args = params.is_open ? [
2979
+ tx.object(clmmConfig.global_config_id),
2980
+ tx.object(params.pool_id),
2981
+ tx.pure.u32(Number(asUintN(BigInt(params.tick_lower)).toString())),
2982
+ tx.pure.u32(Number(asUintN(BigInt(params.tick_upper)).toString())),
2983
+ primaryCoinAInputs.targetCoin,
2984
+ primaryCoinBInputs.targetCoin,
2985
+ tx.pure.u64(max_amount_a),
2986
+ tx.pure.u64(max_amount_b),
2987
+ tx.pure.u64(min_amount_a),
2988
+ tx.pure.u64(min_amount_b),
2989
+ tx.pure.bool(params.fix_amount_a),
2990
+ tx.object(CLOCK_ADDRESS)
2991
+ ] : [
2992
+ tx.object(clmmConfig.global_config_id),
2993
+ tx.object(params.pool_id),
2994
+ tx.object(params.pos_id),
2995
+ primaryCoinAInputs.targetCoin,
2996
+ primaryCoinBInputs.targetCoin,
2997
+ tx.pure.u64(max_amount_a),
2998
+ tx.pure.u64(max_amount_b),
2999
+ tx.pure.u64(min_amount_a),
3000
+ tx.pure.u64(min_amount_b),
3001
+ tx.pure.bool(params.fix_amount_a),
3002
+ tx.object(CLOCK_ADDRESS),
3003
+ tx.object(CLOCK_ADDRESS)
3004
+ ];
3005
+ tx.moveCall({
3006
+ target: `${integrate.published_at}::${ClmmIntegratePoolV2Module}::${functionName}`,
3007
+ typeArguments,
3008
+ arguments: args
3009
+ });
3010
+ return tx;
3011
+ }
2895
3012
  // -------------------------------------------swap--------------------------------------------------//
2896
3013
  /**
2897
3014
  * build add liquidity transaction
@@ -3177,65 +3294,6 @@ var _TransactionUtil = class {
3177
3294
  });
3178
3295
  return tx;
3179
3296
  }
3180
- static buildOpenPositionWithLiquidityByFixCoinWithProtection(sdk, params, allCoinAsset) {
3181
- const tx = new import_transactions.Transaction();
3182
- tx.setSender(sdk.senderAddress);
3183
- const { clmm_pool, integrate } = sdk.sdkOptions;
3184
- const globalConfigID = getPackagerConfigs(clmm_pool).global_config_id;
3185
- const typeArguments = [params.coinAType, params.coinBType];
3186
- const functionName = "open_position_with_liquidity_by_fix_coin_with_protection";
3187
- const coinA = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_a), params.coinAType, false, true);
3188
- const coinB = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_b), params.coinBType, false, true);
3189
- const args = [
3190
- tx.object(globalConfigID),
3191
- tx.object(params.pool),
3192
- tx.pure.u32(params.tick_lower),
3193
- tx.pure.u32(params.tick_upper),
3194
- coinA.targetCoin,
3195
- coinB.targetCoin,
3196
- tx.pure.u64(params.max_amount_a),
3197
- tx.pure.u64(params.max_amount_b),
3198
- tx.pure.u64(params.min_amount_a),
3199
- tx.pure.u64(params.min_amount_b),
3200
- tx.pure.bool(params.by_amount_a),
3201
- tx.object(CLOCK_ADDRESS)
3202
- ];
3203
- tx.moveCall({
3204
- target: `${integrate.published_at}::${Voter}::${functionName}`,
3205
- typeArguments,
3206
- arguments: args
3207
- });
3208
- return tx;
3209
- }
3210
- static buildAddLiquidityByFixCoinWithProtection(sdk, params, allCoinAsset) {
3211
- const tx = new import_transactions.Transaction();
3212
- tx.setSender(sdk.senderAddress);
3213
- const { clmm_pool, integrate } = sdk.sdkOptions;
3214
- const globalConfigID = getPackagerConfigs(clmm_pool).global_config_id;
3215
- const typeArguments = [params.coinAType, params.coinBType];
3216
- const functionName = "add_liquidity_by_fix_coin_with_protection";
3217
- const coinA = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_a), params.coinAType, false, true);
3218
- const coinB = _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(params.max_amount_b), params.coinBType, false, true);
3219
- const args = [
3220
- tx.object(globalConfigID),
3221
- tx.object(params.pool),
3222
- tx.object(params.position),
3223
- coinA.targetCoin,
3224
- coinB.targetCoin,
3225
- tx.pure.u64(params.max_amount_a),
3226
- tx.pure.u64(params.max_amount_b),
3227
- tx.pure.u64(params.min_amount_a),
3228
- tx.pure.u64(params.min_amount_b),
3229
- tx.pure.bool(params.fix_amount_a),
3230
- tx.object(CLOCK_ADDRESS)
3231
- ];
3232
- tx.moveCall({
3233
- target: `${integrate.published_at}::${Voter}::${functionName}`,
3234
- typeArguments,
3235
- arguments: args
3236
- });
3237
- return tx;
3238
- }
3239
3297
  static buildClaimVotingBribe(sdk, locks, incentive_tokens) {
3240
3298
  const tx = new import_transactions.Transaction();
3241
3299
  tx.setGasBudget(5e8);
@@ -5509,6 +5567,36 @@ var PositionModule = class {
5509
5567
  }
5510
5568
  return TransactionUtil.buildAddLiquidityFixToken(this._sdk, allCoinAsset, params, tx, inputCoinA, inputCoinB);
5511
5569
  }
5570
+ /**
5571
+ * create add liquidity transaction payload with fix token
5572
+ * @param {AddLiquidityFixTokenParams} params
5573
+ * @param gasEstimateArg : When the fix input amount is SUI, gasEstimateArg can control whether to recalculate the number of SUI to prevent insufficient gas.
5574
+ * If this parameter is not passed, gas estimation is not performed
5575
+ * @returns {Promise<TransactionBlock>}
5576
+ */
5577
+ async createAddLiquidityFixTokenWithProtectionPayload(params, gasEstimateArg, tx, inputCoinA, inputCoinB) {
5578
+ if (!checkInvalidSuiAddress(this._sdk.senderAddress)) {
5579
+ throw new ClmmpoolsError("this config sdk senderAddress is not set right", "InvalidSendAddress" /* InvalidSendAddress */);
5580
+ }
5581
+ const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5582
+ if (gasEstimateArg) {
5583
+ const { isAdjustCoinA, isAdjustCoinB } = findAdjustCoin(params);
5584
+ params = params;
5585
+ if (params.fix_amount_a && isAdjustCoinA || !params.fix_amount_a && isAdjustCoinB) {
5586
+ tx = await TransactionUtil.buildAddLiquidityFixTokenForGas(
5587
+ this._sdk,
5588
+ allCoinAsset,
5589
+ params,
5590
+ gasEstimateArg,
5591
+ tx,
5592
+ inputCoinA,
5593
+ inputCoinB
5594
+ );
5595
+ return tx;
5596
+ }
5597
+ }
5598
+ return TransactionUtil.buildAddLiquidityWithProtectionFixToken(this._sdk, allCoinAsset, params, tx, inputCoinA, inputCoinB);
5599
+ }
5512
5600
  /**
5513
5601
  * create add liquidity transaction payload
5514
5602
  * @param {AddLiquidityParams} params
@@ -5755,20 +5843,6 @@ var PositionModule = class {
5755
5843
  feeOwedB: "0"
5756
5844
  };
5757
5845
  }
5758
- async openPositionWithLiquidityByFixCoinWithProtection(params) {
5759
- if (this._sdk.senderAddress.length === 0) {
5760
- throw Error("this config sdk senderAddress is empty");
5761
- }
5762
- const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5763
- return TransactionUtil.buildOpenPositionWithLiquidityByFixCoinWithProtection(this.sdk, params, allCoinAsset);
5764
- }
5765
- async addLiquidityByFixCoinWithProtection(params) {
5766
- if (this._sdk.senderAddress.length === 0) {
5767
- throw Error("this config sdk senderAddress is empty");
5768
- }
5769
- const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5770
- return TransactionUtil.buildAddLiquidityByFixCoinWithProtection(this.sdk, params, allCoinAsset);
5771
- }
5772
5846
  /**
5773
5847
  * Updates the cache for the given key.
5774
5848
  * @param {string} key The key of the cache entry to update.