@cetusprotocol/aggregator-sdk 0.0.0-experimental-20250829185106 → 0.0.0-experimental-20250903180855

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.js CHANGED
@@ -3340,7 +3340,7 @@ var SuiZeroCoinFn = "0x2::coin::zero";
3340
3340
  var DEEPBOOK_PACKAGE_ID = "0x000000000000000000000000000000000000000000000000000000000000dee9";
3341
3341
  var DEEPBOOK_PUBLISHED_AT = "0x000000000000000000000000000000000000000000000000000000000000dee9";
3342
3342
  var CETUS_PUBLISHED_AT = "0x70968826ad1b4ba895753f634b0aea68d0672908ca1075a2abdf0fc9e0b2fc6a";
3343
- var CETUS_V3_PUBLISHED_AT = "0x550dcd6070230d8bf18d99d34e3b2ca1d3657b76cc80ffdacdb2b5d28d7e0124";
3343
+ var CETUS_V3_PUBLISHED_AT = "0x73a9bcf98bd1f4602bd4bb76f235b20253a17722cda3438d1710b70c831168af";
3344
3344
  var MAINNET_CETUS_GLOBAL_CONFIG_ID = "0xdaa46292632c3c4d8f31f23ea0f9b36a28ff3677e9684980e4438403a67a3d8f";
3345
3345
  var TESTNET_CETUS_GLOBAL_CONFIG_ID = "0x6f4149091a5aea0e818e7243a13adcfb403842d670b9a2089de058512620687a";
3346
3346
  var MAINNET_FLOWX_AMM_CONTAINER_ID = "0xb65dcbf63fd3ad5d0ebfbf334780dc9f785eff38a4459e37ab08fa79576ee511";
@@ -3411,7 +3411,8 @@ var CLIENT_CONFIG = {
3411
3411
  PYTH_UNAVAILABLE: "All Pyth price nodes are unavailable. Cannot fetch price data. Please switch to or add new available Pyth nodes",
3412
3412
  QUOTE_ID_REQUIRED: "Quote ID is required",
3413
3413
  AGGREGATOR_V3_PACKAGE_REQUIRED: "Aggregator V3 package is required",
3414
- PACKAGES_REQUIRED: "Packages are required"
3414
+ PACKAGES_REQUIRED: "Packages are required",
3415
+ OVERLAY_FEE_RECEIVER_REQUIRED: "Overlay fee rate is set, but overlay fee receiver is not set"
3415
3416
  }
3416
3417
  };
