forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decodeEventLog.ts
205 lines (193 loc) · 5.82 KB
/
decodeEventLog.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type {
Abi,
AbiParameter,
ExtractAbiEventNames,
Narrow,
} from "https://esm.sh/abitype";
import {
AbiDecodingDataSizeTooSmallError,
AbiEventSignatureEmptyTopicsError,
AbiEventSignatureNotFoundError,
DecodeLogDataMismatch,
DecodeLogTopicsMismatch,
} from "https://esm.sh/viem/dist/esm/errors/abi.js";
import type {
EventDefinition,
GetEventArgsFromTopics,
InferEventName,
} from "https://esm.sh/viem/dist/types/types/contract.d.ts";
import type { Hex } from "https://esm.sh/viem/dist/types/types/misc.d.ts";
import type { Prettify } from "https://esm.sh/viem/dist/types/types/utils.d.ts";
import { getEventSelector } from "https://esm.sh/viem/dist/esm/utils/hash/getEventSelector.js";
import { decodeAbiParameters } from "https://esm.sh/viem/dist/esm/utils/abi/decodeAbiParameters.js";
import type { DecodeAbiParametersReturnType } from "https://esm.sh/viem/dist/types/utils/abi/decodeAbiParameters.d.ts";
import { formatAbiItem } from "https://esm.sh/viem/dist/esm/utils/abi/formatAbiItem.js";
export type DecodeEventLogParameters<
TAbi extends Abi | readonly unknown[] = Abi,
TEventName extends string | undefined = string,
TTopics extends Hex[] = Hex[],
TData extends Hex | undefined = undefined,
TStrict extends boolean = true,
> = {
abi: Narrow<TAbi>;
data?: TData;
eventName?: InferEventName<TAbi, TEventName>;
strict?: TStrict;
topics: [signature: Hex, ...args: TTopics] | [];
};
export type DecodeEventLogReturnType<
TAbi extends Abi | readonly unknown[] = Abi,
TEventName extends string | undefined = string,
TTopics extends Hex[] = Hex[],
TData extends Hex | undefined = undefined,
TStrict extends boolean = true,
_EventNames extends string = TAbi extends Abi ? Abi extends TAbi ? string
: ExtractAbiEventNames<TAbi>
: string,
> = TEventName extends _EventNames[number] ? Prettify<
{
eventName: TEventName;
} & GetEventArgsFromTopics<TAbi, TEventName, TTopics, TData, TStrict>
>
: {
[TName in _EventNames]: Prettify<
{
eventName: TName;
} & GetEventArgsFromTopics<TAbi, TName, TTopics, TData, TStrict>
>;
}[_EventNames];
const docsPath = "/docs/contract/decodeEventLog";
export function decodeEventLog<
TAbi extends Abi | readonly unknown[],
TEventName extends string | undefined = undefined,
TTopics extends Hex[] = Hex[],
TData extends Hex | undefined = undefined,
TStrict extends boolean = true,
>({
abi,
data,
strict: strict_,
topics,
}: DecodeEventLogParameters<
TAbi,
TEventName,
TTopics,
TData,
TStrict
>): DecodeEventLogReturnType<TAbi, TEventName, TTopics, TData, TStrict> {
const strict = strict_ ?? true;
const [signature, ...argTopics] = topics;
if (!signature) {
throw new AbiEventSignatureEmptyTopicsError({
docsPath,
});
}
const abiItem = (abi as Abi).find(
(x) =>
x.type === "event" &&
signature === getEventSelector(formatAbiItem(x) as EventDefinition),
);
if (!(abiItem && "name" in abiItem) || abiItem.type !== "event") {
throw new AbiEventSignatureNotFoundError(signature, {
docsPath,
});
}
const { name, inputs } = abiItem;
const indexedInputs = inputs.filter((x) => "indexed" in x && x.indexed);
const nonIndexedInputs = inputs.filter((x) => !indexedInputs.includes(x));
const decodedTopics: DecodeAbiParametersReturnType<typeof indexedInputs> = [];
type decodedDataType = DecodeAbiParametersReturnType<typeof nonIndexedInputs>;
let decodedData: decodedDataType = undefined as unknown as decodedDataType;
// Decode topics (indexed args).
if (argTopics.length > 0) {
for (let i = 0; i < indexedInputs.length; i++) {
const param = indexedInputs[i];
const topic = argTopics[i];
if (!topic) {
throw new DecodeLogTopicsMismatch({
abiItem,
param: param as AbiParameter & { indexed: boolean },
});
}
decodedTopics.push(decodeTopic({ param, value: topic }));
}
}
// Decode data (non-indexed args).
if (nonIndexedInputs.length > 0) {
if (data && data !== "0x") {
try {
decodedData = decodeAbiParameters(nonIndexedInputs, data);
} catch (err) {
if (strict) {
if (err instanceof AbiDecodingDataSizeTooSmallError) {
throw new DecodeLogDataMismatch({
abiItem,
data: err.data,
params: err.params,
size: err.size,
});
}
throw err;
}
}
} else if (strict) {
throw new DecodeLogDataMismatch({
abiItem,
data: "0x",
params: nonIndexedInputs,
size: 0,
});
}
}
const names = inputs.filter((x) => ("name" in x && x.name)).map((x) =>
x.name!
);
type ValueOf<T> = T[keyof T];
type Args =
& Array<ValueOf<typeof decodedTopics | typeof decodedData>>
& {
[K in typeof names[number]]: ValueOf<
typeof decodedTopics | typeof decodedData
>[K];
};
const args: Args = inputs
.reduce(
(acc, x) => {
const arg = indexedInputs.includes(x)
? decodedTopics.shift()
: decodedData
? decodedData.shift()
: undefined;
if (arg !== undefined) {
acc.push(arg);
}
if (x.name) {
acc[x.name] = arg;
}
return acc;
},
[] as unknown as Args,
);
return {
eventName: name,
args: args.length > 0 ? args : undefined,
} as unknown as DecodeEventLogReturnType<
TAbi,
TEventName,
TTopics,
TData,
TStrict
>;
}
function decodeTopic({ param, value }: { param: AbiParameter; value: Hex }) {
if (
param.type === "string" ||
param.type === "bytes" ||
param.type === "tuple" ||
param.type.match(/^(.*)\[(\d+)?\]$/)
) {
return value;
}
const decodedArg = decodeAbiParameters([param], value) || [];
return decodedArg[0];
}