Skip to content

Commit 2759b86

Browse files
authored
Merge pull request #1377 from qbzzt/250216-msg-passing-code-explanation
Added the code segment explanation back
2 parents c6be2fa + 8451545 commit 2759b86

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

pages/stack/interop/tutorials/message-passing.mdx

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,10 @@ For development purposes, we'll first use autorelay mode to handle message execu
229229
```
230230

231231
This function encodes a call to `setGreeting` and sends it to a contract on another chain.
232-
233-
* `abi.encodeCall(Greeter.setGreeting, (greeting))` constructs the [calldata](https://docs.soliditylang.org/en/latest/internals/layout_in_calldata.html) by encoding the function selector and parameters.
234-
235-
* The encoded message is then passed to `messenger.sendMessage`, which forwards it to the destination contract (`greeterAddress`) on the specified chain (`greeterChainId`).
236-
This ensures that `setGreeting` is executed remotely with the provided `greeting` value.
232+
`abi.encodeCall(Greeter.setGreeting, (greeting))` constructs the [calldata](https://docs.soliditylang.org/en/latest/internals/layout_in_calldata.html) by encoding the function selector and parameters.
233+
The encoded message is then passed to `messenger.sendMessage`, which forwards it to the destination contract (`greeterAddress`) on the specified chain (`greeterChainId`).
234+
235+
This ensures that `setGreeting` is executed remotely with the provided `greeting` value (as long as there is an executing message to relay it).
237236
</details>
238237

239238
7. Deploy `GreetingSender` to chain A.
@@ -501,52 +500,33 @@ In production we will not have this, we need to create our own executing message
501500
<details>
502501
<summary>Explanation</summary>
503502

504-
1. **Import Required Libraries**
505-
506-
* Imports functions from `viem` for wallet creation, HTTP transport, and contract interactions.
507-
508-
* Imports `@eth-optimism/viem` utilities for handling OP-Stack-specific actions and interoperability.
509-
510-
* Loads ABI definitions from `Greeter.json` and `GreetingSender.json` for contract interactions.
511-
512-
2. **Initialize Wallet Clients**
513-
514-
* Uses `privateKeyToAccount` to generate an account from an environment variable.
515-
516-
* Creates `walletA` for chain `supersimL2A` and `walletB` for chain `supersimL2B`, extending them with Viem's public and OP-Stack-specific actions.
517-
518-
3. **Get Contract Instances**
519-
520-
* Retrieves contract instances for `greeter` on `walletB` and `greetingSender` on `walletA` using `getContract`.
521-
522-
* The addresses are taken from environment variables, and the clients are set to the respective wallets.
523-
524-
4. **Direct Greeting on Chain B**
525-
526-
* Calls `setGreeting` on `greeter` to store a greeting directly on chain B.
527-
528-
* Waits for the transaction to be confirmed using `waitForTransactionReceipt`.
503+
```typescript file=<rootDir>/public/tutorials/app_v2.mts#L11-L15 hash=721ed87241535b606be281539baa8770
504+
```
529505

530-
* Reads and logs the greeting stored on chain B.
506+
Import from the [`@eth-optimism/viem`](https://www.npmjs.com/package/@eth-optimism/viem) package.
531507

532-
5. **Cross-Chain Greeting via Chain A**
508+
```typescript file=<rootDir>/public/tutorials/app_v2.mts#L22-L28 hash=ffa76edb1191121e15eb4286e16ad041
509+
```
533510

534-
* Calls `setGreeting` on `greetingSender` to send a greeting through chain A.
511+
In addition to extending the wallets with [Viem public actions](https://viem.sh/docs/accounts/local#5-optional-extend-with-public-actions), extend with the OP-Stack actions, both the public ones and the ones that require an account.
535512

536-
* Waits for the transaction receipt on chain A.
513+
```typescript file=<rootDir>/public/tutorials/app_v2.mts#L59 hash=23aa6f24baeb5757130361f30c1b0e9c
514+
```
537515

538-
6. **Retrieve and Relay the Cross-Chain Message**
516+
To relay a message we need the information in the receipt.
517+
Also, we need to wait until the transaction with the relayed message is actually part of a block.
539518

540-
* Extracts the message from the transaction receipt using `createInteropSentL2ToL2Messages`.
519+
```typescript file=<rootDir>/public/tutorials/app_v2.mts#L61-L63 hash=da4b8733c578a393eb36f154a4e816e1
520+
```
541521

542-
* Relays the message to chain B using `walletB.interop.relayMessage`.
522+
A single transaction can send multiple messages.
523+
But here we know we sent just one, so we look for the first one in the list.
543524

544-
* Waits for confirmation of the relay transaction.
525+
```typescript file=<rootDir>/public/tutorials/app_v2.mts#L64-L70 hash=6bfcd99f2e79df79897d230f36d4a682
526+
```
545527

546-
7. **Verify the Updated Greeting on Chain B**
528+
Here we first send the relay message on chain B, and then wait for the receipt for it.
547529

548-
* Reads the greeting from `greeter` after the relay process.
549-
* Logs the updated greeting, showing that it was successfully relayed from chain A to chain B.
550530
</details>
551531

552532
2. Rerun the JavaScript program, and see that the message is relayed.

0 commit comments

Comments
 (0)