-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathgetBatchPosters.ts
More file actions
213 lines (190 loc) Β· 6.98 KB
/
getBatchPosters.ts
File metadata and controls
213 lines (190 loc) Β· 6.98 KB
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
206
207
208
209
210
211
212
213
import {
Address,
Chain,
Hex,
PublicClient,
Transport,
decodeFunctionData,
getAbiItem,
getFunctionSelector,
} from 'viem';
import { rollupCreatorABI as rollupCreatorV3Dot1ABI } from './contracts/RollupCreator';
import { rollupCreatorABI as rollupCreatorV2Dot1ABI } from './contracts/RollupCreator/v2.1';
import { rollupCreatorABI as rollupCreatorV1Dot1ABI } from './contracts/RollupCreator/v1.1';
import { sequencerInboxABI } from './contracts/SequencerInbox';
import { upgradeExecutorABI } from './contracts/UpgradeExecutor';
import { gnosisSafeL2ABI } from './contracts/GnosisSafeL2';
import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { getLogsWithBatching } from './utils/getLogsWithBatching';
const createRollupV3Dot1ABI = getAbiItem({ abi: rollupCreatorV3Dot1ABI, name: 'createRollup' });
const createRollupV3Dot1FunctionSelector = getFunctionSelector(createRollupV3Dot1ABI);
const createRollupV2Dot1ABI = getAbiItem({ abi: rollupCreatorV2Dot1ABI, name: 'createRollup' });
const createRollupV2Dot1FunctionSelector = getFunctionSelector(createRollupV2Dot1ABI);
const createRollupV1Dot1ABI = getAbiItem({ abi: rollupCreatorV1Dot1ABI, name: 'createRollup' });
const createRollupV1Dot1FunctionSelector = getFunctionSelector(createRollupV1Dot1ABI);
const setIsBatchPosterABI = getAbiItem({ abi: sequencerInboxABI, name: 'setIsBatchPoster' });
const setIsBatchPosterFunctionSelector = getFunctionSelector(setIsBatchPosterABI);
const executeCallABI = getAbiItem({ abi: upgradeExecutorABI, name: 'executeCall' });
const upgradeExecutorExecuteCallFunctionSelector = getFunctionSelector(executeCallABI);
const execTransactionABI = getAbiItem({ abi: gnosisSafeL2ABI, name: 'execTransaction' });
const safeL2FunctionSelector = getFunctionSelector(execTransactionABI);
const ownerFunctionCalledEventAbi = getAbiItem({
abi: sequencerInboxABI,
name: 'OwnerFunctionCalled',
});
function getBatchPostersFromFunctionData<
TAbi extends
| (typeof createRollupV3Dot1ABI)[]
| (typeof createRollupV2Dot1ABI)[]
| (typeof createRollupV1Dot1ABI)[]
| (typeof setIsBatchPosterABI)[],
>({ abi, data }: { abi: TAbi; data: Hex }) {
const { args } = decodeFunctionData({
abi,
data,
});
return args;
}
function updateAccumulator(acc: Set<Address>, input: Hex) {
const [batchPoster, isAdd] = getBatchPostersFromFunctionData({
abi: [setIsBatchPosterABI],
data: input,
});
if (isAdd) {
acc.add(batchPoster);
} else {
acc.delete(batchPoster);
}
return acc;
}
export type GetBatchPostersParams = {
/** Address of the rollup we're getting list of batch posters from */
rollup: Address;
/** Address of the sequencerInbox we're getting logs from */
sequencerInbox: Address;
};
export type GetBatchPostersReturnType = {
/**
* If logs contain unknown signature, batch posters list might:
* - contain false positives (batch posters that were removed, but returned as batch poster)
* - contain false negatives (batch posters that were added, but not present in the list)
*/
isAccurate: boolean;
/** List of batch posters for the given rollup */
batchPosters: Address[];
};
/**
*
* @param {PublicClient} publicClient - The chain Viem Public Client
* @param {GetBatchPostersParams} GetBatchPostersParams {@link GetBatchPostersParams}
*
* @returns Promise<{@link GetBatchPostersReturnType}>
*
* @remarks Batch posters list is not guaranteed to be exhaustive if the `isAccurate` flag is false.
* It might contain false positive (batch posters that were removed, but returned as batch poster)
* or false negative (batch posters that were added, but not present in the list)
*
* @example
* const { isAccurate, batchPosters } = getBatchPosters(client, {
* rollup: '0xc47dacfbaa80bd9d8112f4e8069482c2a3221336',
* sequencerInbox: '0x995a9d3ca121D48d21087eDE20bc8acb2398c8B1'
* });
*
* if (isAccurate) {
* // batch posters were all fetched properly
* } else {
* // batch posters list is not guaranteed to be accurate
* }
*/
export async function getBatchPosters<TChain extends Chain>(
publicClient: PublicClient<Transport, TChain>,
{ rollup, sequencerInbox }: GetBatchPostersParams,
): Promise<GetBatchPostersReturnType> {
let blockNumber: bigint;
let createRollupTransactionHash: Address | null = null;
try {
createRollupTransactionHash = await createRollupFetchTransactionHash({
rollup,
publicClient,
});
const receipt = await publicClient.waitForTransactionReceipt({
hash: createRollupTransactionHash,
});
blockNumber = receipt.blockNumber;
} catch (e) {
blockNumber = 0n;
}
const sequencerInboxEvents = await getLogsWithBatching(publicClient, {
address: sequencerInbox,
event: ownerFunctionCalledEventAbi,
args: { id: 1n },
fromBlock: blockNumber,
});
const events = createRollupTransactionHash
? [{ transactionHash: createRollupTransactionHash }, ...sequencerInboxEvents]
: sequencerInboxEvents;
const txs = await Promise.all(
events.map((event) =>
publicClient.getTransaction({
hash: event.transactionHash,
}),
),
);
let isAccurate = true;
const batchPosters = txs.reduce((acc, tx) => {
const txSelectedFunction = tx.input.slice(0, 10);
switch (txSelectedFunction) {
case createRollupV3Dot1FunctionSelector: {
const [{ batchPosters }] = getBatchPostersFromFunctionData({
abi: [createRollupV3Dot1ABI],
data: tx.input,
});
return new Set([...acc, ...batchPosters]);
}
case createRollupV2Dot1FunctionSelector: {
const [{ batchPosters }] = getBatchPostersFromFunctionData({
abi: [createRollupV2Dot1ABI],
data: tx.input,
});
return new Set([...acc, ...batchPosters]);
}
case createRollupV1Dot1FunctionSelector: {
const [{ batchPoster }] = getBatchPostersFromFunctionData({
abi: [createRollupV1Dot1ABI],
data: tx.input,
});
return new Set([...acc, batchPoster]);
}
case setIsBatchPosterFunctionSelector: {
return updateAccumulator(acc, tx.input);
}
case upgradeExecutorExecuteCallFunctionSelector: {
const { args: executeCallCalldata } = decodeFunctionData({
abi: [executeCallABI],
data: tx.input,
});
return updateAccumulator(acc, executeCallCalldata[1]);
}
case safeL2FunctionSelector: {
const { args: execTransactionCalldata } = decodeFunctionData({
abi: [execTransactionABI],
data: tx.input,
});
const { args: executeCallCalldata } = decodeFunctionData({
abi: [executeCallABI],
data: execTransactionCalldata[2],
});
return updateAccumulator(acc, executeCallCalldata[1]);
}
default: {
console.warn(`[getBatchPosters] unknown 4bytes, tx id: ${tx.hash}`);
isAccurate = false;
return acc;
}
}
}, new Set<Address>());
return {
isAccurate,
batchPosters: [...batchPosters],
};
}