From 2ff8f2e66fc89dedf63a7dfdb6ea457c1887fc78 Mon Sep 17 00:00:00 2001 From: Doug <4741454+douglance@users.noreply.github.com> Date: Wed, 27 Dec 2023 15:37:21 -0500 Subject: [PATCH] renames and moves getValidChainId --- src/createRollup.ts | 9 ++------- src/createRollupEnoughCustomFeeTokenAllowance.ts | 4 +--- ...epareCustomFeeTokenApprovalTransactionRequest.ts | 7 +++---- src/createRollupPrepareTransactionRequest.ts | 6 +++--- src/setValidKeyset.ts | 8 ++------ src/setValidKeysetPrepareTransactionRequest.ts | 8 ++------ src/types/ParentChain.ts | 9 +++++++++ src/utils/getters.ts | 13 +++---------- 8 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/createRollup.ts b/src/createRollup.ts index 33731443..5a8c1c78 100644 --- a/src/createRollup.ts +++ b/src/createRollup.ts @@ -1,7 +1,7 @@ import { PublicClient, WalletClient, GetFunctionArgs } from 'viem'; import { rollupCreator } from './contracts'; -import { validParentChainId } from './types/ParentChain'; +import { validateParentChainId } from './types/ParentChain'; import { defaults } from './createRollupDefaults'; import { createRollupGetCallValue } from './createRollupGetCallValue'; import { createRollupGetMaxDataSize } from './createRollupGetMaxDataSize'; @@ -12,7 +12,6 @@ import { import { isCustomFeeTokenAddress } from './utils/isCustomFeeTokenAddress'; import { ChainConfig } from './types/ChainConfig'; import { isAnyTrustChainConfig } from './utils/isAnyTrustChainConfig'; -import { getValidChainId } from './utils/getters'; export type CreateRollupFunctionInputs = GetFunctionArgs< typeof rollupCreator.abi, @@ -33,13 +32,9 @@ export async function createRollup({ publicClient: PublicClient; walletClient: WalletClient; }): Promise { - const chainId = getValidChainId(publicClient); + const chainId = validateParentChainId(publicClient); const account = walletClient.account?.address; - if (!validParentChainId(chainId)) { - throw new Error('chainId is undefined'); - } - if (typeof account === 'undefined') { throw new Error('account is undefined'); } diff --git a/src/createRollupEnoughCustomFeeTokenAllowance.ts b/src/createRollupEnoughCustomFeeTokenAllowance.ts index 7ff05eb7..a121b162 100644 --- a/src/createRollupEnoughCustomFeeTokenAllowance.ts +++ b/src/createRollupEnoughCustomFeeTokenAllowance.ts @@ -1,10 +1,8 @@ import { Address, PublicClient } from 'viem'; -import { rollupCreator } from './contracts'; -import { validParentChainId } from './types/ParentChain'; import { fetchAllowance } from './utils/erc20'; import { deterministicFactoriesDeploymentEstimatedFees } from './constants'; -import { getRollupCreatorAddress, getValidChainId } from './utils/getters'; +import { getRollupCreatorAddress } from './utils/getters'; export type CreateRollupEnoughCustomFeeTokenAllowanceParams = { nativeToken: Address; diff --git a/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts b/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts index 0093f96a..d5ac73a4 100644 --- a/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts +++ b/src/createRollupPrepareCustomFeeTokenApprovalTransactionRequest.ts @@ -1,9 +1,8 @@ import { Address, PublicClient, maxInt256 } from 'viem'; import { approvePrepareTransactionRequest } from './utils/erc20'; -import { validParentChainId } from './types/ParentChain'; -import { rollupCreator } from './contracts'; -import { getRollupCreatorAddress, getValidChainId } from './utils/getters'; +import { validateParentChainId } from './types/ParentChain'; +import { getRollupCreatorAddress } from './utils/getters'; export type CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams = { amount?: bigint; @@ -18,7 +17,7 @@ export async function createRollupPrepareCustomFeeTokenApprovalTransactionReques account, publicClient, }: CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams) { - const chainId = getValidChainId(publicClient); + const chainId = validateParentChainId(publicClient); const request = await approvePrepareTransactionRequest({ address: nativeToken, owner: account, diff --git a/src/createRollupPrepareTransactionRequest.ts b/src/createRollupPrepareTransactionRequest.ts index d6ab160a..2b7d506d 100644 --- a/src/createRollupPrepareTransactionRequest.ts +++ b/src/createRollupPrepareTransactionRequest.ts @@ -5,11 +5,11 @@ import { defaults } from './createRollupDefaults'; import { createRollupGetCallValue } from './createRollupGetCallValue'; import { createRollupGetMaxDataSize } from './createRollupGetMaxDataSize'; import { rollupCreator } from './contracts'; -import { validParentChainId } from './types/ParentChain'; +import { validateParentChainId } from './types/ParentChain'; import { isCustomFeeTokenAddress } from './utils/isCustomFeeTokenAddress'; import { ChainConfig } from './types/ChainConfig'; import { isAnyTrustChainConfig } from './utils/isAnyTrustChainConfig'; -import { getRollupCreatorAddress, getValidChainId } from './utils/getters'; +import { getRollupCreatorAddress } from './utils/getters'; function createRollupEncodeFunctionData(args: CreateRollupFunctionInputs) { return encodeFunctionData({ @@ -28,7 +28,7 @@ export async function createRollupPrepareTransactionRequest({ account: Address; publicClient: PublicClient; }) { - const chainId = getValidChainId(publicClient); + const chainId = validateParentChainId(publicClient); const chainConfig: ChainConfig = JSON.parse(params.config.chainConfig); diff --git a/src/setValidKeyset.ts b/src/setValidKeyset.ts index 1cf6d55b..2affa148 100644 --- a/src/setValidKeyset.ts +++ b/src/setValidKeyset.ts @@ -1,7 +1,7 @@ import { PublicClient, WalletClient } from 'viem'; import { upgradeExecutor } from './contracts'; -import { validParentChainId } from './types/ParentChain'; +import { validateParentChainId } from './types/ParentChain'; import { CoreContracts } from './types/CoreContracts'; import { setValidKeysetEncodeFunctionData } from './setValidKeysetEncodeFunctionData'; @@ -18,13 +18,9 @@ export async function setValidKeyset({ publicClient, walletClient, }: SetValidKeysetParams) { - const chainId = publicClient.chain?.id; + validateParentChainId(publicClient); const account = walletClient.account?.address; - if (!validParentChainId(chainId)) { - throw new Error('chainId is undefined'); - } - if (typeof account === 'undefined') { throw new Error('account is undefined'); } diff --git a/src/setValidKeysetPrepareTransactionRequest.ts b/src/setValidKeysetPrepareTransactionRequest.ts index 1ed2fe2a..3e9c2ccc 100644 --- a/src/setValidKeysetPrepareTransactionRequest.ts +++ b/src/setValidKeysetPrepareTransactionRequest.ts @@ -1,6 +1,6 @@ import { Address } from 'viem'; -import { validParentChainId } from './types/ParentChain'; +import { validateParentChainId } from './types/ParentChain'; import { SetValidKeysetParams } from './setValidKeyset'; import { setValidKeysetEncodeFunctionData } from './setValidKeysetEncodeFunctionData'; import { upgradeExecutorEncodeFunctionData } from './upgradeExecutor'; @@ -18,11 +18,7 @@ export async function setValidKeysetPrepareTransactionRequest({ account, publicClient, }: SetValidKeysetPrepareTransactionRequestParams) { - const chainId = publicClient.chain?.id; - - if (!validParentChainId(chainId)) { - throw new Error('chainId is undefined'); - } + const chainId = validateParentChainId(publicClient); const request = await publicClient.prepareTransactionRequest({ chain: publicClient.chain, diff --git a/src/types/ParentChain.ts b/src/types/ParentChain.ts index 7baef329..a32c4a3c 100644 --- a/src/types/ParentChain.ts +++ b/src/types/ParentChain.ts @@ -1,3 +1,4 @@ +import { PublicClient } from 'viem'; import { chains } from '../chains'; export type ParentChain = (typeof chains)[number]; @@ -9,3 +10,11 @@ export function validParentChainId( const ids = chains.map((chain) => chain.id) as Number[]; return ids.includes(Number(parentChainId)); } + +export const validateParentChainId = (client: PublicClient): ParentChainId => { + const chainId = client.chain?.id; + if (!validParentChainId(chainId)) { + throw new Error('chainId is undefined'); + } + return chainId; +}; diff --git a/src/utils/getters.ts b/src/utils/getters.ts index c12e2a9d..baa95315 100644 --- a/src/utils/getters.ts +++ b/src/utils/getters.ts @@ -1,17 +1,10 @@ import { PublicClient } from 'viem'; import { rollupCreator } from '../contracts'; -import { validParentChainId } from '../types/ParentChain'; - -export const getValidChainId = (client: PublicClient) => { - const chainId = client.chain?.id; - if (!validParentChainId(chainId)) { - throw new Error('chainId is undefined'); - } - return chainId; -}; +import { validateParentChainId } from '../types/ParentChain'; export const getRollupCreatorAddress = (client: PublicClient) => { - const chainId = getValidChainId(client); + const chainId = validateParentChainId(client); + if (!rollupCreator.address[chainId]) { throw new Error(`Invalid chainId or chainId not supported: ${chainId}`); }