UNPKG

@alcorexchange/alcor-swap-sdk

Version:

​​ **npm** ``` npm i @alcorexchange/alcor-swap-sdk ``` **yarn** ``` yarn add @alcorexchange/alcor-swap-sdk ``` ## Usage ### Import:

165 lines (151 loc) 5.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WASMRouteFinder = void 0; exports.computeAllRoutesWASM = computeAllRoutesWASM; var _entities = require("../entities"); function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // WASM imports - lazy loaded let wasmModule = null; function loadWasmModule() { if (!wasmModule) { try { // Load from same directory (utils/) wasmModule = require('./wasm_route_finder.js'); } catch (error) { console.error('Failed to load WASM module:', error); throw new Error('WASM module not available'); } } return wasmModule; } /** * Fast WASM route finder with persistent pool storage * Pools are loaded once and can be updated dynamically */ let WASMRouteFinder = exports.WASMRouteFinder = /*#__PURE__*/function () { function WASMRouteFinder() { _classCallCheck(this, WASMRouteFinder); _defineProperty(this, "initialized", false); _defineProperty(this, "poolsMap", new Map()); } return _createClass(WASMRouteFinder, [{ key: "initialize", value: /** * Initialize with pools - loads them into WASM memory */ async function initialize(pools) { loadWasmModule(); // Build pool map for fast lookup this.poolsMap.clear(); for (const pool of pools) { this.poolsMap.set(String(pool.id), pool); } // Convert pools to minimal format for WASM const wasmPools = pools.map(pool => ({ id: String(pool.id), token_a: { id: pool.tokenA.id }, token_b: { id: pool.tokenB.id } })); // Initialize pools in WASM memory wasmModule.init_pools_fast(wasmPools); this.initialized = true; } /** * Update pools (add new or update existing) * @param pools - Array of pools to update/add */ }, { key: "updatePools", value: async function updatePools(pools) { if (!this.initialized) { await this.initialize(pools); return; } loadWasmModule(); // Update local map for (const pool of pools) { this.poolsMap.set(String(pool.id), pool); } // Convert to WASM format const wasmPools = pools.map(pool => ({ id: String(pool.id), token_a: { id: pool.tokenA.id }, token_b: { id: pool.tokenB.id } })); // Update pools in WASM wasmModule.update_pools_fast(wasmPools); } /** * Compute all routes between two tokens */ }, { key: "computeAllRoutes", value: function computeAllRoutes(tokenIn, tokenOut, maxHops) { if (!this.initialized) { throw new Error('WASMRouteFinder not initialized. Call initialize() first.'); } // Call WASM with just token IDs - no pool serialization const routePoolIds = wasmModule.compute_routes_fast(tokenIn.id, tokenOut.id, maxHops); // Convert pool IDs back to Route objects return routePoolIds.map(poolIds => { const routePools = poolIds.map(poolId => { const pool = this.poolsMap.get(poolId); if (!pool) { throw new Error(`Pool not found: ${poolId}`); } return pool; }); return new _entities.Route(routePools, tokenIn, tokenOut); }); } /** * Get the number of pools currently loaded */ }, { key: "getPoolCount", value: function getPoolCount() { if (!this.initialized) { return 0; } return wasmModule.get_pool_count(); } /** * Clear all pools from memory */ }, { key: "clear", value: function clear() { if (this.initialized) { wasmModule.clear_pools(); this.poolsMap.clear(); this.initialized = false; } } }]); }(); /** * Convenience function for one-off route computation */ async function computeAllRoutesWASM(tokenIn, tokenOut, pools, maxHops) { const finder = new WASMRouteFinder(); await finder.initialize(pools); const routes = finder.computeAllRoutes(tokenIn, tokenOut, maxHops); finder.clear(); // Clean up after one-off use return routes; }