UNPKG

@getclave/lifi-sdk

Version:

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

39 lines (38 loc) 1.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stepComparison = void 0; const constants_1 = require("../errors/constants"); const errors_1 = require("../errors/errors"); const utils_1 = require("./utils"); /** * This method checks whether the new and updated Step meets the required exchange rate conditions. * If yes it returns the updated Step. * If no and if user interaction is allowed it triggers the acceptExchangeRateUpdateHook. If no user interaction is allowed it aborts. * @param statusManager * @param oldStep * @param newStep * @param settings * @param allowUserInteraction * @param executionOptions * @returns Return LiFiStep */ const stepComparison = async (statusManager, oldStep, newStep, allowUserInteraction, executionOptions) => { // Check if changed exchange rate is in the range of slippage threshold if ((0, utils_1.checkStepSlippageThreshold)(oldStep, newStep)) { return statusManager.updateStepInRoute(newStep); } let allowStepUpdate; if (allowUserInteraction) { allowStepUpdate = await executionOptions?.acceptExchangeRateUpdateHook?.({ oldToAmount: oldStep.estimate.toAmount, newToAmount: newStep.estimate.toAmount, toToken: newStep.action.toToken, }); } if (!allowStepUpdate) { // The user declined the new exchange rate, so we are not going to proceed throw new errors_1.TransactionError(constants_1.LiFiErrorCode.ExchangeRateUpdateCanceled, 'Exchange rate has changed!\nTransaction was not sent, your funds are still in your wallet.\nThe exchange rate has changed and the previous estimation can not be fulfilled due to value loss.'); } return statusManager.updateStepInRoute(newStep); }; exports.stepComparison = stepComparison;