UNPKG

swapnet-sdk-test-4

Version:
31 lines (30 loc) 1.06 kB
export const toNodeType = (tokenOp)=>{ const { fromSwaps, toSwaps } = tokenOp; if (fromSwaps.length === 0 && toSwaps.length === 0) { throw new Error(`Invalid TokenOperation node!`); } if (fromSwaps.length === 0) { return 'SOURCE'; } if (toSwaps.length === 0) { return 'DESTINATION'; } if (toSwaps.length === 1) { if (fromSwaps.length === 1) { return 'PASSING_THROUGH'; } return 'MERGING'; } return 'FORKING'; }; export const toAmountOutMinimum = (amountOut, slippageTolerance)=>{ const slippageToleranceMillionth = BigInt(Math.floor(slippageTolerance * 10 ** 6)); const oneMillion = BigInt(10 ** 6); return amountOut * (oneMillion - slippageToleranceMillionth) / oneMillion; }; export const fromTokenUnits = (tokenUnits, decimals)=>{ return 10n ** BigInt(decimals) * BigInt(Math.round(tokenUnits * 1e18)) / BigInt(1e18); }; export const toTokenUnits = (tokenAmount, decimals)=>{ return Number(tokenAmount * 10n ** 9n) / 10 ** (decimals + 9); };