@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.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 = 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();
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();
1037
1037
  return {
1038
1038
  coinAmountA: coinAmounts.coinA,
1039
1039
  coinAmountB: coinAmounts.coinB,
@@ -2520,7 +2520,8 @@ var _TransactionUtil = class {
2520
2520
  params.slippage,
2521
2521
  params.coinTypeA,
2522
2522
  allCoins,
2523
- false
2523
+ false,
2524
+ true
2524
2525
  );
2525
2526
  primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2526
2527
  newTx,
@@ -2529,7 +2530,8 @@ var _TransactionUtil = class {
2529
2530
  params.slippage,
2530
2531
  params.coinTypeB,
2531
2532
  allCoins,
2532
- false
2533
+ false,
2534
+ true
2533
2535
  );
2534
2536
  } else {
2535
2537
  params.amount_b = Number(fixAmount);
@@ -2540,7 +2542,8 @@ var _TransactionUtil = class {
2540
2542
  params.slippage,
2541
2543
  params.coinTypeA,
2542
2544
  allCoins,
2543
- false
2545
+ false,
2546
+ true
2544
2547
  );
2545
2548
  primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2546
2549
  newTx,
@@ -2549,10 +2552,11 @@ var _TransactionUtil = class {
2549
2552
  params.slippage,
2550
2553
  params.coinTypeB,
2551
2554
  allCoins,
2552
- false
2555
+ false,
2556
+ true
2553
2557
  );
2554
2558
  params = _TransactionUtil.fixAddLiquidityFixTokenParams(params, gasEstimateArg.slippage, gasEstimateArg.curSqrtPrice);
2555
- tx = await _TransactionUtil.buildAddLiquidityFixTokenArgs(newTx, sdk, allCoins, params, primaryCoinAInputs, primaryCoinBInputs);
2559
+ tx = _TransactionUtil.buildAddLiquidityFixTokenArgs(newTx, sdk, allCoins, params, primaryCoinAInputs, primaryCoinBInputs);
2556
2560
  return tx;
2557
2561
  }
2558
2562
  }
@@ -2579,7 +2583,8 @@ var _TransactionUtil = class {
2579
2583
  params.slippage,
2580
2584
  params.coinTypeA,
2581
2585
  allCoinAsset,
2582
- false
2586
+ false,
2587
+ true
2583
2588
  );
2584
2589
  primaryCoinBInputs = _TransactionUtil.buildAddLiquidityFixTokenCoinInput(
2585
2590
  tx,
@@ -2588,7 +2593,8 @@ var _TransactionUtil = class {
2588
2593
  params.slippage,
2589
2594
  params.coinTypeB,
2590
2595
  allCoinAsset,
2591
- false
2596
+ false,
2597
+ true
2592
2598
  );
2593
2599
  } else {
2594
2600
  primaryCoinAInputs = {
@@ -2670,14 +2676,15 @@ var _TransactionUtil = class {
2670
2676
  );
2671
2677
  return tx;
2672
2678
  }
2673
- static buildAddLiquidityFixTokenCoinInput(tx, need_interval_amount, amount, slippage, coinType, allCoinAsset, buildVector = true) {
2679
+ static buildAddLiquidityFixTokenCoinInput(tx, need_interval_amount, amount, slippage, coinType, allCoinAsset, buildVector = true, fixAmount = true) {
2674
2680
  return need_interval_amount ? _TransactionUtil.buildCoinForAmountInterval(
2675
2681
  tx,
2676
2682
  allCoinAsset,
2677
2683
  { amountSecond: BigInt(reverSlippageAmount(amount, slippage)), amountFirst: BigInt(amount) },
2678
2684
  coinType,
2679
- buildVector
2680
- ) : _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(amount), coinType, false, true);
2685
+ buildVector,
2686
+ fixAmount
2687
+ ) : _TransactionUtil.buildCoinForAmount(tx, allCoinAsset, BigInt(amount), coinType, buildVector, fixAmount);
2681
2688
  }
