forked from OffchainLabs/arbitrum-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initiate-eth-deposit.ts
92 lines (82 loc) · 2.85 KB
/
initiate-eth-deposit.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
import { providers, Wallet, BigNumber } from "ethers"
import { arbLog, requireEnvVariables } from 'arb-shared-dependencies'
import { Erc20L1L3Bridger, EthL1L3Bridger, getL2Network } from "godzillaba-arbitrum-sdk"
import { ERC20__factory } from "godzillaba-arbitrum-sdk/dist/lib/abi/factories/ERC20__factory"
import yargs from 'yargs/yargs'
import { hideBin } from "yargs/helpers"
// Importing configuration //
require('dotenv').config()
requireEnvVariables(['L1RPC', 'L2RPC', 'L3RPC', 'DEVNET_PRIVKEY'])
// Initial setup //
const l1Provider = new providers.JsonRpcProvider(process.env.L1RPC)
const l2Provider = new providers.JsonRpcProvider(process.env.L2RPC)
const l3Provider = new providers.JsonRpcProvider(process.env.L3RPC)
const l1Signer = new Wallet(process.env.DEVNET_PRIVKEY!, l1Provider)
const main = async (params: {
amount: BigNumber, l3Recipient?: string
}) => {
await arbLog(`Bridging ETH from L1 to L3`)
/**
* Use L3 Network to initialize a bridger
*/
const l3Network = await getL2Network(l3Provider)
const bridger = new EthL1L3Bridger(l3Network)
/**
* Information about the configuration of the bridger
*/
const l1ChainId = (await l1Provider.getNetwork()).chainId
const l2ChainId = (await l2Provider.getNetwork()).chainId
const l3ChainId = (await l3Provider.getNetwork()).chainId
const signerAddress = await l1Signer.getAddress()
console.log('Recipient:', params.l3Recipient || signerAddress)
console.log('Amount:', params.amount.toString())
console.log('L1 chain id:', l1ChainId)
console.log('L2 chain id:', l2ChainId)
console.log('L3 chain id:', l3ChainId)
/**
* Get a deposit request
*/
console.log('Getting deposit request...')
const depositRequest = await bridger.getDepositRequest({
l1Signer,
amount: params.amount,
l2Provider,
l3Provider,
to: params.l3Recipient, // optional, defaults to signer's address
})
console.log('Done')
/**
* Deposit ETH
*/
console.log('Depositing ETH...')
const depositTx = await bridger.deposit({
...depositRequest,
l1Signer
})
await depositTx.wait()
console.log('Done')
console.log('Initiated deposit! An L1 to L2 retryable will be created, which will create an L2 to L3 retryable to deposit ETH.')
console.log('Transaction hash:', depositTx.hash)
console.log(`To monitor your deposit, use:\n\nyarn monitor-eth-deposit-status ${depositTx.hash}`)
};
const args = yargs(hideBin(process.argv))
.options({
amount: {
type: 'number',
description: 'Amount of ETH to bridge',
demandOption: true
},
l3Recipient: {
type: 'string',
description: 'L3 recipient address'
}
})
.usage('Initiate a deposit of ETH from L1 to L3')
.parseSync()
main({
amount: BigNumber.from(args.amount),
l3Recipient: args.l3Recipient,
}).then(() => process.exit(0)).catch(error => {
console.error(error)
process.exit(1)
})