1
1
import { Address , PublicClient } from 'viem' ;
2
+ -
2
3
import { AbiEvent } from 'abitype' ;
3
4
4
5
import { validateParentChain } from './types/ParentChain' ;
@@ -14,9 +15,86 @@ import {
14
15
} from './chains' ;
15
16
16
17
export type CreateRollupFetchTransactionHashParams = {
17
- params : CreateRollupFetchTransactionHashParams ;
18
18
rollup : Address ;
19
19
publicClient : PublicClient ;
20
20
} ;
21
21
22
- const RollupIni ...
22
+ const RollupInitializedEventAbi : AbiEvent = {
23
+ anonymous : false ,
24
+ inputs : [
25
+ {
26
+ indexed : false ,
27
+ internalType : 'bytes32' ,
28
+ name : 'machineHash' ,
29
+ type : 'bytes32' ,
30
+ } ,
31
+ {
32
+ indexed : false ,
33
+ internalType : 'uint256' ,
34
+ name : 'chainId' ,
35
+ type : 'uint256' ,
36
+ } ,
37
+ ] ,
38
+ name : 'RollupInitialized' ,
39
+ type : 'event' ,
40
+ } ;
41
+
42
+ const earliestRollupCreatorDeploymentBlockNumber = {
43
+ // mainnet
44
+ [ mainnet . id ] : 18736164n ,
45
+ [ arbitrumOne . id ] : 150599584n ,
46
+ [ arbitrumNova . id ] : 47798739n ,
47
+ // testnet
48
+ [ sepolia . id ] : 4741823n ,
49
+ [ holesky . id ] : 1083992n ,
50
+ [ arbitrumSepolia . id ] : 654628n ,
51
+ // local nitro-testnode
52
+ [ nitroTestnodeL1 . id ] : 0n ,
53
+ [ nitroTestnodeL2 . id ] : 0n ,
54
+ } ;
55
+
56
+ /**
57
+ * createRollupFetchTransactionHash retrieves the transaction hash of the
58
+ * RollupInitialized event for a specified rollup contract on a given chain. It
59
+ * takes in the rollup contract address and a PublicClient, validates the parent
60
+ * chain, and then fetches the RollupInitialized event logs to extract the
61
+ * transaction hash. This function ensures that only one RollupInitialized event
62
+ * is found for the specified rollup address before returning the transaction
63
+ * hash.
64
+ */
65
+ export async function createRollupFetchTransactionHash ( {
66
+ rollup,
67
+ publicClient,
68
+ } : CreateRollupFetchTransactionHashParams ) {
69
+ const chainId = validateParentChain ( publicClient ) ;
70
+
71
+ const fromBlock =
72
+ chainId in earliestRollupCreatorDeploymentBlockNumber
73
+ ? earliestRollupCreatorDeploymentBlockNumber [ chainId ]
74
+ : 'earliest' ;
75
+
76
+ // Find the RollupInitialized event from that Rollup contract
77
+ const rollupInitializedEvents = await publicClient . getLogs ( {
78
+ address : rollup ,
79
+ event : RollupInitializedEventAbi ,
80
+ fromBlock,
81
+ toBlock : 'latest' ,
82
+ } ) ;
83
+
84
+ if ( rollupInitializedEvents . length !== 1 ) {
85
+ throw new Error (
86
+ `Expected to find 1 RollupInitialized event for rollup address ${ rollup } but found ${ rollupInitializedEvents . length } ` ,
87
+ ) ;
88
+ }
89
+
90
+ // Get the transaction hash that emitted that event
91
+ const transactionHash = rollupInitializedEvents [ 0 ] . transactionHash ;
92
+
93
+ if ( ! transactionHash ) {
94
+ throw new Error (
95
+ `No transactionHash found in RollupInitialized event for rollup address ${ rollup } ` ,
96
+ ) ;
97
+ }
98
+
99
+ return transactionHash ;
100
+ }
0 commit comments