From 1aa2030374f41bb1bf01834ef0c05d2e6663f5e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Blanchemain?= Date: Tue, 11 Jun 2024 10:30:48 -0700 Subject: [PATCH] feat: add docstrings to createRollupFetchTransactionHash --- src/createRollupFetchTransactionHash.ts | 82 ++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/src/createRollupFetchTransactionHash.ts b/src/createRollupFetchTransactionHash.ts index 615684ae..a48f5066 100644 --- a/src/createRollupFetchTransactionHash.ts +++ b/src/createRollupFetchTransactionHash.ts @@ -1,4 +1,5 @@ import { Address, PublicClient } from 'viem'; +- import { AbiEvent } from 'abitype'; import { validateParentChain } from './types/ParentChain'; @@ -14,9 +15,86 @@ import { } from './chains'; export type CreateRollupFetchTransactionHashParams = { - params: CreateRollupFetchTransactionHashParams; rollup: Address; publicClient: PublicClient; }; -const RollupIni... \ No newline at end of file +const RollupInitializedEventAbi: AbiEvent = { + anonymous: false, + inputs: [ + { + indexed: false, + internalType: 'bytes32', + name: 'machineHash', + type: 'bytes32', + }, + { + indexed: false, + internalType: 'uint256', + name: 'chainId', + type: 'uint256', + }, + ], + name: 'RollupInitialized', + type: 'event', +}; + +const earliestRollupCreatorDeploymentBlockNumber = { + // mainnet + [mainnet.id]: 18736164n, + [arbitrumOne.id]: 150599584n, + [arbitrumNova.id]: 47798739n, + // testnet + [sepolia.id]: 4741823n, + [holesky.id]: 1083992n, + [arbitrumSepolia.id]: 654628n, + // local nitro-testnode + [nitroTestnodeL1.id]: 0n, + [nitroTestnodeL2.id]: 0n, +}; + +/** + * createRollupFetchTransactionHash retrieves the transaction hash of the + * RollupInitialized event for a specified rollup contract on a given chain. It + * takes in the rollup contract address and a PublicClient, validates the parent + * chain, and then fetches the RollupInitialized event logs to extract the + * transaction hash. This function ensures that only one RollupInitialized event + * is found for the specified rollup address before returning the transaction + * hash. + */ +export async function createRollupFetchTransactionHash({ + rollup, + publicClient, +}: CreateRollupFetchTransactionHashParams) { + const chainId = validateParentChain(publicClient); + + const fromBlock = + chainId in earliestRollupCreatorDeploymentBlockNumber + ? earliestRollupCreatorDeploymentBlockNumber[chainId] + : 'earliest'; + + // Find the RollupInitialized event from that Rollup contract + const rollupInitializedEvents = await publicClient.getLogs({ + address: rollup, + event: RollupInitializedEventAbi, + fromBlock, + toBlock: 'latest', + }); + + if (rollupInitializedEvents.length !== 1) { + throw new Error( + `Expected to find 1 RollupInitialized event for rollup address ${rollup} but found ${rollupInitializedEvents.length}`, + ); + } + + // Get the transaction hash that emitted that event + const transactionHash = rollupInitializedEvents[0].transactionHash; + + if (!transactionHash) { + throw new Error( + `No transactionHash found in RollupInitialized event for rollup address ${rollup}`, + ); + } + + return transactionHash; +}