From 3ab67404843c0c9fd9ec6fa410b25fbabbe30ee7 Mon Sep 17 00:00:00 2001 From: spsjvc Date: Wed, 9 Oct 2024 18:52:33 +0200 Subject: [PATCH] move stuff --- src/chains.ts | 19 ++++++++++++++++++- ...PrepareDeploymentParamsConfig.unit.test.ts | 10 ++++++++-- ...llupPrepareTransactionRequest.unit.test.ts | 2 +- src/customChains.ts | 15 --------------- src/customChains.unit.test.ts | 2 +- src/index.ts | 2 +- src/types/ParentChain.ts | 12 ++++++------ src/utils/getParentChainFromId.ts | 5 ++--- .../getRollupCreatorAddress.unit.test.ts | 2 +- .../getTokenBridgeCreatorAddress.unit.test.ts | 2 +- 10 files changed, 39 insertions(+), 32 deletions(-) delete mode 100644 src/customChains.ts diff --git a/src/chains.ts b/src/chains.ts index b2525fe4..26584764 100644 --- a/src/chains.ts +++ b/src/chains.ts @@ -1,4 +1,4 @@ -import { defineChain } from 'viem'; +import { defineChain, Chain, ChainContract } from 'viem'; import { mainnet, arbitrum as arbitrumOne, @@ -59,6 +59,23 @@ const nitroTestnodeL3 = defineChain({ testnet: true, }); +const customParentChains: Record = {}; + +export function getCustomParentChains(): Chain[] { + return Object.values(customParentChains); +} + +export type CustomParentChain = Chain & { + contracts: { + rollupCreator: ChainContract; + tokenBridgeCreator: ChainContract; + }; +}; + +export function registerCustomParentChain(chain: CustomParentChain) { + customParentChains[chain.id] = chain; +} + export const chains = [ // mainnet L1 mainnet, diff --git a/src/createRollupPrepareDeploymentParamsConfig.unit.test.ts b/src/createRollupPrepareDeploymentParamsConfig.unit.test.ts index 3652b84d..4b9d925a 100644 --- a/src/createRollupPrepareDeploymentParamsConfig.unit.test.ts +++ b/src/createRollupPrepareDeploymentParamsConfig.unit.test.ts @@ -1,10 +1,16 @@ import { it, expect } from 'vitest'; import { Address, createPublicClient, http } from 'viem'; -import { arbitrumOne, arbitrumSepolia, base, baseSepolia } from './chains'; +import { + arbitrumOne, + arbitrumSepolia, + base, + baseSepolia, + CustomParentChain, + registerCustomParentChain, +} from './chains'; import { prepareChainConfig } from './prepareChainConfig'; import { createRollupPrepareDeploymentParamsConfig } from './createRollupPrepareDeploymentParamsConfig'; -import { CustomParentChain, registerCustomParentChain } from './customChains'; import { createCustomChain } from './customChainsTestHelpers'; const chainId = 69_420n; diff --git a/src/createRollupPrepareTransactionRequest.unit.test.ts b/src/createRollupPrepareTransactionRequest.unit.test.ts index 00fdeeff..5915fadd 100644 --- a/src/createRollupPrepareTransactionRequest.unit.test.ts +++ b/src/createRollupPrepareTransactionRequest.unit.test.ts @@ -10,7 +10,7 @@ import { createRollupPrepareTransactionRequest } from './createRollupPrepareTran import { rollupCreatorAddress } from './contracts/RollupCreator'; import { getNitroTestnodePrivateKeyAccounts } from './testHelpers'; -import { CustomParentChain, registerCustomParentChain } from './customChains'; +import { CustomParentChain, registerCustomParentChain } from './chains'; import { createCustomChain } from './customChainsTestHelpers'; import { getConsensusReleaseByVersion } from './wasmModuleRoot'; diff --git a/src/customChains.ts b/src/customChains.ts deleted file mode 100644 index 2969de21..00000000 --- a/src/customChains.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Chain, ChainContract, Client } from 'viem'; - -export type CustomParentChain = Chain & { - contracts: { - rollupCreator: ChainContract; - tokenBridgeCreator: ChainContract; - }; -}; - -export const customChains: Chain[] = []; - -export function registerCustomParentChain(chain: CustomParentChain) { - // todo: don't duplicate - customChains.push(chain); -} diff --git a/src/customChains.unit.test.ts b/src/customChains.unit.test.ts index 520c1764..ab034697 100644 --- a/src/customChains.unit.test.ts +++ b/src/customChains.unit.test.ts @@ -1,7 +1,7 @@ import { describe, it, expect } from 'vitest'; import { validateParentChain } from './types/ParentChain'; -import { CustomParentChain, registerCustomParentChain } from './customChains'; +import { CustomParentChain, registerCustomParentChain } from './chains'; import { createCustomChain } from './customChainsTestHelpers'; describe(`validateParentChain`, () => { diff --git a/src/index.ts b/src/index.ts index b48a154a..8975f0f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -144,7 +144,7 @@ import { getConsensusReleaseByWasmModuleRoot, GetConsensusReleaseByWasmModuleRoot, } from './wasmModuleRoot'; -import { CustomParentChain, registerCustomParentChain } from './customChains'; +import { CustomParentChain, registerCustomParentChain } from './chains'; export * from './actions'; export { diff --git a/src/types/ParentChain.ts b/src/types/ParentChain.ts index 2fda9882..bc50a1c8 100644 --- a/src/types/ParentChain.ts +++ b/src/types/ParentChain.ts @@ -1,18 +1,18 @@ import { Client, Transport, Chain } from 'viem'; -import { chains, nitroTestnodeL3 } from '../chains'; -import { customChains } from '../customChains'; +import { chains, getCustomParentChains, nitroTestnodeL3 } from '../chains'; // 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']; -function isCustom(chainId: number) { - return customChains.map((chain) => chain.id).includes(chainId); +function isCustomParentChain(chainId: number): boolean { + const ids = getCustomParentChains().map((chain) => chain.id); + return ids.includes(chainId); } function isValidParentChainId(parentChainId: number | undefined): parentChainId is number { - const ids = [...chains, ...customChains] + const ids = [...chains, ...getCustomParentChains()] // exclude nitro-testnode L3 from the list of parent chains .filter((chain) => chain.id !== nitroTestnodeL3.id) .map((chain) => chain.id) as Number[]; @@ -28,7 +28,7 @@ export function validateParentChain( throw new Error(`Parent chain not supported: ${chainId}`); } - if (isCustom(chainId)) { + if (isCustomParentChain(chainId)) { return { chainId, isCustom: true }; } diff --git a/src/utils/getParentChainFromId.ts b/src/utils/getParentChainFromId.ts index ebcd9602..f575f65f 100644 --- a/src/utils/getParentChainFromId.ts +++ b/src/utils/getParentChainFromId.ts @@ -1,14 +1,13 @@ import { Chain, extractChain } from 'viem'; import { validateParentChain } from '../types/ParentChain'; -import { chains } from '../chains'; -import { customChains } from '../customChains'; +import { chains, getCustomParentChains } from '../chains'; export function getParentChainFromId(chainId: number): Chain { const { chainId: parentChainId } = validateParentChain(chainId); return extractChain({ - chains: [...chains, ...customChains], + chains: [...chains, ...getCustomParentChains()], id: parentChainId, }); } diff --git a/src/utils/getRollupCreatorAddress.unit.test.ts b/src/utils/getRollupCreatorAddress.unit.test.ts index 998bd18b..dcfe1607 100644 --- a/src/utils/getRollupCreatorAddress.unit.test.ts +++ b/src/utils/getRollupCreatorAddress.unit.test.ts @@ -3,7 +3,7 @@ import { createPublicClient, http } from 'viem'; import { sepolia } from 'viem/chains'; import { getRollupCreatorAddress } from './getRollupCreatorAddress'; -import { CustomParentChain, registerCustomParentChain } from '../customChains'; +import { CustomParentChain, registerCustomParentChain } from '../chains'; import { createCustomChain } from '../customChainsTestHelpers'; it(`successfully returns address for Sepolia`, () => { diff --git a/src/utils/getTokenBridgeCreatorAddress.unit.test.ts b/src/utils/getTokenBridgeCreatorAddress.unit.test.ts index d67bbe12..fbe0ad73 100644 --- a/src/utils/getTokenBridgeCreatorAddress.unit.test.ts +++ b/src/utils/getTokenBridgeCreatorAddress.unit.test.ts @@ -3,7 +3,7 @@ import { createPublicClient, http } from 'viem'; import { sepolia } from 'viem/chains'; import { getTokenBridgeCreatorAddress } from './getTokenBridgeCreatorAddress'; -import { CustomParentChain, registerCustomParentChain } from '../customChains'; +import { CustomParentChain, registerCustomParentChain } from '../chains'; import { createCustomChain } from '../customChainsTestHelpers'; it(`successfully returns address for Sepolia`, () => {