Skip to content

Commit

Permalink
renames and moves getValidChainId
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance committed Dec 27, 2023
1 parent 333655d commit 2ff8f2e
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 39 deletions.
9 changes: 2 additions & 7 deletions src/createRollup.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand All @@ -33,13 +32,9 @@ export async function createRollup({
publicClient: PublicClient;
walletClient: WalletClient;
}): Promise<CreateRollupTransactionReceipt> {
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');
}
Expand Down
4 changes: 1 addition & 3 deletions src/createRollupEnoughCustomFeeTokenAllowance.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/createRollupPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);

Expand Down
8 changes: 2 additions & 6 deletions src/setValidKeyset.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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');
}
Expand Down
8 changes: 2 additions & 6 deletions src/setValidKeysetPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/types/ParentChain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PublicClient } from 'viem';
import { chains } from '../chains';

export type ParentChain = (typeof chains)[number];
Expand All @@ -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;
};
13 changes: 3 additions & 10 deletions src/utils/getters.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
Expand Down

0 comments on commit 2ff8f2e

Please sign in to comment.