From 52b339312a8ad02cd201bfaf0a719ab3dfe681e0 Mon Sep 17 00:00:00 2001 From: TucksonDev Date: Wed, 27 Dec 2023 14:56:27 +0000 Subject: [PATCH] Export deployment block from wagmi config and use to fetch RollupInitialized events --- src/createRollupFetchTransactionHash.ts | 9 +++++- wagmi.config.ts | 37 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/createRollupFetchTransactionHash.ts b/src/createRollupFetchTransactionHash.ts index 9d6051bc..7f0fe0e6 100644 --- a/src/createRollupFetchTransactionHash.ts +++ b/src/createRollupFetchTransactionHash.ts @@ -1,5 +1,7 @@ import { Address, PublicClient } from 'viem'; import { AbiEvent } from 'abitype'; +import { deploymentBlockNumber } from './generated'; +import { ParentChainId } from './types/ParentChain'; export type CreateRollupFetchTransactionHashParams = { rollupAddress: Address; @@ -31,10 +33,15 @@ export async function createRollupFetchTransactionHash({ publicClient, }: CreateRollupFetchTransactionHashParams) { // Find the RollupInitialized event from that Rollup contract + const chainId = await publicClient.getChainId(); + const fromBlock = (chainId in Object.keys(deploymentBlockNumber.RollupCreator)) + ? deploymentBlockNumber.RollupCreator[chainId as ParentChainId] + : 'earliest'; + const rollupInitializedEvents = await publicClient.getLogs({ address: rollupAddress, event: RollupInitializedEventAbi, - fromBlock: 'earliest', + fromBlock, toBlock: 'latest', }); if (rollupInitializedEvents.length !== 1) { diff --git a/wagmi.config.ts b/wagmi.config.ts index 34ae4c0c..4dcd7f71 100644 --- a/wagmi.config.ts +++ b/wagmi.config.ts @@ -1,4 +1,6 @@ +import { Plugin } from '@wagmi/cli'; import { erc, etherscan } from '@wagmi/cli/plugins'; +import dedent from "dedent"; import dotenv from 'dotenv'; import { ParentChainId } from './src'; @@ -123,6 +125,38 @@ export async function assertContractAbisMatch(contract: ContractConfig) { console.log(`- ${contract.name} ✔`); } +// https://wagmi.sh/cli/plugins#creating-plugins +type DeploymentBlockNumberPluginConfig = { + contracts: ContractConfig[]; +}; +type DeploymentBlockNumberPluginResult = Required> & Omit; + +function deploymentBlockNumberPlugin(config: DeploymentBlockNumberPluginConfig): DeploymentBlockNumberPluginResult { + return { + name: "DeploymentBlockNumber", + async run({ contracts, isTypeScript, outputs }) { + const pluginOutput = dedent` + export const deploymentBlockNumber = { + ${config.contracts.map((contract) => { + return (typeof contract.deploymentBlockNumber === 'bigint') + ? `${contract.name}: ${contract.deploymentBlockNumber}n` + : dedent` + ${contract.name}: { + ${Object.keys(contract.deploymentBlockNumber).map((parentChainId) => { + return `${parentChainId}: ${contract.deploymentBlockNumber[parentChainId]}n`; + })} + }` + })} + } as const + `; + + return { + content: pluginOutput, + }; + } + } +} + export default async function () { console.log(`Checking if contract ABIs match...`); @@ -145,6 +179,9 @@ export default async function () { contracts, cacheDuration: 0, }), + deploymentBlockNumberPlugin({ + contracts + }) ], }; }