forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventResponse.ts
43 lines (40 loc) · 1.24 KB
/
EventResponse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { getAddress, toHex } from "npm:viem";
import { formatAbiItemPrototype } from "./abitype.ts";
import { decodeEventLog } from "./decodeEventLog.ts";
import { EventMessage } from "./EventMessage.ts";
export const serializeEventResponse = (evtMsg: EventMessage) => {
const { args } = decodeEventLog({
abi: [JSON.parse(evtMsg.abi)],
data: toHex(evtMsg.data),
topics: [...evtMsg.topics.toReversed(), evtMsg.sigHash].reduce(
(acc, x) => x != undefined ? [toHex(x), ...acc] : [],
[] as unknown as [] | [
signature: `0x${string}`,
...args: `0x${string}`[],
],
),
});
return {
blockIndex: evtMsg.blockNumber,
logIndex: evtMsg.logIndex,
blockHash: toHex(evtMsg.blockHash),
transactionHash: toHex(evtMsg.txHash),
sourceAddress: getAddress(toHex(evtMsg.address)),
abiHash: toHex(evtMsg.sigHash),
abiSignature: formatAbiItemPrototype(
JSON.parse(evtMsg.abi),
),
args: {
named: Object.keys(args).filter((x) =>
!Object.keys([...(args as unknown[])]).includes(x)
).reduce(
(acc, x) => ({
...acc,
[x]: (args as Record<string, unknown>)[x],
}),
{},
),
ordered: [...(args as unknown[])],
},
};
};