@t2000/sdk 1.24.14 → 1.25.0

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.cjs CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  var transactions = require('@mysten/sui/transactions');
4
4
  var aggregatorSdk = require('@cetusprotocol/aggregator-sdk');
5
+ var BN = require('bn.js');
5
6
  var eventemitter3 = require('eventemitter3');
6
7
  var paymentKit = require('@mysten/payment-kit');
7
8
  var jsonRpc = require('@mysten/sui/jsonRpc');
@@ -17,6 +18,10 @@ var bcs$1 = require('@mysten/sui/bcs');
17
18
  var fs = require('fs');
18
19
  var suins = require('@mysten/suins');
19
20
 
21
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
22
+
23
+ var BN__default = /*#__PURE__*/_interopDefault(BN);
24
+
20
25
  var __create = Object.create;
21
26
  var __defProp = Object.defineProperty;
22
27
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -919,10 +924,111 @@ __export(cetus_swap_exports, {
919
924
  TOKEN_MAP: () => exports.TOKEN_MAP,
920
925
  addSwapToTx: () => addSwapToTx,
921
926
  buildSwapTx: () => buildSwapTx,
927
+ deserializeCetusRoute: () => deserializeCetusRoute,
922
928
  findSwapRoute: () => findSwapRoute,
929
+ isCetusRouteFresh: () => isCetusRouteFresh,
923
930
  resolveTokenType: () => resolveTokenType,
924
- simulateSwap: () => simulateSwap
931
+ serializeCetusRoute: () => serializeCetusRoute,
932
+ simulateSwap: () => simulateSwap,
933
+ verifyCetusRouteCoinMatch: () => verifyCetusRouteCoinMatch
925
934
  });
