Tracing deposits and withdrawals
In this tutorial, you'll learn how to use the viem (opens in a new tab) library to trace a Standard Bridge deposit or withdrawal between L1 and L2. You'll specifically learn how to determine the status of a deposit or withdrawal and how to retrieve the transaction receipt for the executed transaction on L1 (for withdrawals) or L2 (for deposits).
Dependencies
Create a demo project
You're going to use the viem library for this tutorial. Since viem is a Node.js (opens in a new tab) library, you'll need to create a Node.js project to use it.
Make a Project Folder
mkdir trace-trx
cd trace-trx
Initialize the project
pnpm init
Install viem
pnpm add viem
Add RPC URLs to your environment
You'll be using the getTransactionReceipt
function from the viem library during this tutorial. This function uses event queries to retrieve the receipt for a deposit or withdrawal.
Since this function uses large event queries, you'll need to use an RPC provider like Alchemy (opens in a new tab) that supports indexed event queries.
Grab an L1 and L2 RPC URL for Sepolia and OP Sepolia, respectively.
export L1_RPC_URL="https://YOUR_L1_ SEPOLIA_RPC_URL_HERE"
export L2_RPC_URL="https://YOUR_L2_OP_SEPOLIA_RPC_URL_HERE"
Start the Node REPL
You're going to use the Node REPL to interact with viem. To start the Node REPL, run the following command in your terminal:
node
This will bring up a Node REPL prompt that allows you to run JavaScript code.
Import dependencies
You need to import some dependencies into your Node REPL session.
Import viem
const { createPublicClient, http } = require('viem');
const { optimismSepolia, sepolia } = require('viem/chains');
Set session variables
You'll need a few variables throughout this tutorial. Let's set those up now.
Import RPC URLs
const l1RpcUrl = process.env.L1_RPC_URL;
const l2RpcUrl = process.env.L2_RPC_URL;
Set the deposit to trace
You'll be tracing a specific deposit in this tutorial. Deposit tracing is generally based on the transaction hash of the transaction that triggered the deposit. You can replace this transaction hash with your own if you'd like.
const depositHash = '0x5896d6e4a47b465e0d925723bab838c62ef53468139a5e9ba501efd70f90cccb'
Set the withdrawal to trace
You'll also be tracing a specific withdrawal in this tutorial. Like with deposits, withdrawal tracing is generally based on the transaction hash of the transaction that triggered the withdrawal. You can replace this transaction hash with your own if you'd like.
const withdrawalHash = '0x18b8b4022b8d9e380fd89417a2e897adadf31e4f41ca17442870bf89ad024f42'
Create the RPC providers
const l1Client = createPublicClient({
chain: sepolia,
transport: http(l1RpcUrl),
});
const l2Client = createPublicClient({
chain: optimismSepolia,
transport: http(l2RpcUrl),
});
Trace a Deposit
You can use viem to trace a deposit.
Get the deposit status
You can query for the deposit status using the transaction hash of the deposit.
console.log('Grabbing deposit status...')
const depositStatus = await l2Client.getTransactionReceipt({ hash: depositHash });
console.log(depositStatus);
Get the deposit transaction receipt
Retrieve the transaction receipt for the deposit using the viem client.
console.log('Grabbing deposit receipt...')
const depositReceipt = await l2Client.getTransaction({ hash: depositHash });
console.log(depositReceipt);
Get the deposit transaction
You can directly query for the L2 transaction that executed the deposit.
console.log('Grabbing deposit txn...')
const depositTransaction = await l2Client.getTransaction({ hash: depositHash });
console.log(depositTransaction);
Trace a withdrawal
You can use viem's functions to trace a withdrawal.
Get the withdrawal status
Like deposits, withdrawals can have multiple statuses depending on where they are in the process.
console.log('Grabbing withdrawal status...')
const withdrawalStatus = await l1Client.getTransactionReceipt({ hash: withdrawalHash });
console.log(withdrawalStatus);
Get the withdrawal transaction receipt
Retrieve the L1 transaction receipt for the withdrawal.
console.log('Grabbing withdrawal receipt...')
const withdrawalReceipt = await l1Client.getTransaction({ hash: withdrawalHash });
console.log(withdrawalReceipt);
Get the withdrawal transaction
Directly query for the L1 transaction that executed the withdrawal.
console.log('Grabbing withdrawal txn...')
const withdrawalTransaction = await l1Client.getTransaction({ hash: withdrawalHash });
console.log(withdrawalTransaction);
Next steps
- Check out the tutorial on bridging ERC-20 tokens with the Optimism SDK to learn how to create deposits and withdrawals.