Skip to content

Commit

Permalink
refactor: clean up validation and add factory getters (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: spsjvc <[email protected]>
  • Loading branch information
douglance and spsjvc authored Feb 29, 2024
1 parent 2f5a7cb commit 5a341c1
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 94 deletions.
11 changes: 2 additions & 9 deletions src/createRollupEnoughCustomFeeTokenAllowance.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Address, PublicClient } from 'viem';

import { rollupCreator } from './contracts';
import { validParentChainId } from './types/ParentChain';
import { fetchAllowance } from './utils/erc20';
import { getRollupCreatorAddress } from './utils/getters';
import { createRollupDefaultRetryablesFees } from './constants';

import { Prettify } from './types/utils';
Expand All @@ -22,16 +21,10 @@ export async function createRollupEnoughCustomFeeTokenAllowance({
publicClient,
rollupCreatorAddressOverride,
}: CreateRollupEnoughCustomFeeTokenAllowanceParams) {
const chainId = publicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}

const allowance = await fetchAllowance({
address: nativeToken,
owner: account,
spender: rollupCreatorAddressOverride ?? rollupCreator.address[chainId],
spender: rollupCreatorAddressOverride ?? getRollupCreatorAddress(publicClient),
publicClient,
});

Expand Down
8 changes: 1 addition & 7 deletions src/createRollupFetchCoreContracts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, PublicClient } from 'viem';
import { validParentChainId } from './types/ParentChain';

import { CoreContracts } from './types/CoreContracts';
import { createRollupFetchTransactionHash } from './createRollupFetchTransactionHash';
import { createRollupPrepareTransactionReceipt } from './createRollupPrepareTransactionReceipt';
Expand All @@ -13,12 +13,6 @@ export async function createRollupFetchCoreContracts({
rollup,
publicClient,
}: CreateRollupFetchCoreContractsParams): Promise<CoreContracts> {
const chainId = publicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}

