|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import type { TransactionReceipt } from "viem" |
| 3 | +import { |
| 4 | + RPCError, |
| 5 | + buildRevertMessage, |
| 6 | + getTransactionShortMessage, |
| 7 | + getTransactionFullMessage, |
| 8 | + getTransactionErrorMessage, |
| 9 | + getTransactionErrorTitle, |
| 10 | +} from "../transaction-errors" |
| 11 | + |
| 12 | +const fakeReceipt: TransactionReceipt = { |
| 13 | + transactionHash: "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", |
| 14 | + blockNumber: 12345n, |
| 15 | + gasUsed: 98765n, |
| 16 | + status: "reverted", |
| 17 | + transactionIndex: 0, |
| 18 | + blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000", |
| 19 | + from: "0x0000000000000000000000000000000000000001", |
| 20 | + to: "0x0000000000000000000000000000000000000002", |
| 21 | + cumulativeGasUsed: 98765n, |
| 22 | + contractAddress: null, |
| 23 | + logs: [], |
| 24 | + logsBloom: "0x00", |
| 25 | + type: "0x2", |
| 26 | + effectiveGasPrice: 1000000000n, |
| 27 | +} |
| 28 | + |
| 29 | +describe("buildRevertMessage", () => { |
| 30 | + it("includes tx hash, block, and gas from receipt", () => { |
| 31 | + const msg = buildRevertMessage(fakeReceipt) |
| 32 | + expect(msg).toContain("0x12345678…90abcdef") |
| 33 | + expect(msg).toContain("12345") |
| 34 | + expect(msg).toContain("98765") |
| 35 | + expect(msg).toContain("reverted on-chain") |
| 36 | + }) |
| 37 | + |
| 38 | + it("includes statusReason from rawDbRecord when present", () => { |
| 39 | + const msg = buildRevertMessage(fakeReceipt, { |
| 40 | + statusReason: "barter minReturn (100) < user required (200)", |
| 41 | + }) |
| 42 | + expect(msg).toContain("barter minReturn") |
| 43 | + }) |
| 44 | + |
| 45 | + it("includes revertReason from rawDbRecord when present", () => { |
| 46 | + const msg = buildRevertMessage(fakeReceipt, { revertReason: "INSUFFICIENT_OUTPUT_AMOUNT" }) |
| 47 | + expect(msg).toContain("INSUFFICIENT_OUTPUT_AMOUNT") |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +describe("mapErrorMessage via getTransactionShortMessage", () => { |
| 52 | + it("RPCError with receipt passes through actual message, not 'Network error'", () => { |
| 53 | + const err = new RPCError(buildRevertMessage(fakeReceipt), fakeReceipt) |
| 54 | + const short = getTransactionShortMessage(err) |
| 55 | + expect(short).not.toContain("Network error") |
| 56 | + expect(short).toContain("reverted on-chain") |
| 57 | + }) |
| 58 | + |
| 59 | + it("non-RPCError with 'rpc' in message still gets Network error", () => { |
| 60 | + const err = new Error("rpc endpoint unreachable") |
| 61 | + const short = getTransactionShortMessage(err) |
| 62 | + expect(short).toBe("Network error") |
| 63 | + }) |
| 64 | + |
| 65 | + it("non-RPCError with 'fetch' in message still gets Network error", () => { |
| 66 | + const err = new Error("failed to fetch") |
| 67 | + const short = getTransactionShortMessage(err) |
| 68 | + expect(short).toBe("Network error") |
| 69 | + }) |
| 70 | + |
| 71 | + it("unknown error without matching keywords passes through raw message", () => { |
| 72 | + const err = new Error("something completely unexpected happened") |
| 73 | + const short = getTransactionShortMessage(err) |
| 74 | + expect(short).toBe("something completely unexpected happened") |
| 75 | + }) |
| 76 | + |
| 77 | + it("user rejection is recognized", () => { |
| 78 | + const err = new Error("user rejected the request") |
| 79 | + const short = getTransactionShortMessage(err) |
| 80 | + expect(short).toBe("Transaction Cancelled in Wallet") |
| 81 | + }) |
| 82 | + |
| 83 | + it("insufficient funds is recognized", () => { |
| 84 | + const err = new Error("insufficient funds for gas") |
| 85 | + const short = getTransactionShortMessage(err) |
| 86 | + expect(short).toBe("Insufficient funds for gas fees") |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +describe("getTransactionFullMessage", () => { |
| 91 | + it("RPCError with receipt shows full buildRevertMessage content", () => { |
| 92 | + const msg = buildRevertMessage(fakeReceipt, { statusReason: "slippage exceeded" }) |
| 93 | + const err = new RPCError(msg, fakeReceipt) |
| 94 | + const full = getTransactionFullMessage(err) |
| 95 | + expect(full).toContain("reverted on-chain") |
| 96 | + expect(full).toContain("slippage exceeded") |
| 97 | + expect(full).toContain("0x12345678") |
| 98 | + }) |
| 99 | + |
| 100 | + it("plain Error preserves original message", () => { |
| 101 | + const err = new Error("something weird from the relayer: code 500 internal") |
| 102 | + const full = getTransactionFullMessage(err) |
| 103 | + expect(full).toContain("something weird from the relayer: code 500 internal") |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe("getTransactionErrorMessage", () => { |
| 108 | + it("RPCError with receipt does not return network error boilerplate", () => { |
| 109 | + const err = new RPCError(buildRevertMessage(fakeReceipt), fakeReceipt) |
| 110 | + const msg = getTransactionErrorMessage(err) |
| 111 | + expect(msg).not.toContain("Unable to connect to the blockchain") |
| 112 | + expect(msg).toContain("reverted on-chain") |
| 113 | + }) |
| 114 | + |
| 115 | + it("genuine network error surfaces the actual error after the prefix", () => { |
| 116 | + const err = new Error( |
| 117 | + "HttpRequestError: Request to https://fastrpc.mev-commit.xyz failed (status 503)" |
| 118 | + ) |
| 119 | + const msg = getTransactionErrorMessage(err) |
| 120 | + expect(msg.startsWith("Network error: ")).toBe(true) |
| 121 | + expect(msg).toContain("503") |
| 122 | + expect(msg).toContain("fastrpc.mev-commit.xyz") |
| 123 | + expect(msg).not.toContain("Unable to connect to the blockchain") |
| 124 | + }) |
| 125 | +}) |
| 126 | + |
| 127 | +describe("getTransactionErrorTitle", () => { |
| 128 | + it("reverted tx shows 'Failed' not 'Cancelled'", () => { |
| 129 | + const err = new RPCError(buildRevertMessage(fakeReceipt), fakeReceipt) |
| 130 | + expect(getTransactionErrorTitle(err, "swap")).toBe("Swap Failed") |
| 131 | + }) |
| 132 | +}) |
0 commit comments