UNPKG

@xlink-network/xlink-sdk

Version:
157 lines (153 loc) 4.73 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/utils/buildSupportedRoutes.ts var buildSupportedRoutes_exports = {}; __export(buildSupportedRoutes_exports, { buildSupportedRoutes: () => buildSupportedRoutes, defineRoute: () => defineRoute }); module.exports = __toCommonJS(buildSupportedRoutes_exports); // src/utils/errors.ts var XLinkSDKErrorBase = class extends Error { constructor(...args) { super(...args); this.name = "XLinkSDKErrorBase"; } }; var UnsupportedBridgeRouteError = class extends XLinkSDKErrorBase { constructor(fromChain, toChain, fromToken, toToken) { super( `Unsupported chain combination: ${fromToken}(${fromChain}) -> ${toToken ?? "Unknown Token"}(${toChain})` ); this.fromChain = fromChain; this.toChain = toChain; this.fromToken = fromToken; this.toToken = toToken; this.name = "UnsupportedBridgeRouteError"; } }; // src/utils/pMemoize.ts function pMemoize(options, fn) { const cache = /* @__PURE__ */ new Map(); const skipCache = options.skipCache == null ? async () => false : typeof options.skipCache === "function" ? options.skipCache : async () => options.skipCache; return async function(...args) { const key = options.cacheKey(args); if (cache.has(key)) { return cache.get(key); } const promise = (async () => { const cleanCache = () => { queueMicrotask(() => { if (cache.get(key) === promise) { cache.delete(key); } }); }; try { const result = await fn(...args); if (await skipCache(args, result)) { cleanCache(); } return result; } catch (e) { cleanCache(); throw e; } })(); cache.set(key, promise); return promise; }; } // 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; } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { buildSupportedRoutes, defineRoute }); //# sourceMappingURL=buildSupportedRoutes.js.map