3417
3418
  var AGGREGATOR_V3_CONFIG = {
@@ -3420,7 +3421,7 @@ var AGGREGATOR_V3_CONFIG = {
3420
3421
  // 10%
3421
3422
  MAX_AMOUNT_IN: U64_MAX,
3422
3423
  DEFAULT_PUBLISHED_AT: {
3423
- Mainnet: "0x07c27e879ba9282506284b0fef26d393978906fc9496550d978c6f493dbfa3e5",
3424
+ Mainnet: "0x0422e3239f174320760dd675ba3835cf4b1c511be4a36e819cb61fab7d5d404a",
3424
3425
  Testnet: "0x0"
3425
3426
  }
3426
3427
  };
@@ -3444,6 +3445,7 @@ function parseRouterResponse(data, byAmountIn) {
3444
3445
  amountOut: new import_bn2.default(data.amount_out.toString()),
3445
3446
  byAmountIn,
3446
3447
  insufficientLiquidity: false,
3448
+ deviationRatio: data.deviation_ratio,
3447
3449
  packages,
3448
3450
  paths: data.paths.map((path) => ({
3449
3451
  id: path.id,
@@ -3460,50 +3462,6 @@ function parseRouterResponse(data, byAmountIn) {
3460
3462
  }))
3461
3463
  };
3462
3464
  }
3463
- function parseMergeSwapResponse(data) {
3464
- var _a;
3465
- let packages = /* @__PURE__ */ new Map();
3466
- if (data.packages) {
3467
- if (data.packages instanceof Map) {
3468
- packages = data.packages;
3469
- } else if (typeof data.packages === "object") {
3470
- Object.entries(data.packages).forEach(([key, value]) => {
3471
- packages.set(key, value);
3472
- });
3473
- }
3474
- }
3475
- const allRoutes = [];
3476
- if (data.all_routes) {
3477
- for (const route of data.all_routes) {
3478
- const mergeRoute = {
3479
- amountIn: new import_bn2.default(route.amount_in.toString()),
3480
- amountOut: new import_bn2.default(route.amount_out.toString()),
3481
- deviationRatio: route.deviation_ratio,
3482
- paths: route.paths.map((path) => ({
3483
- id: path.id,
3484
- direction: path.direction,
3485
- provider: path.provider,
3486
- from: path.from,
3487
- target: path.target,
3488
- feeRate: path.fee_rate,
3489
- amountIn: path.amount_in.toString(),
3490
- amountOut: path.amount_out.toString(),
3491
- version: path.version,
3492
- publishedAt: path.published_at,
3493
- extendedDetails: path.extended_details
3494
- }))
3495
- };
3496
- allRoutes.push(mergeRoute);
3497
- }
3498
- }
3499
- return {
3500
- quoteID: data.request_id || "",
3501
- totalAmountOut: new import_bn2.default(((_a = data.total_amount_out) == null ? void 0 : _a.toString()) || "0"),
3502
- allRoutes,
3503
- packages,
3504
- gas: data.gas
3505
- };
3506
- }
3507
3465
  function getRouterResult(endpoint, apiKey, params, overlayFee, overlayFeeReceiver) {
3508
3466
  return __async(this, null, function* () {
3509
3467
  let response;
@@ -3632,7 +3590,6 @@ function getRouter(endpoint, apiKey, params) {
3632
3590
  url += `&apiKey=${apiKey}`;
3633
3591
  }
3634
3592
  url += `&v=${SDK_VERSION}`;
3635
- console.log("url", url);
3636
3593
  const response = yield fetch(url);
3637
3594
  return response;
3638
3595
  } catch (error) {
@@ -3692,94 +3649,6 @@ function postRouterWithLiquidityChanges(endpoint, params) {
3692
3649
  }
3693
3650
  });
3694
3651
  }
3695
- function getMergeSwapRouter(endpoint, apiKey, params) {
3696
- return __async(this, null, function* () {
3697
- try {
3698
- const { target, byAmountIn, depth, providers, froms } = params;
3699
- const targetCoin = completionCoin(target);
3700
- let url = `${endpoint}/multi_find_routes?target=${targetCoin}&by_amount_in=${byAmountIn}`;
3701
- if (depth) {
3702
- url += `&depth=${depth}`;
3703
- }
3704
- if (providers && providers.length > 0) {
3705
- url += `&providers=${providers.join(",")}`;
3706
- }
3707
- if (apiKey.length > 0) {
3708
- url += `&apiKey=${apiKey}`;
3709
- }
3710
- url += `&v=${SDK_VERSION}`;
3711
- const fromsData = froms.map((from) => ({
3712
- coin_type: completionCoin(from.coinType),
3713
- amount: Number(from.amount.toString())
3714
- }));
3715
- url += `&froms=${encodeURIComponent(JSON.stringify(fromsData))}`;
3716
- const response = yield fetch(url);
3717
- return response;
3718
- } catch (error) {
3719
- console.error(error);
3720
- return null;
3721
- }
3722
- });
3723
- }
3724
- function getMergeSwapResult(endpoint, apiKey, params, overlayFee, overlayFeeReceiver) {
3725
- return __async(this, null, function* () {
3726
- const response = yield getMergeSwapRouter(endpoint, apiKey, params);
3727
- if (!response) {
3728
- return null;
3729
- }
3730
- if (!response.ok) {
3731
- let errorCode = 1e3 /* NumberTooLarge */;
3732
- if (response.status === 429) {
3733
- errorCode = 1001 /* RateLimitExceeded */;
3734
- }
3735
- return {
3736
- quoteID: "",
3737
- totalAmountOut: ZERO,
3738
- allRoutes: [],
3739
- error: {
3740
- code: errorCode,
3741
- msg: getAggregatorServerErrorMessage(errorCode)
3742
- }
3743
- };
3744
- }
3745
- const responseText = yield response.text();
3746
- const data = JSONbig__default.default.parse(responseText);
3747
- data.msg === "liquidity is not enough";
3748
- if (data.msg && data.msg.indexOf("HoneyPot scam") > -1) {
3749
- return {
3750
- quoteID: "",
3751
- totalAmountOut: ZERO,
3752
- allRoutes: [],
3753
- error: {
3754
- code: 1003 /* HoneyPot */,
3755
- msg: getAggregatorServerErrorMessage(
3756
- 1003 /* HoneyPot */
3757
- )
3758
- }
3759
- };
3760
- }
3761
- if (data.data != null) {
3762
- const res = parseMergeSwapResponse(data.data);
3763
- if (overlayFee > 0 && overlayFeeReceiver !== "0x0" && params.byAmountIn) {
3764
- const overlayFeeAmount = res.totalAmountOut.mul(new import_bn2.default(overlayFee)).div(new import_bn2.default(1e6));
3765
- res.totalAmountOut = res.totalAmountOut.sub(overlayFeeAmount);
3766
- for (const route of res.allRoutes) {
3767
- const routeFee = route.amountOut.mul(new import_bn2.default(overlayFee)).div(new import_bn2.default(1e6));
3768
- route.amountOut = route.amountOut.sub(routeFee);
3769
- }
3770
- }
3771
- if (!res.packages) {
3772
- res.packages = /* @__PURE__ */ new Map();
3773
- }
3774
- return res;
3775
- }
3776
- return {
3777
- quoteID: "",
3778
- totalAmountOut: ZERO,
3779
- allRoutes: []
3780
- };
3781
- });
3782
- }
3783
3652
  function getDeepbookV3Config(endpoint) {
3784
3653
  return __async(this, null, function* () {
3785
3654
  const url = `${endpoint}/deepbookv3_config`;
@@ -3811,7 +3680,6 @@ function processFlattenRoutes(routerData) {
3811
3680
  flattenedPaths[i].isLastUseOfIntermediateToken = true;
3812
3681
  }
3813
3682
  }
3814
- console.log("flattenedPaths", flattenedPaths);
3815
3683
  return {
3816
3684
  quoteID: routerData.quoteID || "",
3817
3685
  amountIn: routerData.amountIn,
@@ -8398,6 +8266,11 @@ var _AggregatorClient = class _AggregatorClient {
8398
8266
  return DEEPBOOK_V3_DEEP_FEE_TYPES.Testnet;
8399
8267
  }
8400
8268
  }
8269
+ getDeepbookV3Config() {
8270
+ return __async(this, null, function* () {
8271
+ return yield getDeepbookV3Config(this.endpoint);
8272
+ });
8273
+ }
8401
8274
  getOneCoinUsedToMerge(coinType) {
8402
8275
  return __async(this, null, function* () {
8403
8276
  try {
@@ -8426,17 +8299,6 @@ var _AggregatorClient = class _AggregatorClient {
8426
8299
  );
8427
8300
  });
8428
8301
  }
8429
- findMergeSwapRouters(params) {
8430
- return __async(this, null, function* () {
8431
- return getMergeSwapResult(
8432
- this.endpoint,
8433
- this.apiKey,
8434
- params,
8435
- this.overlayFeeRate,
8436
- this.overlayFeeReceiver
8437
- );
8438
- });
8439
- }
8440
8302
  executeFlexibleInputSwap(txb, inputCoin, routerData, expectedAmountOut, amountLimit, pythPriceIDs, partner, deepbookv3DeepFee, packages) {
8441
8303
  return __async(this, null, function* () {
8442
8304
  });
@@ -8659,20 +8521,22 @@ var _AggregatorClient = class _AggregatorClient {
8659
8521
  }
8660
8522
  routerSwap(params) {
8661
8523
  return __async(this, null, function* () {
8662
- const { router, inputCoin, slippage, txb, fixable, partner } = params;
8524
+ const { router, inputCoin, slippage, txb, partner } = params;
8663
8525
  if (slippage > 1 || slippage < 0) {
8664
8526
  throw new Error(CLIENT_CONFIG.ERRORS.INVALID_SLIPPAGE);
8665
8527
  }
8528
+ if (!params.router.packages || !params.router.packages.get(PACKAGE_NAMES.AGGREGATOR_V3)) {
8529
+ throw new Error(CLIENT_CONFIG.ERRORS.PACKAGES_REQUIRED);
8530
+ }
8666
8531
  const byAmountIn = params.router.byAmountIn;
8667
8532
  const amountIn = router.amountIn;
8668
8533
  const amountOut = router.amountOut;
8534
+ checkOverlayFeeConfig(this.overlayFeeRate, this.overlayFeeReceiver);
8669
8535
  let overlayFee = new import_bn6.default(0);
8670
- if (this.overlayFeeRate > 0 && this.overlayFeeReceiver !== "0x0") {
8671
- if (byAmountIn) {
8672
- overlayFee = amountOut.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6));
8673
- } else {
8674
- overlayFee = amountIn.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6));
8675
- }
8536
+ if (byAmountIn) {
8537
+ overlayFee = amountOut.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6));
8538
+ } else {
8539
+ overlayFee = amountIn.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6));
8676
8540
  }
8677
8541
  const expectedAmountOut = byAmountIn ? amountOut.sub(overlayFee) : amountOut;
8678
8542
  const expectedAmountIn = byAmountIn ? amountIn : amountIn.add(overlayFee);
@@ -8681,9 +8545,6 @@ var _AggregatorClient = class _AggregatorClient {
8681
8545
  byAmountIn,
8682
8546
  slippage
8683
8547
  );
8684
- if (!params.router.packages || !params.router.packages.get(PACKAGE_NAMES.AGGREGATOR_V3)) {
8685
- throw new Error(CLIENT_CONFIG.ERRORS.PACKAGES_REQUIRED);
8686
- }
8687
8548
  const priceIDs = findPythPriceIDs(router.paths);
8688
8549
  const priceInfoObjectIds = priceIDs.length > 0 ? yield this.updatePythPriceIDs(priceIDs, txb) : /* @__PURE__ */ new Map();
8689
8550
  if (byAmountIn) {
@@ -8716,17 +8577,16 @@ var _AggregatorClient = class _AggregatorClient {
8716
8577
  const fromCoinType = router.paths[0].from;
8717
8578
  const targetCoinType = router.paths[router.paths.length - 1].target;
8718
8579
  const byAmountIn = router.byAmountIn;
8580
+ checkOverlayFeeConfig(this.overlayFeeRate, this.overlayFeeReceiver);
8719
8581
  let overlayFee = 0;
8720
- if (this.overlayFeeRate > 0 && this.overlayFeeReceiver !== "0x0") {
8721
- if (byAmountIn) {
8722
- overlayFee = Number(
8723
- router.amountOut.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8724
- );
8725
- } else {
8726
- overlayFee = Number(
8727
- router.amountIn.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8728
- );
8729
- }
8582
+ if (byAmountIn) {
8583
+ overlayFee = Number(
8584
+ router.amountOut.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8585
+ );
8586
+ } else {
8587
+ overlayFee = Number(
8588
+ router.amountIn.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8589
+ );
8730
8590
  }
8731
8591
  const expectedAmountOut = byAmountIn ? router.amountOut.sub(new import_bn6.default(overlayFee)) : router.amountOut;
8732
8592
  const expectedAmountIn = byAmountIn ? router.amountIn : router.amountIn.add(new import_bn6.default(overlayFee));
@@ -8776,116 +8636,19 @@ var _AggregatorClient = class _AggregatorClient {
8776
8636
  }
8777
8637
  });
8778
8638
  }
8779
- mergeSwap(params) {
8780
- return __async(this, null, function* () {
8781
- const { router, inputCoins, slippage, txb, partner } = params;
8782
- if (slippage > 1 || slippage < 0) {
8783
- throw new Error(CLIENT_CONFIG.ERRORS.INVALID_SLIPPAGE);
8784
- }
8785
- if (!router.packages || !router.packages.get(PACKAGE_NAMES.AGGREGATOR_V3)) {
8786
- throw new Error(CLIENT_CONFIG.ERRORS.PACKAGES_REQUIRED);
8787
- }
8788
- if (!router.allRoutes || router.allRoutes.length === 0) {
8789
- throw new Error("No routes found in merge swap response");
8790
- }
8791
- const outputCoins = [];
8792
- for (let i = 0; i < router.allRoutes.length && i < inputCoins.length; i++) {
8793
- const route = router.allRoutes[i];
8794
- const inputCoin = inputCoins[i];
8795
- const routeRouter = {
8796
- quoteID: router.quoteID,
8797
- amountIn: route.amountIn,
8798
- amountOut: route.amountOut,
8799
- byAmountIn: true,
8800
- // Merge swap is always by amount in
8801
- paths: route.paths,
8802
- insufficientLiquidity: false,
8803
- packages: router.packages
8804
- };
8805
- const routerParams = {
8806
- router: routeRouter,
8807
- inputCoin: inputCoin.coin,
8808
- slippage,
8809
- txb,
8810
- partner: partner != null ? partner : this.partner
8811
- };
8812
- const outputCoin = yield this.routerSwap(routerParams);
8813
- outputCoins.push(outputCoin);
8814
- }
8815
- if (outputCoins.length === 0) {
8816
- throw new Error("No output coins generated from merge swap");
8817
- }
8818
- let finalOutputCoin = outputCoins[0];
8819
- if (outputCoins.length > 1) {
8820
- txb.mergeCoins(finalOutputCoin, outputCoins.slice(1));
8821
- }
8822
- return finalOutputCoin;
8823
- });
8824
- }
8825
- fastMergeSwap(params) {
8826
- return __async(this, null, function* () {
8827
- const { router, slippage, txb, partner } = params;
8828
- if (!router || !router.allRoutes || router.allRoutes.length === 0) {
8829
- throw new Error("Invalid router: no routes found");
8830
- }
8831
- const firstRoute = router.allRoutes[0];
8832
- const targetCoinType = firstRoute.paths[firstRoute.paths.length - 1].target;
8833
- const inputCoins = [];
8834
- const coinTypeSet = /* @__PURE__ */ new Set();
8835
- for (const route of router.allRoutes) {
8836
- const firstCoinType = route.paths[0].from;
8837
- if (coinTypeSet.has(firstCoinType)) {
8838
- continue;
8839
- }
8840
- coinTypeSet.add(firstCoinType);
8841
- const coin = transactions.coinWithBalance({
8842
- balance: BigInt(route.amountIn.toString()),
8843
- useGasCoin: CoinUtils.isSuiCoin(firstCoinType),
8844
- type: firstCoinType
8845
- });
8846
- inputCoins.push({ coinType: firstCoinType, coin });
8847
- }
8848
- const mergeSwapParams = {
8849
- router,
8850
- inputCoins,
8851
- slippage,
8852
- txb,
8853
- partner: partner != null ? partner : this.partner
8854
- };
8855
- const targetCoin = yield this.mergeSwap(mergeSwapParams);
8856
- if (CoinUtils.isSuiCoin(targetCoinType)) {
8857
- txb.mergeCoins(txb.gas, [targetCoin]);
8858
- } else {
8859
- const targetCoinObjID = yield this.getOneCoinUsedToMerge(targetCoinType);
8860
- if (targetCoinObjID != null) {
8861
- txb.mergeCoins(txb.object(targetCoinObjID), [targetCoin]);
8862
- } else {
8863
- transferOrDestroyCoin(
8864
- {
8865
- coin: targetCoin,
8866
- coinType: targetCoinType,
8867
- packages: router.packages
8868
- },
8869
- txb
8870
- );
8871
- }
8872
- }
8873
- });
8874
- }
8875
8639
  fixableRouterSwapV3(params) {
8876
8640
  return __async(this, null, function* () {
8877
- const { router, inputCoin, slippage, txb, deepbookv3DeepFee, partner } = params;
8641
+ const { router, inputCoin, slippage, txb, partner } = params;
8642
+ checkOverlayFeeConfig(this.overlayFeeRate, this.overlayFeeReceiver);
8878
8643
  let overlayFee = 0;
8879
- if (this.overlayFeeRate > 0 && this.overlayFeeReceiver !== "0x0") {
8880
- if (router.byAmountIn) {
8881
- overlayFee = Number(
8882
- router.amountOut.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8883
- );
8884
- } else {
8885
- overlayFee = Number(
8886
- router.amountIn.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8887
- );
8888
- }
8644
+ if (router.byAmountIn) {
8645
+ overlayFee = Number(
8646
+ router.amountOut.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8647
+ );
8648
+ } else {
8649
+ overlayFee = Number(
8650
+ router.amountIn.mul(new import_bn6.default(this.overlayFeeRate)).div(new import_bn6.default(1e6)).toString()
8651
+ );
8889
8652
  }
8890
8653
  const expectedAmountOut = router.byAmountIn ? router.amountOut.sub(new import_bn6.default(overlayFee)) : router.amountOut;
8891
8654
  const expectedAmountIn = router.byAmountIn ? router.amountIn : router.amountIn.add(new import_bn6.default(overlayFee));
@@ -8994,6 +8757,7 @@ var _AggregatorClient = class _AggregatorClient {
8994
8757
  const routeData = {
8995
8758
  amountIn,
8996
8759
  amountOut: new import_bn6.default((_d = event.amount_out) != null ? _d : 0),
8760
+ deviationRatio: 0,
8997
8761
  paths: [
8998
8762
  {
8999
8763
  id: pools[tempIndex],
@@ -9111,6 +8875,16 @@ function recordFirstCoinIndex(paths) {
9111
8875
  }
9112
8876
  return newCoinRecord;
9113
8877
  }
8878
+ function checkOverlayFeeConfig(overlayFeeRate, overlayFeeReceiver) {
8879
+ if (overlayFeeRate > CLIENT_CONFIG.MAX_OVERLAY_FEE_RATE) {
8880
+ throw new Error(CLIENT_CONFIG.ERRORS.INVALID_OVERLAY_FEE_RATE);
8881
+ }
8882
+ if (overlayFeeReceiver === "0x0" && overlayFeeRate > 0) {
8883
+ throw new Error(
8884
+ CLIENT_CONFIG.ERRORS.OVERLAY_FEE_RECEIVER_REQUIRED
8885
+ );
8886
+ }
8887
+ }
9114
8888
 
9115
8889
  // src/types/sui.ts
9116
8890
  var getDefaultSuiInputType = (value) => {
@@ -9290,7 +9064,6 @@ exports.getAggregatorV2PublishedAt = getAggregatorV2PublishedAt;
9290
9064
  exports.getAllProviders = getAllProviders;
9291
9065
  exports.getDeepbookV3Config = getDeepbookV3Config;
9292
9066
  exports.getDefaultSuiInputType = getDefaultSuiInputType;
9293
- exports.getMergeSwapResult = getMergeSwapResult;
9294
9067
  exports.getOrCreateAccountCap = getOrCreateAccountCap;
9295
9068
  exports.getProvidersExcluding = getProvidersExcluding;
9296
9069
  exports.getProvidersIncluding = getProvidersIncluding;