opBNB: How to listen to deposits on the opBNB bridge
TLDR
- opBNB is a Layer 2 scalability solution for the BNB Smart Chain built on the Optimism OP Stack.
- The opBNB Bridge contract emits
DepositFinalized
events whenever deposits from BNB Chain occur. - This tutorial shows how to listen for these deposit events using ethers.js, enabling real-time tracking of cross-chain activity.
- By monitoring transaction hashes and deposit details, developers can interact directly with the opBNB ecosystem.
Main article
opBNB is an EVM-compatible Layer 2 scalable network that brings unique features. Developed as an extension of the Binance Smart Chain (BNB Chain) ecosystem, opBNB aims to provide high-performance blockchain solutions. It leverages the bedrock version of the Optimism OP Stack to offer a Layer 2 scaling solution for the BNB Smart Chain.
The opBNB network enhances scalability by offloading transaction processing and resource usage from the BNB Smart Chain while posting data to the underlying mainnet. This approach enables high throughput and low fees, making opBNB an attractive choice for developers and users.
One of the key components of opBNB is the opBNB Bridge, which facilitates the secure transfer of assets between BNB Chain and opBNB. This bridge is essential for those who need to move assets while maintaining the benefits of both networks. Users interact with the opBNB network by depositing funds from BNB and using applications and contracts on opBNB.
This tutorial will guide you through setting up a listener for deposit events on the opBNB Bridge. You can then capture deposit events and extract relevant transaction data. Understanding how to monitor and interact with these on-chain events is crucial for developers leveraging opBNB’s capabilities.
Prerequisites
- Chainstack account to deploy an opBNB Mainnet node
- Node.js
- ether.js
Step-by-step
Get an opBNB node
Log in to your Chainstack account and deploy a node.
Create a new ethers project
Once you have Node.js installed, create a new project in a new directory.
This will create a new project and a package.json
file. You can then install the ethers library.
Learn more about Node projects for Web3 by reading Web3 node.js: From zero to a full-fledged project.
Create the script
Now that the project is set, create a new file named index.js
.
With this DApp, we listen to the DepositFinalized
event on the opBNB Bridge smart contract, 0x4200000000000000000000000000000000000010
in this case. This even is emitted when a deposit is made, you can use this data to monitor activity.
Note that this address is a proxy contract. To find the contract event to use in the code, check the actual bridge contract: 0xc0d3c0d3c0d3c0d3c0d3c0d3c0d3c0d3c0d30010. The event is defined in the contract as the following:
Paste the following code.
where
YOUR_CHAINSTACK_NODE
— your opBNB node endpoint
Code breakdown
Here’s a brief breakdown explaining the code step-by-step:
Importing Ethers.js Library
The code starts by importing the Ethers.js library, which is essential for interacting with the Ethereum network and its derivatives like opBNB.
Connecting to the opBNB Node
Using a JSON-RPC provider, it connects to an opBNB node. Replace "YOUR_CHAINSTACK_NODE"
with your actual Chainstack node URL.
Defining the Contract ABI
The ABI (Application Binary Interface) is defined for the DepositFinalized
event. This ABI tells Ethers.js how to interpret the event data.
Specifying the Contract Address
The address of the opBNB Bridge contract is specified. This address points to the smart contract on the opBNB network that emits the DepositFinalized
events.
Creating a Contract Instance
The contract address, ABI, and provider are used to create an instance of the contract. This instance allows interaction with the contract and listening for events.
Defining the Event Filter
A filter for the DepositFinalized
event is defined. If needed, this filter can be customized to listen for specific events based on additional parameters.
Handling the Event
The script sets up an event listener that triggers when a DepositFinalized
event is detected.
- Logging Event Details: It logs the event details, such as the transaction hash, which can be used to view the transaction on the opBNB block explorer.
- Extracting Event Data: The
from
address and theamount
of BNB deposited are extracted from the event arguments and logged. The amount is formatted to be readable in BNB units.
The script listens for DepositFinalized
events on the opBNB Bridge and prints relevant details to the console whenever such an event is emitted by the smart contract.
Conclusion
By following this tutorial, you have successfully set up a listener for DepositFinalized
events on the opBNB Bridge. This allows you to capture and analyze deposit events in real-time, providing valuable insights into the activity on the opBNB network.