@rhea-finance/cross-chain-aggregation-dex 0.1.2 → 0.1.3
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.mts +40 -1
- package/dist/index.d.ts +40 -1
- package/dist/index.js +236 -200
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +234 -200
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var Big3 = require('big.js');
|
|
4
4
|
|
|
5
5
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
6
|
|
|
7
|
-
var
|
|
7
|
+
var Big3__default = /*#__PURE__*/_interopDefault(Big3);
|
|
8
8
|
|
|
9
9
|
// src/types/index.ts
|
|
10
10
|
function requiresRecipient(params) {
|
|
@@ -58,8 +58,6 @@ var logger = {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
|
-
|
|
62
|
-
// src/utils/index.ts
|
|
63
61
|
var bluechipTokensConfig = null;
|
|
64
62
|
function setBluechipTokensConfig(config) {
|
|
65
63
|
bluechipTokensConfig = config;
|
|
@@ -247,6 +245,13 @@ function formatGasString(gas) {
|
|
|
247
245
|
}
|
|
248
246
|
return gasStr;
|
|
249
247
|
}
|
|
248
|
+
function selectBestQuote(quotes) {
|
|
249
|
+
return quotes.reduce((best, current) => {
|
|
250
|
+
const bestAmount = new Big3__default.default(best.quote.amountOut);
|
|
251
|
+
const currentAmount = new Big3__default.default(current.quote.amountOut);
|
|
252
|
+
return currentAmount.gt(bestAmount) ? current : best;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
250
255
|
var NearSmartRouter = class {
|
|
251
256
|
constructor(config) {
|
|
252
257
|
this.findPathAdapter = config.findPathAdapter;
|
|
@@ -345,7 +350,7 @@ var NearSmartRouter = class {
|
|
|
345
350
|
};
|
|
346
351
|
}
|
|
347
352
|
const { routes: serverRoutes, amount_out } = response.result_data;
|
|
348
|
-
const slippageDecimal = new
|
|
353
|
+
const slippageDecimal = new Big3__default.default(slippageBps).div(1e4);
|
|
349
354
|
const routes = serverRoutes.map((route) => ({
|
|
350
355
|
pools: route.pools.map((pool) => ({
|
|
351
356
|
pool_id: Number(pool.pool_id),
|
|
@@ -358,8 +363,8 @@ var NearSmartRouter = class {
|
|
|
358
363
|
amountIn,
|
|
359
364
|
amountOut: route.amount_out || amount_out || "0"
|
|
360
365
|
}));
|
|
361
|
-
const amountOut = new
|
|
362
|
-
const minAmountOut = amountOut.mul(new
|
|
366
|
+
const amountOut = new Big3__default.default(amount_out || 0);
|
|
367
|
+
const minAmountOut = amountOut.mul(new Big3__default.default(1).minus(slippageDecimal)).toFixed(0, Big3__default.default.roundDown);
|
|
363
368
|
return {
|
|
364
369
|
success: true,
|
|
365
370
|
tokenIn,
|
|
@@ -697,7 +702,7 @@ var AggregateDexRouter = class {
|
|
|
697
702
|
});
|
|
698
703
|
}
|
|
699
704
|
async reFetchQuoteWithBalance(quoteParams, actualBalance, context) {
|
|
700
|
-
const balanceBig = new
|
|
705
|
+
const balanceBig = new Big3__default.default(actualBalance);
|
|
701
706
|
const adjustedParams = {
|
|
702
707
|
...quoteParams,
|
|
703
708
|
amountIn: balanceBig.toFixed(0)
|
|
@@ -714,18 +719,18 @@ var AggregateDexRouter = class {
|
|
|
714
719
|
}
|
|
715
720
|
}
|
|
716
721
|
async ensureQuoteAmountWithinBalance(quoteParams, actualBalance, context) {
|
|
717
|
-
const requestedAmountBig = new
|
|
718
|
-
const balanceBig = new
|
|
722
|
+
const requestedAmountBig = new Big3__default.default(quoteParams.amountIn);
|
|
723
|
+
const balanceBig = new Big3__default.default(actualBalance);
|
|
719
724
|
const isNativeNear = (quoteParams.tokenIn.symbol === "NEAR" || quoteParams.tokenIn.address === "near" || !quoteParams.tokenIn.address && quoteParams.tokenIn.symbol === "NEAR") && quoteParams.tokenIn.address !== this.wrapNearContractId;
|
|
720
725
|
let effectiveBalanceBig = balanceBig;
|
|
721
726
|
let effectiveBalanceStr = actualBalance;
|
|
722
727
|
if (isNativeNear) {
|
|
723
|
-
const reserveAmount = new
|
|
728
|
+
const reserveAmount = new Big3__default.default("50000000000000000000000");
|
|
724
729
|
if (balanceBig.gt(reserveAmount)) {
|
|
725
730
|
effectiveBalanceBig = balanceBig.minus(reserveAmount);
|
|
726
731
|
effectiveBalanceStr = effectiveBalanceBig.toFixed(0);
|
|
727
732
|
} else {
|
|
728
|
-
effectiveBalanceBig = new
|
|
733
|
+
effectiveBalanceBig = new Big3__default.default(0);
|
|
729
734
|
effectiveBalanceStr = "0";
|
|
730
735
|
}
|
|
731
736
|
}
|
|
@@ -745,7 +750,7 @@ var AggregateDexRouter = class {
|
|
|
745
750
|
throw new Error(`Failed to fetch quote: ${quote.error || "Unknown error"}`);
|
|
746
751
|
}
|
|
747
752
|
if (quote.amountIn !== quoteParams.amountIn) {
|
|
748
|
-
const apiAmountBig = new
|
|
753
|
+
const apiAmountBig = new Big3__default.default(quote.amountIn);
|
|
749
754
|
if (apiAmountBig.gt(effectiveBalanceBig) && balanceBig.gt(0)) {
|
|
750
755
|
return await this.reFetchQuoteWithBalance(quoteParams, effectiveBalanceStr, `${context} (API returned amount_in exceeds available balance)`);
|
|
751
756
|
}
|
|
@@ -943,8 +948,8 @@ var AggregateDexRouter = class {
|
|
|
943
948
|
(_, index) => !registeredStatus[index]
|
|
944
949
|
);
|
|
945
950
|
if (unregisteredTokens.length > 0) {
|
|
946
|
-
const depositPerToken = new
|
|
947
|
-
new
|
|
951
|
+
const depositPerToken = new Big3__default.default("0.005").mul(
|
|
952
|
+
new Big3__default.default("1000000000000000000000000")
|
|
948
953
|
);
|
|
949
954
|
const totalDeposit = depositPerToken.mul(unregisteredTokens.length);
|
|
950
955
|
transactions.push({
|
|
@@ -1055,7 +1060,8 @@ async function completeQuote(params, config) {
|
|
|
1055
1060
|
const {
|
|
1056
1061
|
sourceToken,
|
|
1057
1062
|
targetToken,
|
|
1058
|
-
sourceChain,
|
|
1063
|
+
sourceChain: _sourceChain,
|
|
1064
|
+
// Reserved for future use
|
|
1059
1065
|
targetChain: _targetChain,
|
|
1060
1066
|
// Reserved for future use
|
|
1061
1067
|
amountIn,
|
|
@@ -1069,7 +1075,8 @@ async function completeQuote(params, config) {
|
|
|
1069
1075
|
dexRouter,
|
|
1070
1076
|
bluechipTokens,
|
|
1071
1077
|
configAdapter,
|
|
1072
|
-
currentUserAddress
|
|
1078
|
+
currentUserAddress,
|
|
1079
|
+
isIntentsSupportedToken: customIsIntentsSupportedToken
|
|
1073
1080
|
} = config;
|
|
1074
1081
|
const wrapNearContractId = configAdapter.getWrapNearContractId();
|
|
1075
1082
|
const routers = dexRouters || (dexRouter ? [dexRouter] : []);
|
|
@@ -1086,7 +1093,7 @@ async function completeQuote(params, config) {
|
|
|
1086
1093
|
if (!targetToken?.address) {
|
|
1087
1094
|
throw new Error("Target token address is required");
|
|
1088
1095
|
}
|
|
1089
|
-
const
|
|
1096
|
+
const isTokenIntentsSupported = customIsIntentsSupportedToken ? customIsIntentsSupportedToken(sourceToken) : isNearIntentsSupportedToken(sourceToken, bluechipTokens);
|
|
1090
1097
|
const bluechipToken = findBestBluechipToken(
|
|
1091
1098
|
bluechipTokens,
|
|
1092
1099
|
wrapNearContractId
|
|
@@ -1098,214 +1105,241 @@ async function completeQuote(params, config) {
|
|
|
1098
1105
|
});
|
|
1099
1106
|
throw new Error("Failed to find bluechip token address");
|
|
1100
1107
|
}
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
if (needsPreSwap) {
|
|
1109
|
-
if (!sourceToken?.address) {
|
|
1110
|
-
throw new Error("Source token address is required");
|
|
1111
|
-
}
|
|
1112
|
-
logger.debug("DEX Aggregator - Pre-swap quote params:", {
|
|
1113
|
-
tokenIn: {
|
|
1114
|
-
address: sourceToken.address,
|
|
1115
|
-
symbol: sourceToken.symbol
|
|
1116
|
-
},
|
|
1117
|
-
tokenOut: {
|
|
1118
|
-
address: bluechipToken.address,
|
|
1119
|
-
symbol: bluechipToken.symbol
|
|
1120
|
-
},
|
|
1108
|
+
const quotePaths = [];
|
|
1109
|
+
routers.forEach((router, index) => {
|
|
1110
|
+
const routeType = index === 0 ? "v1" : "v2";
|
|
1111
|
+
const capabilities = router.getCapabilities();
|
|
1112
|
+
const quoteParams = capabilities.requiresRecipient ? {
|
|
1113
|
+
tokenIn: sourceToken,
|
|
1114
|
+
tokenOut: bluechipToken,
|
|
1121
1115
|
amountIn,
|
|
1122
1116
|
slippage,
|
|
1123
|
-
|
|
1124
|
-
userAddress
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
swapType: "EXACT_INPUT"
|
|
1143
|
-
};
|
|
1144
|
-
return router.quote(quoteParams);
|
|
1145
|
-
})
|
|
1146
|
-
);
|
|
1147
|
-
const validQuotes = quotes.filter(
|
|
1148
|
-
(r) => r.status === "fulfilled" && r.value.success
|
|
1149
|
-
).map((r) => r.value);
|
|
1150
|
-
if (validQuotes.length === 0) {
|
|
1151
|
-
const errors = quotes.map((r, index) => {
|
|
1152
|
-
if (r.status === "rejected") {
|
|
1153
|
-
return `Router ${index}: ${r.reason}`;
|
|
1117
|
+
swapType: "EXACT_INPUT",
|
|
1118
|
+
sender: userAddress,
|
|
1119
|
+
recipient: userAddress
|
|
1120
|
+
} : {
|
|
1121
|
+
tokenIn: sourceToken,
|
|
1122
|
+
tokenOut: bluechipToken,
|
|
1123
|
+
amountIn,
|
|
1124
|
+
slippage,
|
|
1125
|
+
swapType: "EXACT_INPUT"
|
|
1126
|
+
};
|
|
1127
|
+
quotePaths.push({
|
|
1128
|
+
type: routeType,
|
|
1129
|
+
router,
|
|
1130
|
+
promise: (async () => {
|
|
1131
|
+
const preSwapQuote = await router.quote(quoteParams);
|
|
1132
|
+
if (!preSwapQuote.success) {
|
|
1133
|
+
throw new Error(
|
|
1134
|
+
`${routeType} pre-swap failed: ${preSwapQuote.error}`
|
|
1135
|
+
);
|
|
1154
1136
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1137
|
+
const bluechipKey = bluechipToken.symbol?.toUpperCase() === "WNEAR" ? "NEAR" : bluechipToken.symbol?.toUpperCase();
|
|
1138
|
+
const bluechipTokenConfig = bluechipKey && bluechipTokens[bluechipKey] || void 0;
|
|
1139
|
+
const normalizedSourceAsset = bluechipTokenConfig?.assetId ? bluechipTokenConfig.assetId : `nep141:${bluechipToken.address}`;
|
|
1140
|
+
let normalizedTargetAsset = targetToken.address;
|
|
1141
|
+
if (normalizedTargetAsset && !normalizedTargetAsset.startsWith("nep141:") && !normalizedTargetAsset.startsWith("nep245:") && normalizedTargetAsset.includes(".")) {
|
|
1142
|
+
normalizedTargetAsset = `nep141:${normalizeTokenId(
|
|
1143
|
+
normalizedTargetAsset,
|
|
1144
|
+
wrapNearContractId
|
|
1145
|
+
)}`;
|
|
1157
1146
|
}
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1147
|
+
normalizedTargetAsset = normalizeDestinationAsset(normalizedTargetAsset, wrapNearContractId) || normalizedTargetAsset;
|
|
1148
|
+
const slippageBps = convertSlippageToBasisPoints(slippage);
|
|
1149
|
+
const intentsQuote = await intentsQuotationAdapter.quote({
|
|
1150
|
+
originAsset: normalizedSourceAsset,
|
|
1151
|
+
destinationAsset: normalizedTargetAsset,
|
|
1152
|
+
amount: preSwapQuote.amountOut,
|
|
1153
|
+
refundTo: refundTo || recipient,
|
|
1154
|
+
recipient,
|
|
1155
|
+
slippageTolerance: slippageBps,
|
|
1156
|
+
swapType: "FLEX_INPUT"
|
|
1157
|
+
});
|
|
1158
|
+
if (intentsQuote.quoteStatus !== "success") {
|
|
1159
|
+
throw new Error(
|
|
1160
|
+
`${routeType} Intents quote failed: ${intentsQuote.message}`
|
|
1161
|
+
);
|
|
1162
|
+
}
|
|
1163
|
+
return {
|
|
1164
|
+
intentsQuote,
|
|
1165
|
+
preSwapQuote,
|
|
1166
|
+
router,
|
|
1167
|
+
finalAmountOut: intentsQuote.quoteSuccessResult?.quote?.amountOut || "0"
|
|
1168
|
+
};
|
|
1169
|
+
})()
|
|
1177
1170
|
});
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1171
|
+
});
|
|
1172
|
+
if (isTokenIntentsSupported) {
|
|
1173
|
+
quotePaths.push({
|
|
1174
|
+
type: "intents",
|
|
1175
|
+
promise: (async () => {
|
|
1176
|
+
let normalizedSourceAsset;
|
|
1177
|
+
if (sourceToken.symbol) {
|
|
1178
|
+
const sourceKey = sourceToken.symbol.toUpperCase();
|
|
1179
|
+
const sourceTokenConfig = bluechipTokens[sourceKey];
|
|
1180
|
+
if (sourceTokenConfig?.assetId) {
|
|
1181
|
+
normalizedSourceAsset = sourceTokenConfig.assetId;
|
|
1182
|
+
} else {
|
|
1183
|
+
normalizedSourceAsset = normalizeTokenId(
|
|
1184
|
+
sourceToken.address,
|
|
1185
|
+
wrapNearContractId
|
|
1186
|
+
);
|
|
1187
|
+
if (!normalizedSourceAsset.startsWith("nep141:")) {
|
|
1188
|
+
normalizedSourceAsset = `nep141:${normalizedSourceAsset}`;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
} else {
|
|
1192
|
+
normalizedSourceAsset = normalizeTokenId(
|
|
1193
|
+
sourceToken.address,
|
|
1194
|
+
wrapNearContractId
|
|
1195
|
+
);
|
|
1196
|
+
if (!normalizedSourceAsset.startsWith("nep141:")) {
|
|
1197
|
+
normalizedSourceAsset = `nep141:${normalizedSourceAsset}`;
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
let normalizedTargetAsset = targetToken.address;
|
|
1201
|
+
if (normalizedTargetAsset && !normalizedTargetAsset.startsWith("nep141:") && !normalizedTargetAsset.startsWith("nep245:") && normalizedTargetAsset.includes(".")) {
|
|
1202
|
+
normalizedTargetAsset = `nep141:${normalizeTokenId(
|
|
1203
|
+
normalizedTargetAsset,
|
|
1204
|
+
wrapNearContractId
|
|
1205
|
+
)}`;
|
|
1206
|
+
}
|
|
1207
|
+
normalizedTargetAsset = normalizeDestinationAsset(normalizedTargetAsset, wrapNearContractId) || normalizedTargetAsset;
|
|
1208
|
+
const slippageBps = convertSlippageToBasisPoints(slippage);
|
|
1209
|
+
const intentsQuote = await intentsQuotationAdapter.quote({
|
|
1210
|
+
originAsset: normalizedSourceAsset,
|
|
1211
|
+
destinationAsset: normalizedTargetAsset,
|
|
1212
|
+
amount: amountIn,
|
|
1213
|
+
refundTo: refundTo || recipient,
|
|
1214
|
+
recipient,
|
|
1215
|
+
slippageTolerance: slippageBps,
|
|
1216
|
+
swapType: "EXACT_INPUT"
|
|
1217
|
+
});
|
|
1218
|
+
if (intentsQuote.quoteStatus !== "success") {
|
|
1219
|
+
throw new Error(
|
|
1220
|
+
`Direct Intents quote failed: ${intentsQuote.message}`
|
|
1221
|
+
);
|
|
1222
|
+
}
|
|
1223
|
+
return {
|
|
1224
|
+
intentsQuote,
|
|
1225
|
+
finalAmountOut: intentsQuote.quoteSuccessResult?.quote?.amountOut || "0"
|
|
1226
|
+
};
|
|
1227
|
+
})()
|
|
1193
1228
|
});
|
|
1194
1229
|
}
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1230
|
+
const pathResults = await Promise.allSettled(
|
|
1231
|
+
quotePaths.map((p) => p.promise)
|
|
1232
|
+
);
|
|
1233
|
+
const validPaths = [];
|
|
1234
|
+
pathResults.forEach((result, index) => {
|
|
1235
|
+
const pathType = quotePaths[index].type;
|
|
1236
|
+
if (result.status === "fulfilled") {
|
|
1237
|
+
validPaths.push({
|
|
1238
|
+
type: pathType,
|
|
1239
|
+
...result.value
|
|
1205
1240
|
});
|
|
1206
1241
|
} else {
|
|
1207
|
-
|
|
1208
|
-
logger.warn(
|
|
1209
|
-
"Bluechip token assetId not found, using contractAddress with prefix:",
|
|
1210
|
-
{
|
|
1211
|
-
symbol: bluechipToken.symbol,
|
|
1212
|
-
normalizedSourceAsset
|
|
1213
|
-
}
|
|
1214
|
-
);
|
|
1215
|
-
}
|
|
1216
|
-
} else {
|
|
1217
|
-
if (sourceToken.symbol) {
|
|
1218
|
-
const sourceKey = sourceToken.symbol.toUpperCase();
|
|
1219
|
-
const sourceTokenConfig = bluechipTokens[sourceKey];
|
|
1220
|
-
if (sourceTokenConfig?.assetId) {
|
|
1221
|
-
normalizedSourceAsset = sourceTokenConfig.assetId;
|
|
1222
|
-
} else {
|
|
1223
|
-
normalizedSourceAsset = normalizeTokenId(
|
|
1224
|
-
sourceToken.address,
|
|
1225
|
-
wrapNearContractId
|
|
1226
|
-
);
|
|
1227
|
-
if (!normalizedSourceAsset.startsWith("nep141:")) {
|
|
1228
|
-
normalizedSourceAsset = `nep141:${normalizedSourceAsset}`;
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
} else {
|
|
1232
|
-
normalizedSourceAsset = normalizeTokenId(
|
|
1233
|
-
sourceToken.address,
|
|
1234
|
-
wrapNearContractId
|
|
1235
|
-
);
|
|
1236
|
-
if (!normalizedSourceAsset.startsWith("nep141:")) {
|
|
1237
|
-
normalizedSourceAsset = `nep141:${normalizedSourceAsset}`;
|
|
1238
|
-
}
|
|
1242
|
+
logger.warn(`Path ${pathType} failed:`, result.reason);
|
|
1239
1243
|
}
|
|
1240
|
-
}
|
|
1241
|
-
let normalizedTargetAsset = targetToken.address;
|
|
1242
|
-
if (normalizedTargetAsset && !normalizedTargetAsset.startsWith("nep141:") && !normalizedTargetAsset.startsWith("nep245:") && normalizedTargetAsset.includes(".")) {
|
|
1243
|
-
normalizedTargetAsset = `nep141:${normalizeTokenId(
|
|
1244
|
-
normalizedTargetAsset,
|
|
1245
|
-
wrapNearContractId
|
|
1246
|
-
)}`;
|
|
1247
|
-
}
|
|
1248
|
-
normalizedTargetAsset = normalizeDestinationAsset(normalizedTargetAsset, wrapNearContractId) || normalizedTargetAsset;
|
|
1249
|
-
const slippageBps = convertSlippageToBasisPoints(slippage);
|
|
1250
|
-
const intentsAmount = needsPreSwap ? preSwapQuote.amountOut : amountIn;
|
|
1251
|
-
logger.debug("DEX Aggregator - Calling NearIntents quotation:", {
|
|
1252
|
-
originAsset: normalizedSourceAsset,
|
|
1253
|
-
destinationAsset: normalizedTargetAsset,
|
|
1254
|
-
amount: intentsAmount,
|
|
1255
|
-
needsPreSwap,
|
|
1256
|
-
preSwapAmountOut: needsPreSwap ? preSwapQuote.amountOut : void 0
|
|
1257
|
-
});
|
|
1258
|
-
const swapTypeForIntents = needsPreSwap ? "FLEX_INPUT" : void 0;
|
|
1259
|
-
logger.debug("DEX Aggregator - swapType for NearIntents:", {
|
|
1260
|
-
needsPreSwap,
|
|
1261
|
-
swapType: swapTypeForIntents || "EXACT_INPUT (default)"
|
|
1262
1244
|
});
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
recipient,
|
|
1269
|
-
slippageTolerance: slippageBps,
|
|
1270
|
-
swapType: swapTypeForIntents
|
|
1245
|
+
logger.debug("Cross-chain Quote Comparison:", {
|
|
1246
|
+
paths: validPaths.map((p) => ({
|
|
1247
|
+
type: p.type.toUpperCase(),
|
|
1248
|
+
finalAmountOut: p.finalAmountOut
|
|
1249
|
+
}))
|
|
1271
1250
|
});
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
originAsset: normalizedSourceAsset,
|
|
1282
|
-
destinationAsset: normalizedTargetAsset,
|
|
1283
|
-
amount: intentsAmount,
|
|
1284
|
-
needsPreSwap,
|
|
1285
|
-
preSwapAmountOut: needsPreSwap ? preSwapQuote.amountOut : void 0
|
|
1286
|
-
});
|
|
1287
|
-
throw new Error(`Intents quote failed: ${errorMessage}`);
|
|
1251
|
+
if (validPaths.length === 0) {
|
|
1252
|
+
const errors = pathResults.map((r, index) => {
|
|
1253
|
+
const pathType = quotePaths[index].type;
|
|
1254
|
+
if (r.status === "rejected") {
|
|
1255
|
+
return `${pathType}: ${r.reason}`;
|
|
1256
|
+
}
|
|
1257
|
+
return null;
|
|
1258
|
+
}).filter(Boolean);
|
|
1259
|
+
throw new Error(`All quote paths failed: ${errors.join("; ")}`);
|
|
1288
1260
|
}
|
|
1289
|
-
const
|
|
1261
|
+
const bestPath = validPaths.reduce((best, current) => {
|
|
1262
|
+
const bestAmount = new Big3__default.default(best.finalAmountOut);
|
|
1263
|
+
const currentAmount = new Big3__default.default(current.finalAmountOut);
|
|
1264
|
+
return currentAmount.gt(bestAmount) ? current : best;
|
|
1265
|
+
});
|
|
1266
|
+
logger.debug(
|
|
1267
|
+
`\u2713 Selected best path: [${bestPath.type.toUpperCase()}] with finalAmountOut: ${bestPath.finalAmountOut}`
|
|
1268
|
+
);
|
|
1269
|
+
const depositAddress = bestPath.intentsQuote.quoteSuccessResult?.quote?.depositAddress || "";
|
|
1290
1270
|
if (!depositAddress) {
|
|
1291
1271
|
throw new Error("Deposit address not found in intents quote");
|
|
1292
1272
|
}
|
|
1293
|
-
const finalQuote = preSwapQuote;
|
|
1294
1273
|
return {
|
|
1295
1274
|
intents: {
|
|
1296
|
-
quote: intentsQuote,
|
|
1275
|
+
quote: bestPath.intentsQuote,
|
|
1297
1276
|
depositAddress
|
|
1298
1277
|
},
|
|
1299
|
-
preSwap:
|
|
1300
|
-
quote:
|
|
1278
|
+
preSwap: bestPath.preSwapQuote && bestPath.router ? {
|
|
1279
|
+
quote: bestPath.preSwapQuote,
|
|
1301
1280
|
tokenIn: sourceToken,
|
|
1302
1281
|
tokenOut: bluechipToken,
|
|
1303
|
-
executor:
|
|
1282
|
+
executor: bestPath.router,
|
|
1283
|
+
routeType: bestPath.type
|
|
1304
1284
|
} : void 0,
|
|
1305
|
-
finalAmountOut:
|
|
1285
|
+
finalAmountOut: bestPath.finalAmountOut,
|
|
1286
|
+
routeType: bestPath.type
|
|
1306
1287
|
};
|
|
1307
1288
|
}
|
|
1308
1289
|
|
|
1290
|
+
// src/integration/quoteSameChainSwap.ts
|
|
1291
|
+
async function quoteSameChainSwap(params, dexRouters) {
|
|
1292
|
+
const {
|
|
1293
|
+
tokenIn,
|
|
1294
|
+
tokenOut,
|
|
1295
|
+
amountIn,
|
|
1296
|
+
slippage,
|
|
1297
|
+
recipient,
|
|
1298
|
+
currentUserAddress
|
|
1299
|
+
} = params;
|
|
1300
|
+
const quoteResults = await Promise.allSettled(
|
|
1301
|
+
dexRouters.map((router) => {
|
|
1302
|
+
const capabilities = router.getCapabilities();
|
|
1303
|
+
const quoteParams = capabilities.requiresRecipient ? {
|
|
1304
|
+
tokenIn,
|
|
1305
|
+
tokenOut,
|
|
1306
|
+
amountIn,
|
|
1307
|
+
slippage,
|
|
1308
|
+
swapType: "EXACT_INPUT",
|
|
1309
|
+
sender: currentUserAddress,
|
|
1310
|
+
recipient
|
|
1311
|
+
} : {
|
|
1312
|
+
tokenIn,
|
|
1313
|
+
tokenOut,
|
|
1314
|
+
amountIn,
|
|
1315
|
+
slippage,
|
|
1316
|
+
swapType: "EXACT_INPUT",
|
|
1317
|
+
recipient
|
|
1318
|
+
};
|
|
1319
|
+
return router.quote(quoteParams);
|
|
1320
|
+
})
|
|
1321
|
+
);
|
|
1322
|
+
const validQuotes = quoteResults.filter(
|
|
1323
|
+
(r) => r.status === "fulfilled" && r.value.success
|
|
1324
|
+
).map((r, index) => ({
|
|
1325
|
+
quote: r.value,
|
|
1326
|
+
router: dexRouters[index]
|
|
1327
|
+
}));
|
|
1328
|
+
if (validQuotes.length === 0) {
|
|
1329
|
+
const errors = quoteResults.map((r, index) => {
|
|
1330
|
+
if (r.status === "rejected") {
|
|
1331
|
+
return `Router ${index}: ${r.reason}`;
|
|
1332
|
+
}
|
|
1333
|
+
if (r.status === "fulfilled" && !r.value.success) {
|
|
1334
|
+
return `Router ${index}: ${r.value.error}`;
|
|
1335
|
+
}
|
|
1336
|
+
return null;
|
|
1337
|
+
}).filter(Boolean);
|
|
1338
|
+
throw new Error(`All router quotes failed: ${errors.join("; ")}`);
|
|
1339
|
+
}
|
|
1340
|
+
return selectBestQuote(validQuotes);
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1309
1343
|
exports.AggregateDexRouter = AggregateDexRouter;
|
|
1310
1344
|
exports.NearSmartRouter = NearSmartRouter;
|
|
1311
1345
|
exports.completeQuote = completeQuote;
|
|
@@ -1318,8 +1352,10 @@ exports.isNearIntentsSupportedToken = isNearIntentsSupportedToken;
|
|
|
1318
1352
|
exports.logger = logger;
|
|
1319
1353
|
exports.normalizeDestinationAsset = normalizeDestinationAsset;
|
|
1320
1354
|
exports.normalizeTokenId = normalizeTokenId;
|
|
1355
|
+
exports.quoteSameChainSwap = quoteSameChainSwap;
|
|
1321
1356
|
exports.requiresRecipient = requiresRecipient;
|
|
1322
1357
|
exports.requiresRecipientInExecute = requiresRecipientInExecute;
|
|
1358
|
+
exports.selectBestQuote = selectBestQuote;
|
|
1323
1359
|
exports.setBluechipTokensConfig = setBluechipTokensConfig;
|
|
1324
1360
|
//# sourceMappingURL=index.js.map
|
|
1325
1361
|
//# sourceMappingURL=index.js.map
|