@lifi/sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
35 lines • 1.72 kB
JavaScript
import { LiFiErrorCode } from '../errors/constants.js';
import { TransactionError } from '../errors/errors.js';
import { checkStepSlippageThreshold } from './utils.js';
/**
* 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
*/
export const stepComparison = async (statusManager, oldStep, newStep, allowUserInteraction, executionOptions) => {
// Check if changed exchange rate is in the range of slippage threshold
if (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 TransactionError(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);
};
//# sourceMappingURL=stepComparison.js.map