2682
2689
  /**
2683
2690
  * fix add liquidity fix token for coin amount
@@ -2772,11 +2779,6 @@ var _TransactionUtil = class {
2772
2779
  max_amount_a = params.amount_a;
2773
2780
  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))).toDecimalPlaces(0).toNumber();
2774
2781
  }
2775
- console.log("min_amount_a", min_amount_a);
2776
- console.log("max_amount_a", max_amount_a);
2777
- console.log("min_amount_b", min_amount_b);
2778
- console.log("max_amount_b", max_amount_b);
2779
- console.log("params.fix_amount_a", params.fix_amount_a);
2780
2782
  const args = params.is_open ? [
2781
2783
  tx.object(clmmConfig.global_config_id),
2782
2784
  tx.object(params.pool_id),
@@ -3331,17 +3333,17 @@ var _TransactionUtil = class {
3331
3333
  }
3332
3334
  return params;
3333
3335
  }
3334
- static async syncBuildCoinInputForAmount(sdk, tx, amount, coinType, buildVector = true) {
3336
+ static async syncBuildCoinInputForAmount(sdk, tx, amount, coinType, buildVector = true, fixAmount = true) {
3335
3337
  if (sdk.senderAddress.length === 0) {
3336
3338
  throw Error("this config sdk senderAddress is empty");
3337
3339
  }
3338
3340
  const allCoins = await sdk.getOwnerCoinAssets(sdk.senderAddress, coinType);
3339
- const primaryCoinInput = _TransactionUtil.buildCoinForAmount(tx, allCoins, amount, coinType, buildVector).targetCoin;
3341
+ const primaryCoinInput = _TransactionUtil.buildCoinForAmount(tx, allCoins, amount, coinType, buildVector, fixAmount).targetCoin;
3340
3342
  return primaryCoinInput;
3341
3343
  }
3342
- static buildCoinForAmount(tx, allCoins, amount, coinType, buildVector = true, fixAmount = false) {
3344
+ static buildCoinForAmount(tx, allCoins, amount, coinType, buildVector = true, fixAmount = true) {
3343
3345
  const coinAssets = CoinAssist.getCoinAssets(coinType, allCoins);
3344
- if (amount === BigInt(0) && coinAssets.length === 0) {
3346
+ if (amount === BigInt(0)) {
3345
3347
  return _TransactionUtil.buildZeroValueCoin(allCoins, tx, coinType, buildVector);
3346
3348
  }
3347
3349
  const amountTotal = CoinAssist.calculateTotalBalance(coinAssets);
@@ -3353,18 +3355,41 @@ var _TransactionUtil = class {
3353
3355
  }
3354
3356
  return _TransactionUtil.buildCoin(tx, allCoins, coinAssets, amount, coinType, buildVector, fixAmount);
3355
3357
  }
3356
- static buildCoin(tx, allCoins, coinAssets, amount, coinType, buildVector = true, fixAmount = false) {
3358
+ static buildVectorCoin(tx, allCoins, coinAssets, amount, coinType, fixAmount = true) {
3359
+ if (CoinAssist.isSuiCoin(coinType)) {
3360
+ const amountCoin = tx.splitCoins(tx.gas, [tx.pure.u64(amount)]);
3361
+ return {
3362
+ targetCoin: tx.makeMoveVec({ elements: [amountCoin] }),
3363
+ remainCoins: allCoins,
3364
+ tragetCoinAmount: amount.toString(),
3365
+ isMintZeroCoin: false,
3366
+ originalSplitedCoin: tx.gas
3367
+ };
3368
+ }
3369
+ const { targetCoin, originalSplitedCoin, tragetCoinAmount, selectedCoinsResult, coinObjectIds } = this.buildSpitTargeCoin(
3370
+ tx,
3371
+ amount,
3372
+ coinAssets,
3373
+ fixAmount
3374
+ );
3375
+ if (fixAmount) {
3376
+ return {
3377
+ targetCoin: tx.makeMoveVec({ elements: [targetCoin] }),
3378
+ remainCoins: selectedCoinsResult.remainCoins,
3379
+ originalSplitedCoin,
3380
+ tragetCoinAmount,
3381
+ isMintZeroCoin: false
3382
+ };
3383
+ }
3384
+ return {
3385
+ targetCoin: tx.makeMoveVec({ elements: coinObjectIds.map((id) => tx.object(id)) }),
3386
+ remainCoins: selectedCoinsResult.remainCoins,
3387
+ tragetCoinAmount: selectedCoinsResult.amountArray.reduce((a, b) => Number(a) + Number(b), 0).toString(),
3388
+ isMintZeroCoin: false
3389
+ };
3390
+ }
3391
+ static buildOneCoin(tx, coinAssets, amount, coinType, fixAmount = true) {
3357
3392
  if (CoinAssist.isSuiCoin(coinType)) {
3358
- if (buildVector) {
3359
- const amountCoin2 = tx.splitCoins(tx.gas, [tx.pure.u64(amount)]);
3360
- return {
3361
- targetCoin: tx.makeMoveVec({ elements: [amountCoin2] }),
3362
- remainCoins: allCoins,
3363
- tragetCoinAmount: amount.toString(),
3364
- isMintZeroCoin: false,
3365
- originalSplitedCoin: tx.gas
3366
- };
3367
- }
3368
3393
  if (amount === 0n && coinAssets.length > 1) {
3369
3394
  const selectedCoinsResult3 = CoinAssist.selectCoinObjectIdGreaterThanOrEqual(coinAssets, amount);
3370
3395
  return {
@@ -3384,17 +3409,24 @@ var _TransactionUtil = class {
3384
3409
  originalSplitedCoin: tx.gas
3385
3410
  };
3386
3411
  }
3412
+ const { targetCoin, originalSplitedCoin, tragetCoinAmount, selectedCoinsResult } = this.buildSpitTargeCoin(
3413
+ tx,
3414
+ amount,
3415
+ coinAssets,
3416
+ fixAmount
3417
+ );
3418
+ return {
3419
+ targetCoin,
3420
+ remainCoins: selectedCoinsResult.remainCoins,
3421
+ originalSplitedCoin,
3422
+ tragetCoinAmount,
3423
+ isMintZeroCoin: false
3424
+ };
3425
+ }
3426
+ static buildSpitTargeCoin(tx, amount, coinAssets, fixAmount) {
3387
3427
  const selectedCoinsResult = CoinAssist.selectCoinObjectIdGreaterThanOrEqual(coinAssets, amount);
3388
3428
  const totalSelectedCoinAmount = selectedCoinsResult.amountArray.reduce((a, b) => Number(a) + Number(b), 0).toString();
3389
3429
  const coinObjectIds = selectedCoinsResult.objectArray;
3390
- if (buildVector) {
3391
- return {
3392
- targetCoin: tx.makeMoveVec({ elements: coinObjectIds.map((id) => tx.object(id)) }),
3393
- remainCoins: selectedCoinsResult.remainCoins,
3394
- tragetCoinAmount: selectedCoinsResult.amountArray.reduce((a, b) => Number(a) + Number(b), 0).toString(),
3395
- isMintZeroCoin: false
3396
- };
3397
- }
3398
3430
  const [primaryCoinA, ...mergeCoinAs] = coinObjectIds;
3399
3431
  const primaryCoinAObject = tx.object(primaryCoinA);
3400
3432
  let targetCoin = primaryCoinAObject;
@@ -3411,13 +3443,19 @@ var _TransactionUtil = class {
3411
3443
  originalSplitedCoin = primaryCoinAObject;
3412
3444
  }
3413
3445
  return {
3414
- targetCoin,
3415
- remainCoins: selectedCoinsResult.remainCoins,
3416
3446
  originalSplitedCoin,
3447
+ targetCoin,
3417
3448
  tragetCoinAmount,
3418
- isMintZeroCoin: false
3449
+ selectedCoinsResult,
3450
+ coinObjectIds
3419
3451
  };
3420
3452
  }
3453
+ static buildCoin(tx, allCoins, coinAssets, amount, coinType, buildVector = true, fixAmount = true) {
3454
+ if (buildVector) {
3455
+ return this.buildVectorCoin(tx, allCoins, coinAssets, amount, coinType, fixAmount);
3456
+ }
3457
+ return this.buildOneCoin(tx, coinAssets, amount, coinType, fixAmount);
3458
+ }
3421
3459
  static buildZeroValueCoin(allCoins, tx, coinType, buildVector = true) {
3422
3460
  const zeroCoin = _TransactionUtil.callMintZeroValueCoin(tx, coinType);
3423
3461
  let targetCoin;
@@ -3433,17 +3471,17 @@ var _TransactionUtil = class {
3433
3471
  tragetCoinAmount: "0"
3434
3472
  };
3435
3473
  }
3436
- static buildCoinForAmountInterval(tx, allCoins, amounts, coinType, buildVector = true) {
3474
+ static buildCoinForAmountInterval(tx, allCoins, amounts, coinType, buildVector = true, fixAmount = true) {
3437
3475
  const coinAssets = CoinAssist.getCoinAssets(coinType, allCoins);
3438
3476
  if (amounts.amountFirst === BigInt(0)) {
3439
3477
  if (coinAssets.length > 0) {
3440
- return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector);
3478
+ return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector, fixAmount);
3441
3479
  }
3442
3480
  return _TransactionUtil.buildZeroValueCoin(allCoins, tx, coinType, buildVector);
3443
3481
  }
3444
3482
  const amountTotal = CoinAssist.calculateTotalBalance(coinAssets);
3445
3483
  if (amountTotal >= amounts.amountFirst) {
3446
- return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector);
3484
+ return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountFirst, coinType, buildVector, fixAmount);
3447
3485
  }
3448
3486
  if (amountTotal < amounts.amountSecond) {
3449
3487
  throw new ClmmpoolsError(
@@ -3451,7 +3489,7 @@ var _TransactionUtil = class {
3451
3489
  "InsufficientBalance" /* InsufficientBalance */
