'Get transfer event for BNB on Binance Smart Chain Using Web3.js

Please I would like to know how I could get only transfer event of BNB on BSC using web3.js

Objective: Each time a user makes a BNB deposit to an address on my system, I get notified and record the deposit for the user.

Achievement: I have been able to configure and host a BSC node on a droplet and also get transfer events on the Binance Smart Chain.

Problem: It will be very expensive on our part to check if the "To" address is on our database for each transfer event executed on the Binance Smart Chain. So Instead of getting all transfer events that occurs on the chain, I just want to get transfer events that has to do with BNB.

Current Implementation:

const web3New = new Web3('ws://my-node-ip:my-node-port')

web3New.eth.subscribe('logs', {
            topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'] // Transfer event as our topic
        }, async function (error, result) {
            if (!error)
                data = await web3New.eth.getTransaction(result.transactionHash)
                try {
                    if (data.logsBloom) {
                        console.log("FROM:     " + data.from)
                        console.log("Logboom:     " + data.logsBloom)
                        console.log("TO:     " + data.to)
                        console.log("VALUE:     " + parseInt(data.value)/1000000000000000000)
                        console.log("HASH:   " + data.hash)
                        console.log("\n")
                    }
                } catch (error) {
                  console.log(error)
                }

You might be wondering why I am using the data.logsBloom condition. It's because that's one of the distinguished properties of a BNB hash compared to other token hash on BSC.

Your inputs will be very much appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source