-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcreateTokenBridge-ethers.ts
More file actions
345 lines (308 loc) Β· 12.7 KB
/
createTokenBridge-ethers.ts
File metadata and controls
345 lines (308 loc) Β· 12.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { Address, PublicClient, Transport, Chain } from 'viem';
import { BigNumber, ContractFactory, ethers, ContractInterface } from 'ethers';
import { ParentToChildMessageGasEstimator } from '@arbitrum/sdk';
import { getBaseFee } from '@arbitrum/sdk/dist/lib/utils/lib';
import { RollupAdminLogic__factory } from '@arbitrum/sdk/dist/lib/abi/factories/RollupAdminLogic__factory';
import L1AtomicTokenBridgeCreator from '@arbitrum/token-bridge-contracts/build/contracts/contracts/tokenbridge/ethereum/L1AtomicTokenBridgeCreator.sol/L1AtomicTokenBridgeCreator.json';
import L2AtomicTokenBridgeFactory from '@arbitrum/token-bridge-contracts/build/contracts/contracts/tokenbridge/arbitrum/L2AtomicTokenBridgeFactory.sol/L2AtomicTokenBridgeFactory.json';
import { applyPercentIncrease } from './utils/gasOverrides';
import { TransactionRequestRetryableGasOverrides } from './createTokenBridgePrepareTransactionRequest';
import { registerNewNetwork } from './utils/registerNewNetwork';
import { publicClientToProvider } from './ethers-compat/publicClientToProvider';
type NamedFactory = ContractFactory & { contractName: string };
const NamedFactoryInstance = (contractJson: {
abi: ContractInterface;
bytecode: string;
contractName: string;
}): NamedFactory => {
const factory = new ContractFactory(contractJson.abi, contractJson.bytecode) as NamedFactory;
factory['contractName'] = contractJson.contractName;
return factory;
};
// import from token-bridge-contracts directly to make sure the bytecode is the same
const L2AtomicTokenBridgeFactory__factory = NamedFactoryInstance(L2AtomicTokenBridgeFactory);
export type CreateTokenBridgeGetInputsResult = {
inbox: Address;
maxGasForContracts: bigint;
maxGasPrice: bigint;
retryableFee: bigint;
};
export async function createTokenBridgeGetInputs<
TParentChain extends Chain | undefined,
TOrbitChain extends Chain | undefined,
>(
l1DeployerAddress: string,
l1PublicClient: PublicClient<Transport, TParentChain>,
l2PublicClient: PublicClient<Transport, TOrbitChain>,
l1TokenBridgeCreatorAddress: string,
rollupAddress: string,
retryableGasOverrides?: TransactionRequestRetryableGasOverrides,
): Promise<CreateTokenBridgeGetInputsResult> {
const l1Provider = publicClientToProvider(l1PublicClient);
const l2Provider = publicClientToProvider(l2PublicClient);
await registerNewNetwork(l1Provider, l2Provider, rollupAddress);
//// run retryable estimate for deploying L2 factory
const {
maxSubmissionCost: maxSubmissionCostForFactoryEstimation,
maxGas: maxGasForFactoryEstimation,
} = await getEstimateForDeployingFactory(
l1DeployerAddress,
l1TokenBridgeCreatorAddress,
l1Provider,
l2Provider,
);
const {
maxSubmissionCost: maxSubmissionCostForContractsEstimation,
maxGas: maxGasForContractsEstimation,
} = await getEstimateForDeployingContracts(
l1DeployerAddress,
l1TokenBridgeCreatorAddress,
l1Provider,
l2Provider,
);
//// apply gas overrides
const maxSubmissionCostForFactory =
retryableGasOverrides && retryableGasOverrides.maxSubmissionCostForFactory
? BigNumber.from(
applyPercentIncrease({
base:
retryableGasOverrides.maxSubmissionCostForFactory.base ??
BigInt(maxSubmissionCostForFactoryEstimation.toHexString()),
percentIncrease: retryableGasOverrides.maxSubmissionCostForFactory.percentIncrease,
}),
)
: maxSubmissionCostForFactoryEstimation;
const maxGasForFactory =
retryableGasOverrides && retryableGasOverrides.maxGasForFactory
? BigNumber.from(
applyPercentIncrease({
base:
retryableGasOverrides.maxGasForFactory.base ??
BigInt(maxGasForFactoryEstimation.toHexString()),
percentIncrease: retryableGasOverrides.maxGasForFactory.percentIncrease,
}),
)
: maxGasForFactoryEstimation;
const maxSubmissionCostForContracts =
retryableGasOverrides && retryableGasOverrides.maxSubmissionCostForContracts
? BigNumber.from(
applyPercentIncrease({
base:
retryableGasOverrides.maxSubmissionCostForContracts.base ??
BigInt(maxSubmissionCostForContractsEstimation.toHexString()),
percentIncrease: retryableGasOverrides.maxSubmissionCostForContracts.percentIncrease,
}),
)
: maxSubmissionCostForContractsEstimation;
const maxGasForContracts =
retryableGasOverrides && retryableGasOverrides.maxGasForContracts
? BigNumber.from(
applyPercentIncrease({
base:
retryableGasOverrides.maxGasForContracts.base ??
BigInt(maxGasForContractsEstimation.toHexString()),
percentIncrease: retryableGasOverrides.maxGasForContracts.percentIncrease,
}),
)
: maxGasForContractsEstimation;
const maxGasPrice =
retryableGasOverrides && retryableGasOverrides.maxGasPrice
? BigNumber.from(retryableGasOverrides.maxGasPrice)
: await l2Provider.getGasPrice();
const retryableFee = maxSubmissionCostForFactory
.add(maxSubmissionCostForContracts)
.add(maxGasForFactory.mul(maxGasPrice))
.add(maxGasForContracts.mul(maxGasPrice));
// get inbox from rollup contract
const inbox = await RollupAdminLogic__factory.connect(rollupAddress, l1Provider).inbox();
return {
inbox: inbox as Address,
maxGasForContracts: maxGasForContracts.toBigInt(),
maxGasPrice: maxGasPrice.toBigInt(),
retryableFee: retryableFee.toBigInt(),
};
}
const getEstimateForDeployingFactory = async (
l1DeployerAddress: string,
l1TokenBridgeCreatorAddress: string,
l1Provider: ethers.providers.Provider,
l2Provider: ethers.providers.Provider,
): Promise<{
maxSubmissionCost: BigNumber;
maxGas: BigNumber;
}> => {
const L1AtomicTokenBridgeCreator__factory = new ethers.Contract(
l1TokenBridgeCreatorAddress,
L1AtomicTokenBridgeCreator.abi,
);
const l1TokenBridgeCreator = L1AtomicTokenBridgeCreator__factory.connect(l1Provider);
//// run retryable estimate for deploying L2 factory
const l1ToL2MsgGasEstimate = new ParentToChildMessageGasEstimator(l2Provider);
const { maxSubmissionCost } = await l1ToL2MsgGasEstimate.estimateAll(
{
from: ethers.Wallet.createRandom().address,
to: ethers.constants.AddressZero,
l2CallValue: BigNumber.from(0),
excessFeeRefundAddress: l1DeployerAddress,
callValueRefundAddress: l1DeployerAddress,
data: L2AtomicTokenBridgeFactory__factory.bytecode,
},
await getBaseFee(l1Provider),
l1Provider,
);
const maxGas = (await l1TokenBridgeCreator.gasLimitForL2FactoryDeployment()) as BigNumber;
return {
// there's already a 300% increase buffer in the SDK
// https://github.com/OffchainLabs/arbitrum-sdk/blob/main/src/lib/message/ParentToChildMessageGasEstimator.ts#L27
maxSubmissionCost,
maxGas,
};
};
async function getEstimateForDeployingContracts(
l1DeployerAddress: string,
l1TokenBridgeCreatorAddress: string,
l1Provider: ethers.providers.Provider,
l2Provider: ethers.providers.Provider,
): Promise<{
maxSubmissionCost: BigNumber;
maxGas: BigNumber;
}> {
const L1AtomicTokenBridgeCreator__factory = new ethers.Contract(
l1TokenBridgeCreatorAddress,
L1AtomicTokenBridgeCreator.abi,
);
const l1TokenBridgeCreator = L1AtomicTokenBridgeCreator__factory.connect(l1Provider);
const l2FactoryTemplate = L2AtomicTokenBridgeFactory__factory.attach(
await l1TokenBridgeCreator.l2TokenBridgeFactoryTemplate(),
).connect(l1Provider);
const l2Code = {
router: await l1Provider.getCode(await l1TokenBridgeCreator.l2RouterTemplate()),
standardGateway: await l1Provider.getCode(
await l1TokenBridgeCreator.l2StandardGatewayTemplate(),
),
customGateway: await l1Provider.getCode(await l1TokenBridgeCreator.l2CustomGatewayTemplate()),
wethGateway: await l1Provider.getCode(await l1TokenBridgeCreator.l2WethGatewayTemplate()),
aeWeth: await l1Provider.getCode(await l1TokenBridgeCreator.l2WethTemplate()),
upgradeExecutor: await l1Provider.getCode(
(
await l1TokenBridgeCreator.l1Templates()
).upgradeExecutor,
),
multicall: await l1Provider.getCode(await l1TokenBridgeCreator.l2MulticallTemplate()),
};
const l1ToL2MsgGasEstimate = new ParentToChildMessageGasEstimator(l2Provider);
const calldata = l2FactoryTemplate.interface.encodeFunctionData('deployL2Contracts', [
l2Code,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
]);
const maxSubmissionCost = await l1ToL2MsgGasEstimate.estimateSubmissionFee(
l1Provider,
await l1Provider.getGasPrice(),
ethers.utils.hexDataLength(calldata),
);
const maxGas = await l2FactoryTemplate.estimateGas.deployL2Contracts(
l2Code,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
);
return {
// there's already a 300% increase buffer in the SDK
// https://github.com/OffchainLabs/arbitrum-sdk/blob/main/src/lib/message/ParentToChildMessageGasEstimator.ts#L27
maxSubmissionCost,
maxGas: maxGas.mul(2),
};
}
export async function getEstimateForSettingGateway<
TParentChain extends Chain | undefined,
TOrbitChain extends Chain | undefined,
>(
l1ChainOwnerAddress: Address,
l1UpgradeExecutorAddress: Address,
l1GatewayRouterAddress: Address,
setGatewaysCalldata: `0x${string}`,
parentChainPublicClient: PublicClient<Transport, TParentChain>,
orbitChainPublicClient: PublicClient<Transport, TOrbitChain>,
) {
// ethers providers
const parentChainProvider = publicClientToProvider(parentChainPublicClient);
const orbitChainProvider = publicClientToProvider(orbitChainPublicClient);
// run retryable estimate for setting a token gateway in the router
const l1ToL2MsgGasEstimate = new ParentToChildMessageGasEstimator(orbitChainProvider);
const setGatewaysGasParams = await l1ToL2MsgGasEstimate.estimateAll(
{
from: l1UpgradeExecutorAddress,
to: l1GatewayRouterAddress,
l2CallValue: BigNumber.from(0),
excessFeeRefundAddress: l1ChainOwnerAddress,
callValueRefundAddress: l1ChainOwnerAddress,
data: setGatewaysCalldata,
},
await getBaseFee(parentChainProvider),
parentChainProvider,
);
return {
gasLimit: setGatewaysGasParams.gasLimit.toBigInt(),
maxFeePerGas: setGatewaysGasParams.maxFeePerGas.toBigInt(),
maxSubmissionCost: setGatewaysGasParams.maxSubmissionCost.toBigInt(),
deposit: setGatewaysGasParams.deposit.toBigInt(),
};
}
export function getFactoryDeploymentDataSize(): number {
return ethers.utils.hexDataLength(L2AtomicTokenBridgeFactory__factory.bytecode);
}
export async function getContractsDeploymentData(
l1TokenBridgeCreatorAddress: string,
l1Provider: ethers.providers.Provider,
) {
const l1TokenBridgeCreator = new ethers.Contract(
l1TokenBridgeCreatorAddress,
L1AtomicTokenBridgeCreator.abi,
).connect(l1Provider);
const l2FactoryTemplate = L2AtomicTokenBridgeFactory__factory.attach(
await l1TokenBridgeCreator.l2TokenBridgeFactoryTemplate(),
).connect(l1Provider);
const l2Code = {
router: await l1Provider.getCode(await l1TokenBridgeCreator.l2RouterTemplate()),
standardGateway: await l1Provider.getCode(
await l1TokenBridgeCreator.l2StandardGatewayTemplate(),
),
customGateway: await l1Provider.getCode(await l1TokenBridgeCreator.l2CustomGatewayTemplate()),
wethGateway: await l1Provider.getCode(await l1TokenBridgeCreator.l2WethGatewayTemplate()),
aeWeth: await l1Provider.getCode(await l1TokenBridgeCreator.l2WethTemplate()),
upgradeExecutor: await l1Provider.getCode(
(
await l1TokenBridgeCreator.l1Templates()
).upgradeExecutor,
),
multicall: await l1Provider.getCode(await l1TokenBridgeCreator.l2MulticallTemplate()),
};
const calldata = l2FactoryTemplate.interface.encodeFunctionData('deployL2Contracts', [
l2Code,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
ethers.Wallet.createRandom().address,
]);
return {
dataSize: ethers.utils.hexDataLength(calldata),
l2Code,
l2FactoryTemplate,
};
}