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