Skip to content

Instantly share code, notes, and snippets.

@jorpic
Created February 24, 2017 14:11
Show Gist options
  • Save jorpic/9573387d718a60c5f2dce1212019fc19 to your computer and use it in GitHub Desktop.
Save jorpic/9573387d718a60c5f2dce1212019fc19 to your computer and use it in GitHub Desktop.
Web3.js get all recent transactions and total value
const Web3 = require('web3');
const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
const date = new Date();
const minTimestamp = date.setHours(date.getHours() - 1) / 1000;
console.log('Retreiving all transactions from', date);
const {currentBlock, highestBlock} = web3.eth.syncing;
if(currentBlock < highestBlock) {
console.log('Warning! Node is not synced:\n', web3.eth.syncing);
}
let blockNum = web3.eth.blockNumber;
const transactions = [];
while(true) {
const block = web3.eth.getBlock(blockNum);
if(block.timestamp < minTimestamp) break;
transactions.push.apply(transactions, block.transactions);
--blockNum;
}
console.log('Retreived', transactions.length, 'transactions');
const total = transactions.reduce(
(sum, tx) => web3.eth.getTransaction(tx).value.plus(sum),
new web3.BigNumber(0)
);
console.log('Total value is', web3.fromWei(total, 'ether').toNumber(), 'Ether');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment