So I am connecting to Binance Smart Chain RPC websocket from Moralis.io. And trying to listen to wallet amount changes of a certain wallet address.
What I want to do is, I want to trigger a event whenever there is a transfer from or to this wallet address. I understand how to do it with BEP20 tokens but I require a solution to monitor the wallet address for BNB transfers.
The code that I put together:
const web3 = new Web3(new Web3.providers.WebsocketProvider('URI'))
let options = {
address: '0xe....'
}
const subscribe = web3.eth.subscribe('logs', options, (err, res) => {})
subscribe.on('data', (txLog) => console.log(txLog))
This doesn't work when I send a certain amount of BNB to this account.
I went through the documentation of web3js but couldn't figure out.
I found a longer way around for this problem where someone suggests to listen to all pending transactions and try to get the transaction data of each of these transactions and evaluate if any of these are from or to the wallet address. But I think it is an extremely ineffective and inefficient way to do it as it requires someone to run their own node to do it in a meaningful way.
Please guide me through this. Thanks
listen to all pending transactions and try to get the transaction data of each of these transactions
I agree that this is an ineffective way. But with the current state of the JSON-RPC API, it's still the most effective way available. Apart from using a 3rd party service that does all this work in the background and provides the data over their custom API.
Note: The link goes to Ethereum documentation, but Binance Smart Chain implements the same JSON-RPC API.
There's simply no method to subscribe to or poll incoming native transactions to a specified address.
And since web3, ethers.js, and many other libraries are wrappers of this API, they can only support methods that the API supports.
There is an answer here to your similar question, which in this case the is on Polygon Blockchain, but they still do operate in same way with BNB.