Skip to content

Commit

Permalink
Export deployment block from wagmi config and use to fetch RollupInit…
Browse files Browse the repository at this point in the history
…ialized events
  • Loading branch information
TucksonDev committed Dec 27, 2023
1 parent dcb9b61 commit 52b3393
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/createRollupFetchTransactionHash.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions wagmi.config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<Pick<Plugin, 'run'>> & Omit<Plugin, 'run'>;

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...`);

Expand All @@ -145,6 +179,9 @@ export default async function () {
contracts,
cacheDuration: 0,
}),
deploymentBlockNumberPlugin({
contracts
})
],
};
}

0 comments on commit 52b3393

Please sign in to comment.