Skip to content

Commit 1aa2030

Browse files
committed
feat: add docstrings to createRollupFetchTransactionHash
1 parent 332f529 commit 1aa2030

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

src/createRollupFetchTransactionHash.ts

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Address, PublicClient } from 'viem';
2+
-
23
import { AbiEvent } from 'abitype';
34

45
import { validateParentChain } from './types/ParentChain';
@@ -14,9 +15,86 @@ import {
1415
} from './chains';
1516

1617
export type CreateRollupFetchTransactionHashParams = {
17-
params: CreateRollupFetchTransactionHashParams;
1818
rollup: Address;
1919
publicClient: PublicClient;
2020
};
2121

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

Comments
 (0)