This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitForInclusion.js
60 lines (51 loc) · 2.22 KB
/
waitForInclusion.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
const { Provider, utils } = require('zksync-web3')
const wait = require('wait')
require('dotenv').config()
async function main() {
const l2ContractAddress = process.env.L2_CONTRACT
const l2TransactionHash = process.env.L2_TX_HASH
const greeting = process.env.GREETING
const l1Provider = new ethers.providers.StaticJsonRpcProvider('https://goerli.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161')
const zkSyncProvider = new Provider('https://zksync2-testnet.zksync.dev')
const l1ContractAbi = require('../artifacts-zk/contracts/L1Contract.sol/L1Contract.json').abi
const iface = new ethers.utils.Interface(l1ContractAbi)
const message = iface.encodeFunctionData('setGreeting', [greeting])
const messageHash = ethers.utils.keccak256(message)
console.log('Waiting for L1 block inclusion (this may take up to 1 hour)...')
while (true) {
const { l1BatchNumber, l1BatchTxIndex, blockNumber } = await zkSyncProvider.getTransactionReceipt(l2TransactionHash)
if (l1BatchNumber) {
const zkAddress = await zkSyncProvider.getMainContractAddress()
const sender = l2ContractAddress
const proofInfo = await zkSyncProvider.getMessageProof(blockNumber, sender, messageHash)
if (!proofInfo) {
throw new Error('No proof found')
}
const index = proofInfo.id
const proof = proofInfo.proof
const mailboxL1Contract = new ethers.Contract(zkAddress, utils.ZKSYNC_MAIN_ABI, l1Provider)
// all the information of the message sent from L2
const messageInfo = {
txNumberInBlock: l1BatchTxIndex,
sender,
data: message
}
try {
const result = await mailboxL1Contract.proveL2MessageInclusion(l1BatchNumber, index, messageInfo, proof)
console.log('L2 block:', blockNumber)
console.log('L1 Index for Tx in block:', l1BatchTxIndex)
console.log('L1 Batch for block: ', l1BatchNumber)
console.log('Inclusion proof:', proof)
console.log('proveL2MessageInclusion:', result)
break
} catch (err) {}
}
await wait(5 * 1000)
}
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error)
process.exitCode = 1
})