-
Notifications
You must be signed in to change notification settings - Fork 100
/
index.js
executable file
Β·556 lines (520 loc) Β· 15.7 KB
/
index.js
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
546
547
548
549
550
551
552
553
554
555
556
#!/usr/bin/env node
const path = require('path')
const os = require('os')
const { program } = require('commander')
const { CleanCommand } = require('./commands/clean')
const { PrepareCommand } = require('./commands/prepare')
const { StatusCommand } = require('./commands/status')
const {
StartEth2NearRelayCommand,
} = require('./commands/start/eth2near-relay.js')
const {
StartNear2EthRelayCommand,
} = require('./commands/start/near2eth-relay.js')
const { StartWatchdogCommand } = require('./commands/start/watchdog.js')
const { StartGanacheNodeCommand } = require('./commands/start/ganache.js')
const { StartLocalNearNodeCommand } = require('./commands/start/near.js')
const { StopManagedProcessCommand } = require('./commands/stop/process.js')
const {
DangerSubmitInvalidNearBlock,
} = require('./commands/danger-submit-invalid-near-block')
const {
TransferETHERC20ToNear,
TransferEthERC20FromNear,
} = require('rainbow-bridge-lib/transfer-eth-erc20')
const { ETHDump } = require('./commands/eth-dump')
const { NearDump } = require('rainbow-bridge-lib/rainbow/near-dump')
const { RainbowConfig } = require('rainbow-bridge-lib/config')
const {
InitNearContracts,
InitNearFunToken,
InitEthEd25519,
InitEthErc20,
InitEthLocker,
InitEthClient,
InitEthProver,
} = require('rainbow-bridge-lib/init')
// source dir or where rainbow cli is installed (when install with npm)
const BRIDGE_SRC_DIR = __dirname
const LIBS_SOL_SRC_DIR = path.join(
BRIDGE_SRC_DIR,
'node_modules/rainbow-bridge-sol'
)
const LIBS_RS_SRC_DIR = path.join(
BRIDGE_SRC_DIR,
'node_modules/rainbow-bridge-rs'
)
RainbowConfig.declareOption(
'near-network-id',
'The identifier of the NEAR network that the given NEAR node is expected to represent.'
)
RainbowConfig.declareOption('near-node-url', 'The URL of the NEAR node.')
RainbowConfig.declareOption('eth-node-url', 'The URL of the Ethereum node.')
RainbowConfig.declareOption(
'near-master-account',
'The account of the master account on NEAR blockchain that can be used to deploy and initialize the test contracts.' +
' This account will also own the initial supply of the fungible tokens.'
)
RainbowConfig.declareOption(
'near-master-sk',
'The secret key of the master account on NEAR blockchain.'
)
RainbowConfig.declareOption(
'eth-master-sk',
'The secret key of the master account on Ethereum blockchain.'
)
RainbowConfig.declareOption(
'near-client-account',
'The account of the Near Client contract that can be used to accept ETH headers.',
'rainbow_bridge_eth_on_near_client'
)
RainbowConfig.declareOption(
'near-client-sk',
'The secret key of the Near Client account. If not specified will use master SK.'
)
RainbowConfig.declareOption(
'near-client-contract-path',
'The path to the Wasm file containing the Near Client contract.',
path.join(LIBS_RS_SRC_DIR, 'res/eth_client.wasm')
)
RainbowConfig.declareOption(
'near-client-init-balance',
'The initial balance of Near Client contract in femtoNEAR.',
'100000000000000000000000000'
)
RainbowConfig.declareOption(
'near-client-validate-ethash',
'The initial balance of Near Client contract in femtoNEAR.',
'true'
)
RainbowConfig.declareOption(
'near-prover-account',
'The account of the Near Prover contract that can be used to accept ETH headers.',
'rainbow_bridge_eth_on_near_prover'
)
RainbowConfig.declareOption(
'near-prover-sk',
'The secret key of the Near Prover account. If not specified will use master SK.'
)
RainbowConfig.declareOption(
'near-prover-contract-path',
'The path to the Wasm file containing the Near Prover contract.',
path.join(LIBS_RS_SRC_DIR, 'res/eth_prover.wasm')
)
RainbowConfig.declareOption(
'near-prover-init-balance',
'The initial balance of Near Prover contract in femtoNEAR.',
'100000000000000000000000000'
)
RainbowConfig.declareOption(
'daemon',
'Whether the process should be launched as a daemon.',
'true',
true
)
RainbowConfig.declareOption(
'core-src',
'Path to the nearcore source. It will be downloaded if not provided.',
''
)
RainbowConfig.declareOption(
'nearup-src',
'Path to the nearup source. It will be downloaded if not provided.',
''
)
RainbowConfig.declareOption(
'eth-gas-multiplier',
'How many times more in Ethereum gas are we willing to overpay.',
'1'
)
// User-specific arguments.
RainbowConfig.declareOption(
'near-fun-token-account',
'The account of the fungible token contract that will be used to mint tokens locked on Ethereum.',
'nearfuntoken'
)
RainbowConfig.declareOption(
'near-fun-token-sk',
'The secret key of the fungible token account. If not specified will use master SK.'
)
RainbowConfig.declareOption(
'near-fun-token-contract-path',
'The path to the Wasm file containing the fungible contract. Note, this version of fungible contract should support minting.',
path.join(LIBS_RS_SRC_DIR, 'res/mintable_fungible_token.wasm')
)
RainbowConfig.declareOption(
'near-fun-token-init-balance',
'The initial balance of fungible token contract in femtoNEAR.',
'100000000000000000000000000'
)
RainbowConfig.declareOption(
'eth-locker-address',
'ETH address of the locker contract.'
)
RainbowConfig.declareOption(
'eth-locker-abi-path',
'Path to the .abi file defining Ethereum locker contract. This contract works in pair with mintable fungible token on NEAR blockchain.',
path.join(LIBS_SOL_SRC_DIR, 'token-locker/dist/TokenLocker.full.abi')
)
RainbowConfig.declareOption(
'eth-locker-bin-path',
'Path to the .bin file defining Ethereum locker contract. This contract works in pair with mintable fungible token on NEAR blockchain.',
path.join(LIBS_SOL_SRC_DIR, 'token-locker/dist/TokenLocker.full.bin')
)
RainbowConfig.declareOption(
'eth-erc20-address',
'ETH address of the ERC20 contract.'
)
RainbowConfig.declareOption(
'eth-erc20-abi-path',
'Path to the .abi file defining Ethereum ERC20 contract.',
path.join(LIBS_SOL_SRC_DIR, 'token-locker/dist/MyERC20.full.abi')
)
RainbowConfig.declareOption(
'eth-erc20-bin-path',
'Path to the .bin file defining Ethereum ERC20 contract.',
path.join(LIBS_SOL_SRC_DIR, 'token-locker/dist/MyERC20.full.bin')
)
RainbowConfig.declareOption(
'eth-ed25519-address',
'ETH address of the ED25519 contract.'
)
RainbowConfig.declareOption(
'eth-ed25519-abi-path',
'Path to the .abi file defining Ethereum ED25519 contract.',
path.join(LIBS_SOL_SRC_DIR, 'nearbridge/dist/Ed25519.full.abi')
)
RainbowConfig.declareOption(
'eth-ed25519-bin-path',
'Path to the .bin file defining Ethereum ED25519 contract.',
path.join(LIBS_SOL_SRC_DIR, 'nearbridge/dist/Ed25519.full.bin')
)
RainbowConfig.declareOption(
'eth-client-lock-eth-amount',
'Amount of Ether that should be temporarily locked when submitting a new header to EthClient, in wei.',
1e20
)
RainbowConfig.declareOption(
'eth-client-lock-duration',
'The challenge window during which anyone can challenge an incorrect ED25519 signature of the Near block, in EthClient, in seconds.',
14400
)
RainbowConfig.declareOption(
'eth-client-address',
'ETH address of the EthClient contract.'
)
RainbowConfig.declareOption(
'eth-client-abi-path',
'Path to the .abi file defining Ethereum Client contract.',
path.join(LIBS_SOL_SRC_DIR, 'nearbridge/dist/NearBridge.full.abi')
)
RainbowConfig.declareOption(
'eth-client-bin-path',
'Path to the .bin file defining Ethereum Client contract.',
path.join(LIBS_SOL_SRC_DIR, 'nearbridge/dist/NearBridge.full.bin')
)
RainbowConfig.declareOption(
'eth-prover-address',
'ETH address of the EthProver contract.'
)
RainbowConfig.declareOption(
'eth-prover-abi-path',
'Path to the .abi file defining Ethereum Prover contract.',
path.join(LIBS_SOL_SRC_DIR, 'nearprover/dist/NearProver.full.abi')
)
RainbowConfig.declareOption(
'eth-prover-bin-path',
'Path to the .bin file defining Ethereum Prover contract.',
path.join(LIBS_SOL_SRC_DIR, 'nearprover/dist/NearProver.full.bin')
)
RainbowConfig.declareOption(
'near2eth-relay-delay',
'How many seconds should we wait after the NEAR header becomes valid before we submit the next one.',
'0'
)
program.version('0.1.0')
// General-purpose commands.
program.command('clean').action(CleanCommand.execute)
RainbowConfig.addOptions(
program.command('prepare').action(PrepareCommand.execute),
['core-src', 'nearup-src']
)
program.command('status').action(StatusCommand.execute)
// Maintainer commands.
const startCommand = program.command('start')
startCommand.command('near-node').action(StartLocalNearNodeCommand.execute)
RainbowConfig.addOptions(
startCommand.command('ganache').action(StartGanacheNodeCommand.execute),
['daemon']
)
RainbowConfig.addOptions(
startCommand
.command('eth2near-relay')
.action(StartEth2NearRelayCommand.execute),
[
'near-master-account',
'near-master-sk',
'near-client-account',
'near-network-id',
'near-node-url',
'daemon',
]
)
RainbowConfig.addOptions(
startCommand
.command('near2eth-relay')
.action(StartNear2EthRelayCommand.execute),
[
'eth-node-url',
'eth-master-sk',
'near-node-url',
'near-network-id',
'eth-client-abi-path',
'eth-client-address',
'near2eth-relay-delay',
'eth-gas-multiplier',
'daemon',
]
)
RainbowConfig.addOptions(
startCommand.command('bridge-watchdog').action(StartWatchdogCommand.execute),
['eth-node-url', 'eth-master-sk', 'eth-client-abi-path', 'daemon']
)
const stopCommand = program.command('stop')
stopCommand.command('all').action(StopManagedProcessCommand.execute)
stopCommand.command('near-node').action(StopManagedProcessCommand.execute)
stopCommand.command('ganache').action(StopManagedProcessCommand.execute)
stopCommand.command('eth2near-relay').action(StopManagedProcessCommand.execute)
stopCommand.command('near2eth-relay').action(StopManagedProcessCommand.execute)
stopCommand.command('bridge-watchdog').action(StopManagedProcessCommand.execute)
RainbowConfig.addOptions(
program
.command('init-near-contracts')
.description(
'Deploys and initializes Near Client and Near Prover contracts to NEAR blockchain.'
)
.action(InitNearContracts.execute),
[
'near-network-id',
'near-node-url',
'eth-node-url',
'near-master-account',
'near-master-sk',
'near-client-account',
'near-client-sk',
'near-client-contract-path',
'near-client-init-balance',
'near-client-validate-ethash',
'near-prover-account',
'near-prover-sk',
'near-prover-contract-path',
'near-prover-init-balance',
]
)
RainbowConfig.addOptions(
program
.command('init-eth-ed25519')
.description(
'Deploys and initializes ED25519 Solidity contract. It replaces missing precompile.'
)
.action(InitEthEd25519.execute),
[
'eth-node-url',
'eth-master-sk',
'eth-ed25519-abi-path',
'eth-ed25519-bin-path',
'eth-gas-multiplier',
]
)
RainbowConfig.addOptions(
program
.command('init-eth-client')
.description('Deploys and initializes EthClient.')
.action(InitEthClient.execute),
[
'eth-node-url',
'eth-master-sk',
'eth-client-abi-path',
'eth-client-bin-path',
'eth-ed25519-address',
'eth-client-lock-eth-amount',
'eth-client-lock-duration',
'eth-gas-multiplier',
]
)
RainbowConfig.addOptions(
program
.command('init-eth-prover')
.description('Deploys and initializes EthProver.')
.action(InitEthProver.execute),
[
'eth-node-url',
'eth-master-sk',
'eth-prover-abi-path',
'eth-prover-bin-path',
'eth-client-address',
'eth-gas-multiplier',
]
)
// User commands.
RainbowConfig.addOptions(
program
.command('init-near-fun-token')
.description(
'Deploys and initializes mintable fungible token to NEAR blockchain. Requires locker on Ethereum side.'
)
.action(InitNearFunToken.execute),
[
'near-fun-token-account',
'near-fun-token-sk',
'near-fun-token-contract-path',
'near-fun-token-init-balance',
'eth-locker-address',
]
)
RainbowConfig.addOptions(
program
.command('init-eth-locker')
.description(
'Deploys and initializes locker contract on Ethereum blockchain. Requires mintable fungible token on Near side.'
)
.action(InitEthLocker.execute),
[
'eth-node-url',
'eth-master-sk',
'eth-locker-abi-path',
'eth-locker-bin-path',
'eth-erc20-address',
'near-fun-token-account',
'eth-prover-address',
'eth-gas-multiplier',
]
)
RainbowConfig.addOptions(
program
.command('init-eth-erc20')
.description(
'Deploys and initializes ERC20 contract on Ethereum blockchain.'
)
.action(InitEthErc20.execute),
[
'eth-node-url',
'eth-master-sk',
'eth-erc20-abi-path',
'eth-erc20-bin-path',
'eth-gas-multiplier',
]
)
RainbowConfig.addOptions(
program
.command('transfer-eth-erc20-to-near')
.action(TransferETHERC20ToNear.execute)
.option('--amount <amount>', 'Amount of ERC20 tokens to transfer')
.option(
'--eth-sender-sk <eth_sender_sk>',
'The secret key of the Ethereum account that will be sending ERC20 token.'
)
.option(
'--near-receiver-account <near_receiver_account>',
'The account on NEAR blockchain that will be receiving the minted token.'
),
[
'eth-node-url',
'eth-erc20-address',
'eth-erc20-abi-path',
'eth-locker-address',
'eth-locker-abi-path',
'near-node-url',
'near-network-id',
'near-fun-token-account',
'near-client-account',
'near-master-account',
'near-master-sk',
'eth-gas-multiplier',
]
)
RainbowConfig.addOptions(
program
.command('transfer-eth-erc20-from-near')
.action(TransferEthERC20FromNear.execute)
.option('--amount <amount>', 'Amount of ERC20 tokens to transfer')
.option(
'--near-sender-account <near_sender_account>',
'Near account that will be sending fungible token.'
)
.option(
'--near-sender-sk <near_sender_sk>',
'The secret key of Near account that will be sending the fungible token.'
)
.option(
'--eth-receiver-address <eth_receiver_address>',
'The account that will be receiving the token on Ethereum side.'
),
[
'near-node-url',
'near-network-id',
'near-fun-token-account',
'eth-node-url',
'eth-erc20-address',
'eth-erc20-abi-path',
'eth-locker-address',
'eth-locker-abi-path',
'eth-client-abi-path',
'eth-client-address',
'eth-master-sk',
'eth-prover-abi-path',
'eth-prover-address',
'eth-gas-multiplier',
]
)
// Testing command
const dangerCommand = program
.command('DANGER')
.description(
'Dangerous commands that should only be used for testing purpose.'
)
RainbowConfig.addOptions(
dangerCommand
.command('submit_invalid_near_block')
.description(
'Fetch latest near block, randomly mutate one byte and submit to NearBridge'
)
.action(DangerSubmitInvalidNearBlock.execute),
[
'eth-node-url',
'eth-master-sk',
'near-node-url',
'near-network-id',
'eth-client-abi-path',
'eth-client-address',
'near2eth-relay-delay',
'eth-gas-multiplier',
]
)
program
.command('eth-dump <kind_of_data>')
.option('--eth-node-url <eth_node_url>', 'ETH node API url')
.option('--path <path>', 'Dir path to dump eth data')
.option(
'--start-block <start_block>',
'Start block number (inclusive), default to be 4.3K blocks away from start block'
)
.option(
'--end-block <end_block>',
'End block number (inclusive), default to be latest block'
)
.action(ETHDump.execute)
RainbowConfig.addOptions(
program
.command('near-dump <kind_of_data>')
.option('--path <path>', 'Dir path to dump near data')
.option(
'--num-blocks <num_blocks>',
'Number of blocks to dump, default: 100'
)
.action(NearDump.execute),
['near-node-url']
)
;(async () => {
await program.parseAsync(process.argv)
})()