Skip to content

Commit

Permalink
Merge branch 'main' into feat-improve-contract-gas-estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
spsjvc committed Oct 2, 2024
2 parents 8ad3d9d + a645665 commit e59c1a9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 55 deletions.
57 changes: 3 additions & 54 deletions src/createTokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,58 +34,7 @@ import { isCustomFeeTokenAddress } from './utils/isCustomFeeTokenAddress';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { TransactionRequestGasOverrides } from './utils/gasOverrides';
import { getBlockExplorerUrl } from './utils/getBlockExplorerUrl';
import { tokenBridgeCreatorABI } from './contracts/TokenBridgeCreator';
import { getTokenBridgeCreatorAddress } from './utils';
import { rollupABI } from './contracts/Rollup';

/**
* If token bridge was already deployed, `createTokenBridge` will fail when waiting for retryables.
* This function returns true if token bridge was deployed previously.
*
* @param {String} assertTokenBridgeDoesntExistParams.parentChainPublicClient - The parent chain Viem Public Client
* @param {String} assertTokenBridgeDoesntExistParams.orbitChainPublicClient - The orbit chain Viem Public Client
* @param {String=} assertTokenBridgeDoesntExistParams.tokenBridgeCreatorAddress - The TokenBridgeCreator address.
* Default to getTokenBridgeCreatorAddress(parentChainPublicClient) if not provided
* @param {String} assertTokenBridgeDoesntExistParams.rollupAddress - The address of the rollup on the parent chain
*
* @returns true if token bridge was already deployed
*/
export async function isTokenBridgeDeployed<
TParentChain extends Chain | undefined,
TOrbitChain extends Chain | undefined,
>({
parentChainPublicClient,
orbitChainPublicClient,
tokenBridgeCreatorAddress,
rollupAddress,
}: {
parentChainPublicClient: PublicClient<Transport, TParentChain>;
orbitChainPublicClient: PublicClient<Transport, TOrbitChain>;
tokenBridgeCreatorAddress?: Address;
rollupAddress: Address;
}) {
const inbox = await parentChainPublicClient.readContract({
address: rollupAddress,
abi: rollupABI,
functionName: 'inbox',
});

const [router] = await parentChainPublicClient.readContract({
address: tokenBridgeCreatorAddress ?? getTokenBridgeCreatorAddress(parentChainPublicClient),
abi: tokenBridgeCreatorABI,
functionName: 'inboxToL2Deployment',
args: [inbox],
});

if (router) {
const code = await orbitChainPublicClient.getBytecode({ address: router });
if (code) {
return true;
}
}

return false;
}
import { isTokenBridgeDeployed } from './isTokenBridgeDeployed';

export type CreateTokenBridgeParams<
TParentChain extends Chain | undefined,
Expand Down Expand Up @@ -226,8 +175,8 @@ export async function createTokenBridge<
const isTokenBridgeAlreadyDeployed = await isTokenBridgeDeployed({
parentChainPublicClient,
orbitChainPublicClient,
tokenBridgeCreatorAddress: tokenBridgeCreatorAddressOverride,
rollupAddress,
rollup: rollupAddress,
tokenBridgeCreatorAddressOverride,
});

if (isTokenBridgeAlreadyDeployed) {
Expand Down
12 changes: 12 additions & 0 deletions src/createTokenBridgePrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { Prettify } from './types/utils';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { getTokenBridgeCreatorAddress } from './utils/getTokenBridgeCreatorAddress';
import { isTokenBridgeDeployed } from './isTokenBridgeDeployed';

export type TransactionRequestRetryableGasOverrides = {
maxSubmissionCostForFactory?: GasOverrideOptions;
Expand Down Expand Up @@ -50,6 +51,17 @@ export async function createTokenBridgePrepareTransactionRequest<
}: CreateTokenBridgePrepareTransactionRequestParams<TParentChain, TOrbitChain>) {
const chainId = validateParentChain(parentChainPublicClient);

const isTokenBridgeAlreadyDeployed = await isTokenBridgeDeployed({
parentChainPublicClient,
orbitChainPublicClient,
rollup: params.rollup,
tokenBridgeCreatorAddressOverride,
});

if (isTokenBridgeAlreadyDeployed) {
throw new Error(`Token bridge contracts for Rollup ${params.rollup} are already deployed`);
}

const tokenBridgeCreatorAddress =
tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(parentChainPublicClient);

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ import {
CreateTokenBridgeParams,
CreateTokenBridgeResults,
createTokenBridge,
isTokenBridgeDeployed,
} from './createTokenBridge';
import { isTokenBridgeDeployed } from './isTokenBridgeDeployed';
import {
createTokenBridgeEnoughCustomFeeTokenAllowance,
CreateTokenBridgeEnoughCustomFeeTokenAllowanceParams,
Expand Down
54 changes: 54 additions & 0 deletions src/isTokenBridgeDeployed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Address, Chain, PublicClient, Transport } from 'viem';

import { tokenBridgeCreatorABI } from './contracts/TokenBridgeCreator';
import { getTokenBridgeCreatorAddress } from './utils';
import { rollupABI } from './contracts/Rollup';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';

/**
* Checks whether the token bridge contracts were deployed for a given Rollup.
*
* @param {String} params.parentChainPublicClient - The parent chain viem PublicClient.
* @param {String} params.orbitChainPublicClient - The orbit chain viem PublicClient.
* @param {String} params.rollup - The address of the Rollup on the parent chain.
* @param {String} params.tokenBridgeCreatorAddress - Specifies a custom address for the TokenBridgeCreator. By default, the address will be automatically detected based on the provided chain.
*
* @returns true if token bridge was already deployed
*/
export async function isTokenBridgeDeployed<
TParentChain extends Chain | undefined,
TOrbitChain extends Chain | undefined,
>({
parentChainPublicClient,
orbitChainPublicClient,
rollup,
tokenBridgeCreatorAddressOverride,
}: WithTokenBridgeCreatorAddressOverride<{
parentChainPublicClient: PublicClient<Transport, TParentChain>;
orbitChainPublicClient: PublicClient<Transport, TOrbitChain>;
rollup: Address;
}>): Promise<boolean> {
const inbox = await parentChainPublicClient.readContract({
address: rollup,
abi: rollupABI,
functionName: 'inbox',
});

const [router] = await parentChainPublicClient.readContract({
address:
tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(parentChainPublicClient),
abi: tokenBridgeCreatorABI,
functionName: 'inboxToL2Deployment',
args: [inbox],
});

if (router) {
const code = await orbitChainPublicClient.getBytecode({ address: router });

if (code) {
return true;
}
}

return false;
}

0 comments on commit e59c1a9

Please sign in to comment.