A script can fetch event logs successfully and still crash when it prints them:
TypeError: Do not know how to serialize a BigInt
An empty result may pass the same code path. There is simply no decoded integer in [] to trigger the failure.
That is the distinction in four-meme-ai issue #3: non-empty BSC event results broke JSON output in two scripts. We submitted PR #4 with a two-line serialization fix and regression tests. The PR is open as of October 26, 2026; it has not been accepted upstream.
Keep decoded integers exact
In this case, viem decodes the events' uint256 arguments as JavaScript bigint. Raw JSON-RPC logs contain hex strings; the BigInts appear in the decoded JavaScript values. A successful RPC response does not guarantee that those values can be passed directly to JSON.stringify.
Use a replacer at the output boundary:
const args = { amount: 9007199254740993n, fee: 0n };
const json = JSON.stringify(
args,
(_key, value) => typeof value === 'bigint' ? value.toString() : value,
2,
);
console.log(json);
// { "amount": "9007199254740993", "fee": "0" }
The replacer also reaches BigInts nested inside event arrays and argument objects. It leaves ordinary strings, booleans and existing numbers unchanged. This addresses BigInts; it does not make circular references or other unsupported structures serializable.
Avoid converting token amounts to Number just to make JSON work. For example, Number(9007199254740993n) becomes 9007199254740992. The exception disappears, but the value has changed. MDN explains the BigInt serialization error and replacer options.
The JSON contract now contains decimal strings. A consumer that knows a field represents an integer can use BigInt(parsed.amount) for arithmetic. Do not convert every decimal-looking string indiscriminately: identifiers and other textual fields may also contain digits. Decimal places still come from the token's metadata, not from serialization.
Test a non-empty result, an empty result, and an error
The patch's tests run both actual TypeScript scripts against a loopback JSON-RPC server:
| Case, for each script | Expected result |
|---|---|
Four synthetic event types with zero, values above the safe-integer limit, and uint256 max |
Valid JSON with exact decimal-string arguments |
| Empty log array | Successful empty output |
| RPC error | Non-zero exit, without empty-success output |
The event types are TokenCreate, TokenPurchase, TokenSale and LiquidityAdded. Each non-empty test checks all four; these are six tests total, not eight separate event tests.
Before the fix, both non-empty tests failed: four passed and two failed. After the fix, all six passed. The TypeScript build also passed. The fixtures are synthetic and encoded with viem, so this verifies the scripts' serialization and error behavior—not the ABI against an independent source, a live BSC provider, or the complete CLI wrapper.
To run the same tests from the proposed patch, install Node.js 24 and check out the fixed commit:
git clone --branch fix/event-bigint-json https://github.com/gjwang/four-meme-ai.git
cd four-meme-ai
git checkout e797b768307250ac59065dd646739c4750dc3e3d
npm ci --ignore-scripts
node --test test/events-json.test.mjs
npm run build
Dependency installation needs network access. The regression tests use local synthetic responses and need no RPC key, wallet or transaction.
For a log reader, transport errors, valid empty results and output failures need separate handling. If you are also building a historical scan, our eth_getLogs block-range and recovery guide covers bounded queries and saved progress. Fixing serialization does not prove that an empty response is complete, and this patch does not change the default BSC RPC provider.
Top comments (0)