935
+ function serializeCetusRoute(route, context) {
936
+ return {
937
+ routerData: serializeRouterDataV3(route.routerData),
938
+ amountIn: route.amountIn,
939
+ amountOut: route.amountOut,
940
+ byAmountIn: route.byAmountIn,
941
+ priceImpact: route.priceImpact,
942
+ insufficientLiquidity: route.insufficientLiquidity,
943
+ discoveredAt: Date.now(),
944
+ fromCoinType: context.fromCoinType,
945
+ toCoinType: context.toCoinType
946
+ };
947
+ }
948
+ function deserializeCetusRoute(serialized) {
949
+ return {
950
+ routerData: deserializeRouterDataV3(serialized.routerData),
951
+ amountIn: serialized.amountIn,
952
+ amountOut: serialized.amountOut,
953
+ byAmountIn: serialized.byAmountIn,
954
+ priceImpact: serialized.priceImpact,
955
+ insufficientLiquidity: serialized.insufficientLiquidity
956
+ };
957
+ }
958
+ function serializeRouterDataV3(rd) {
959
+ const out = {
960
+ amountIn: rd.amountIn.toString(),
961
+ amountOut: rd.amountOut.toString(),
962
+ byAmountIn: rd.byAmountIn,
963
+ paths: rd.paths.map(serializeCetusRoutePath),
964
+ insufficientLiquidity: rd.insufficientLiquidity,
965
+ deviationRatio: rd.deviationRatio
966
+ };
967
+ if (rd.quoteID !== void 0) out.quoteID = rd.quoteID;
968
+ if (rd.packages) {
969
+ const obj = {};
970
+ for (const [k2, v] of rd.packages) obj[k2] = v;
971
+ out.packages = obj;
972
+ }
973
+ if (rd.totalDeepFee !== void 0) out.totalDeepFee = rd.totalDeepFee;
974
+ if (rd.error) out.error = { code: rd.error.code, msg: rd.error.msg };
975
+ if (rd.overlayFee !== void 0) out.overlayFee = rd.overlayFee;
976
+ return out;
977
+ }
978
+ function deserializeRouterDataV3(s) {
979
+ const out = {
980
+ amountIn: new BN__default.default(s.amountIn),
981
+ amountOut: new BN__default.default(s.amountOut),
982
+ byAmountIn: s.byAmountIn,
983
+ paths: s.paths.map(deserializeCetusRoutePath),
984
+ insufficientLiquidity: s.insufficientLiquidity,
985
+ deviationRatio: s.deviationRatio
986
+ };
987
+ if (s.quoteID !== void 0) out.quoteID = s.quoteID;
988
+ if (s.packages) out.packages = new Map(Object.entries(s.packages));
989
+ if (s.totalDeepFee !== void 0) out.totalDeepFee = s.totalDeepFee;
990
+ if (s.error) out.error = { code: s.error.code, msg: s.error.msg };
991
+ if (s.overlayFee !== void 0) out.overlayFee = s.overlayFee;
992
+ return out;
993
+ }
994
+ function serializeCetusRoutePath(p) {
995
+ const out = {
996
+ id: p.id,
997
+ direction: p.direction,
998
+ provider: p.provider,
999
+ from: p.from,
1000
+ target: p.target,
1001
+ feeRate: p.feeRate,
1002
+ amountIn: p.amountIn,
1003
+ amountOut: p.amountOut
1004
+ };
1005
+ if (p.version !== void 0) out.version = p.version;
1006
+ if (p.publishedAt !== void 0) out.publishedAt = p.publishedAt;
1007
+ if (p.extendedDetails) out.extendedDetails = { ...p.extendedDetails };
1008
+ return out;
1009
+ }
1010
+ function deserializeCetusRoutePath(p) {
1011
+ const out = {
1012
+ id: p.id,
1013
+ direction: p.direction,
1014
+ provider: p.provider,
1015
+ from: p.from,
1016
+ target: p.target,
1017
+ feeRate: p.feeRate,
1018
+ amountIn: p.amountIn,
1019
+ amountOut: p.amountOut
1020
+ };
1021
+ if (p.version !== void 0) out.version = p.version;
1022
+ if (p.publishedAt !== void 0) out.publishedAt = p.publishedAt;
1023
+ if (p.extendedDetails) out.extendedDetails = { ...p.extendedDetails };
1024
+ return out;
1025
+ }
1026
+ function verifyCetusRouteCoinMatch(serialized, expected) {
1027
+ return serialized.fromCoinType === expected.fromCoinType && serialized.toCoinType === expected.toCoinType;
1028
+ }
1029
+ function isCetusRouteFresh(serialized, maxAgeMs = 3e4) {
1030
+ return Date.now() - serialized.discoveredAt < maxAgeMs;
1031
+ }
926
1032
  function getClient(walletAddress, overlayFee) {
927
1033
  const rate = overlayFee?.rate ?? 0;
928
1034
  const receiver = overlayFee?.receiver ?? "";
@@ -1012,15 +1118,22 @@ async function addSwapToTx(tx, client, address, input) {
1012
1118
  inputCoin = result.coin;
1013
1119
  effectiveRaw = result.effectiveAmount;
1014
1120
  }
1015
- const route = await findSwapRoute({
1016
- walletAddress: address,
1017
- from: fromType,
1018
- to: toType,
1019
- amount: effectiveRaw,
1020
- byAmountIn,
1021
- overlayFee: input.overlayFee,
1022
- providers: input.providers
1023
- });
1121
+ let route;
1122
+ let usedPrecomputedRoute = false;
1123
+ if (input.precomputedRoute && input.precomputedRoute.amountIn === effectiveRaw.toString() && input.precomputedRoute.byAmountIn === byAmountIn) {
1124
+ route = input.precomputedRoute;
1125
+ usedPrecomputedRoute = true;
1126
+ } else {
1127
+ route = await findSwapRoute({
1128
+ walletAddress: address,
1129
+ from: fromType,
1130
+ to: toType,
1131
+ amount: effectiveRaw,
1132
+ byAmountIn,
1133
+ overlayFee: input.overlayFee,
1134
+ providers: input.providers
1135
+ });
1136
+ }
1024
1137
  if (!route) {
1025
1138
  throw new T2000Error2("SWAP_NO_ROUTE", `No swap route found for ${input.from} \u2192 ${input.to}`);
1026
1139
  }
@@ -1039,7 +1152,8 @@ async function addSwapToTx(tx, client, address, input) {
1039
1152
  coin: outputCoin,
1040
1153
  effectiveAmountIn: Number(effectiveRaw) / 10 ** fromDecimals,
1041
1154
  expectedAmountOut: Number(route.amountOut) / 10 ** toDecimals,
1042
- route
1155
+ route,
1156
+ usedPrecomputedRoute
1043
1157
  };
1044
1158
  }
