@hikaru-fi/swap-router
Version:
Package for calculating optimal path for executing swaps
46 lines (41 loc) • 1.05 kB
JavaScript
/**
* @typedef {Record<String,import("./pathCalculation").SwapRoute[]>} PathCache
*/
/**
* @type {PathCache}
*/
const pathes = {};
/**
*
* @param {String} tokenIn
* @param {String} tokenOut
* @param {String} swapType
*/
function constructPathName(tokenIn, tokenOut, swapType) {
return `${swapType}:${tokenIn}->${tokenOut}`;
}
/**
* @param {String} tokenIn
* @param {String} tokenOut
* @param {String} swapType
* @param {import("./pathCalculation").SwapRoute[]} path
*/
function addPathToCache(tokenIn, tokenOut, swapType, path) {
const name = constructPathName(tokenIn, tokenOut, swapType);
pathes[name] = path;
}
/**
*
* @param {String} tokenIn
* @param {String} tokenOut
* @param {String} swapType
* @returns {undefined|import("./pathCalculation").SwapRoute[]}
*/
function checkCacheForPath(tokenIn, tokenOut, swapType) {
const swapPath = pathes[constructPathName(tokenIn, tokenOut, swapType)];
return swapPath? swapPath : [];
}
module.exports = {
addPathToCache,
checkCacheForPath
}