opBNB: How to listen to deposits on the opBNB bridge
opBNB support is deprecated
Chainstack deprecated support for opBNB nodes. This page here is for legacy and in case you may find it useful.
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.
npm init --yes
This will create a new project and a package.json
file. You can then install the ethers library.
npm i ethers
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:
event DepositFinalized(
address indexed l1Token,
address indexed l2Token,
address indexed from,
address to,
uint256 amount,
bytes extraData
);
Paste the following code.
const { ethers } = require("ethers");
// Connect to your opBNB node
const provider = new ethers.JsonRpcProvider(
"YOUR_CHAINSTACK_NODE"
);
// Define the contract ABI
const abi = [
"event DepositFinalized(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256 amount, bytes extraData)",
];
// Define the contract address
const contractAddress = "0x4200000000000000000000000000000000000010";
// Create a contract instance
const contract = new ethers.Contract(contractAddress, abi, provider);
// Define the event filter (optional: add any specific filters if required)
const filter = contract.filters.DepositFinalized();
console.log("Listening for DepositFinalized events on the opBNB Bridge...");
// Listen for the event
contract.on(filter, (event) => {
console.log("Deposit Finalized Event:");
console.log(`Transaction Hash: ${event.log.transactionHash}`);
console.log(
`Explorer link: https://opbnbscan.com/tx/${event.log.transactionHash}`
);
console.log(`From: ${event.args[2]}`); // from
console.log(`Amount: ${ethers.formatEther(event.args[4].toString())} BNB`); // amount
console.log(
"------------------------------------------------------------------------------------------"
);
});
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.
const { ethers } = require("ethers");
// Connect to your opBNB node
const provider = new ethers.JsonRpcProvider(
"YOUR_CHAINSTACK_NODE"
);
- 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.
// Define the contract ABI
const abi = [
"event DepositFinalized(address indexed l1Token, address indexed l2Token, address indexed from, address to, uint256 amount, bytes extraData)",
];
- 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.
// Define the contract address
const contractAddress = "0x4200000000000000000000000000000000000010";
- 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.
// Create a contract instance
const contract = new ethers.Contract(contractAddress, abi, provider);
- 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.
// Define the event filter (optional: add any specific filters if required)
const filter = contract.filters.DepositFinalized();
- 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.
// Listen for the event
contract.on(filter, (event) => {
console.log("Deposit Finalized Event:");
console.log(`Transaction Hash: ${event.log.transactionHash}`);
console.log(
`Explorer link: https://opbnbscan.com/tx/${event.log.transactionHash}`
);
console.log(`From: ${event.args[2]}`); // from
console.log(`Amount: ${ethers.formatEther(event.args[4].toString())} BNB`); // amount
console.log(
"------------------------------------------------------------------------------------------"
);
});
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.
About the author
Updated about 1 month ago