美文网首页
web3js subscription logs

web3js subscription logs

作者: 已不再更新_转移到qiita | 来源:发表于2018-04-07 00:47 被阅读480次

    geth

    geth --datadir /mnt/eth  --rpc --ws --wsaddr localhost --wsorigins "*" --wsapi "eth,web3,net,txpool,shh, subpub" 
    

    rpc eth_newFilter eth_getLogs

    var my = "0xd551234ae421e3bcba99a0da6d736074f22192ff";
    
    curl http://localhost:8545 -H 'content-type:application/json;' -X POST \
    --data '{ "method":"eth_newFilter", "params":[ {"fromBlock": "0x1", "toBlock" : "latest",  "address": "0xd551234ae421e3bcba99a0da6d736074f22192ff", "topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] } ],"id":1}'
    
    curl http://localhost:8545 -H 'content-type:application/json;' -X POST \
    --data '{ "method":"eth_getLogs", "params":[ {"fromBlock": "0x1", "toBlock" : "latest",  "address": "0xd551234ae421e3bcba99a0da6d736074f22192ff", "topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] } ],"id":1}'
    
    {"jsonrpc":"2.0","id":1,"result":[]}
    

    js script

    var Web3 = require('web3');
    var web3 = new Web3();
    
    var my = "0xd551234ae421e3bcba99a0da6d736074f22192ff";
    var eos_contract_address = "0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0";
    
    web3.setProvider(new web3.providers.WebsocketProvider("ws://localhost:8546") );
    
    var sha3 = web3.utils.sha3("Transfer(address,address,uint256)");
    
    var padding = new Array(24+1).join('0'); // 24
    var topic_1 = "0x" + padding + my.slice(2);
    
    var subscription = web3.eth.subscribe('logs',
      { fromBlock: 1, toBlock: "latest", address: eos_contract_address, topics: [sha3, topic_1] }, function() {})
    .on("data", function(tx_data){
    
      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(tx_data);
    
      console.log( "------------------------------" );
    
      console.log("Contract address:" + tx_data.address);
      console.log("transaction value: " + web3.utils.hexToNumberString(tx_data.data) );
      console.log("from: " + formatAddress(tx_data.topics['1']));
      console.log("to: " + formatAddress(tx_data.topics['2']) ) ;
    
    });
    
    
    // {
    //   "address":"0x86Fa049857E0209aa7D9e616F7eb3b3B78ECfdb0",
    //   "topics":[
    //     "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
    //     "0x000000000000000000000000ce69522b62df911fc11ea8627e18fe2c91fb7189",
    //     "0x0000000000000000000000005da3121f442e3192173cede87bf31f3608c510ab"
    //   ],
    //   "data":"0x000000000000000000000000000000000000000000000ca4f63d7ec15ea04000",
    //   "blockNumber":5287061,
    //   "transactionHash":"0xf2f25bbc4a21919c16e554babc9b3cfd7884b2f6c446ea6fd4b1fc7354bae308",
    //   "transactionIndex":65,
    //   "blockHash":"0xca557515bc567d1ca88e245937097957d0351a3f7277435027312dc55bdebb51",
    //   "logIndex":24,
    //   "removed":false,
    //   "id":"log_8db999fc"
    // }
    
    

    参考:

    https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter
    https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getlogs
    https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterlogs
    https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getfilterchanges

    https://ethereum.stackexchange.com/questions/26621/subscribe-to-all-token-transfers-for-entire-blockchain

    相关文章

      网友评论

          本文标题:web3js subscription logs

          本文链接:https://www.haomeiwen.com/subject/tqnchftx.html