BNB Smart Chain - Web3js Listen to Events
-
https://pastecode.io/s/5kfzxtgs
const Web3 = require('web3'); const web3 = new Web3('wss://rpc-bsc.bnb48.club'); let options = { topics: [ web3.utils.sha3('Transfer(address,address,uint256)') ] }; const abi = [ { "constant": true, "inputs": [], "name": "symbol", "outputs": [ { "name": "", "type": "string" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "decimals", "outputs": [ { "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "view", "type": "function" } ]; let subscription = web3.eth.subscribe('logs', options); async function collectData(contract) { const [decimals, symbol] = await Promise.all([ contract.methods.decimals().call(), contract.methods.symbol().call() ]); return { decimals, symbol }; } subscription.on('data', event => { if (event.topics.length == 3) { let transaction = web3.eth.abi.decodeLog([{ type: 'address', name: 'from', indexed: true }, { type: 'address', name: 'to', indexed: true }, { type: 'uint256', name: 'value', indexed: false }], event.data, [event.topics[1], event.topics[2], event.topics[3]]); const contract = new web3.eth.Contract(abi, event.address) collectData(contract).then(contractData => { const unit = Object.keys(web3.utils.unitMap).find(key => web3.utils.unitMap[key] === web3.utils.toBN(10).pow(web3.utils.toBN(contractData.decimals)).toString()); console.log(`Transfer of ${web3.utils.fromWei(transaction.value, unit)} ${contractData.symbol} from ${transaction.from} to ${transaction.to}`) if (event.address == '0x55d398326f99059fF775485246999027B3197955') { console.log('Specified ERC-20 transfer!') }; }) } }); subscription.on('error', err => { throw err }); subscription.on('connected', nr => console.log('Subscription on ERC-20 started with ID %s', nr));
-