1

I am looking how to subscribe to all token transfer events within entire blockchain. I wonder how is this is implemented on etherscan.io

https://ropsten.etherscan.io/tokentxns

I searched thought web3.js api and googled wherever is there on the internet but it seems to me that there is no simple solution for that. Can I get this data somehow via any available API or I have to go deeper and run my own EVM with embedded listeners?

4

Ok, after quite some fight I found a solution

var Web3 = require('web3');

var web3 = new Web3('ws://127.0.0.1:8585');
console.log('Initiated');

// Track all the token transactions in whole blockchain
var subscription = web3.eth.subscribe('logs', { fromBlock: 1, topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] }, function() {})
.on("data", function(trxData){
  function formatAddress(data) {
    var step1 = web3.utils.hexToBytes(data);
    for (var i = 0; i < step1.length; i++) if (step1[0] == 0) step1.splice(0, 1);
    return web3.utils.bytesToHex(step1);
  }

  console.log("Register new transfer: " + trxData.transactionHash);
  console.log("Contract " + trxData.address + " has transaction of " + web3.utils.hexToNumberString(trxData.data) + " from " + formatAddress(trxData.topics['1']) + " to " + formatAddress(trxData.topics['2']));
  //console.log(trxData);
  web3.eth.getTransactionReceipt(trxData.transactionHash, function(error, reciept) {
    console.log('Sent by ' + reciept.from + ' to contract ' + reciept.to);
  });
});

Script will output data in following format:

Register new transfer: 0x3bb3d83fa26560002cad0e920b78979eee5c5652a623f98cea5460dbb8d8fc94
Contract 0x0F5B3e7B0074F43c8C9C5324fabF83942c1FEf5c has transaction of 1 from 0x3cd40959e6fa78d4636cad472fdd4fa15d9867c7 to 0xd5142d2778f26471c1017f79687a91783636cfeb
Sent by 0xc270d8a6cf39b560c2746d4efdb24290fed76123 to contract 0x2c09402b4a1a42321160fd5af7737ca432e68c90

It contains ifo about who triggered the transfer, which contract was the main entry point of transaction, what is the token contract address, how many kones were transfered, from which address to which address and transaction hash.

You can now spy on the WHOLE blockchain! happy hacking!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.