-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathcreateRollupPrepareTransactionRequest.ts
More file actions
101 lines (87 loc) · 3.94 KB
/
createRollupPrepareTransactionRequest.ts
File metadata and controls
101 lines (87 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { Address, PublicClient, Transport, Chain, encodeFunctionData, zeroAddress } from 'viem';
import { defaults } from './createRollupDefaults';
import { createRollupGetCallValue } from './createRollupGetCallValue';
import { createRollupGetMaxDataSize } from './createRollupGetMaxDataSize';
import { rollupCreatorABI } from './contracts/RollupCreator';
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';
import { Prettify } from './types/utils';
import {
CreateRollupFunctionInputs,
CreateRollupParams,
WithRollupCreatorAddressOverride,
} from './types/createRollupTypes';
function createRollupEncodeFunctionData(args: CreateRollupFunctionInputs) {
return encodeFunctionData({
abi: rollupCreatorABI,
functionName: 'createRollup',
args,
});
}
export type CreateRollupPrepareTransactionRequestParams<TChain extends Chain | undefined> =
Prettify<
WithRollupCreatorAddressOverride<{
params: CreateRollupParams;
account: Address;
publicClient: PublicClient<Transport, TChain>;
gasOverrides?: TransactionRequestGasOverrides;
}>
>;
export async function createRollupPrepareTransactionRequest<TChain extends Chain | undefined>({
params,
account,
publicClient,
gasOverrides,
rollupCreatorAddressOverride,
}: CreateRollupPrepareTransactionRequestParams<TChain>) {
const chainId = validateParentChain(publicClient);
if (params.batchPosters.length === 0 || params.batchPosters.includes(zeroAddress)) {
throw new Error(`"params.batchPosters" can't be empty or contain the zero address.`);
}
if (params.validators.length === 0 || params.validators.includes(zeroAddress)) {
throw new Error(`"params.validators" can't be empty or contain the zero address.`);
}
const chainConfig: ChainConfig = JSON.parse(params.config.chainConfig);
if (isCustomFeeTokenAddress(params.nativeToken)) {
// custom fee token is only allowed for anytrust chains
if (!isAnyTrustChainConfig(chainConfig)) {
throw new Error(
`"params.nativeToken" can only be used on AnyTrust chains. Set "arbitrum.DataAvailabilityCommittee" to "true" in the chain config.`,
);
}
// custom fee token is only allowed to have 18 decimals
if ((await fetchDecimals({ address: params.nativeToken, publicClient })) !== 18) {
throw new Error(
`"params.nativeToken" can only be configured with a token that uses 18 decimals.`,
);
}
}
const maxDataSize = createRollupGetMaxDataSize(chainId);
const batchPosterManager = params.batchPosterManager ?? zeroAddress;
const paramsWithDefaults = { ...defaults, ...params, maxDataSize, batchPosterManager };
// @ts-ignore (todo: fix viem type issue)
const request = await publicClient.prepareTransactionRequest({
chain: publicClient.chain,
to: rollupCreatorAddressOverride ?? getRollupCreatorAddress(publicClient),
data: createRollupEncodeFunctionData([paramsWithDefaults]),
value: createRollupGetCallValue(paramsWithDefaults),
account,
// if the base gas limit override was provided, hardcode gas to 0 to skip estimation
// we'll set the actual value in the code below
gas: typeof gasOverrides?.gasLimit?.base !== 'undefined' ? 0n : undefined,
});
// potential gas overrides (gas limit)
if (gasOverrides && gasOverrides.gasLimit) {
request.gas = applyPercentIncrease({
// the ! is here because we should let it error in case we don't have the estimated gas
base: gasOverrides.gasLimit.base ?? request.gas!,
percentIncrease: gasOverrides.gasLimit.percentIncrease,
});
}
return { ...request, chainId };
}