Skip to content

Commit

Permalink
better design
Browse files Browse the repository at this point in the history
  • Loading branch information
typedarray committed Jan 7, 2025
1 parent dca13b5 commit 1a7365e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 7 additions & 3 deletions packages/core/src/sync/events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
buildEvents,
decodeEventLog,
decodeEvents,
removeNullCharacters,
} from "./events.js";
import type { LogFactory, LogFilter } from "./source.js";

Expand Down Expand Up @@ -834,7 +835,7 @@ test("buildEvents() matches getEvents() trace", async (context) => {
await cleanup();
});

test("decodeEventLog removes null characters", () => {
test("removeNullCharacters removes null characters", () => {
// NameRegistered event from this transaction contains null characters:
// https://etherscan.io/tx/0x2e67be22d5e700e61e102b926f28ba451c53a6cd6438c53b43dbb783c2081a12#eventlog
const log = {
Expand Down Expand Up @@ -890,6 +891,9 @@ test("decodeEventLog removes null characters", () => {
data: log.data,
});

// Failing test: 'tencentclub\x00\x00\x00\x00\x00\x00'
expect(args.name).toBe("tencentclub");
expect(args.name).toBe("tencentclub\x00\x00\x00\x00\x00\x00");

const cleanedArgs = removeNullCharacters(args);

expect((cleanedArgs as any).name).toBe("tencentclub");
});
13 changes: 5 additions & 8 deletions packages/core/src/sync/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ export const decodeEvents = (

event: {
name: safeName,
args,
args: removeNullCharacters(args),
log: event.log!,
block: event.block,
transaction: event.transaction!,
Expand Down Expand Up @@ -556,8 +556,8 @@ export const decodeEvents = (
name: `${source.name}.${safeName}`,

event: {
args,
result,
args: removeNullCharacters(args),
result: removeNullCharacters(result),
trace: event.trace!,
block: event.block,
transaction: event.transaction!,
Expand Down Expand Up @@ -716,10 +716,7 @@ export function decodeEventLog({
}
}

// Remove null bytes from any string values present in the decoded args.
return Object.values(args).length > 0
? removeNullCharacters(args)
: undefined;
return Object.values(args).length > 0 ? args : undefined;
}

function decodeTopic({ param, value }: { param: AbiParameter; value: Hex }) {
Expand All @@ -734,7 +731,7 @@ function decodeTopic({ param, value }: { param: AbiParameter; value: Hex }) {
return decodedArg[0];
}

function removeNullCharacters(obj: unknown): unknown {
export function removeNullCharacters(obj: unknown): unknown {
if (typeof obj === "string") {
return obj.replace(/\0/g, "");
}
Expand Down

0 comments on commit 1a7365e

Please sign in to comment.