forked from OffchainLabs/arbitrum-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exec.js
86 lines (73 loc) · 2.69 KB
/
exec.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
const { utils, providers, Wallet } = require('ethers')
const {
EthBridger,
getL2Network,
addDefaultLocalNetwork,
L2ToL1Message,
} = require('@arbitrum/sdk')
const { parseEther } = utils
const { arbLog, requireEnvVariables } = require('arb-shared-dependencies')
require('dotenv').config()
requireEnvVariables(['DEVNET_PRIVKEY', 'L2RPC', 'L1RPC'])
/**
* Set up: instantiate L2 wallet connected to provider
*/
const walletPrivateKey = process.env.DEVNET_PRIVKEY
const l2Provider = new providers.JsonRpcProvider(process.env.L2RPC)
const l2Wallet = new Wallet(walletPrivateKey, l2Provider)
/**
* Set the amount to be withdrawn from L2 (in wei)
*/
const ethFromL2WithdrawAmount = parseEther('0.000001')
const main = async () => {
await arbLog('Withdraw Eth via Arbitrum SDK')
/**
* Add the default local network configuration to the SDK
* to allow this script to run on a local node
*/
addDefaultLocalNetwork()
/**
* Use l2Network to create an Arbitrum SDK EthBridger instance
* We'll use EthBridger for its convenience methods around transferring ETH from L2 to L1
*/
const l2Network = await getL2Network(l2Provider)
const ethBridger = new EthBridger(l2Network)
/**
* First, let's check our L2 wallet's initial ETH balance and ensure there's some ETH to withdraw
*/
const l2WalletInitialEthBalance = await l2Wallet.getBalance()
if (l2WalletInitialEthBalance.lt(ethFromL2WithdrawAmount)) {
console.log(
`Oops - not enough ether; fund your account L2 wallet currently ${l2Wallet.address} with at least 0.000001 ether`
)
process.exit(1)
}
console.log('Wallet properly funded: initiating withdrawal now')
/**
* We're ready to withdraw ETH using the ethBridger instance from Arbitrum SDK
* It will use our current wallet's address as the default destination
*/
const withdrawTx = await ethBridger.withdraw({
amount: ethFromL2WithdrawAmount,
l2Signer: l2Wallet,
destinationAddress: l2Wallet.address,
})
const withdrawRec = await withdrawTx.wait()
/**
* And with that, our withdrawal is initiated! No additional time-sensitive actions are required.
* Any time after the transaction's assertion is confirmed, funds can be transferred out of the bridge via the outbox contract
* We'll display the withdrawals event data here:
*/
console.log(`Ether withdrawal initiated! 🥳 ${withdrawRec.transactionHash}`)
const withdrawEventsData = await withdrawRec.getL2ToL1Events()
console.log('Withdrawal data:', withdrawEventsData)
console.log(
`To to claim funds (after dispute period), see outbox-execute repo 🫡`
)
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})