Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: return object from parent chain validator #207

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/actions/buildInvalidateKeysetHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function buildInvalidateKeysetHash<TChain extends Chain | undefined
params,
}: BuildInvalidateKeysetHashParameters,
): Promise<BuildInvalidateKeysetHashReturnType> {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/buildSetIsBatchPoster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function buildSetIsBatchPoster<TChain extends Chain | undefined>(
params,
}: BuildSetIsBatchPosterParameters & { params: { enable: boolean } },
): Promise<BuildSetIsBatchPosterReturnType> {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/buildSetMaxTimeVariation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function buildSetMaxTimeVariation<TChain extends Chain | undefined>
params,
}: BuildSetMaxTimeVariationParameters,
): Promise<BuildSetMaxTimeVariationReturnType> {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand Down
2 changes: 1 addition & 1 deletion src/actions/buildSetValidKeyset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function buildSetValidKeyset<TChain extends Chain | undefined>(
params,
}: BuildSetValidKeysetParameters,
): Promise<BuildSetValidKeysetReturnType> {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

const request = await client.prepareTransactionRequest({
chain: client.chain,
Expand Down
2 changes: 1 addition & 1 deletion src/createRollupFetchTransactionHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function createRollupFetchTransactionHash<TChain extends Chain | un
rollup,
publicClient,
}: CreateRollupFetchTransactionHashParams<TChain>) {
const chainId = validateParentChain(publicClient);
const { chainId } = validateParentChain(publicClient);

const fromBlock =
chainId in earliestRollupCreatorDeploymentBlockNumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function createRollupPrepareCustomFeeTokenApprovalTransactionReques
publicClient,
rollupCreatorAddressOverride,
}: CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams<TChain>) {
const chainId = validateParentChain(publicClient);
const { chainId } = validateParentChain(publicClient);

const fees = await createRollupGetRetryablesFeesWithDefaults(publicClient, {
account,
Expand Down
2 changes: 1 addition & 1 deletion src/createRollupPrepareDeploymentParamsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function createRollupPrepareDeploymentParamsConfig<TChain extends Chain |
client: Client<Transport, TChain>,
{ chainConfig, ...params }: CreateRollupPrepareDeploymentParamsConfigParams,
): CreateRollupPrepareDeploymentParamsConfigResult {
const parentChainId = validateParentChain(client);
const { chainId: parentChainId } = validateParentChain(client);

const defaultsBasedOnParentChain = {
confirmPeriodBlocks: getDefaultConfirmPeriodBlocks(parentChainId),
Expand Down
2 changes: 1 addition & 1 deletion src/createRollupPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function createRollupPrepareTransactionRequest<TChain extends Chain
gasOverrides,
rollupCreatorAddressOverride,
}: CreateRollupPrepareTransactionRequestParams<TChain>) {
const chainId = validateParentChain(publicClient);
const { chainId } = validateParentChain(publicClient);

if (params.batchPosters.length === 0 || params.batchPosters.includes(zeroAddress)) {
throw new Error(`"params.batchPosters" can't be empty or contain the zero address.`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function createTokenBridgePrepareCustomFeeTokenApprovalTransactionR
publicClient,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgePrepareCustomFeeTokenApprovalTransactionRequestParams<TChain>) {
const chainId = validateParentChain(publicClient);
const { chainId } = validateParentChain(publicClient);

const request = await approvePrepareTransactionRequest({
address: nativeToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function createTokenBridgePrepareSetWethGatewayTransactionRequest<
retryableGasOverrides,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgePrepareRegisterWethGatewayTransactionRequestParams<TParentChain, TOrbitChain>) {
const chainId = validateParentChain(parentChainPublicClient);
const { chainId } = validateParentChain(parentChainPublicClient);

// Ensure that networks are registered
await registerNewNetwork(
Expand Down
2 changes: 1 addition & 1 deletion src/createTokenBridgePrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function createTokenBridgePrepareTransactionRequest<
retryableGasOverrides,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgePrepareTransactionRequestParams<TParentChain, TOrbitChain>) {
const chainId = validateParentChain(parentChainPublicClient);
const { chainId } = validateParentChain(parentChainPublicClient);

const isTokenBridgeAlreadyDeployed = await isTokenBridgeDeployed({
parentChainPublicClient,
Expand Down
2 changes: 1 addition & 1 deletion src/getDefaultConfirmPeriodBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { base, baseSepolia } from './chains';
export function getDefaultConfirmPeriodBlocks<TChain extends Chain | undefined>(
parentChainIdOrClient: ParentChainId | Client<Transport, TChain>,
): bigint {
const parentChainId = validateParentChain(parentChainIdOrClient);
const { chainId: parentChainId } = validateParentChain(parentChainIdOrClient);

const isMainnet = parentChainIsMainnet(parentChainId);
const confirmPeriodBlocks = isMainnet ? 45_818n : 150n;
Expand Down
2 changes: 1 addition & 1 deletion src/getDefaultSequencerInboxMaxTimeVariation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type SequencerInboxMaxTimeVariation = {
export function getDefaultSequencerInboxMaxTimeVariation<TChain extends Chain | undefined>(
parentChainIdOrClient: ParentChainId | Client<Transport, TChain>,
): SequencerInboxMaxTimeVariation {
const parentChainId = validateParentChain(parentChainIdOrClient);
const { chainId: parentChainId } = validateParentChain(parentChainIdOrClient);

const delaySeconds = 60 * 60 * 24 * 4; // 4 days;
const futureSeconds = 60 * 60; // 1 hour;
Expand Down
4 changes: 3 additions & 1 deletion src/prepareNodeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ export function prepareNodeConfig({
throw new Error(`"parentChainBeaconRpcUrl" is required for L2 Orbit chains.`);
}

const { chainId: parentChainIdValidated } = validateParentChain(parentChainId);

const config: NodeConfig = {
'chain': {
'info-json': stringifyInfoJson([
{
'chain-id': chainConfig.chainId,
'parent-chain-id': parentChainId,
'parent-chain-is-arbitrum': parentChainIsArbitrum(validateParentChain(parentChainId)),
'parent-chain-is-arbitrum': parentChainIsArbitrum(parentChainIdValidated),
'chain-name': chainName,
'chain-config': chainConfig,
'rollup': {
Expand Down
2 changes: 1 addition & 1 deletion src/rollupAdminLogicPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function rollupAdminLogicPrepareTransactionRequest<
client: PublicClient<TTransport, TChain>,
params: RollupAdminLogicPrepareTransactionRequestParameters<TFunctionName>,
) {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

// params is extending RollupAdminLogicPrepareFunctionDataParameters, it's safe to cast
const { to, data, value } = rollupAdminLogicPrepareFunctionData({
Expand Down
2 changes: 1 addition & 1 deletion src/sequencerInboxPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function sequencerInboxPrepareTransactionRequest<
client: PublicClient<TTransport, TChain>,
params: SequencerInboxPrepareTransactionRequestParameters<TFunctionName>,
) {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

// params is extending SequencerInboxPrepareFunctionDataParameters, it's safe to cast
const { to, data, value } = sequencerInboxPrepareFunctionData({
Expand Down
2 changes: 1 addition & 1 deletion src/setValidKeysetPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function setValidKeysetPrepareTransactionRequest<TChain extends Cha
account,
publicClient,
}: SetValidKeysetPrepareTransactionRequestParams<TChain>) {
const chainId = validateParentChain(publicClient);
const { chainId } = validateParentChain(publicClient);

// @ts-ignore (todo: fix viem type issue)
const request = await publicClient.prepareTransactionRequest({
Expand Down
4 changes: 2 additions & 2 deletions src/types/ParentChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ function isValidParentChainId(parentChainId: number | undefined): parentChainId

export function validateParentChain<TChain extends Chain | undefined>(
chainIdOrClient: number | Client<Transport, TChain>,
): ParentChainId {
): { chainId: ParentChainId } {
const chainId = typeof chainIdOrClient === 'number' ? chainIdOrClient : chainIdOrClient.chain?.id;

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

return chainId;
return { chainId };
}
2 changes: 1 addition & 1 deletion src/utils/getParentChainFromId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { chains } from '../chains';
import { ParentChain, validateParentChain } from '../types/ParentChain';

export function getParentChainFromId(chainId: number): ParentChain {
const parentChainId = validateParentChain(chainId);
const { chainId: parentChainId } = validateParentChain(chainId);

return extractChain({
chains,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getRollupCreatorAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { validateParentChain } from '../types/ParentChain';
export function getRollupCreatorAddress<TChain extends Chain | undefined>(
client: Client<Transport, TChain>,
) {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

if (!rollupCreatorAddress[chainId]) {
throw new Error(`Parent chain not supported: ${chainId}`);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getTokenBridgeCreatorAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { validateParentChain } from '../types/ParentChain';
export function getTokenBridgeCreatorAddress<TChain extends Chain | undefined>(
client: Client<Transport, TChain>,
) {
const chainId = validateParentChain(client);
const { chainId } = validateParentChain(client);

if (!tokenBridgeCreatorAddress[chainId]) {
throw new Error(`Parent chain not supported: ${chainId}`);
Expand Down