TRON Block Reader

const { interval, pipe } = require("rxjs");
const { 
  take,
  concatMap,
  concatAll,
  filter,
  toArray,
  distinctUntilChanged,
  mergeMap,
  map,
} = require("rxjs/operators");
const xhr = require("axios");

const BLOCK_INTERVAL = 2000; // new block every 3 seconds

function findTransactionsByType(type) {
    return pipe(
        filter(tx => tx.raw_data.contract[0].type == type)
    );

}

function getNowBlock() {
    return pipe(
        concatMap(x => xhr.get("https://api.trongrid.io/wallet/getnowblock")),
        map(x => x.data),
    );
}

function getTxFromBlock() {
    return pipe(map(x => x.transactions), concatAll());
}

// every 3 seconds retrieve the latest transactions

let counter = 1;

interval(BLOCK_INTERVAL)
    .pipe(
        take(4),
        getNowBlock(),
        distinctUntilChanged((currentBlock, previousBlock) => {
            return currentBlock.blockID === previousBlock.blockID;
        }),
        getTxFromBlock(),
        findTransactionsByType("TransferContract"),
        // every transaction also has energy information that we can read
        
        // requests to this URL needs to be done in parallel because it 
        // can be a lot of transactions
        // and the block needs to be handled within 3 seconds
        mergeMap(async tx => {
            const {  data } = await xhr.get("https://api.trongrid.io/wallet/gettransactioninfobyid?value=" + tx.txID);
            return tx.txID + " => " + data.receipt.net_usage;
        }, 4), // run 4 procedures concurrently
    )
    .subscribe(x => console.log(x));
Made with by Rovak