Skip to content

Commit

Permalink
fix: parsing of non-string event arguments from simulation (#4512)
Browse files Browse the repository at this point in the history
Only attempt to call `toLowerCase` on string event arguments.
Skip the parsing of unsupported events.
Rename the `parseX` methods to `normalizeX` for clarity.
  • Loading branch information
matthewwalsh0 authored Jul 15, 2024
1 parent a520e59 commit 562e6a1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
8 changes: 4 additions & 4 deletions packages/transaction-controller/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 93.65,
functions: 98.38,
lines: 98.8,
statements: 98.81,
branches: 93.44,
functions: 98.4,
lines: 98.72,
statements: 98.73,
},
},

Expand Down
10 changes: 7 additions & 3 deletions packages/transaction-controller/src/utils/simulation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ const PARSED_ERC20_TRANSFER_EVENT_MOCK = {
const PARSED_ERC721_TRANSFER_EVENT_MOCK = {
name: 'Transfer',
contractAddress: CONTRACT_ADDRESS_1_MOCK,
args: [OTHER_ADDRESS_MOCK, USER_ADDRESS_MOCK, TOKEN_ID_MOCK],
args: [
OTHER_ADDRESS_MOCK,
USER_ADDRESS_MOCK,
{ toHexString: () => TOKEN_ID_MOCK },
],
} as unknown as LogDescription;

const PARSED_ERC1155_TRANSFER_SINGLE_EVENT_MOCK = {
Expand All @@ -60,7 +64,7 @@ const PARSED_ERC1155_TRANSFER_SINGLE_EVENT_MOCK = {
OTHER_ADDRESS_MOCK,
OTHER_ADDRESS_MOCK,
USER_ADDRESS_MOCK,
TOKEN_ID_MOCK,
{ toHexString: () => TOKEN_ID_MOCK },
{ toHexString: () => VALUE_MOCK },
],
} as unknown as LogDescription;
Expand All @@ -72,7 +76,7 @@ const PARSED_ERC1155_TRANSFER_BATCH_EVENT_MOCK = {
OTHER_ADDRESS_MOCK,
OTHER_ADDRESS_MOCK,
USER_ADDRESS_MOCK,
[TOKEN_ID_MOCK],
[{ toHexString: () => TOKEN_ID_MOCK }],
[{ toHexString: () => VALUE_MOCK }],
],
} as unknown as LogDescription;
Expand Down
40 changes: 25 additions & 15 deletions packages/transaction-controller/src/utils/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,14 @@ export function getEvents(response: SimulationResponse): ParsedEvent[] {
return undefined;
}

const args = parseEventArgs(event.args, inputs);
if (!SUPPORTED_EVENTS.includes(event.name)) {
log('Ignoring unsupported event', event.name, event);
return undefined;
}

log('Normalizing event args', event.name, event);

const args = normalizeEventArgs(event.args, inputs);

return {
contractAddress: currentLog.address,
Expand All @@ -247,18 +254,18 @@ export function getEvents(response: SimulationResponse): ParsedEvent[] {
}

/**
* Parse event arguments using ABI input definitions.
* Normalize event arguments using ABI input definitions.
* @param args - The raw event arguments.
* @param abiInputs - The ABI input definitions.
* @returns The parsed event arguments.
* @returns The normalized event arguments.
*/
function parseEventArgs(
function normalizeEventArgs(
args: Result,
abiInputs: { name: string }[],
): Record<string, Hex | Hex[]> {
return args.reduce((result, arg, index) => {
const name = abiInputs[index].name.replace('_', '');
const value = parseEventArgValue(arg);
const value = normalizeEventArgValue(arg);

result[name] = value;

Expand All @@ -267,17 +274,22 @@ function parseEventArgs(
}

/**
* Parse an event argument value.
* Normalize an event argument value.
* @param value - The event argument value.
* @returns The parsed event argument value.
* @returns The normalized event argument value.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseEventArgValue(value: any): Hex | Hex[] {
function normalizeEventArgValue(value: any): any {
if (Array.isArray(value)) {
return value.map(parseEventArgValue) as Hex[];
return value.map(normalizeEventArgValue);
}

return (value.toHexString?.() ?? value).toLowerCase();
let normalizedValue = value;

normalizedValue = normalizedValue.toHexString?.() ?? normalizedValue;
normalizedValue = normalizedValue.toLowerCase?.() ?? normalizedValue;

return normalizedValue;
}

/**
Expand Down Expand Up @@ -367,18 +379,16 @@ function getTokenBalanceTransactions(
const before = new Map();
const after = new Map();

const userEvents = events.filter(
(event) =>
SUPPORTED_EVENTS.includes(event.name) &&
[event.args.from, event.args.to].includes(request.from),
const userEvents = events.filter((event) =>
[event.args.from, event.args.to].includes(request.from),
);

log('Filtered user events', userEvents);

for (const event of userEvents) {
const tokenIds = getEventTokenIds(event);

log('Extracted token ids', tokenIds);
log('Extracted token IDs', tokenIds);

for (const tokenId of tokenIds) {
const simulationToken: SimulationToken = {
Expand Down

0 comments on commit 562e6a1

Please sign in to comment.