forked from OffchainLabs/arbitrum-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.js
114 lines (94 loc) · 3.85 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
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
const { BigNumber, providers, Wallet } = require('ethers')
const { ethers } = require('hardhat')
const { Bridge } = require('arb-ts')
const { arbLog, requireEnvVariables } = require('arb-shared-dependencies')
require('dotenv').config()
requireEnvVariables(['DEVNET_PRIVKEY', 'L2RPC', 'L1RPC'])
/**
* Set up: instantiate L1 / L2 wallets connected to providers
*/
const walletPrivateKey = process.env.DEVNET_PRIVKEY
const l1Provider = new providers.JsonRpcProvider(process.env.L1RPC)
const l2Provider = new providers.JsonRpcProvider(process.env.L2RPC)
const l1Wallet = new Wallet(walletPrivateKey, l1Provider)
const l2Wallet = new Wallet(walletPrivateKey, l2Provider)
/**
* Set the amount of token to be deposited and then withdrawn
*/
const tokenDepositAmount = BigNumber.from(50)
const tokenWithdrawAmount = BigNumber.from(20)
const main = async () => {
await arbLog('Withdraw token')
/**
* Use wallets to create an arb-ts bridge instance
*/
const bridge = await Bridge.init(l1Wallet, l2Wallet)
/**
* Setup: we'll deploy a token contract, then approve and deposit onto L2 so we have some coin to withdraw
* (For play-by-play details on the deposit flow, see token_deposit repo)
*/
console.log('Deploying token:')
const L1DappToken = await (
await ethers.getContractFactory('DappToken')
).connect(l1Wallet)
const l1DappToken = await L1DappToken.deploy(1000000000000000)
await l1DappToken.deployed()
console.log('Deployed! Approving:')
const erc20Address = l1DappToken.address
const approveTx = await bridge.approveToken(erc20Address)
await approveTx.wait()
console.log('Approved! Depositing:')
const param = {
erc20L1Address: erc20Address,
amount: tokenDepositAmount,
destinationAddress: l1Wallet.address
}
const depositTx =await bridge.deposit(param)
const depositRec = await depositTx.wait()
console.log(
`Deposit initiated: waiting for L2 retryable (takes < 10 minutes; current time: ${new Date().toTimeString()}) `
)
const seqNumArr = await bridge.getInboxSeqNumFromContractTransaction(
depositRec
)
const seqNum = seqNumArr[0]
const l2TxHash = await bridge.calculateL2RetryableTransactionHash(seqNum)
//Now, we have to wait for the L2 tx to go through
await l2Provider.waitForTransaction(l2TxHash, undefined, 1000 * 60 * 12)
const l2ERC20Address = await bridge.getERC20L2Address(erc20Address)
const l2Data = await bridge.l2Bridge.getL2TokenData(l2ERC20Address)
const l2WalletTokenBalance = l2Data.balance
console.log(
`Setup complete: your l2Wallet has ${l2WalletTokenBalance.toString()} DappToken now!`
)
console.log('Withdrawing:')
/**
* ... Okay, Now we begin withdrawing DappToken from L2. To withdraw, we'll use the arb-ts helper method withdrawERC20
* withdrawERC20 will call our L2 Gateway Router to initiate a withdrawal via the Standard ERC20 gateway
* This transaction is constructed and paid for like any other L2 transaction (it just happens to (ultimately) make a call to ArbSys.sendTxToL1)
*/
const withdrawTx = await bridge.withdrawERC20(
erc20Address,
tokenWithdrawAmount
)
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:
*/
const withdrawEventData = (
await bridge.getWithdrawalsInL2Transaction(withdrawRec)
)[0]
console.log(`Token withdrawal initiated! 🥳 ${withdrawRec.transactionHash}`)
console.log('Withdrawal data:', withdrawEventData)
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)
})