1045
1159
  async function simulateSwap(params) {
@@ -7839,7 +7953,8 @@ var WRITE_APPENDER_REGISTRY = {
7839
7953
  byAmountIn: input.byAmountIn,
7840
7954
  overlayFee: ctx.overlayFee,
7841
7955
  providers,
7842
- inputCoin: ctx.chainedCoin
7956
+ inputCoin: ctx.chainedCoin,
7957
+ precomputedRoute: input.precomputedRoute
7843
7958
  });
7844
7959
  if (!ctx.isOutputConsumed) {
7845
7960
  tx.transferObjects([result.coin], ctx.sender);
@@ -8230,13 +8345,16 @@ async function getSwapQuote(params) {
8230
8345
  const fromAmount = Number(route.amountIn) / 10 ** fromDecimals;
8231
8346
  const toAmount = Number(route.amountOut) / 10 ** toDecimals;
8232
8347
  const routeDesc = route.routerData.paths?.map((p) => p.provider).filter(Boolean).slice(0, 3).join(" + ") ?? "Cetus Aggregator";
8348
+ const { serializeCetusRoute: serializeCetusRoute2 } = await Promise.resolve().then(() => (init_cetus_swap(), cetus_swap_exports));
8349
+ const serializedRoute = serializeCetusRoute2(route, { fromCoinType: fromType, toCoinType: toType });
8233
8350
  return {
8234
8351
  fromToken: params.from,
8235
8352
  toToken: params.to,
8236
8353
  fromAmount,
8237
8354
  toAmount,
8238
8355
  priceImpact: route.priceImpact,
8239
- route: routeDesc
8356
+ route: routeDesc,
8357
+ serializedRoute
8240
8358
  };
8241
8359
  }
8242
8360
 
@@ -8366,6 +8484,7 @@ exports.classifyLabel = classifyLabel;
8366
8484
  exports.classifyTransaction = classifyTransaction;
8367
8485
  exports.composeTx = composeTx;
8368
8486
  exports.deriveAllowedAddressesFromPtb = deriveAllowedAddressesFromPtb;
8487
+ exports.deserializeCetusRoute = deserializeCetusRoute;
8369
8488
  exports.displayHandle = displayHandle;
8370
8489
  exports.exportPrivateKey = exportPrivateKey;
8371
8490
  exports.extractTransferDetails = extractTransferDetails;
@@ -8391,6 +8510,7 @@ exports.getSwapQuote = getSwapQuote;
8391
8510
  exports.getTier = getTier;
8392
8511
  exports.getVoloStats = getVoloStats;
8393
8512
  exports.isAllowedAsset = isAllowedAsset;
8513
+ exports.isCetusRouteFresh = isCetusRouteFresh;
8394
8514
  exports.isInRegistry = isInRegistry;
8395
8515
  exports.isSupported = isSupported;
8396
8516
  exports.isTier1 = isTier1;
@@ -8414,6 +8534,7 @@ exports.resolveTokenType = resolveTokenType;
8414
8534
  exports.saveKey = saveKey;
8415
8535
  exports.selectAndSplitCoin = selectAndSplitCoin;
8416
8536
  exports.selectSuiCoin = selectSuiCoin;
8537
+ exports.serializeCetusRoute = serializeCetusRoute;
8417
8538
  exports.simulateTransaction = simulateTransaction;
8418
8539
  exports.stableToRaw = stableToRaw;
8419
8540
  exports.suiToMist = suiToMist;
@@ -8422,6 +8543,7 @@ exports.truncateAddress = truncateAddress;
8422
8543
  exports.usdcToRaw = usdcToRaw;
8423
8544
  exports.validateAddress = validateAddress;
8424
8545
  exports.validateLabel = validateLabel;
8546
+ exports.verifyCetusRouteCoinMatch = verifyCetusRouteCoinMatch;
8425
8547
  exports.walletExists = walletExists;
8426
8548
  //# sourceMappingURL=index.cjs.map
8427
8549
  //# sourceMappingURL=index.cjs.map