|
| 1 | +import { BaseError, ContractFunctionRevertedError, encodeErrorResult } from 'viem'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { isolatedPoolComptrollerAbi } from 'libs/contracts'; |
| 5 | + |
| 6 | +import { parseContractError } from '..'; |
| 7 | + |
| 8 | +describe('parseContractError', () => { |
| 9 | + it('decodes a viem ContractFunctionRevertedError that already carries decoded data', () => { |
| 10 | + const data = encodeErrorResult({ |
| 11 | + abi: isolatedPoolComptrollerAbi, |
| 12 | + errorName: 'ActionPaused', |
| 13 | + args: ['0xED827b80Bd838192EA95002C01B5c6dA8354219a', 1], |
| 14 | + }); |
| 15 | + |
| 16 | + const error = new ContractFunctionRevertedError({ |
| 17 | + abi: isolatedPoolComptrollerAbi, |
| 18 | + data, |
| 19 | + functionName: 'preBorrowHook', |
| 20 | + }); |
| 21 | + |
| 22 | + const parsed = parseContractError(error); |
| 23 | + expect(parsed?.errorName).toBe('ActionPaused'); |
| 24 | + expect(parsed?.args).toEqual(['0xED827b80Bd838192EA95002C01B5c6dA8354219a', 1]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('decodes raw hex revert data nested in the cause chain (estimateGas path)', () => { |
| 28 | + const rawData = encodeErrorResult({ |
| 29 | + abi: isolatedPoolComptrollerAbi, |
| 30 | + errorName: 'BorrowCapExceeded', |
| 31 | + args: ['0xED827b80Bd838192EA95002C01B5c6dA8354219a', 1000n], |
| 32 | + }); |
| 33 | + |
| 34 | + const error = new BaseError('execution reverted', { |
| 35 | + cause: { data: rawData } as unknown as Error, |
| 36 | + }); |
| 37 | + |
| 38 | + const parsed = parseContractError(error); |
| 39 | + expect(parsed?.errorName).toBe('BorrowCapExceeded'); |
| 40 | + expect(parsed?.args?.[1]).toBe(1000n); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns UnknownContractError when the selector is not in any Venus ABI', () => { |
| 44 | + const error = new BaseError('execution reverted', { |
| 45 | + cause: { data: '0xdeadbeef' } as unknown as Error, |
| 46 | + }); |
| 47 | + |
| 48 | + const parsed = parseContractError(error); |
| 49 | + expect(parsed?.errorName).toBe('UnknownContractError'); |
| 50 | + expect(parsed?.signature).toBe('0xdeadbeef'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('returns undefined for non-viem errors', () => { |
| 54 | + expect(parseContractError(new Error('random'))).toBeUndefined(); |
| 55 | + expect(parseContractError(null)).toBeUndefined(); |
| 56 | + expect(parseContractError('boom')).toBeUndefined(); |
| 57 | + }); |
| 58 | +}); |
0 commit comments