@magmaprotocol/magma-clmm-sdk 0.5.5 → 0.5.7

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
@@ -3722,7 +3722,7 @@ declare class TransactionUtil {
3722
3722
  * @returns
3723
3723
  */
3724
3724
  static buildAddLiquidityWithProtectionFixToken(sdk: MagmaClmmSDK, allCoinAsset: CoinAsset[], params: AddLiquidityFixTokenParams, tx?: Transaction, inputCoinA?: TransactionObjectArgument, inputCoinB?: TransactionObjectArgument): Promise<Transaction>;
3725
- static buildAddLiquidityFixTokenCoinInput(tx: Transaction, need_interval_amount: boolean, amount: number | string, slippage: number, coinType: string, allCoinAsset: CoinAsset[], buildVector?: boolean): BuildCoinResult;
3725
+ static buildAddLiquidityFixTokenCoinInput(tx: Transaction, need_interval_amount: boolean, amount: number | string, slippage: number, coinType: string, allCoinAsset: CoinAsset[], buildVector?: boolean, fixAmount?: boolean): BuildCoinResult;
3726
3726
  /**
3727
3727
  * fix add liquidity fix token for coin amount
3728
3728
  * @param params
@@ -3823,11 +3823,14 @@ declare class TransactionUtil {
3823
3823
  swapTicks: Array<TickData>;
3824
3824
  currentPool: Pool;
3825
3825
  }): Promise<SwapParams>;
3826
- static syncBuildCoinInputForAmount(sdk: MagmaClmmSDK, tx: Transaction, amount: bigint, coinType: string, buildVector?: boolean): Promise<TransactionObjectArgument | undefined>;
3826
+ static syncBuildCoinInputForAmount(sdk: MagmaClmmSDK, tx: Transaction, amount: bigint, coinType: string, buildVector?: boolean, fixAmount?: boolean): Promise<TransactionObjectArgument | undefined>;
3827
3827
  static buildCoinForAmount(tx: Transaction, allCoins: CoinAsset[], amount: bigint, coinType: string, buildVector?: boolean, fixAmount?: boolean): BuildCoinResult;
3828
+ private static buildVectorCoin;
3829
+ private static buildOneCoin;
3830
+ private static buildSpitTargeCoin;
3828
3831
  private static buildCoin;
3829
3832
  private static buildZeroValueCoin;
3830
- static buildCoinForAmountInterval(tx: Transaction, allCoins: CoinAsset[], amounts: CoinInputInterval, coinType: string, buildVector?: boolean): BuildCoinResult;
3833
+ static buildCoinForAmountInterval(tx: Transaction, allCoins: CoinAsset[], amounts: CoinInputInterval, coinType: string, buildVector?: boolean, fixAmount?: boolean): BuildCoinResult;
3831
3834
  static callMintZeroValueCoin: (txb: Transaction, coinType: string) => _mysten_sui_transactions.TransactionResult;
3832
3835
  static buildRouterSwapTransaction(sdk: MagmaClmmSDK, params: SwapWithRouterParams, byAmountIn: boolean, allCoinAsset: CoinAsset[], recipient?: string): Promise<Transaction>;
3833
3836
  static buildRouterBasePathTx(sdk: MagmaClmmSDK, params: SwapWithRouterParams, byAmountIn: boolean, allCoinAsset: CoinAsset[], tx: Transaction, recipient?: string): Promise<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 = 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();
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();
1240
1240
  return {
1241
1241
  coinAmountA: coinAmounts.coinA,
1242
1242
  coinAmountB: coinAmounts.coinB,
@@ -2723,7 +2723,8 @@ var _TransactionUtil = class {
2723
2723
  params.slippage,
2724
2724
  params.coinTypeA,
2725
2725
  allCoins,
2726
- false
2726
+ false,
2727
+ true
2727
2728
  );
2728
2729
  primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2729
2730
  newTx,
@@ -2732,7 +2733,8 @@ var _TransactionUtil = class {
2732
2733
  params.slippage,
2733
2734
  params.coinTypeB,
2734
2735
  allCoins,
2735
- false
2736
+ false,
2737
+ true
2736
2738
  );
2737
2739
  } else {
2738
2740
  params.amount_b = Number(fixAmount);
@@ -2743,7 +2745,8 @@ var _TransactionUtil = class {
2743
2745
  params.slippage,
2744
2746
  params.coinTypeA,
2745
2747
  allCoins,
2746
- false
2748
+ false,
2749
+ true
2747
2750
  );
2748
2751
  primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2749
2752
  newTx,
@@ -2752,10 +2755,11 @@ var _TransactionUtil = class {
2752
2755
  params.slippage,
2753
2756
  params.coinTypeB,
2754
2757
  allCoins,
2755
- false
2758
+ false,
2759
+ true
2756
2760
  );
2757
2761
  params = _TransactionUtil.fixAddLiquidityFixTokenParams(params, gasEstimateArg.slippage, gasEstimateArg.curSqrtPrice);
2758
- tx = await _TransactionUtil.buildAddLiquidityFixTokenArgs(newTx, sdk, allCoins, params, primaryCoinAInputs, primaryCoinBInputs);
2762
+ tx = _TransactionUtil.buildAddLiquidityFixTokenArgs(newTx, sdk, allCoins, params, primaryCoinAInputs, primaryCoinBInputs);
2759
2763
  return tx;
2760
2764
  }
2761
2765
  }
@@ -2782,7 +2786,8 @@ var _TransactionUtil = class {
2782
2786
  params.slippage,
2783
2787
  params.coinTypeA,
2784
2788
  allCoinAsset,
2785
- false
2789
+ false,
2790
+ true
2786
2791
  );
2787
2792
  primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2788
2793
  tx,
@@ -2791,7 +2796,8 @@ var _TransactionUtil = class {
2791
2796
  params.slippage,
2792
2797
  params.coinTypeB,
2793
2798
  allCoinAsset,
2794
- false
2799
+ false,
2800
+ true
2795
2801
  );
2796
2802
  } else {
2797
2803
  primaryCoinAInputs = {
@@ -2873,14 +2879,15 @@ var _TransactionUtil = class {
2873
2879
  );
2874
2880
  return tx;
2875
2881
  }
2876
- static buildAddLiquidityFixTokenCoinInput(tx, need_interval_amount, amount, slippage, coinType, allCoinAsset, buildVector = true) {
2882
+ static buildAddLiquidityFixTokenCoinInput(tx, need_interval_amount, amount, slippage, coinType, allCoinAsset, buildVector = true, fixAmount = true) {
2877
2883
  return need_interval_amount ? _TransactionUtil.buildCoinForAmountInterval(
2878
2884
  tx,
2879
2885
  allCoinAsset,
2880
2886
  { amountSecond: BigInt(reverSlippageAmount(amount, slippage)), amountFirst: BigInt(amount) },
2881
2887
  coinType,
2882
- buildVector
2883
- ) : _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(amount), coinType, false, true);
2888
+ buildVector,
2889
+ fixAmount
2890
+ ) : _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(amount), coinType, buildVector, fixAmount);
2884
2891
  }
2885
2892
  /**
2886
2893
  * fix add liquidity fix token for coin amount
@@ -2975,11 +2982,6 @@ var _TransactionUtil = class {
2975
2982
  max_amount_a = params.amount_a;
2976
2983
  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))).toDecimalPlaces(0).toNumber();
2977
2984
  }
2978
- console.log("min_amount_a", min_amount_a);
2979
- console.log("max_amount_a", max_amount_a);
2980
- console.log("min_amount_b", min_amount_b);
2981
- console.log("max_amount_b", max_amount_b);
2982
- console.log("params.fix_amount_a", params.fix_amount_a);
2983
2985
  const args = params.is_open ? [
2984
2986
  tx.object(clmmConfig.global_config_id),
2985
2987
  tx.object(params.pool_id),
@@ -3534,17 +3536,17 @@ var _TransactionUtil = class {
3534
3536
  }
3535
3537
  return params;
3536
3538
  }
3537
- static async syncBuildCoinInputForAmount(sdk, tx, amount, coinType, buildVector = true) {
3539
+ static async syncBuildCoinInputForAmount(sdk, tx, amount, coinType, buildVector = true, fixAmount = true) {
3538
3540
  if (sdk.senderAddress.length === 0) {
3539
3541
  throw Error("this config sdk senderAddress is empty");
3540
3542
  }
3541
3543
  const allCoins = await sdk.getOwnerCoinAssets(sdk.senderAddress, coinType);
3542
- const primaryCoinInput = _TransactionUtil.buildCoinForAmount(tx, allCoins, amount, coinType, buildVector).targetCoin;
3544
+ const primaryCoinInput = _TransactionUtil.buildCoinForAmount(tx, allCoins, amount, coinType, buildVector, fixAmount).targetCoin;
3543
3545
  return primaryCoinInput;
3544
3546
  }
3545
- static buildCoinForAmount(tx, allCoins, amount, coinType, buildVector = true, fixAmount = false) {
3547
+ static buildCoinForAmount(tx, allCoins, amount, coinType, buildVector = true, fixAmount = true) {
3546
3548
  const coinAssets = CoinAssist.getCoinAssets(coinType, allCoins);
3547
- if (amount === BigInt(0) && coinAssets.length === 0) {
3549
+ if (amount === BigInt(0)) {
3548
3550
  return _TransactionUtil.buildZeroValueCoin(allCoins, tx, coinType, buildVector);
3549
3551
  }
3550
3552
  const amountTotal = CoinAssist.calculateTotalBalance(coinAssets);
@@ -3556,18 +3558,41 @@ var _TransactionUtil = class {
3556
3558
  }
3557
3559
  return _TransactionUtil.buildCoin(tx, allCoins, coinAssets, amount, coinType, buildVector, fixAmount);
3558
3560
  }
3559
- static buildCoin(tx, allCoins, coinAssets, amount, coinType, buildVector = true, fixAmount = false) {
3561
+ static buildVectorCoin(tx, allCoins, coinAssets, amount, coinType, fixAmount = true) {
3562
+ if (CoinAssist.isSuiCoin(coinType)) {
3563
+ const amountCoin = tx.splitCoins(tx.gas, [tx.pure.u64(amount)]);
3564
+ return {
3565
+ targetCoin: tx.makeMoveVec({ elements: [amountCoin] }),
3566
+ remainCoins: allCoins,
3567
+ tragetCoinAmount: amount.toString(),
3568
+ isMintZeroCoin: false,
3569
+ originalSplitedCoin: tx.gas
3570
+ };
3571
+ }
3572
+ const { targetCoin, originalSplitedCoin, tragetCoinAmount, selectedCoinsResult, coinObjectIds } = this.buildSpitTargeCoin(
3573
+ tx,
3574
+ amount,
3575
+ coinAssets,
3576
+ fixAmount
3577
+ );
3578
+ if (fixAmount) {
3579
+ return {
3580
+ targetCoin: tx.makeMoveVec({ elements: [targetCoin] }),
3581
+ remainCoins: selectedCoinsResult.remainCoins,
3582
+ originalSplitedCoin,
3583
+ tragetCoinAmount,
3584
+ isMintZeroCoin: false
3585
+ };
3586
+ }
3587
+ return {
3588
+ targetCoin: tx.makeMoveVec({ elements: coinObjectIds.map((id) => tx.object(id)) }),
3589
+ remainCoins: selectedCoinsResult.remainCoins,
3590
+ tragetCoinAmount: selectedCoinsResult.amountArray.reduce((a, b) => Number(a) + Number(b), 0).toString(),
3591
+ isMintZeroCoin: false
3592
+ };
3593
+ }
3594
+ static buildOneCoin(tx, coinAssets, amount, coinType, fixAmount = true) {
3560
3595
  if (CoinAssist.isSuiCoin(coinType)) {
3561
- if (buildVector) {
3562
- const amountCoin2 = tx.splitCoins(tx.gas, [tx.pure.u64(amount)]);
3563
- return {
3564
- targetCoin: tx.makeMoveVec({ elements: [amountCoin2] }),
3565
- remainCoins: allCoins,
3566
- tragetCoinAmount: amount.toString(),
3567
- isMintZeroCoin: false,
3568
- originalSplitedCoin: tx.gas
3569
- };
3570
- }
3571
3596
  if (amount === 0n && coinAssets.length > 1) {
3572
3597
  const selectedCoinsResult3 = CoinAssist.selectCoinObjectIdGreaterThanOrEqual(coinAssets, amount);
3573
3598
  return {
@@ -3587,17 +3612,24 @@ var _TransactionUtil = class {
3587
3612
  originalSplitedCoin: tx.gas
3588
3613
  };
3589
3614
  }
3615
+ const { targetCoin, originalSplitedCoin, tragetCoinAmount, selectedCoinsResult } = this.buildSpitTargeCoin(
3616
+ tx,
3617
+ amount,
3618
+ coinAssets,
3619
+ fixAmount
3620
+ );
3621
+ return {
3622
+ targetCoin,
3623
+ remainCoins: selectedCoinsResult.remainCoins,
3624
+ originalSplitedCoin,
3625
+ tragetCoinAmount,
3626
+ isMintZeroCoin: false
3627
+ };
3628
+ }
3629
+ static buildSpitTargeCoin(tx, amount, coinAssets, fixAmount) {
3590
3630
  const selectedCoinsResult = CoinAssist.selectCoinObjectIdGreaterThanOrEqual(coinAssets, amount);
3591
3631
  const totalSelectedCoinAmount = selectedCoinsResult.amountArray.reduce((a, b) => Number(a) + Number(b), 0).toString();
3592
3632
  const coinObjectIds = selectedCoinsResult.objectArray;
3593
- if (buildVector) {
3594
- return {
3595
- targetCoin: tx.makeMoveVec({ elements: coinObjectIds.map((id) => tx.object(id)) }),
3596
- remainCoins: selectedCoinsResult.remainCoins,
3597
- tragetCoinAmount: selectedCoinsResult.amountArray.reduce((a, b) => Number(a) + Number(b), 0).toString(),
3598
- isMintZeroCoin: false
3599
- };
3600
- }
3601
3633
  const [primaryCoinA, ...mergeCoinAs] = coinObjectIds;
3602
3634
  const primaryCoinAObject = tx.object(primaryCoinA);
3603
3635
  let targetCoin = primaryCoinAObject;
@@ -3614,13 +3646,19 @@ var _TransactionUtil = class {
3614
3646
  originalSplitedCoin = primaryCoinAObject;
3615
3647
  }
3616
3648
  return {
3617
- targetCoin,
3618
- remainCoins: selectedCoinsResult.remainCoins,
3619
3649
  originalSplitedCoin,
3650
+ targetCoin,
3620
3651
  tragetCoinAmount,
3621
- isMintZeroCoin: false
3652
+ selectedCoinsResult,
3653
+ coinObjectIds
3622
3654
  };
3623
3655
  }
3656
+ static buildCoin(tx, allCoins, coinAssets, amount, coinType, buildVector = true, fixAmount = true) {
3657
+ if (buildVector) {
3658
+ return this.buildVectorCoin(tx, allCoins, coinAssets, amount, coinType, fixAmount);
3659
+ }
3660
+ return this.buildOneCoin(tx, coinAssets, amount, coinType, fixAmount);
3661
+ }
3624
3662
  static buildZeroValueCoin(allCoins, tx, coinType, buildVector = true) {
3625
3663
  const zeroCoin = _TransactionUtil.callMintZeroValueCoin(tx, coinType);
3626
3664
  let targetCoin;
@@ -3636,17 +3674,17 @@ var _TransactionUtil = class {
3636
3674
  tragetCoinAmount: "0"
3637
3675
  };
3638
3676
  }
3639
- static buildCoinForAmountInterval(tx, allCoins, amounts, coinType, buildVector = true) {
3677
+ static buildCoinForAmountInterval(tx, allCoins, amounts, coinType, buildVector = true, fixAmount = true) {
3640
3678
  const coinAssets = CoinAssist.getCoinAssets(coinType, allCoins);
3641
3679
  if (amounts.amountFirst === BigInt(0)) {
3642
3680
  if (coinAssets.length > 0) {
3643
- return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector);
3681
+ return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector, fixAmount);
3644
3682
  }
3645
3683
  return _TransactionUtil.buildZeroValueCoin(allCoins, tx, coinType, buildVector);
3646
3684
  }
3647
3685
  const amountTotal = CoinAssist.calculateTotalBalance(coinAssets);
3648
3686
  if (amountTotal >= amounts.amountFirst) {
3649
- return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector);
3687
+ return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector, fixAmount);
3650
3688
  }
3651
3689
  if (amountTotal < amounts.amountSecond) {
3652
3690
  throw new ClmmpoolsError(
@@ -3654,7 +3692,7 @@ var _TransactionUtil = class {
3654
3692
  "InsufficientBalance" /* InsufficientBalance */
3655
3693
  );
3656
3694
  }
3657
- return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountSecond, coinType, buildVector);
3695
+ return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountSecond, coinType, buildVector, fixAmount);
3658
3696
  }
3659
3697
  // ------------------------------------------router-v1-------------------------------------------------//
3660
3698
  static async buildRouterSwapTransaction(sdk, params, byAmountIn, allCoinAsset, recipient) {
@@ -5622,8 +5660,8 @@ var PositionModule = class {
5622
5660
  let primaryCoinBInputs;
5623
5661
  if (inputCoinA == null || inputCoinB == null) {
5624
5662
  const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5625
- primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_a, params.coinTypeA, false);
5626
- primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_b, params.coinTypeB, false);
5663
+ primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_a, params.coinTypeA, false, true);
5664
+ primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_b, params.coinTypeB, false, true);
5627
5665
  } else {
5628
5666
  primaryCoinAInputs = {
5629
5667
  targetCoin: inputCoinA,