Skip to content

Commit 9a54183

Browse files
committed
feat: log warning for reverted transactions
1 parent 249a2c0 commit 9a54183

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/constants.ts

+18
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ export const SETTLER_META_TXN_ABI = [
4848

4949
export const FUNCTION_SELECTORS = { EXECUTE_META_TXN: "0xfd3ad6d4" };
5050

51+
export const SUPPORTED_CHAINS = [
52+
bsc,
53+
base,
54+
mode,
55+
blast,
56+
linea,
57+
scroll,
58+
mantle,
59+
mainnet,
60+
polygon,
61+
arbitrum,
62+
unichain,
63+
optimism,
64+
avalanche,
65+
berachain,
66+
worldchain,
67+
];
68+
5169
export const NATIVE_SYMBOL_BY_CHAIN_ID: { [key in SupportedChainId]: string } =
5270
{
5371
[bsc.id]: bsc.nativeCurrency.symbol,

src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
decodeFunctionData,
77
} from "viem";
88
import {
9+
SUPPORTED_CHAINS,
910
MULTICALL3_ADDRESS,
1011
FUNCTION_SELECTORS,
1112
ERC_4337_ENTRY_POINT,
@@ -60,6 +61,13 @@ export async function parseSwap({
6061

6162
const transactionReceipt = await publicClient.getTransactionReceipt({ hash });
6263

64+
if (transactionReceipt.status === "reverted") {
65+
const chain = SUPPORTED_CHAINS.find((chain) => chain.id === chainId);
66+
const message = `Unable to parse. Transaction ${hash} on ${chain?.name} has reverted.`;
67+
console.warn(message);
68+
return null;
69+
}
70+
6371
const isNativeSell = value > 0n;
6472

6573
const logs = await transferLogs({
@@ -159,7 +167,6 @@ export async function parseSwap({
159167
};
160168
} /* v8 ignore start */ else {
161169
// Unknown if this case actually happens. If it does, please file a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose".
162-
output = { symbol: "", amount: "", address: "" };
163170
console.error(
164171
"File a bug report here, including the expected results (URL to a block explorer) and the unexpected results: https://github.com/0xProject/0x-parser/issues/new/choose."
165172
);

src/tests/index.test.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
worldchain,
2020
berachain,
2121
} from "viem/chains";
22-
import { test, expect } from "vitest";
22+
import { vi, test, expect } from "vitest";
2323
import { parseSwap } from "../index";
2424
import { NATIVE_TOKEN_ADDRESS } from "../constants";
2525

@@ -1384,3 +1384,18 @@ test("parse a swap on Berachain (WETH for WBERA) with AllowanceHolder", async ()
13841384
},
13851385
});
13861386
});
1387+
1388+
// warns on revert
1389+
// https://etherscan.io/tx/0x25f24a7e6ec93abc99dca56a0ef2de1999deb17f67e6ed91cad1271757fff810
1390+
test.only("logs a warning for reverted transactions)", async () => {
1391+
const warnSpy = vi.spyOn(console, "warn");
1392+
const transactionHash = `0x25f24a7e6ec93abc99dca56a0ef2de1999deb17f67e6ed91cad1271757fff810`;
1393+
await parseSwap({ publicClient, transactionHash });
1394+
1395+
expect(warnSpy).toHaveBeenCalled();
1396+
expect(warnSpy).toHaveBeenCalledWith(
1397+
`Unable to parse. Transaction ${transactionHash} on Ethereum has reverted.`
1398+
);
1399+
1400+
warnSpy.mockRestore();
1401+
});

0 commit comments

Comments
 (0)