From 7ea184df1671574f98fd2e770e2dd67ccd10bcf3 Mon Sep 17 00:00:00 2001 From: spsjvc Date: Thu, 3 Oct 2024 14:04:07 +0200 Subject: [PATCH] refactor: reduce number of validation functions --- src/actions/buildInvalidateKeysetHash.ts | 6 +++--- src/actions/buildSetIsBatchPoster.ts | 6 +++--- src/actions/buildSetMaxTimeVariation.ts | 6 +++--- src/actions/buildSetValidKeyset.ts | 6 +++--- src/createRollup.ts | 19 ++++++++++--------- ...llupAdminLogicPrepareTransactionRequest.ts | 6 +++--- ...sequencerInboxPrepareTransactionRequest.ts | 6 +++--- src/types/ParentChain.ts | 19 +------------------ 8 files changed, 29 insertions(+), 45 deletions(-) diff --git a/src/actions/buildInvalidateKeysetHash.ts b/src/actions/buildInvalidateKeysetHash.ts index fdc29550..1034fca1 100644 --- a/src/actions/buildInvalidateKeysetHash.ts +++ b/src/actions/buildInvalidateKeysetHash.ts @@ -7,7 +7,7 @@ import { WithUpgradeExecutor, } from '../types/Actions'; import { Prettify } from '../types/utils'; -import { validateParentChainPublicClient } from '../types/ParentChain'; +import { validateParentChain } from '../types/ParentChain'; import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters'; export type BuildInvalidateKeysetHashParameters = Prettify< @@ -35,7 +35,7 @@ export async function buildInvalidateKeysetHash { - const validatedPublicClient = validateParentChainPublicClient(client); + const chainId = validateParentChain(client); const request = await client.prepareTransactionRequest({ chain: client.chain, @@ -49,5 +49,5 @@ export async function buildInvalidateKeysetHash( params, }: BuildSetIsBatchPosterParameters & { params: { enable: boolean } }, ): Promise { - const validatedPublicClient = validateParentChainPublicClient(client); + const chainId = validateParentChain(client); const request = await client.prepareTransactionRequest({ chain: client.chain, @@ -43,7 +43,7 @@ export async function buildSetIsBatchPoster( }), } satisfies PrepareTransactionRequestParameters); - return { ...request, chainId: validatedPublicClient.chain.id }; + return { ...request, chainId }; } export async function buildEnableBatchPoster( diff --git a/src/actions/buildSetMaxTimeVariation.ts b/src/actions/buildSetMaxTimeVariation.ts index 3fad2176..8c0b3d16 100644 --- a/src/actions/buildSetMaxTimeVariation.ts +++ b/src/actions/buildSetMaxTimeVariation.ts @@ -8,7 +8,7 @@ import { } from '../types/Actions'; import { Prettify } from '../types/utils'; import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters'; -import { validateParentChainPublicClient } from '../types/ParentChain'; +import { validateParentChain } from '../types/ParentChain'; type Args = { delayBlocks: bigint; @@ -31,7 +31,7 @@ export async function buildSetMaxTimeVariation params, }: BuildSetMaxTimeVariationParameters, ): Promise { - const validatedPublicClient = validateParentChainPublicClient(client); + const chainId = validateParentChain(client); const request = await client.prepareTransactionRequest({ chain: client.chain, @@ -45,5 +45,5 @@ export async function buildSetMaxTimeVariation }), } satisfies PrepareTransactionRequestParameters); - return { ...request, chainId: validatedPublicClient.chain.id }; + return { ...request, chainId }; } diff --git a/src/actions/buildSetValidKeyset.ts b/src/actions/buildSetValidKeyset.ts index 666b1fda..70f0ca85 100644 --- a/src/actions/buildSetValidKeyset.ts +++ b/src/actions/buildSetValidKeyset.ts @@ -7,7 +7,7 @@ import { WithUpgradeExecutor, } from '../types/Actions'; import { Prettify } from '../types/utils'; -import { validateParentChainPublicClient } from '../types/ParentChain'; +import { validateParentChain } from '../types/ParentChain'; import { prepareUpgradeExecutorCallParameters } from '../prepareUpgradeExecutorCallParameters'; export type BuildSetValidKeysetParameters = Prettify< @@ -35,7 +35,7 @@ export async function buildSetValidKeyset( params, }: BuildSetValidKeysetParameters, ): Promise { - const validatedPublicClient = validateParentChainPublicClient(client); + const chainId = validateParentChain(client); const request = await client.prepareTransactionRequest({ chain: client.chain, @@ -49,5 +49,5 @@ export async function buildSetValidKeyset( }), } satisfies PrepareTransactionRequestParameters); - return { ...request, chainId: validatedPublicClient.chain.id }; + return { ...request, chainId }; } diff --git a/src/createRollup.ts b/src/createRollup.ts index fab36b79..4a0cbc27 100644 --- a/src/createRollup.ts +++ b/src/createRollup.ts @@ -14,11 +14,11 @@ import { createRollupPrepareTransaction, } from './createRollupPrepareTransaction'; import { CreateRollupParams } from './types/createRollupTypes'; -import { ParentChainPublicClient, validateParentChainPublicClient } from './types/ParentChain'; +import { validateParentChain } from './types/ParentChain'; type EnsureCustomGasTokenAllowanceGrantedToRollupCreatorParams = { nativeToken: Address; - parentChainPublicClient: ParentChainPublicClient; + parentChainPublicClient: PublicClient; account: PrivateKeyAccount; }; @@ -156,15 +156,16 @@ export async function createRollup({ account, parentChainPublicClient, }: CreateRollupFunctionParams): Promise { - const validatedParentChainPublicClient = validateParentChainPublicClient(parentChainPublicClient); - const parentChain = validatedParentChainPublicClient.chain; + validateParentChain(parentChainPublicClient); + + const parentChain = parentChainPublicClient.chain; const nativeToken = params.nativeToken ?? zeroAddress; if (nativeToken !== zeroAddress) { // check Rollup Creator custom gas token spending allowance and approve if necessary await ensureCustomGasTokenAllowanceGrantedToRollupCreator({ nativeToken, - parentChainPublicClient: validatedParentChainPublicClient, + parentChainPublicClient, account, }); } @@ -173,18 +174,18 @@ export async function createRollup({ const txRequest = await createRollupPrepareTransactionRequest({ params, account: account.address, - publicClient: validatedParentChainPublicClient, + publicClient: parentChainPublicClient, }); // sign and send the transaction console.log(`Deploying the Rollup...`); - const txHash = await validatedParentChainPublicClient.sendRawTransaction({ + const txHash = await parentChainPublicClient.sendRawTransaction({ serializedTransaction: await account.signTransaction(txRequest), }); // get the transaction receipt after waiting for the transaction to complete const txReceipt = createRollupPrepareTransactionReceipt( - await validatedParentChainPublicClient.waitForTransactionReceipt({ hash: txHash }), + await parentChainPublicClient.waitForTransactionReceipt({ hash: txHash }), ); // get the transaction @@ -193,7 +194,7 @@ export async function createRollup({ // https://github.com/wevm/viem/issues/1056#issuecomment-1689800265 ) const tx = createRollupPrepareTransaction( // @ts-ignore (todo: fix viem type issue) - await validatedParentChainPublicClient.getTransaction({ hash: txHash }), + await parentChainPublicClient.getTransaction({ hash: txHash }), ); console.log(`Deployed in ${getBlockExplorerUrl(parentChain)}/tx/${txReceipt.transactionHash}`); diff --git a/src/rollupAdminLogicPrepareTransactionRequest.ts b/src/rollupAdminLogicPrepareTransactionRequest.ts index 59f7260d..05cc9762 100644 --- a/src/rollupAdminLogicPrepareTransactionRequest.ts +++ b/src/rollupAdminLogicPrepareTransactionRequest.ts @@ -11,7 +11,7 @@ import { rollupABI } from './contracts/Rollup'; import { upgradeExecutorEncodeFunctionData } from './upgradeExecutorEncodeFunctionData'; import { GetFunctionName } from './types/utils'; -import { validateParentChainPublicClient } from './types/ParentChain'; +import { validateParentChain } from './types/ParentChain'; export type RollupAdminLogicAbi = typeof rollupABI; export type RollupAdminLogicFunctionName = GetFunctionName; @@ -83,7 +83,7 @@ export async function rollupAdminLogicPrepareTransactionRequest< client: PublicClient, params: RollupAdminLogicPrepareTransactionRequestParameters, ) { - const validatedPublicClient = validateParentChainPublicClient(client); + const chainId = validateParentChain(client); // params is extending RollupAdminLogicPrepareFunctionDataParameters, it's safe to cast const { to, data, value } = rollupAdminLogicPrepareFunctionData({ @@ -100,5 +100,5 @@ export async function rollupAdminLogicPrepareTransactionRequest< account: params.account, }); - return { ...request, chainId: validatedPublicClient.chain.id }; + return { ...request, chainId }; } diff --git a/src/sequencerInboxPrepareTransactionRequest.ts b/src/sequencerInboxPrepareTransactionRequest.ts index 1bbd63e3..2d84b65a 100644 --- a/src/sequencerInboxPrepareTransactionRequest.ts +++ b/src/sequencerInboxPrepareTransactionRequest.ts @@ -10,7 +10,7 @@ import { import { sequencerInboxABI } from './contracts/SequencerInbox'; import { upgradeExecutorEncodeFunctionData } from './upgradeExecutorEncodeFunctionData'; import { GetFunctionName } from './types/utils'; -import { validateParentChainPublicClient } from './types/ParentChain'; +import { validateParentChain } from './types/ParentChain'; export type SequencerInboxAbi = typeof sequencerInboxABI; export type SequencerInboxFunctionName = GetFunctionName; @@ -80,7 +80,7 @@ export async function sequencerInboxPrepareTransactionRequest< client: PublicClient, params: SequencerInboxPrepareTransactionRequestParameters, ) { - const validatedPublicClient = validateParentChainPublicClient(client); + const chainId = validateParentChain(client); // params is extending SequencerInboxPrepareFunctionDataParameters, it's safe to cast const { to, data, value } = sequencerInboxPrepareFunctionData({ @@ -97,5 +97,5 @@ export async function sequencerInboxPrepareTransactionRequest< account: params.account, }); - return { ...request, chainId: validatedPublicClient.chain.id }; + return { ...request, chainId }; } diff --git a/src/types/ParentChain.ts b/src/types/ParentChain.ts index 261a24cb..8ee24a15 100644 --- a/src/types/ParentChain.ts +++ b/src/types/ParentChain.ts @@ -1,16 +1,11 @@ -import { Client, PublicClient, Transport, Chain } from 'viem'; +import { Client, Transport, Chain } from 'viem'; import { chains, nitroTestnodeL3 } from '../chains'; -import { Prettify } from './utils'; // exclude nitro-testnode L3 from the list of parent chains export type ParentChain = Exclude<(typeof chains)[number], { id: typeof nitroTestnodeL3.id }>; export type ParentChainId = ParentChain['id']; -export type ParentChainPublicClient = Prettify< - PublicClient & { chain: { id: ParentChainId } } ->; - export function isValidParentChainId( parentChainId: number | undefined, ): parentChainId is ParentChainId { @@ -32,15 +27,3 @@ export function validateParentChain( return chainId; } - -export function validateParentChainPublicClient( - publicClient: PublicClient, -): ParentChainPublicClient { - const chainId = publicClient.chain?.id; - - if (!isValidParentChainId(chainId)) { - throw new Error(`Parent chain not supported: ${chainId}`); - } - - return publicClient as ParentChainPublicClient; -}