UNPKG

@xlink-network/xlink-sdk

Version:
84 lines (82 loc) 2.22 kB
import { pMemoize } from "./chunk-OQHA7TS3.mjs"; import { UnsupportedBridgeRouteError } from "./chunk-KTUPNUM5.mjs"; // src/utils/buildSupportedRoutes.ts function defineRoute(chainPairs, tokenPairs) { const result = []; for (const fromChain of chainPairs[0]) { for (const toChain of chainPairs[1]) { tokenPairs.forEach((tokenPair) => { result.push({ fromChain, toChain, fromToken: tokenPair[0], toToken: tokenPair[1] }); }); } } return result; } var memoizedIsSupportedFactory = (isSupported) => { return pMemoize( { cacheKey([, route]) { return `${route.fromChain}:${route.fromToken}->${route.toChain}:${route.toToken}`; }, skipCache: true }, isSupported ); }; function buildSupportedRoutes(routes, options = {}) { const isSupported = memoizedIsSupportedFactory( options.isSupported || (() => Promise.resolve(true)) ); const getSupportedRoutes = async (ctx, conditions = {}) => { const filteredDefinitions = routes.filter((r) => { if (conditions.fromChain != null && conditions.fromChain !== r.fromChain) { return false; } if (conditions.toChain != null && conditions.toChain !== r.toChain) { return false; } if (conditions.fromToken != null && conditions.fromToken !== r.fromToken) { return false; } if (conditions.toToken != null && conditions.toToken !== r.toToken) { return false; } return true; }); const res = await Promise.all( filteredDefinitions.map( async (route) => [await isSupported(ctx, route), route] ) ); return res.filter(([isSupported2]) => isSupported2).map(([, route]) => route); }; return { getSupportedRoutes, async checkRouteValid(ctx, route) { const isValid = await isSupported(ctx, route); if (!isValid) { throw new UnsupportedBridgeRouteError( route.fromChain, route.toChain, route.fromToken, route.toToken ); } return route; } }; } export { defineRoute, buildSupportedRoutes }; //# sourceMappingURL=chunk-BSXD4QA3.mjs.map