Viewing Transactions between Layers
Look up transactions passed between Mantle and Ethereum
Mantle v2 Tectonic has been released, please move to the new documentation!
In this tutorial, we'll be going over how to use the Mantle SDK to view transactions passed between Mantle network (L2) and Ethereum (L1). This function is particularly useful for dApps that make contract calls between L2 and L1.
Set up local environment
Let's download the JS script that we'll be using to make SDK calls. The easiest way is to download the files from the Mantle GitHub repo using a Git command like so:
We need to configure the wallet that we're going to be using to send transactions. In the main directory, you'll see a .env.testnet
file. You can specify your wallet private key in the PRIV_KEY
field, and an L1_RPC
URL for Goerli network.
Now, navigate to the ./sdk-view-tx
directory and run the yarn
command to download the necessary dependencies to your local environment, such as the ethers.js library, the SDK modules, and more. They can all be found in the node_modules
directory once you successfully run Yarn
.
Let's go over the files in the ./sdk-view-tx
directory and see what purpose each of them serves.
index.js
: the main JS script that sends requests to node RPCs on L1 and L2 to query on-chain datapackage.json
: specifies dependencies and commands for script automationyarn.lock
: specifies dependencies
Script Logic
We can now start looking at the code in index.js
.
We first create a crossChainMessenger
object that we'll use to fetch and view transaction information. This operation is limited to fetching public on-chain data using view
functions, so we don't need signers to send these requests. However, we do need chain ID values to direct requests to the correct node RPC.
If the l1Addr
field contains all zeroes, this implies the transferred token was $ETH. We also check the l1Addr
for $MNT token's contract address.
Otherwise, we fetch the token symbol from the contract. The same query logic can be used for both L1 and L2 contracts.
The response of crossDomainMessenger.getMessageStatus()
is a MessageStatus
enumerated value. What we're checking for is whether the deposit/withdrawal transaction is completed or still in progress.
The crossChainMessenger.getDepositsByAddress()
function call returns records of all deposit transactions made by an address, and crossChainMessenger.getWithdrawalsByAddress()
returns records of all withdrawal transactions sent by an address. Finally, we print the deposit and withdraw records to the console.
Running the Script
With L1 and L2 RPC endpoints and the wallet private key configured, you can go ahead and run the index.js
script by simply running the yarn testnet
command. If you have a local L2 or L1 instance running in your environment, you can switch up the respective RPC URLs in the .env.local
configuration file in the main directory and use the yarn local
command to run the script locally.
Results
The output would look something like:
Conclusion
At this point, you should be able to look up deposits and withdrawals performed by any specific address. There are some additional tracing functions in CrossChainMessenger
, but they are very similar in terms of operation.
Last updated