-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathrollupCreation.ts
369 lines (337 loc) · 11.7 KB
/
rollupCreation.ts
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
import { ethers } from 'hardhat'
import '@nomiclabs/hardhat-ethers'
import { run } from 'hardhat'
import { abi as rollupCreatorAbi } from '../build/contracts/src/rollup/RollupCreator.sol/RollupCreator.json'
import { config, maxDataSize } from './config'
import { BigNumber, Signer } from 'ethers'
import { ERC20, ERC20__factory, IERC20__factory } from '../build/types'
import { sleep } from './testSetup'
import { promises as fs } from 'fs'
import { _isRunningOnArbitrum, verifyContract } from './deploymentUtils'
// 1 gwei
const MAX_FER_PER_GAS = BigNumber.from('1000000000')
interface RollupCreatedEvent {
event: string
address: string
args?: {
rollupAddress: string
nativeToken: string
inboxAddress: string
outbox: string
rollupEventInbox: string
challengeManager: string
adminProxy: string
sequencerInbox: string
bridge: string
upgradeExecutor: string
validatorWalletCreator: string
}
}
interface RollupCreationResult {
bridge: string
inbox: string
'sequencer-inbox': string
'deployed-at': number
rollup: string
'native-token': string
'upgrade-executor': string
'validator-wallet-creator': string
}
interface ChainInfo {
'chain-name': string
'parent-chain-id': number
'parent-chain-is-arbitrum': boolean
'chain-config': any
rollup: RollupCreationResult
'sequencer-url': string
'secondary-forwarding-target': string
'feed-url': string
'secondary-feed-url': string
'das-index-url': string
'has-genesis-state': boolean
}
export async function createRollup(
signer: Signer,
isDevDeployment: boolean,
rollupCreatorAddress: string,
feeToken: string,
stakeToken: string
): Promise<{
rollupCreationResult: RollupCreationResult
chainInfo: ChainInfo
} | null> {
if (!rollupCreatorAbi) {
throw new Error(
'You need to first run <deployment.ts> script to deploy and compile the contracts first'
)
}
const rollupCreator = new ethers.Contract(
rollupCreatorAddress,
rollupCreatorAbi,
signer
)
const validatorWalletCreator = await rollupCreator.validatorWalletCreator()
try {
//// funds for deploying L2 factories
// 0.13 ETH is enough to deploy L2 factories via retryables. Excess is refunded
let feeCost = ethers.utils.parseEther('0.13')
if (feeToken != ethers.constants.AddressZero) {
// in case fees are paid via fee token, then approve rollup cretor to spend required amount
feeCost = await _getPrescaledAmount(
ERC20__factory.connect(feeToken, signer),
feeCost
)
await (
await IERC20__factory.connect(feeToken, signer).approve(
rollupCreator.address,
feeCost
)
).wait()
feeCost = BigNumber.from(0)
}
// Call the createRollup function
console.log('Calling createRollup to generate a new rollup ...')
const deployParams = isDevDeployment
? await _getDevRollupConfig(feeToken, validatorWalletCreator, stakeToken)
: {
config: config.rollupConfig,
validators: config.validators,
maxDataSize: ethers.BigNumber.from(maxDataSize),
nativeToken: feeToken,
deployFactoriesToL2: true,
maxFeePerGasForRetryables: MAX_FER_PER_GAS,
batchPosters: config.batchPosters,
batchPosterManager: config.batchPosterManager,
}
const createRollupTx = await rollupCreator.createRollup(deployParams, {
value: feeCost,
})
const createRollupReceipt = await createRollupTx.wait()
const rollupCreatedEvent = createRollupReceipt.events?.find(
(event: RollupCreatedEvent) =>
event.event === 'RollupCreated' &&
event.address.toLowerCase() === rollupCreatorAddress.toLowerCase()
)
// Checking for RollupCreated event for new rollup address
if (rollupCreatedEvent) {
const rollupAddress = rollupCreatedEvent.args?.rollupAddress
const nativeToken = rollupCreatedEvent.args?.nativeToken
const inboxAddress = rollupCreatedEvent.args?.inboxAddress
const outbox = rollupCreatedEvent.args?.outbox
const rollupEventInbox = rollupCreatedEvent.args?.rollupEventInbox
const challengeManager = rollupCreatedEvent.args?.challengeManager
const adminProxy = rollupCreatedEvent.args?.adminProxy
const sequencerInbox = rollupCreatedEvent.args?.sequencerInbox
const bridge = rollupCreatedEvent.args?.bridge
const upgradeExecutor = rollupCreatedEvent.args?.upgradeExecutor
const validatorWalletCreator =
rollupCreatedEvent.args?.validatorWalletCreator
console.log("Congratulations! 🎉🎉🎉 All DONE! Here's your addresses:")
console.log('RollupProxy Contract created at address:', rollupAddress)
if (!isDevDeployment) {
console.log('Wait a minute before starting the contract verification')
await sleep(1 * 60 * 1000)
console.log(
`Attempting to verify Rollup contract at address ${rollupAddress}...`
)
await verifyContract(
'RollupProxy',
rollupAddress,
[],
'src/rollup/RollupProxy.sol:RollupProxy'
)
}
console.log('Inbox (proxy) Contract created at address:', inboxAddress)
console.log('Outbox (proxy) Contract created at address:', outbox)
console.log(
'rollupEventInbox (proxy) Contract created at address:',
rollupEventInbox
)
console.log(
'challengeManager (proxy) Contract created at address:',
challengeManager
)
console.log('AdminProxy Contract created at address:', adminProxy)
console.log('SequencerInbox (proxy) created at address:', sequencerInbox)
console.log('Bridge (proxy) Contract created at address:', bridge)
console.log(
'ValidatorWalletCreator Contract created at address:',
validatorWalletCreator
)
const blockNumber = createRollupReceipt.blockNumber
console.log('All deployed at block number:', blockNumber)
const rollupCreationResult: RollupCreationResult = {
bridge: bridge,
inbox: inboxAddress,
'sequencer-inbox': sequencerInbox,
'deployed-at': blockNumber,
rollup: rollupAddress,
'native-token': nativeToken,
'upgrade-executor': upgradeExecutor,
'validator-wallet-creator': validatorWalletCreator,
}
const chainInfo: ChainInfo = {
'chain-name': 'dev-chain',
'parent-chain-id': +process.env.PARENT_CHAIN_ID!,
'parent-chain-is-arbitrum': await _isRunningOnArbitrum(signer),
'sequencer-url': '',
'secondary-forwarding-target': '',
'feed-url': '',
'secondary-feed-url': '',
'das-index-url': '',
'has-genesis-state': false,
'chain-config': JSON.parse(deployParams.config.chainConfig),
rollup: rollupCreationResult,
}
return { rollupCreationResult, chainInfo }
} else {
console.error('RollupCreated event not found')
}
} catch (error) {
console.error(
'Deployment failed:',
error instanceof Error ? error.message : error
)
}
return null
}
async function _getDevRollupConfig(
feeToken: string,
validatorWalletCreator: string,
stakeToken: string
) {
// set up owner address
const ownerAddress =
process.env.OWNER_ADDRESS !== undefined ? process.env.OWNER_ADDRESS : ''
// set up max data size
const _maxDataSize =
process.env.MAX_DATA_SIZE !== undefined
? ethers.BigNumber.from(process.env.MAX_DATA_SIZE)
: ethers.BigNumber.from(117964)
// set up validators
const authorizeValidators: number =
parseInt(process.env.AUTHORIZE_VALIDATORS as string, 0) || 0
const validators: string[] = []
for (let i = 1; i <= authorizeValidators; i++) {
validators.push(_createValidatorAddress(validatorWalletCreator, i))
}
// get chain config
const childChainConfigPath =
process.env.CHILD_CHAIN_CONFIG_PATH !== undefined
? process.env.CHILD_CHAIN_CONFIG_PATH
: 'l2_chain_config.json'
const chainConfig = await fs.readFile(childChainConfigPath, {
encoding: 'utf8',
})
// get wasmModuleRoot
const wasmModuleRoot =
process.env.WASM_MODULE_ROOT !== undefined
? process.env.WASM_MODULE_ROOT
: ''
// set up batch posters
const sequencerAddress =
process.env.SEQUENCER_ADDRESS !== undefined
? process.env.SEQUENCER_ADDRESS
: ''
const batchPostersString =
process.env.BATCH_POSTERS !== undefined ? process.env.BATCH_POSTERS : ''
let batchPosters: string[] = []
if (batchPostersString.length == 0) {
batchPosters.push(sequencerAddress)
} else {
const batchPostesArr = batchPostersString.split(',')
for (let i = 0; i < batchPostesArr.length; i++) {
if (ethers.utils.isAddress(batchPostesArr[i])) {
batchPosters.push(batchPostesArr[i])
} else {
throw new Error('Invalid address in batch posters array')
}
}
}
// set up batch poster manager
const batchPosterManagerEnv =
process.env.BATCH_POSTER_MANAGER !== undefined
? process.env.BATCH_POSTER_MANAGER
: ''
let batchPosterManager = ''
if (ethers.utils.isAddress(batchPosterManagerEnv)) {
batchPosterManager = batchPosterManagerEnv
} else {
if (batchPosterManagerEnv.length == 0) {
batchPosterManager = ownerAddress
} else {
throw new Error('Invalid address for batch poster manager')
}
}
return {
config: {
confirmPeriodBlocks: ethers.BigNumber.from('20'),
extraChallengeTimeBlocks: ethers.BigNumber.from('200'),
stakeToken: stakeToken,
baseStake: ethers.utils.parseEther('1'),
wasmModuleRoot: wasmModuleRoot,
owner: ownerAddress,
loserStakeEscrow: ethers.constants.AddressZero,
chainId: JSON.parse(chainConfig)['chainId'],
chainConfig: chainConfig,
minimumAssertionPeriod: 75,
validatorAfkBlocks: 201600,
genesisAssertionState: {}, // AssertionState
genesisInboxCount: 0,
miniStakeValues: [
ethers.utils.parseEther('1'),
ethers.utils.parseEther('1'),
ethers.utils.parseEther('1'),
],
layerZeroBlockEdgeHeight: 2 ** 5,
layerZeroBigStepEdgeHeight: 2 ** 5,
layerZeroSmallStepEdgeHeight: 2 ** 5,
numBigStepLevel: 1,
challengeGracePeriodBlocks: 10,
bufferConfig: { threshold: 600, max: 14400, replenishRateInBasis: 500 },
sequencerInboxMaxTimeVariation: {
delayBlocks: ethers.BigNumber.from('5760'),
futureBlocks: ethers.BigNumber.from('12'),
delaySeconds: ethers.BigNumber.from('86400'),
futureSeconds: ethers.BigNumber.from('3600'),
},
},
validators: validators,
maxDataSize: _maxDataSize,
nativeToken: feeToken,
deployFactoriesToL2: true,
maxFeePerGasForRetryables: MAX_FER_PER_GAS,
batchPosters: batchPosters,
batchPosterManager: batchPosterManager,
}
function _createValidatorAddress(
deployerAddress: string,
nonce: number
): string {
const nonceHex = BigNumber.from(nonce).toHexString()
return ethers.utils.getContractAddress({
from: deployerAddress,
nonce: nonceHex,
})
}
}
async function _getPrescaledAmount(
nativeToken: ERC20,
amount: BigNumber
): Promise<BigNumber> {
const decimals = BigNumber.from(await nativeToken.decimals())
if (decimals.lt(BigNumber.from(18))) {
const scalingFactor = BigNumber.from(10).pow(
BigNumber.from(18).sub(decimals)
)
let prescaledAmount = amount.div(scalingFactor)
// round up if needed
if (prescaledAmount.mul(scalingFactor).lt(amount)) {
prescaledAmount = prescaledAmount.add(BigNumber.from(1))
}
return prescaledAmount
} else if (decimals.gt(BigNumber.from(18))) {
return amount.mul(BigNumber.from(10).pow(decimals.sub(BigNumber.from(18))))
}
return amount
}