// getting core contract addresses
const transactionHash = await createRollupFetchTransactionHash({
rollup,
Expand Down
11 changes: 7 additions & 4 deletions src/createRollupFetchTransactionHash.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, PublicClient } from 'viem';
import { AbiEvent } from 'abitype';

import { ParentChainId } from './types/ParentChain';
import { validateParentChain } from './types/ParentChain';
import {
mainnet,
arbitrumOne,
Expand Down Expand Up @@ -56,19 +56,21 @@ export async function createRollupFetchTransactionHash({
rollup,
publicClient,
}: CreateRollupFetchTransactionHashParams) {
// Find the RollupInitialized event from that Rollup contract
const chainId = await publicClient.getChainId();
const chainId = validateParentChain(publicClient);

const fromBlock =
chainId in Object.keys(earliestRollupCreatorDeploymentBlockNumber)
? earliestRollupCreatorDeploymentBlockNumber[chainId as ParentChainId]
? earliestRollupCreatorDeploymentBlockNumber[chainId]
: 'earliest';

// Find the RollupInitialized event from that Rollup contract
const rollupInitializedEvents = await publicClient.getLogs({
address: rollup,
event: RollupInitializedEventAbi,
fromBlock,
toBlock: 'latest',
});

if (rollupInitializedEvents.length !== 1) {
throw new Error(
`Expected to find 1 RollupInitialized event for rollup address ${rollup} but found ${rollupInitializedEvents.length}`,
Expand All @@ -77,6 +79,7 @@ export async function createRollupFetchTransactionHash({

// Get the transaction hash that emitted that event
const transactionHash = rollupInitializedEvents[0].transactionHash;

if (!transactionHash) {
throw new Error(
`No transactionHash found in RollupInitialized event for rollup address ${rollup}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Address, PublicClient } from 'viem';

import { approvePrepareTransactionRequest } from './utils/erc20';
import { validParentChainId } from './types/ParentChain';
import { rollupCreator } from './contracts';
import { validateParentChain } from './types/ParentChain';
import { getRollupCreatorAddress } from './utils/getters';
import { createRollupDefaultRetryablesFees } from './constants';

import { Prettify } from './types/utils';
Expand All @@ -24,16 +24,12 @@ export async function createRollupPrepareCustomFeeTokenApprovalTransactionReques
publicClient,
rollupCreatorAddressOverride,
}: CreateRollupPrepareCustomFeeTokenApprovalTransactionRequestParams) {
const chainId = publicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}
const chainId = validateParentChain(publicClient);

const request = await approvePrepareTransactionRequest({
address: nativeToken,
owner: account,
spender: rollupCreatorAddressOverride ?? rollupCreator.address[chainId],
spender: rollupCreatorAddressOverride ?? getRollupCreatorAddress(publicClient),
amount,
publicClient,
});
Expand Down
11 changes: 4 additions & 7 deletions src/createRollupPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { defaults } from './createRollupDefaults';
import { createRollupGetCallValue } from './createRollupGetCallValue';
import { createRollupGetMaxDataSize } from './createRollupGetMaxDataSize';
import { rollupCreator } from './contracts';
import { validParentChainId } from './types/ParentChain';
import { validateParentChain } from './types/ParentChain';
import { isCustomFeeTokenAddress } from './utils/isCustomFeeTokenAddress';
import { ChainConfig } from './types/ChainConfig';
import { isAnyTrustChainConfig } from './utils/isAnyTrustChainConfig';
import { getRollupCreatorAddress } from './utils/getters';
import { fetchDecimals } from './utils/erc20';
import { TransactionRequestGasOverrides, applyPercentIncrease } from './utils/gasOverrides';

Expand Down Expand Up @@ -42,11 +43,7 @@ export async function createRollupPrepareTransactionRequest({
gasOverrides,
rollupCreatorAddressOverride,
}: CreateRollupPrepareTransactionRequestParams) {
const chainId = publicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error(`"publicClient.chain" can't be undefined.`);
}
const chainId = validateParentChain(publicClient);

if (params.batchPoster === zeroAddress) {
throw new Error(`"params.batchPoster" can't be set to the zero address.`);
Expand Down Expand Up @@ -79,7 +76,7 @@ export async function createRollupPrepareTransactionRequest({

const request = await publicClient.prepareTransactionRequest({
chain: publicClient.chain,
to: rollupCreatorAddressOverride ?? rollupCreator.address[chainId],
to: rollupCreatorAddressOverride ?? getRollupCreatorAddress(publicClient),
data: createRollupEncodeFunctionData([paramsWithDefaults]),
value: createRollupGetCallValue(paramsWithDefaults),
account,
Expand Down
11 changes: 2 additions & 9 deletions src/createTokenBridgeEnoughCustomFeeTokenAllowance.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Address, PublicClient } from 'viem';

import { tokenBridgeCreator } from './contracts';
import { validParentChainId } from './types/ParentChain';
import { fetchAllowance } from './utils/erc20';
import { createTokenBridgeDefaultRetryablesFees } from './constants';

import { Prettify } from './types/utils';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { getTokenBridgeCreatorAddress } from './utils/getters';

export type CreateTokenBridgeEnoughCustomFeeTokenAllowanceParams = Prettify<
WithTokenBridgeCreatorAddressOverride<{
Expand All @@ -22,16 +21,10 @@ export async function createTokenBridgeEnoughCustomFeeTokenAllowance({
publicClient,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgeEnoughCustomFeeTokenAllowanceParams) {
const chainId = publicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}

const allowance = await fetchAllowance({
address: nativeToken,
owner,
spender: tokenBridgeCreatorAddressOverride ?? tokenBridgeCreator.address[chainId],
spender: tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(publicClient),
publicClient,
});

Expand Down
10 changes: 2 additions & 8 deletions src/createTokenBridgeFetchTokenBridgeContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { tokenBridgeCreator } from './contracts';

import { Prettify } from './types/utils';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { validParentChainId } from './types/ParentChain';
import { TokenBridgeContracts } from './types/TokenBridgeContracts';
import { getTokenBridgeCreatorAddress } from './utils/getters';

export type CreateTokenBridgeFetchTokenBridgeContractsParams = Prettify<
WithTokenBridgeCreatorAddressOverride<{
Expand All @@ -19,14 +19,8 @@ export async function createTokenBridgeFetchTokenBridgeContracts({
parentChainPublicClient,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgeFetchTokenBridgeContractsParams): Promise<TokenBridgeContracts> {
const chainId = parentChainPublicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}

const tokenBridgeCreatorAddress =
tokenBridgeCreatorAddressOverride ?? tokenBridgeCreator.address[chainId];
tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(parentChainPublicClient);

// getting parent chain addresses
const [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Address, PublicClient, maxInt256 } from 'viem';

import { approvePrepareTransactionRequest } from './utils/erc20';
import { validParentChainId } from './types/ParentChain';
import { tokenBridgeCreator } from './contracts';

import { Prettify } from './types/utils';
import { validateParentChain } from './types/ParentChain';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { getTokenBridgeCreatorAddress } from './utils/getters';

export type CreateTokenBridgePrepareCustomFeeTokenApprovalTransactionRequestParams = Prettify<
WithTokenBridgeCreatorAddressOverride<{
Expand All @@ -23,16 +23,12 @@ export async function createTokenBridgePrepareCustomFeeTokenApprovalTransactionR
publicClient,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgePrepareCustomFeeTokenApprovalTransactionRequestParams) {
const chainId = publicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}
const chainId = validateParentChain(publicClient);

const request = await approvePrepareTransactionRequest({
address: nativeToken,
owner,
spender: tokenBridgeCreatorAddressOverride ?? tokenBridgeCreator.address[chainId],
spender: tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(publicClient),
amount,
publicClient,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { publicClientToProvider } from './ethers-compat/publicClientToProvider';
import { getEstimateForSettingGateway } from './createTokenBridge-ethers';
import { GasOverrideOptions, applyPercentIncrease } from './utils/gasOverrides';
import { Prettify } from './types/utils';
import { validParentChainId } from './types/ParentChain';
import { validateParentChain } from './types/ParentChain';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';

export type TransactionRequestRetryableGasOverrides = {
Expand Down Expand Up @@ -96,11 +96,7 @@ export async function createTokenBridgePrepareSetWethGatewayTransactionRequest({
retryableGasOverrides,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgePrepareRegisterWethGatewayTransactionRequestParams) {
const chainId = parentChainPublicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}
const chainId = validateParentChain(parentChainPublicClient);

// check for custom fee token chain
if (
Expand Down
11 changes: 4 additions & 7 deletions src/createTokenBridgePrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Address, PublicClient, encodeFunctionData } from 'viem';

import { tokenBridgeCreator } from './contracts';
import { validParentChainId } from './types/ParentChain';
import { validateParentChain } from './types/ParentChain';
import { createTokenBridgeGetInputs } from './createTokenBridge-ethers';
import { publicClientToProvider } from './ethers-compat/publicClientToProvider';
import { isCustomFeeTokenChain } from './utils/isCustomFeeTokenChain';
Expand All @@ -13,6 +13,7 @@ import {

import { Prettify } from './types/utils';
import { WithTokenBridgeCreatorAddressOverride } from './types/createTokenBridgeTypes';
import { getTokenBridgeCreatorAddress } from './utils/getters';

export type TransactionRequestRetryableGasOverrides = {
maxSubmissionCostForFactory?: GasOverrideOptions;
Expand Down Expand Up @@ -42,14 +43,10 @@ export async function createTokenBridgePrepareTransactionRequest({
retryableGasOverrides,
tokenBridgeCreatorAddressOverride,
}: CreateTokenBridgePrepareTransactionRequestParams) {
const chainId = parentChainPublicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}
const chainId = validateParentChain(parentChainPublicClient);

const tokenBridgeCreatorAddress =
tokenBridgeCreatorAddressOverride ?? tokenBridgeCreator.address[chainId];
tokenBridgeCreatorAddressOverride ?? getTokenBridgeCreatorAddress(parentChainPublicClient);

const parentChainProvider = publicClientToProvider(parentChainPublicClient);
const orbitChainProvider = publicClientToProvider(orbitChainPublicClient);
Expand Down
8 changes: 2 additions & 6 deletions src/prepareNodeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from './types/NodeConfig';
import { ChainConfig } from './types/ChainConfig';
import { CoreContracts } from './types/CoreContracts';
import { ParentChainId, validParentChainId } from './types/ParentChain';
import { ParentChainId, validateParentChain } from './types/ParentChain';
import {
mainnet,
arbitrumOne,
Expand Down Expand Up @@ -66,17 +66,13 @@ export function prepareNodeConfig({
parentChainId: number;
parentChainRpcUrl: string;
}): NodeConfig {
if (!validParentChainId(parentChainId)) {
throw new Error(`[prepareNodeConfig] invalid parent chain id: ${parentChainId}`);
}

const config: NodeConfig = {
'chain': {
'info-json': stringifyInfoJson([
{
'chain-id': chainConfig.chainId,
'parent-chain-id': parentChainId,
'parent-chain-is-arbitrum': parentChainIsArbitrum(parentChainId),
'parent-chain-is-arbitrum': parentChainIsArbitrum(validateParentChain(parentChainId)),
'chain-name': chainName,
'chain-config': chainConfig,
'rollup': {
Expand Down
8 changes: 2 additions & 6 deletions src/setValidKeyset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PublicClient, WalletClient } from 'viem';

import { upgradeExecutor } from './contracts';
import { validParentChainId } from './types/ParentChain';
import { validateParentChain } from './types/ParentChain';
import { CoreContracts } from './types/CoreContracts';
import { setValidKeysetEncodeFunctionData } from './setValidKeysetEncodeFunctionData';

Expand All @@ -18,13 +18,9 @@ export async function setValidKeyset({
publicClient,
walletClient,
}: SetValidKeysetParams) {
const chainId = publicClient.chain?.id;
validateParentChain(publicClient);
const account = walletClient.account?.address;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}

if (typeof account === 'undefined') {
throw new Error('account is undefined');
}
Expand Down
8 changes: 2 additions & 6 deletions src/setValidKeysetPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address } from 'viem';

import { validParentChainId } from './types/ParentChain';
import { validateParentChain } from './types/ParentChain';
import { SetValidKeysetParams } from './setValidKeyset';
import { setValidKeysetEncodeFunctionData } from './setValidKeysetEncodeFunctionData';
import { upgradeExecutorEncodeFunctionData } from './upgradeExecutor';
Expand All @@ -18,11 +18,7 @@ export async function setValidKeysetPrepareTransactionRequest({
account,
publicClient,
}: SetValidKeysetPrepareTransactionRequestParams) {
const chainId = publicClient.chain?.id;

if (!validParentChainId(chainId)) {
throw new Error('chainId is undefined');
}
const chainId = validateParentChain(publicClient);

const request = await publicClient.prepareTransactionRequest({
chain: publicClient.chain,
Expand Down
Loading

0 comments on commit 5a341c1

Please sign in to comment.