|
1 |
| -const { providers, Wallet, ethers } = require('ethers') |
2 |
| -const hre = require('hardhat') |
3 |
| -const { arbLog, requireEnvVariables } = require('arb-shared-dependencies') |
| 1 | +const { ethers } = require('hardhat') |
| 2 | +const { providers, Wallet } = require('ethers') |
4 | 3 | const {
|
5 |
| - getL2Network, |
6 |
| - addDefaultLocalNetwork, |
7 |
| -} = require('@arbitrum/sdk/dist/lib/dataEntities/networks') |
8 |
| -const { InboxTools } = require('@arbitrum/sdk') |
9 |
| -requireEnvVariables(['DEVNET_PRIVKEY', 'L2RPC', 'L1RPC']) |
| 4 | + arbLog, |
| 5 | + requireEnvVariables, |
| 6 | + addCustomNetworkFromFile, |
| 7 | +} = require('arb-shared-dependencies') |
| 8 | +const { getArbitrumNetwork, InboxTools } = require('@arbitrum/sdk') |
| 9 | +require('dotenv').config() |
| 10 | +requireEnvVariables(['PRIVATE_KEY', 'CHAIN_RPC', 'PARENT_CHAIN_RPC']) |
10 | 11 |
|
11 | 12 | /**
|
12 |
| - * Set up: instantiate L1 / L2 wallets connected to providers |
| 13 | + * Set up: instantiate wallets connected to providers |
13 | 14 | */
|
14 |
| -const walletPrivateKey = process.env.DEVNET_PRIVKEY |
| 15 | +const walletPrivateKey = process.env.PRIVATE_KEY |
15 | 16 |
|
16 |
| -const l1Provider = new providers.JsonRpcProvider(process.env.L1RPC) |
17 |
| -const l2Provider = new providers.JsonRpcProvider(process.env.L2RPC) |
| 17 | +const parentChainProvider = new providers.JsonRpcProvider( |
| 18 | + process.env.PARENT_CHAIN_RPC |
| 19 | +) |
| 20 | +const childChainProvider = new providers.JsonRpcProvider(process.env.CHAIN_RPC) |
18 | 21 |
|
19 |
| -const l1Wallet = new Wallet(walletPrivateKey, l1Provider) |
20 |
| -const l2Wallet = new Wallet(walletPrivateKey, l2Provider) |
| 22 | +const parentChainWallet = new Wallet(walletPrivateKey, parentChainProvider) |
| 23 | +const childChainWallet = new Wallet(walletPrivateKey, childChainProvider) |
21 | 24 |
|
22 | 25 | const main = async () => {
|
23 | 26 | await arbLog('DelayedInbox normal contract call (L2MSG_signedTx)')
|
24 | 27 |
|
25 | 28 | /**
|
26 |
| - * Add the default local network configuration to the SDK |
27 |
| - * to allow this script to run on a local node |
| 29 | + * Add the custom network configuration to the SDK if present |
28 | 30 | */
|
29 |
| - addDefaultLocalNetwork() |
30 |
| - |
31 |
| - const l2Network = await getL2Network(await l2Wallet.getChainId()) |
32 |
| - |
33 |
| - const inboxSdk = new InboxTools(l1Wallet, l2Network) |
| 31 | + addCustomNetworkFromFile() |
34 | 32 |
|
35 | 33 | /**
|
36 |
| - * We deploy greeter to L2, to see if delayed inbox tx can be executed as we thought |
| 34 | + * Use childChainNetwork to create an Arbitrum SDK InboxTools instance |
37 | 35 | */
|
38 |
| - const L2Greeter = await ( |
39 |
| - await hre.ethers.getContractFactory('Greeter') |
40 |
| - ).connect(l2Wallet) |
41 |
| - |
42 |
| - console.log('Deploying Greeter on L2 👋👋') |
43 |
| - |
44 |
| - const l2Greeter = await L2Greeter.deploy('Hello world') |
45 |
| - await l2Greeter.deployed() |
46 |
| - console.log(`deployed to ${l2Greeter.address}`) |
| 36 | + const childChainNetwork = await getArbitrumNetwork(childChainProvider) |
| 37 | + const inboxTools = new InboxTools(parentChainWallet, childChainNetwork) |
47 | 38 |
|
48 | 39 | /**
|
49 |
| - * Let's log the L2 greeting string |
| 40 | + * We deploy greeter to the child chain, to interact with it from the parent chain |
50 | 41 | */
|
51 |
| - const currentL2Greeting = await l2Greeter.greet() |
52 |
| - console.log(`Current L2 greeting: "${currentL2Greeting}"`) |
| 42 | + console.log('Deploying Greeter to the child chain 👋👋') |
53 | 43 |
|
54 |
| - console.log( |
55 |
| - `Now we send a l2 tx through l1 delayed inbox (Please don't send any tx on l2 using ${l2Wallet.address} during this time):` |
| 44 | + const Greeter = (await ethers.getContractFactory('Greeter')).connect( |
| 45 | + childChainWallet |
56 | 46 | )
|
| 47 | + const greeter = await Greeter.deploy('Hello world') |
| 48 | + await greeter.deployed() |
| 49 | + console.log(`Greeter deployed to ${greeter.address}`) |
57 | 50 |
|
58 | 51 | /**
|
59 |
| - * Here we have a new greeting message that we want to set as the L2 greeting; we'll be setting it by sending it as a message from delayed inbox!!! |
| 52 | + * Let's log the starting greeting string |
60 | 53 | */
|
61 |
| - const newGreeting = 'Greeting from delayedInbox' |
62 |
| - |
63 |
| - const GreeterIface = l2Greeter.interface |
| 54 | + const currentGreeting = await greeter.greet() |
| 55 | + console.log(`Current greeting: "${currentGreeting}"`) |
64 | 56 |
|
65 |
| - const calldatal2 = GreeterIface.encodeFunctionData('setGreeting', [ |
66 |
| - newGreeting, |
| 57 | + /** |
| 58 | + * Here we have a new greeting message that we want to set in the contract; |
| 59 | + * we'll be setting it by sending it as a message from the parent chain through the delayed inbox!!! |
| 60 | + */ |
| 61 | + console.log( |
| 62 | + `Now we send a message to be executed on the child chain, through the delayed inbox of the parent chain (make sure you don't send any transaction directly on the child chain using ${childChainWallet.address} during this time):` |
| 63 | + ) |
| 64 | + const newGreetingToSet = 'Greeting from delayedInbox' |
| 65 | + const GreeterIface = greeter.interface |
| 66 | + const calldata = GreeterIface.encodeFunctionData('setGreeting', [ |
| 67 | + newGreetingToSet, |
67 | 68 | ])
|
68 |
| - |
69 |
| - const transactionl2Request = { |
70 |
| - data: calldatal2, |
71 |
| - to: l2Greeter.address, |
| 69 | + const transactionRequest = { |
| 70 | + data: calldata, |
| 71 | + to: greeter.address, |
72 | 72 | value: 0,
|
73 | 73 | }
|
74 | 74 |
|
75 | 75 | /**
|
76 |
| - * We need extract l2's tx hash first so we can check if this tx executed on l2 later. |
| 76 | + * We need to extract the transaction hash in the child chain first so we can check later if it was executed |
77 | 77 | */
|
78 |
| - const l2SignedTx = await inboxSdk.signL2Tx(transactionl2Request, l2Wallet) |
79 |
| - |
80 |
| - const l2Txhash = ethers.utils.parseTransaction(l2SignedTx).hash |
81 |
| - |
82 |
| - const l1Tx = await inboxSdk.sendL2SignedTx(l2SignedTx) |
83 |
| - |
84 |
| - const inboxRec = await l1Tx.wait() |
85 |
| - |
86 |
| - console.log(`Greeting txn confirmed on L1! 🙌 ${inboxRec.transactionHash}`) |
| 78 | + const signedTransaction = await inboxTools.signChildTx( |
| 79 | + transactionRequest, |
| 80 | + childChainWallet |
| 81 | + ) |
| 82 | + const transactionHash = ethers.utils.parseTransaction(signedTransaction).hash |
87 | 83 |
|
88 | 84 | /**
|
89 |
| - * Now we successfully send the tx to l1 delayed inbox, then we need to wait the tx executed on l2 |
| 85 | + * We now send the transaction through the Delayed Inbox on the parent chain |
90 | 86 | */
|
| 87 | + const sendMessageParentChainTransactionRequest = |
| 88 | + await inboxTools.sendChildSignedTx(signedTransaction) |
| 89 | + const sendMessageParentChainTransactionReceipt = |
| 90 | + await sendMessageParentChainTransactionRequest.wait() |
91 | 91 | console.log(
|
92 |
| - `Now we need to wait tx: ${l2Txhash} to be included on l2 (may take 15 minutes) ....... ` |
| 92 | + `Greeting transaction confirmed on the parent chain! 🙌 ${sendMessageParentChainTransactionReceipt.transactionHash}` |
93 | 93 | )
|
94 | 94 |
|
95 |
| - const l2TxReceipt = await l2Provider.waitForTransaction(l2Txhash) |
96 |
| - |
97 |
| - const status = l2TxReceipt.status |
| 95 | + /** |
| 96 | + * Now we successfully send the transaction to the delayed inbox on the parent chain |
| 97 | + * We wait for the transaction to be executed on the child chain |
| 98 | + */ |
| 99 | + console.log( |
| 100 | + `Now we need to wait tx: ${transactionHash} to be executed on the child chain (may take ~15 minutes) ... ` |
| 101 | + ) |
| 102 | + const transactionReceipt = await childChainProvider.waitForTransaction( |
| 103 | + transactionHash |
| 104 | + ) |
| 105 | + const status = transactionReceipt.status |
98 | 106 | if (status == true) {
|
99 |
| - console.log(`L2 txn executed!!! 🥳 `) |
| 107 | + console.log(`Transaction executed on the child chain!!! 🥳`) |
100 | 108 | } else {
|
101 |
| - console.log(`L2 txn failed, see if your gas is enough?`) |
| 109 | + console.log( |
| 110 | + `The transaction failed to execute on the child chain. Please verify if the gas provided was enough` |
| 111 | + ) |
102 | 112 | return
|
103 | 113 | }
|
104 | 114 |
|
105 | 115 | /**
|
106 |
| - * Now when we call greet again, we should see our new string on L2! |
| 116 | + * Now when we call greet again, we should see our new string! |
107 | 117 | */
|
108 |
| - const newGreetingL2 = await l2Greeter.greet() |
109 |
| - console.log(`Updated L2 greeting: "${newGreetingL2}"`) |
| 118 | + const newGreeting = await greeter.greet() |
| 119 | + console.log(`Updated greeting: "${newGreeting}"`) |
110 | 120 | console.log('✌️')
|
111 | 121 | }
|
112 | 122 |
|
|
0 commit comments