Skip to content

Commit

Permalink
feat: Add prepareRegisterNewNetworkParams
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstph-dvx committed Oct 30, 2024
1 parent 771d78a commit 80b0c0b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/createRollupFetchTransactionHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type CreateRollupFetchTransactionHashParams<TChain extends Chain | undefi
fromBlock?: bigint;
};

const RollupInitializedEventAbi: AbiEvent = {
const RollupInitializedEventAbi = {
anonymous: false,
inputs: [
{
Expand All @@ -28,9 +28,9 @@ const RollupInitializedEventAbi: AbiEvent = {
],
name: 'RollupInitialized',
type: 'event',
};
} as const satisfies AbiEvent;

export async function createRollupFetchTransactionHash<TChain extends Chain | undefined>({
export async function getRollupInitializedEvents<TChain extends Chain | undefined>({
rollup,
publicClient,
fromBlock,
Expand All @@ -48,8 +48,17 @@ export async function createRollupFetchTransactionHash<TChain extends Chain | un
);
}

return rollupInitializedEvents;
}

export async function createRollupFetchTransactionHash<TChain extends Chain | undefined>({
rollup,
publicClient,
fromBlock,
}: CreateRollupFetchTransactionHashParams<TChain>) {
// Get the transaction hash that emitted that event
const transactionHash = rollupInitializedEvents[0].transactionHash;
const transactionHash = (await getRollupInitializedEvents({ rollup, publicClient, fromBlock }))[0]
.transactionHash;

if (!transactionHash) {
throw new Error(
Expand Down
45 changes: 45 additions & 0 deletions src/utils/registerNewNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,45 @@ import {
registerCustomArbitrumNetwork,
} from '@arbitrum/sdk';
import { testnets } from '../chains';
import { getRollupInitializedEvents } from '../createRollupFetchTransactionHash';
import { Address, Chain, PublicClient, Transport } from 'viem';
import { publicClientToProvider } from '../ethers-compat/publicClientToProvider';

const isTestnet = (parentChainId: number) => {
return testnets.some((testnet) => testnet.id === parentChainId);
};

export async function prepareRegisterNewNetworkParams<TChain extends Chain | undefined>(
parentChainPublicClient: PublicClient<Transport, TChain>,
rollupAddress: Address,
): Promise<ArbitrumNetwork> {
const rollupInitializedEvent = await getRollupInitializedEvents({
rollup: rollupAddress,
publicClient: parentChainPublicClient,
});
const { chainId } = rollupInitializedEvent[0].args;

if (!chainId) {
throw new Error("RollupInitialized event doesn't contain chainId");
}

const { parentChainId, ethBridge, confirmPeriodBlocks } =
await getArbitrumNetworkInformationFromRollup(
rollupAddress,
publicClientToProvider(parentChainPublicClient),
);

return {
name: String(`${chainId}-arbitrum-network`),
chainId: Number(chainId),
parentChainId,
confirmPeriodBlocks,
ethBridge,
isCustom: true,
isTestnet: isTestnet(parentChainId),
} satisfies ArbitrumNetwork;
}

export const registerNewNetwork = async (
parentProvider: JsonRpcProvider,
childProvider: JsonRpcProvider,
Expand All @@ -31,3 +65,14 @@ export const registerNewNetwork = async (

return registerCustomArbitrumNetwork(arbitrumNetwork);
};

export const registerNewNetworkFromParentPublicClient = async <TChain extends Chain | undefined>(
parentChainPublicClient: PublicClient<Transport, TChain>,
rollupAddress: Address,
): Promise<ArbitrumNetwork> => {
const arbitrumNetwork = await prepareRegisterNewNetworkParams(
parentChainPublicClient,
rollupAddress,
);
return registerCustomArbitrumNetwork(arbitrumNetwork);
};

0 comments on commit 80b0c0b

Please sign in to comment.