Skip to content

Commit

Permalink
refactor: reduce number of validation functions
Browse files Browse the repository at this point in the history
  • Loading branch information
spsjvc committed Oct 3, 2024
1 parent 7669197 commit 7ea184d
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/actions/buildInvalidateKeysetHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Curried extends boolean = false> = Prettify<
Expand Down Expand Up @@ -35,7 +35,7 @@ export async function buildInvalidateKeysetHash<TChain extends Chain | undefined
params,
}: BuildInvalidateKeysetHashParameters,
): Promise<BuildInvalidateKeysetHashReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const chainId = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand All @@ -49,5 +49,5 @@ export async function buildInvalidateKeysetHash<TChain extends Chain | undefined
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
return { ...request, chainId };
}
6 changes: 3 additions & 3 deletions src/actions/buildSetIsBatchPoster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
batchPoster: Address;
Expand All @@ -29,7 +29,7 @@ export async function buildSetIsBatchPoster<TChain extends Chain | undefined>(
params,
}: BuildSetIsBatchPosterParameters & { params: { enable: boolean } },
): Promise<BuildSetIsBatchPosterReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const chainId = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand All @@ -43,7 +43,7 @@ export async function buildSetIsBatchPoster<TChain extends Chain | undefined>(
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
return { ...request, chainId };
}

export async function buildEnableBatchPoster<TChain extends Chain | undefined>(
Expand Down
6 changes: 3 additions & 3 deletions src/actions/buildSetMaxTimeVariation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,7 +31,7 @@ export async function buildSetMaxTimeVariation<TChain extends Chain | undefined>
params,
}: BuildSetMaxTimeVariationParameters,
): Promise<BuildSetMaxTimeVariationReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const chainId = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand All @@ -45,5 +45,5 @@ export async function buildSetMaxTimeVariation<TChain extends Chain | undefined>
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
return { ...request, chainId };
}
6 changes: 3 additions & 3 deletions src/actions/buildSetValidKeyset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Curried extends boolean = false> = Prettify<
Expand Down Expand Up @@ -35,7 +35,7 @@ export async function buildSetValidKeyset<TChain extends Chain | undefined>(
params,
}: BuildSetValidKeysetParameters,
): Promise<BuildSetValidKeysetReturnType> {
const validatedPublicClient = validateParentChainPublicClient(client);
const chainId = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand All @@ -49,5 +49,5 @@ export async function buildSetValidKeyset<TChain extends Chain | undefined>(
}),
} satisfies PrepareTransactionRequestParameters);

return { ...request, chainId: validatedPublicClient.chain.id };
return { ...request, chainId };
}
19 changes: 10 additions & 9 deletions src/createRollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TChain extends Chain | undefined> = {
nativeToken: Address;
parentChainPublicClient: ParentChainPublicClient<TChain>;
parentChainPublicClient: PublicClient<Transport, TChain>;
account: PrivateKeyAccount;
};

Expand Down Expand Up @@ -156,15 +156,16 @@ export async function createRollup<TChain extends Chain | undefined>({
account,
parentChainPublicClient,
}: CreateRollupFunctionParams<TChain>): Promise<CreateRollupResults> {
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,
});
}
Expand All @@ -173,18 +174,18 @@ export async function createRollup<TChain extends Chain | undefined>({
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
Expand All @@ -193,7 +194,7 @@ export async function createRollup<TChain extends Chain | undefined>({
// 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}`);
Expand Down
6 changes: 3 additions & 3 deletions src/rollupAdminLogicPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RollupAdminLogicAbi>;
Expand Down Expand Up @@ -83,7 +83,7 @@ export async function rollupAdminLogicPrepareTransactionRequest<
client: PublicClient<TTransport, TChain>,
params: RollupAdminLogicPrepareTransactionRequestParameters<TFunctionName>,
) {
const validatedPublicClient = validateParentChainPublicClient(client);
const chainId = validateParentChain(client);

// params is extending RollupAdminLogicPrepareFunctionDataParameters, it's safe to cast
const { to, data, value } = rollupAdminLogicPrepareFunctionData({
Expand All @@ -100,5 +100,5 @@ export async function rollupAdminLogicPrepareTransactionRequest<
account: params.account,
});

return { ...request, chainId: validatedPublicClient.chain.id };
return { ...request, chainId };
}
6 changes: 3 additions & 3 deletions src/sequencerInboxPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SequencerInboxAbi>;
Expand Down Expand Up @@ -80,7 +80,7 @@ export async function sequencerInboxPrepareTransactionRequest<
client: PublicClient<TTransport, TChain>,
params: SequencerInboxPrepareTransactionRequestParameters<TFunctionName>,
) {
const validatedPublicClient = validateParentChainPublicClient(client);
const chainId = validateParentChain(client);

// params is extending SequencerInboxPrepareFunctionDataParameters, it's safe to cast
const { to, data, value } = sequencerInboxPrepareFunctionData({
Expand All @@ -97,5 +97,5 @@ export async function sequencerInboxPrepareTransactionRequest<
account: params.account,
});

return { ...request, chainId: validatedPublicClient.chain.id };
return { ...request, chainId };
}
19 changes: 1 addition & 18 deletions src/types/ParentChain.ts
Original file line number Diff line number Diff line change
@@ -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<TChain extends Chain | undefined> = Prettify<
PublicClient<Transport, TChain> & { chain: { id: ParentChainId } }
>;

export function isValidParentChainId(
parentChainId: number | undefined,
): parentChainId is ParentChainId {
Expand All @@ -32,15 +27,3 @@ export function validateParentChain<TChain extends Chain | undefined>(

return chainId;
}

export function validateParentChainPublicClient<TChain extends Chain | undefined>(
publicClient: PublicClient<Transport, TChain>,
): ParentChainPublicClient<TChain> {
const chainId = publicClient.chain?.id;

if (!isValidParentChainId(chainId)) {
throw new Error(`Parent chain not supported: ${chainId}`);
}

return publicClient as ParentChainPublicClient<TChain>;
}

0 comments on commit 7ea184d

Please sign in to comment.