Skip to content

Commit

Permalink
move stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
spsjvc committed Oct 9, 2024
1 parent f403266 commit 3ab6740
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 32 deletions.
19 changes: 18 additions & 1 deletion src/chains.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineChain } from 'viem';
import { defineChain, Chain, ChainContract } from 'viem';
import {
mainnet,
arbitrum as arbitrumOne,
Expand Down Expand Up @@ -59,6 +59,23 @@ const nitroTestnodeL3 = defineChain({
testnet: true,
});

const customParentChains: Record<number, Chain> = {};

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,
Expand Down
10 changes: 8 additions & 2 deletions src/createRollupPrepareDeploymentParamsConfig.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/createRollupPrepareTransactionRequest.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
15 changes: 0 additions & 15 deletions src/customChains.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/customChains.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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`, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ import {
getConsensusReleaseByWasmModuleRoot,
GetConsensusReleaseByWasmModuleRoot,
} from './wasmModuleRoot';
import { CustomParentChain, registerCustomParentChain } from './customChains';
import { CustomParentChain, registerCustomParentChain } from './chains';
export * from './actions';

export {
Expand Down
12 changes: 6 additions & 6 deletions src/types/ParentChain.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand All @@ -28,7 +28,7 @@ export function validateParentChain<TChain extends Chain | undefined>(
throw new Error(`Parent chain not supported: ${chainId}`);
}

if (isCustom(chainId)) {
if (isCustomParentChain(chainId)) {
return { chainId, isCustom: true };
}

Expand Down
5 changes: 2 additions & 3 deletions src/utils/getParentChainFromId.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}
2 changes: 1 addition & 1 deletion src/utils/getRollupCreatorAddress.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getTokenBridgeCreatorAddress.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`, () => {
Expand Down

0 comments on commit 3ab6740

Please sign in to comment.