forked from OffchainLabs/arbitrum-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.js
72 lines (61 loc) · 2.81 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
const { providers, Wallet } = require('ethers')
const { L2TransactionReceipt, getL2Network } = require('@arbitrum/sdk')
const { arbLog, requireEnvVariables } = require('arb-shared-dependencies')
require('dotenv').config()
requireEnvVariables(['DEVNET_PRIVKEY', 'L2RPC', 'L1RPC'])
/**
* Set up: instantiate L1 wallet connected to provider
*/
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)
module.exports = async txnHash => {
await arbLog('Outbox Execution')
/**
/ * We start with a txn hash; we assume this is transaction that triggered an L2 to L1 Message on L2 (i.e., ArbSys.sendTxToL1)
*/
if (!txnHash)
throw new Error(
'Provide a transaction hash of an L2 transaction that sends an L2 to L1 message'
)
if (!txnHash.startsWith('0x') || txnHash.trim().length != 66)
throw new Error(`Hmm, ${txnHash} doesn't look like a txn hash...`)
/**
* First, let's find the Arbitrum txn from the txn hash provided
*/
const receipt = await l2Provider.getTransactionReceipt(txnHash)
const l2Receipt = new L2TransactionReceipt(receipt)
/**
* Note that in principle, a single transaction could trigger any number of outgoing messages; the common case will be there's only one.
* For the sake of this script, we assume there's only one / just grad the first one.
*/
const l2Network = await getL2Network(l2Provider)
const messages = await l2Receipt.getL2ToL1Messages(l1Wallet, l2Network)
const l2ToL1Msg = messages[0]
/**
* before we try to execute out message, we need to make sure the l2 block it's included in is confirmed! (It can only be confirmed after the dispute period; Arbitrum is an optimistic rollup after-all)
* waitUntilOutboxEntryCreated() waits until the item outbox entry exists
*/
const timeToWaitMs = 1000 * 60
console.log("Waiting for the outbox entry to be created. This only happens when the L2 block is confirmed on L1, ~1 week after it's creation.")
await l2ToL1Msg.waitUntilOutboxEntryCreated(timeToWaitMs)
console.log('Outbox entry exists! Trying to execute now')
/**
* Now fetch the proof info we'll need in order to execute, or check execution
*/
const proofInfo = await l2ToL1Msg.tryGetProof(l2Provider)
/**
* Execute if not alredy executed
*/
if(await l2ToL1Msg.hasExecuted(proofInfo)) {
console.log(`Message already executed! Nothing else to do here`)
process.exit(1)
}
/**
* Now that its confirmed and not executed, we can use the Merkle proof data to execute our message in its outbox entry.
*/
const res = await l2ToL1Msg.execute(proofInfo)
const rec = await res.wait()
console.log('Done! Your transaction is executed', rec)
}