-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathcreateTokenBridge.integration.test.ts
More file actions
545 lines (495 loc) Β· 20.8 KB
/
createTokenBridge.integration.test.ts
File metadata and controls
545 lines (495 loc) Β· 20.8 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import { describe, it, expect } from 'vitest';
import { createPublicClient, encodeFunctionData, http, zeroAddress, parseAbi } from 'viem';
import { nitroTestnodeL1, nitroTestnodeL2, nitroTestnodeL3 } from './chains';
import {
getInformationFromTestnode,
getNitroTestnodePrivateKeyAccounts,
PrivateKeyAccountWithPrivateKey,
} from './testHelpers';
import { createTokenBridgePrepareTransactionRequest } from './createTokenBridgePrepareTransactionRequest';
import { createTokenBridgePrepareTransactionReceipt } from './createTokenBridgePrepareTransactionReceipt';
import { deployTokenBridgeCreator } from './createTokenBridge-testHelpers';
import { deployTokenBridgeCreator as deployAnvilTokenBridgeCreator } from './integrationTestHelpers/anvilHarnessHelpers';
import { CreateTokenBridgeEnoughCustomFeeTokenAllowanceParams } from './createTokenBridgeEnoughCustomFeeTokenAllowance';
import { createTokenBridgePrepareCustomFeeTokenApprovalTransactionRequest } from './createTokenBridgePrepareCustomFeeTokenApprovalTransactionRequest';
import { erc20ABI } from './contracts/ERC20';
import { createTokenBridgePrepareSetWethGatewayTransactionRequest } from './createTokenBridgePrepareSetWethGatewayTransactionRequest';
import { createTokenBridgePrepareSetWethGatewayTransactionReceipt } from './createTokenBridgePrepareSetWethGatewayTransactionReceipt';
import { createTokenBridge } from './createTokenBridge';
import { TokenBridgeContracts } from './types/TokenBridgeContracts';
import { scaleFrom18DecimalsToNativeTokenDecimals } from './utils/decimals';
import { getWethAddress } from './utils/getWethAddress';
import { isAnvilTestMode, getAnvilTestStack } from './integrationTestHelpers/injectedMode';
import { Address } from 'viem';
import { testConstants } from './integrationTestHelpers/constants';
const env = isAnvilTestMode() ? getAnvilTestStack() : undefined;
let l2Rollup: Address;
let l2RollupOwner: PrivateKeyAccountWithPrivateKey;
let l3Rollup: Address;
let l3RollupOwner: PrivateKeyAccountWithPrivateKey;
let l3TokenBridgeDeployer: PrivateKeyAccountWithPrivateKey;
let l3NativeToken: Address;
if (env) {
l2Rollup = env.l2.rollup;
l2RollupOwner = env.l2.accounts.rollupOwner;
l3RollupOwner = env.l3.accounts.rollupOwner;
l3TokenBridgeDeployer = env.l3.accounts.tokenBridgeDeployer;
l3NativeToken = env.l3.nativeToken;
l3Rollup = env.l3.rollup;
} else {
const testnodeAccounts = getNitroTestnodePrivateKeyAccounts();
l2RollupOwner = testnodeAccounts.l2RollupOwner;
l3RollupOwner = testnodeAccounts.l3RollupOwner;
l3TokenBridgeDeployer = testnodeAccounts.l3TokenBridgeDeployer;
const testnodeInformation = getInformationFromTestnode();
l2Rollup = testnodeInformation.rollup;
l3Rollup = testnodeInformation.l3Rollup;
l3NativeToken = testnodeInformation.l3NativeToken;
}
const l1Client = createPublicClient({
chain: env ? env.l1.chain : nitroTestnodeL1,
transport: env ? http(env.l1.rpcUrl) : http(nitroTestnodeL1.rpcUrls.default.http[0]),
...(env ? { pollingInterval: testConstants.POLLING_INTERVAL } : {}),
});
const l2Client = createPublicClient({
chain: env ? env.l2.chain : nitroTestnodeL2,
transport: env ? http(env.l2.rpcUrl) : http(nitroTestnodeL2.rpcUrls.default.http[0]),
...(env ? { pollingInterval: testConstants.POLLING_INTERVAL } : {}),
});
const l3Client = createPublicClient({
chain: env ? env.l3.chain : nitroTestnodeL3,
transport: env ? http(env.l3.rpcUrl) : http(nitroTestnodeL3.rpcUrls.default.http[0]),
...(env ? { pollingInterval: testConstants.POLLING_INTERVAL } : {}),
});
function checkTokenBridgeContracts(tokenBridgeContracts: TokenBridgeContracts) {
expect(Object.keys(tokenBridgeContracts)).toHaveLength(2);
// parent chain contracts
expect(Object.keys(tokenBridgeContracts.parentChainContracts)).toHaveLength(6);
expect(tokenBridgeContracts.parentChainContracts.router).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.parentChainContracts.standardGateway).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.parentChainContracts.customGateway).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.parentChainContracts.multicall).not.toEqual(zeroAddress);
// orbit chain contracts
expect(Object.keys(tokenBridgeContracts.orbitChainContracts)).toHaveLength(9);
expect(tokenBridgeContracts.orbitChainContracts.router).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.standardGateway).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.customGateway).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.proxyAdmin).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.beaconProxyFactory).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.upgradeExecutor).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.multicall).not.toEqual(zeroAddress);
}
async function checkWethGateways(
tokenBridgeContracts: TokenBridgeContracts,
{ customFeeToken }: { customFeeToken: boolean },
) {
if (customFeeToken) {
// wethGateway and weth should be the zeroAddress on custom-fee-token chains
expect(tokenBridgeContracts.orbitChainContracts.wethGateway).toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.weth).toEqual(zeroAddress);
expect(tokenBridgeContracts.parentChainContracts.wethGateway).toEqual(zeroAddress);
expect(tokenBridgeContracts.parentChainContracts.weth).toEqual(zeroAddress);
return;
}
// verify weth gateway (parent chain)
const registeredWethGatewayOnParentChain = await l1Client.readContract({
address: tokenBridgeContracts.parentChainContracts.router,
abi: parseAbi(['function l1TokenToGateway(address) view returns (address)']),
functionName: 'l1TokenToGateway',
args: [tokenBridgeContracts.parentChainContracts.weth],
});
expect(registeredWethGatewayOnParentChain).toEqual(
tokenBridgeContracts.parentChainContracts.wethGateway,
);
// verify weth gateway (orbit chain)
// Note: we pass the address of the token on the parent chain when asking for the registered gateway on the orbit chain
const registeredWethGatewayOnOrbitChain = await l2Client.readContract({
address: tokenBridgeContracts.orbitChainContracts.router,
abi: parseAbi(['function l1TokenToGateway(address) view returns (address)']),
functionName: 'l1TokenToGateway',
args: [tokenBridgeContracts.parentChainContracts.weth],
});
expect(registeredWethGatewayOnOrbitChain).toEqual(
tokenBridgeContracts.orbitChainContracts.wethGateway,
);
expect(tokenBridgeContracts.parentChainContracts.weth).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.parentChainContracts.wethGateway).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.weth).not.toEqual(zeroAddress);
expect(tokenBridgeContracts.orbitChainContracts.wethGateway).not.toEqual(zeroAddress);
}
const nativeTokenDecimals = process.env.INTEGRATION_TEST_DECIMALS
? Number(process.env.INTEGRATION_TEST_DECIMALS)
: 18;
describe('createTokenBridge utils function', () => {
it(`successfully deploys token bridge contracts through token bridge creator`, async () => {
// deploy a fresh token bridge creator, because it is only possible to deploy one token bridge per rollup per token bridge creator
const tokenBridgeCreator = env
? await deployAnvilTokenBridgeCreator({
rpcUrl: env.l1.rpcUrl,
deployerPrivateKey: env.l2.accounts.deployer.privateKey,
wethAddress: getWethAddress(l1Client),
})
: await deployTokenBridgeCreator({
publicClient: l1Client,
});
const txRequest = await createTokenBridgePrepareTransactionRequest({
params: {
rollup: l2Rollup,
rollupOwner: l2RollupOwner.address,
},
parentChainPublicClient: l1Client,
orbitChainPublicClient: l2Client,
account: l2RollupOwner.address,
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,
},
},
tokenBridgeCreatorAddressOverride: tokenBridgeCreator,
});
// sign and send the transaction
const txHash = await l1Client.sendRawTransaction({
serializedTransaction: await l2RollupOwner.signTransaction(txRequest),
});
// get the transaction receipt after waiting for the transaction to complete
const txReceipt = createTokenBridgePrepareTransactionReceipt(
await l1Client.waitForTransactionReceipt({ hash: txHash }),
);
expect(txReceipt.status).toEqual('success');
// checking retryables execution
const orbitChainRetryableReceipts = await txReceipt.waitForRetryables({
orbitPublicClient: l2Client,
});
expect(orbitChainRetryableReceipts).toHaveLength(2);
expect(orbitChainRetryableReceipts[0].status).toEqual('success');
expect(orbitChainRetryableReceipts[1].status).toEqual('success');
// get contracts
const tokenBridgeContracts = await txReceipt.getTokenBridgeContracts({
parentChainPublicClient: l1Client,
});
checkTokenBridgeContracts(tokenBridgeContracts);
// set weth gateway
const setWethGatewayTxRequest = await createTokenBridgePrepareSetWethGatewayTransactionRequest({
rollup: l2Rollup,
parentChainPublicClient: l1Client,
orbitChainPublicClient: l2Client,
account: l2RollupOwner.address,
retryableGasOverrides: {
gasLimit: {
base: 100_000n,
},
},
tokenBridgeCreatorAddressOverride: tokenBridgeCreator,
});
// sign and send the transaction
const setWethGatewayTxHash = await l1Client.sendRawTransaction({
serializedTransaction: await l2RollupOwner.signTransaction(setWethGatewayTxRequest),
});
// get the transaction receipt after waiting for the transaction to complete
const setWethGatewayTxReceipt = createTokenBridgePrepareSetWethGatewayTransactionReceipt(
await l1Client.waitForTransactionReceipt({ hash: setWethGatewayTxHash }),
);
// checking retryables execution
const orbitChainSetGatewayRetryableReceipt = await setWethGatewayTxReceipt.waitForRetryables({
orbitPublicClient: l2Client,
});
expect(orbitChainSetGatewayRetryableReceipt).toHaveLength(1);
expect(orbitChainSetGatewayRetryableReceipt[0].status).toEqual('success');
checkWethGateways(tokenBridgeContracts, { customFeeToken: false });
});
it(`successfully deploys token bridge contracts with a custom fee token through token bridge creator`, async () => {
// deploy a fresh token bridge creator, because it is only possible to deploy one token bridge per rollup per token bridge creator
const tokenBridgeCreator = env
? await deployAnvilTokenBridgeCreator({
rpcUrl: env.l2.rpcUrl,
deployerPrivateKey: env.l2.accounts.deployer.privateKey,
wethAddress: (env.l2.chain.contracts as { weth: { address: Address } }).weth.address,
blockAdvancer: env.l2.blockAdvancer,
})
: await deployTokenBridgeCreator({
publicClient: l2Client,
});
// -----------------------------
// 1. fund l3deployer account
const fundTxRequestRaw = await l2Client.prepareTransactionRequest({
chain: l2Client.chain,
to: l3NativeToken,
data: encodeFunctionData({
abi: erc20ABI,
functionName: 'transfer',
args: [
l3RollupOwner.address,
scaleFrom18DecimalsToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
],
}),
value: BigInt(0),
account: l3TokenBridgeDeployer,
});
// sign and send the transaction
const fundTxRequest = { ...fundTxRequestRaw, chainId: l2Client.chain.id };
const fundTxHash = await l2Client.sendRawTransaction({
serializedTransaction: await l3TokenBridgeDeployer.signTransaction(fundTxRequest),
});
// get the transaction receipt after waiting for the transaction to complete
const fundTxReceipt = await l2Client.waitForTransactionReceipt({
hash: fundTxHash,
});
expect(fundTxReceipt.status).toEqual('success');
// -----------------------------
// 2. approve custom fee token to be spent by the TokenBridgeCreator
const allowanceParams: CreateTokenBridgeEnoughCustomFeeTokenAllowanceParams<
typeof nitroTestnodeL2
> = {
nativeToken: l3NativeToken,
owner: l3RollupOwner.address,
publicClient: l2Client,
tokenBridgeCreatorAddressOverride: tokenBridgeCreator,
};
// sign and send the transaction
const approvalForTokenBridgeCreatorTxHash = await l2Client.sendRawTransaction({
serializedTransaction: await l3RollupOwner.signTransaction(
await createTokenBridgePrepareCustomFeeTokenApprovalTransactionRequest(allowanceParams),
),
});
// get the transaction receipt after waiting for the transaction to complete
const approvalForNewTokenBridgeCreatorTxReceipt = await l2Client.waitForTransactionReceipt({
hash: approvalForTokenBridgeCreatorTxHash,
});
expect(approvalForNewTokenBridgeCreatorTxReceipt.status).toEqual('success');
// -----------------------------
// 3. create the token bridge
const txRequest = await createTokenBridgePrepareTransactionRequest({
params: {
rollup: l3Rollup,
rollupOwner: l3RollupOwner.address,
},
parentChainPublicClient: l2Client,
orbitChainPublicClient: l3Client,
account: l3RollupOwner.address,
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,
},
},
tokenBridgeCreatorAddressOverride: tokenBridgeCreator,
});
// sign and send the transaction
const txHash = await l2Client.sendRawTransaction({
serializedTransaction: await l3RollupOwner.signTransaction(txRequest),
});
// get the transaction receipt after waiting for the transaction to complete
const txReceipt = createTokenBridgePrepareTransactionReceipt(
await l2Client.waitForTransactionReceipt({ hash: txHash }),
);
expect(txReceipt.status).toEqual('success');
// checking retryables execution
const orbitChainRetryableReceipts = await txReceipt.waitForRetryables({
orbitPublicClient: l3Client,
});
expect(orbitChainRetryableReceipts).toHaveLength(2);
expect(orbitChainRetryableReceipts[0].status).toEqual('success');
expect(orbitChainRetryableReceipts[1].status).toEqual('success');
// get contracts
const tokenBridgeContracts = await txReceipt.getTokenBridgeContracts({
parentChainPublicClient: l2Client,
});
checkTokenBridgeContracts(tokenBridgeContracts);
checkWethGateways(tokenBridgeContracts, { customFeeToken: true });
});
});
describe('createTokenBridge', () => {
it('successfully deploys token bridge contracts', async () => {
// deploy a fresh token bridge creator, because it is only possible to deploy one token bridge per rollup per token bridge creator
const tokenBridgeCreator = env
? await deployAnvilTokenBridgeCreator({
rpcUrl: env.l1.rpcUrl,
deployerPrivateKey: env.l2.accounts.deployer.privateKey,
wethAddress: getWethAddress(l1Client),
})
: await deployTokenBridgeCreator({
publicClient: l1Client,
});
const { tokenBridgeContracts } = await createTokenBridge({
rollupOwner: l2RollupOwner.address,
rollupAddress: l2Rollup,
account: l2RollupOwner,
parentChainPublicClient: l1Client,
orbitChainPublicClient: l2Client,
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,
},
},
});
checkTokenBridgeContracts(tokenBridgeContracts);
checkWethGateways(tokenBridgeContracts, { customFeeToken: false });
});
it('successfully deploys token bridge contracts with a custom fee token', async () => {
// // deploy a fresh token bridge creator, because it is only possible to deploy one token bridge per rollup per token bridge creator
const tokenBridgeCreator = env
? await deployAnvilTokenBridgeCreator({
rpcUrl: env.l2.rpcUrl,
deployerPrivateKey: env.l2.accounts.deployer.privateKey,
wethAddress: (env.l2.chain.contracts as { weth: { address: Address } }).weth.address,
blockAdvancer: env.l2.blockAdvancer,
})
: await deployTokenBridgeCreator({
publicClient: l2Client,
});
// -----------------------------
// 1. fund l3deployer account
const fundTxRequestRaw = await l2Client.prepareTransactionRequest({
chain: l2Client.chain,
to: l3NativeToken,
data: encodeFunctionData({
abi: erc20ABI,
functionName: 'transfer',
args: [
l3RollupOwner.address,
scaleFrom18DecimalsToNativeTokenDecimals({ amount: 500n, decimals: nativeTokenDecimals }),
],
}),
value: BigInt(0),
account: l3TokenBridgeDeployer,
});
// sign and send the transaction
const fundTxRequest = { ...fundTxRequestRaw, chainId: l2Client.chain.id };
const fundTxHash = await l2Client.sendRawTransaction({
serializedTransaction: await l3TokenBridgeDeployer.signTransaction(fundTxRequest),
});
// get the transaction receipt after waiting for the transaction to complete
const fundTxReceipt = await l2Client.waitForTransactionReceipt({
hash: fundTxHash,
});
expect(fundTxReceipt.status).toEqual('success');
// -----------------------------
// 2. Deploy token bridge contracts
const { tokenBridgeContracts } = await createTokenBridge({
rollupOwner: l3RollupOwner.address,
rollupAddress: l3Rollup,
account: l3RollupOwner,
parentChainPublicClient: l2Client,
orbitChainPublicClient: l3Client,
nativeTokenAddress: l3NativeToken,
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,
},
},
});
checkTokenBridgeContracts(tokenBridgeContracts);
checkWethGateways(tokenBridgeContracts, { customFeeToken: true });
});
it('should throw when createTokenBridge is called multiple times', async () => {
const tokenBridgeCreator = env
? await deployAnvilTokenBridgeCreator({
rpcUrl: env.l1.rpcUrl,
deployerPrivateKey: env.l2.accounts.deployer.privateKey,
wethAddress: getWethAddress(l1Client),
})
: await deployTokenBridgeCreator({
publicClient: l1Client,
});
const cfg = {
rollupOwner: l2RollupOwner.address,
rollupAddress: l2Rollup,
account: l2RollupOwner,
parentChainPublicClient: l1Client,
orbitChainPublicClient: l2Client,
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.toThrowError(
`Token bridge contracts for Rollup ${l2Rollup} are already deployed`,
);
checkTokenBridgeContracts(tokenBridgeContracts);
checkWethGateways(tokenBridgeContracts, { customFeeToken: false });
});
});