UNPKG

@getclave/lifi-sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

28 lines (27 loc) 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkStepSlippageThreshold = checkStepSlippageThreshold; // Standard threshold for destination amount difference (0.5%) const standardThreshold = 0.005; /** * Used to check if changed exchange rate is in the range of slippage threshold. * We use a slippage value as a threshold to trigger the rate change hook. * This can result in almost doubled slippage for the user and need to be revisited. * @param oldStep - old step * @param newStep - new step * @returns Boolean */ function checkStepSlippageThreshold(oldStep, newStep) { const setSlippage = oldStep.action.slippage || standardThreshold; const oldEstimatedToAmount = BigInt(oldStep.estimate.toAmountMin); const newEstimatedToAmount = BigInt(newStep.estimate.toAmountMin); const amountDifference = oldEstimatedToAmount - newEstimatedToAmount; // oldEstimatedToAmount can be 0 when we use contract calls let actualSlippage = 0; if (oldEstimatedToAmount > 0) { actualSlippage = Number((amountDifference * 1000000000n) / oldEstimatedToAmount) / 1_000_000_000; } return actualSlippage <= setSlippage; }