Skip to content

Commit

Permalink
fix: better error handling while parsing events
Browse files Browse the repository at this point in the history
  • Loading branch information
nvtaveras committed Jul 26, 2024
1 parent cacf862 commit 0fba369
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 39 deletions.
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert from "assert/strict";

// Types
import type {
HttpFunction,
Expand Down Expand Up @@ -27,7 +29,7 @@ export const watchdogNotifier: HttpFunction = async (
console.info("[HealthCheck]: Block", parsedEvent.block);
break;
default:
throw new Error("Unknown event type");
assert(false, `Unknown event type from payload: ${req.body}`);
}
}

Expand Down
84 changes: 46 additions & 38 deletions src/parse-transaction-receipts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import assert from "assert/strict";

// External
import { decodeEventLog } from "viem";

// Internal
import GovernorABI from "./governor-abi.js";
import { HealthCheckEvent, ProposalCreatedEvent } from "./types.js";
import { EventType, HealthCheckEvent, ProposalCreatedEvent } from "./types.js";
import hasLogs from "./utils/has-logs.js";
import getEventByTopic from "./utils/get-event-by-topic.js";
import isHealthCheckEvent from "./utils/is-health-check-event.js";
import isProposalCreatedEvent from "./utils/is-proposal-created-event.js";
import isTransactionReceipt from "./utils/is-transaction-receipt.js";
Expand Down Expand Up @@ -45,48 +48,53 @@ export default function parseTransactionReceipts(
}

for (const log of receipt.logs) {
if (!log.topics) {
throw new Error("No topics found in log");
}
assert(log.topics && log.topics.length > 0, "No topics found in log");

try {
const event = decodeEventLog({
abi: GovernorABI,
data: log.data as `0x${string}`,
topics: log.topics as [
signature: `0x${string}`,
...args: `0x${string}`[],
],
});
const eventSignature = log.topics[0];
const eventType = getEventByTopic(eventSignature);

if (isProposalCreatedEvent(event)) {
result.push({
event,
txHash: log.transactionHash,
switch (eventType) {
case EventType.Unknown:
// It can happen that a single transaction fired multiple events, some of which we are not interested in
continue;
case EventType.ProposalCreated:
const proposalEvent = decodeEventLog({
abi: GovernorABI,
data: log.data as `0x${string}`,
topics: log.topics as [
signature: `0x${string}`,
...args: `0x${string}`[],
],
});
}
// eslint-disable-next-line no-empty
} catch {}

try {
const event = decodeEventLog({
abi: SortedOraclesABI,
data: log.data as `0x${string}`,
topics: log.topics as [
signature: `0x${string}`,
...args: `0x${string}`[],
],
});

if (isHealthCheckEvent(event)) {
result.push({
block: Number(receipt.blockNumber),
event,
txHash: log.transactionHash,
if (isProposalCreatedEvent(proposalEvent)) {
result.push({
event: proposalEvent,
txHash: log.transactionHash,
});
}
break;
case EventType.MedianUpdated:
const healthCheckEvent = decodeEventLog({
abi: SortedOraclesABI,
data: log.data as `0x${string}`,
topics: log.topics as [
signature: `0x${string}`,
...args: `0x${string}`[],
],
});
}
// eslint-disable-next-line no-empty
} catch {}

if (isHealthCheckEvent(healthCheckEvent)) {
result.push({
block: Number(receipt.blockNumber),
event: healthCheckEvent,
txHash: log.transactionHash,
});
}
break;
default:
assert(false, `Unknown event type: ${eventType}`);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface LogsEntity {
}

export enum EventType {
Unknown = "Unknown",
ProposalCreated = "ProposalCreated",
MedianUpdated = "MedianUpdated",
}
Expand Down
14 changes: 14 additions & 0 deletions src/utils/get-event-by-topic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EventType } from "../types";

export default function getEventByTopic(topic: string): EventType {
switch (topic) {
// keccak256(abi.encodePacked("ProposalCreated(uint256,address,address[],uint256[],string[],bytes[],uint256,uint256,string)"))
case "0x7d84a6263ae0d98d3329bd7b46bb4e8d6f98cd35a7adb45c274c8b7fd5ebd5e0":
return EventType.ProposalCreated;
// keccak256(abi.encodePacked("MedianUpdated(address,uint256)"))
case "0xa9981ebfc3b766a742486e898f54959b050a66006dbce1a4155c1f84a08bcf41":
return EventType.MedianUpdated;
default:
return EventType.Unknown;
}
}

0 comments on commit 0fba369

Please sign in to comment.