Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add safety check in createTokenBridge #186

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/createTokenBridge.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,50 @@ describe('createTokenBridge', () => {
checkTokenBridgeContracts(tokenBridgeContracts);
checkWethGateways(tokenBridgeContracts, { customFeeToken: true });
});

it('should throw when createTokenBridge is called multiple times', async () => {
const testnodeInformation = getInformationFromTestnode();

const tokenBridgeCreator = await deployTokenBridgeCreator({
publicClient: nitroTestnodeL1Client,
});

const cfg = {
rollupOwner: l2RollupOwner.address,
rollupAddress: testnodeInformation.rollup,
account: l2RollupOwner,
parentChainPublicClient: nitroTestnodeL1Client,
orbitChainPublicClient: nitroTestnodeL2Client,
tokenBridgeCreatorAddressOverride: tokenBridgeCreator,
gasOverrides: {
gasLimit: {
base: 6_000_000n,
},
},
retryableGasOverrides: {
maxGasForFactory: {
base: 20_000_000n,
},
maxGasForContracts: {
base: 20_000_000n,
},
maxSubmissionCostForFactory: {
base: 4_000_000_000_000n,
},
maxSubmissionCostForContracts: {
base: 4_000_000_000_000n,
},
},
setWethGatewayGasOverrides: {
gasLimit: {
base: 100_000n,
},
},
};
const { tokenBridgeContracts } = await createTokenBridge(cfg);
await expect(createTokenBridge(cfg)).rejects.toThrow();

checkTokenBridgeContracts(tokenBridgeContracts);
checkWethGateways(tokenBridgeContracts, { customFeeToken: false });
});
});
57 changes: 57 additions & 0 deletions src/createTokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,56 @@ 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 allows us to assert that token bridge wasn't deployed.
*
* @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
*
* @throws if token bridge was already deployed, it throws an error
*/
export async function assertTokenBridgeDoesntExist<
fionnachan marked this conversation as resolved.
Show resolved Hide resolved
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) {
throw new Error('Token bridge was already deployed');
}
}
}
fionnachan marked this conversation as resolved.
Show resolved Hide resolved

export type CreateTokenBridgeParams<
TParentChain extends Chain | undefined,
Expand Down Expand Up @@ -171,6 +221,13 @@ export async function createTokenBridge<
}: CreateTokenBridgeParams<TParentChain, TOrbitChain>): Promise<
CreateTokenBridgeResults<TParentChain, TOrbitChain>
> {
await assertTokenBridgeDoesntExist({
parentChainPublicClient,
orbitChainPublicClient,
tokenBridgeCreatorAddress: tokenBridgeCreatorAddressOverride,
rollupAddress,
});

const isCustomFeeTokenBridge = isCustomFeeTokenAddress(nativeTokenAddress);
if (isCustomFeeTokenBridge) {
// set the custom fee token
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
CreateTokenBridgeParams,
CreateTokenBridgeResults,
createTokenBridge,
assertTokenBridgeDoesntExist,
} from './createTokenBridge';
import {
createTokenBridgeEnoughCustomFeeTokenAllowance,
Expand Down Expand Up @@ -210,6 +211,7 @@ export {
prepareKeyset,
utils,
//
assertTokenBridgeDoesntExist,
CreateTokenBridgeParams,
CreateTokenBridgeResults,
createTokenBridge,
Expand Down