Skip to content

Commit

Permalink
feat: add docstrings to createRollupFetchTransactionHash
Browse files Browse the repository at this point in the history
  • Loading branch information
anegg0 committed Jun 11, 2024
1 parent 332f529 commit 1aa2030
Showing 1 changed file with 80 additions and 2 deletions.
82 changes: 80 additions & 2 deletions src/createRollupFetchTransactionHash.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Address, PublicClient } from 'viem';
-
import { AbiEvent } from 'abitype';

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

export type CreateRollupFetchTransactionHashParams = {
params: CreateRollupFetchTransactionHashParams;
rollup: Address;
publicClient: PublicClient;
};

const RollupIni...
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;
}

0 comments on commit 1aa2030

Please sign in to comment.