最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Web3js event listensubscribe to transactions to or from a wallet address - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Aug 1, 2022 at 21:10 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Feb 6, 2022 at 14:03 Rudra Pratap SinhaRudra Pratap Sinha 1694 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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 ining 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.

Moralis has Streams feature which allows to listen to transfer events of ERC20, native tokens and NFTs on BSC/ETH chains https://docs.moralis.io/streams-api/evm/how-to-listen-to-all-erc20-contract-transfers-over-certain-amount-sent-by-specific-address

Also, it's possible to self-host a blockchain crawler which will search for transactions from/to a given address, here is a project which does that

Also Alchemy has similar feature, but they don't support BSC

发布评论

评论列表(0)

  1. 暂无评论