UNPKG

@swapnet-xyz/sdk

Version:
160 lines (159 loc) 5.33 kB
import { LiquiditySourceUname } from "../index.js"; const convertWithoutDetails = (route)=>{ return { source: route.name, address: route.address }; }; const convertWithFeeInBps = (route)=>{ if (route.details === undefined || route.details.feeInBps === undefined) { throw new Error(`Invalid Uniswap V2 Like route details!`); } const feeInBps = BigInt(route.details.feeInBps); return { source: route.name, address: route.address, feeInBps }; }; const convertWithFee = (route)=>{ if (route.details === undefined || route.details.fee === undefined) { throw new Error(`Invalid Uniswap V3 route details!`); } const fee = BigInt(route.details.fee); return { source: route.name, address: route.address, fee }; }; const convertUniswapV4 = (route)=>{ if (route.details === undefined) { throw new Error(`Invalid Uniswap V4 route details!`); } const details = route.details; if (details.fee === undefined || details.tickSpacing === undefined || details.hooks === undefined) { throw new Error(`Invalid Uniswap V4 route details!`); } const fee = BigInt(details.fee); const tickSpacing = BigInt(details.tickSpacing); return { source: route.name, address: details.hooks, fee, tickSpacing, isToken0Native: details.isToken0Native }; }; const notSupported = (route)=>{ throw new Error(`Liquidity source ${route.name} is not supported in parser!`); }; const invalidRoute = (route)=>{ throw new Error(`Liquidity source ${route.name} is invalid in routing plan!`); }; export const parserPluginByLiquiditySourceUname = { [LiquiditySourceUname.UniswapV2]: { convertToLiquidityInfo: convertWithFeeInBps }, [LiquiditySourceUname.ThrusterV2_3k]: { convertToLiquidityInfo: convertWithoutDetails }, [LiquiditySourceUname.ThrusterV2_10k]: { convertToLiquidityInfo: convertWithoutDetails }, [LiquiditySourceUname.RingswapV2]: { convertToLiquidityInfo: (route)=>{ const { fromFewWrappedTokenAddress, toFewWrappedTokenAddress } = route.details; return { source: route.name, address: route.address, fromFewWrappedTokenAddress, toFewWrappedTokenAddress }; } }, [LiquiditySourceUname.UniswapV3]: { convertToLiquidityInfo: convertWithFee }, [LiquiditySourceUname.UniswapV4]: { convertToLiquidityInfo: convertUniswapV4 }, [LiquiditySourceUname.PancakeswapV3]: { convertToLiquidityInfo: convertWithFee }, [LiquiditySourceUname.ThrusterV3]: { convertToLiquidityInfo: convertWithFee }, // [LiquiditySourceUname.RingswapV3]: { // convertToLiquidityInfo: converWithFee, // }, [LiquiditySourceUname.Cetus]: { convertToLiquidityInfo: convertWithFee }, [LiquiditySourceUname.CurveV1]: { convertToLiquidityInfo: convertWithoutDetails }, [LiquiditySourceUname.BebopLimitOrder]: { convertToLiquidityInfo: (route)=>{ const details = route.details; return { source: route.name, address: route.address, isSingleOrder: details.isSingleOrder, calldata: details.calldata, partialFillOffset: details.partialFillOffset }; } }, [LiquiditySourceUname.NativeLimitOrder]: { convertToLiquidityInfo: (route)=>{ const details = route.details; return { source: route.name, address: details.maker, maker: details.maker, makerToken: details.makerToken, takerToken: details.takerToken, makerAmount: BigInt(details.makerAmount), takerAmount: BigInt(details.takerAmount), nonce: BigInt(details.nonce), deadline: BigInt(details.deadline), signature: details.signature }; } }, [LiquiditySourceUname.NativeOrderbook]: { convertToLiquidityInfo: invalidRoute }, [LiquiditySourceUname.BebopOrderbook]: { convertToLiquidityInfo: invalidRoute }, [LiquiditySourceUname.Clipper]: { convertToLiquidityInfo: notSupported }, [LiquiditySourceUname.SushiswapV2]: { convertToLiquidityInfo: notSupported }, [LiquiditySourceUname.SushiswapV3]: { convertToLiquidityInfo: notSupported }, [LiquiditySourceUname.AerodromeV2]: { convertToLiquidityInfo: convertWithFeeInBps }, [LiquiditySourceUname.AerodromeV3]: { convertToLiquidityInfo: (route)=>{ if (route.details === undefined || route.details.tickSpacing === undefined) { throw new Error(`Invalid Aerodrome V3 route details!`); } const tickSpacing = BigInt(route.details.tickSpacing); return { source: route.name, address: route.address, tickSpacing }; } }, [LiquiditySourceUname.Blasterswap]: { convertToLiquidityInfo: notSupported } };