3452
3490
  );
3453
3491
  }
3454
- return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountSecond, coinType, buildVector);
3492
+ return _TransactionUtil.buildCoin(tx, [...allCoins], [...coinAssets], amounts.amountSecond, coinType, buildVector, fixAmount);
3455
3493
  }
3456
3494
  // ------------------------------------------router-v1-------------------------------------------------//
3457
3495
  static async buildRouterSwapTransaction(sdk, params, byAmountIn, allCoinAsset, recipient) {
@@ -5419,8 +5457,8 @@ var PositionModule = class {
5419
5457
  let primaryCoinBInputs;
5420
5458
  if (inputCoinA == null || inputCoinB == null) {
5421
5459
  const allCoinAsset = await this._sdk.getOwnerCoinAssets(this._sdk.senderAddress);
5422
- primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_a, params.coinTypeA, false);
5423
- primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_b, params.coinTypeB, false);
5460
+ primaryCoinAInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_a, params.coinTypeA, false, true);
5461
+ primaryCoinBInputs = TransactionUtil.buildCoinForAmount(tx, allCoinAsset, max_amount_b, params.coinTypeB, false, true);
5424
5462
  } else {
5425
5463
  primaryCoinAInputs = {
5426
5464
  targetCoin: inputCoinA,