@getclave/lifi-sdk
Version:
LI.FI Any-to-Any Cross-Chain-Swap SDK
134 lines (133 loc) • 8.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const SDKError_1 = require("../../errors/SDKError");
const baseError_1 = require("../../errors/baseError");
const constants_1 = require("../../errors/constants");
const errors_1 = require("../../errors/errors");
const fixtures_1 = require("../../tests/fixtures");
const setup_1 = require("../../tests/setup");
const helpers = require("../../utils/fetchTxErrorDetails");
const parseEVMErrors_1 = require("./parseEVMErrors");
(0, vitest_1.beforeAll)(setup_1.setupTestEnvironment);
(0, vitest_1.describe)('parseEVMStepErrors', () => {
(0, vitest_1.describe)('when a SDKError is passed', async () => {
(0, vitest_1.it)('should return the original error', async () => {
const error = new SDKError_1.SDKError(new baseError_1.BaseError(constants_1.ErrorName.UnknownError, constants_1.LiFiErrorCode.InternalError, 'there was an error'));
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(error);
(0, vitest_1.expect)(parsedError).toBe(error);
(0, vitest_1.expect)(parsedError.step).toBeUndefined();
(0, vitest_1.expect)(parsedError.process).toBeUndefined();
});
});
(0, vitest_1.describe)('when step and process is passed', () => {
(0, vitest_1.it)('should return the original error with step and process added', async () => {
const error = new SDKError_1.SDKError(new baseError_1.BaseError(constants_1.ErrorName.UnknownError, constants_1.LiFiErrorCode.InternalError, 'there was an error'));
const step = (0, fixtures_1.buildStepObject)({ includingExecution: true });
const process = step.execution.process[0];
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(error, step, process);
(0, vitest_1.expect)(parsedError).toBe(error);
(0, vitest_1.expect)(parsedError.step).toBe(step);
(0, vitest_1.expect)(parsedError.process).toBe(process);
});
});
(0, vitest_1.describe)('when the SDKError already has a step and process', () => {
(0, vitest_1.it)('should return the original error with teh existing step and process specified', async () => {
const expectedStep = (0, fixtures_1.buildStepObject)({ includingExecution: true });
const expectedProcess = expectedStep.execution.process[0];
const error = new SDKError_1.SDKError(new baseError_1.BaseError(constants_1.ErrorName.UnknownError, constants_1.LiFiErrorCode.InternalError, 'there was an error'), expectedStep, expectedProcess);
const step = (0, fixtures_1.buildStepObject)({ includingExecution: true });
const process = step.execution.process[0];
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(error, step, process);
(0, vitest_1.expect)(parsedError).toBe(error);
(0, vitest_1.expect)(parsedError.step).toBe(expectedStep);
(0, vitest_1.expect)(parsedError.process).toBe(expectedProcess);
});
});
(0, vitest_1.describe)('when a BaseError is passed', () => {
(0, vitest_1.it)('should return the BaseError as the cause on a SDKError', async () => {
const error = new baseError_1.BaseError(constants_1.ErrorName.BalanceError, constants_1.LiFiErrorCode.BalanceError, 'there was an error');
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(error);
(0, vitest_1.expect)(parsedError).toBeInstanceOf(SDKError_1.SDKError);
(0, vitest_1.expect)(parsedError.step).toBeUndefined();
(0, vitest_1.expect)(parsedError.process).toBeUndefined();
(0, vitest_1.expect)(parsedError.cause).toBe(error);
});
(0, vitest_1.describe)('when step and process is passed', () => {
(0, vitest_1.it)('should return the SDKError with step and process added', async () => {
const error = new baseError_1.BaseError(constants_1.ErrorName.BalanceError, constants_1.LiFiErrorCode.BalanceError, 'there was an error');
const step = (0, fixtures_1.buildStepObject)({ includingExecution: true });
const process = step.execution.process[0];
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(error, step, process);
(0, vitest_1.expect)(parsedError).toBeInstanceOf(SDKError_1.SDKError);
(0, vitest_1.expect)(parsedError.step).toBe(step);
(0, vitest_1.expect)(parsedError.process).toBe(process);
(0, vitest_1.expect)(parsedError.cause).toBe(error);
});
});
});
(0, vitest_1.describe)('when a generic Error is passed', () => {
(0, vitest_1.it)('should return the Error as he cause on a BaseError which is wrapped in an SDKError', async () => {
const error = new Error('Somethings fishy');
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(error);
(0, vitest_1.expect)(parsedError).toBeInstanceOf(SDKError_1.SDKError);
(0, vitest_1.expect)(parsedError.step).toBeUndefined();
(0, vitest_1.expect)(parsedError.process).toBeUndefined();
const baseError = parsedError.cause;
(0, vitest_1.expect)(baseError).toBeInstanceOf(baseError_1.BaseError);
const causeError = baseError.cause;
(0, vitest_1.expect)(causeError).toBe(error);
});
(0, vitest_1.describe)('when step and process is passed', () => {
(0, vitest_1.it)('should return an SDKError with step and process added', async () => {
const error = new Error('Somethings fishy');
const step = (0, fixtures_1.buildStepObject)({ includingExecution: true });
const process = step.execution?.process[0];
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(error, step, process);
(0, vitest_1.expect)(parsedError).toBeInstanceOf(SDKError_1.SDKError);
(0, vitest_1.expect)(parsedError.step).toBe(step);
(0, vitest_1.expect)(parsedError.process).toBe(process);
});
});
});
(0, vitest_1.describe)('when specific Errors are passed', () => {
(0, vitest_1.describe)('when the error is the viem UserRejectedRequestError error', () => {
(0, vitest_1.it)('should return the BaseError with the SignatureRejected code as the cause on a SDKError', async () => {
const mockViemError = new Error();
const UserRejectedRequestError = new Error();
UserRejectedRequestError.name = 'UserRejectedRequestError';
mockViemError.cause = UserRejectedRequestError;
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(mockViemError);
(0, vitest_1.expect)(parsedError).toBeInstanceOf(SDKError_1.SDKError);
const baseError = parsedError.cause;
(0, vitest_1.expect)(baseError).toBeInstanceOf(errors_1.TransactionError);
(0, vitest_1.expect)(baseError.code).toEqual(constants_1.LiFiErrorCode.SignatureRejected);
(0, vitest_1.expect)(baseError.cause?.cause).toBe(UserRejectedRequestError);
});
});
});
(0, vitest_1.describe)('when the error is a Transaction reverted error caused by low gas', () => {
(0, vitest_1.it)('should return the TransactionError with the GasLimitError code and GasLimitLow message', async () => {
vitest_1.vi.spyOn(helpers, 'fetchTxErrorDetails').mockResolvedValue({
error_message: 'out of gas',
});
const mockTransactionError = new errors_1.TransactionError(constants_1.LiFiErrorCode.TransactionFailed, constants_1.ErrorMessage.TransactionReverted);
const mockStep = {
action: {
fromChainId: 10,
},
};
const mockProcess = {
txHash: '0x5c73f72a72a75d8b716ed42cd620042f53b958f028d0c9ad772908b7791c017b',
};
const parsedError = await (0, parseEVMErrors_1.parseEVMErrors)(mockTransactionError, mockStep, mockProcess);
(0, vitest_1.expect)(parsedError).toBeInstanceOf(SDKError_1.SDKError);
const baseError = parsedError.cause;
(0, vitest_1.expect)(baseError).toBeInstanceOf(errors_1.TransactionError);
(0, vitest_1.expect)(baseError.code).toEqual(constants_1.LiFiErrorCode.GasLimitError);
(0, vitest_1.expect)(baseError.message).toEqual(constants_1.ErrorMessage.GasLimitLow);
(0, vitest_1.expect)(baseError.cause).toBe(mockTransactionError);
vitest_1.vi.clearAllMocks();
});
});
});