Created
September 17, 2018 21:24
-
-
Save RossPfeiffer/02d0c30151eeb7ece51234dfd21a527f to your computer and use it in GitHub Desktop.
Partial Block Explorer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Web3 = require('web3'); | |
var web3 = new Web3('https://mainnet.infura.io/v3/bf5c3d819a0d4492a387eb40cdf82d6b'); | |
var finalBlock = web3.eth.getBlockNumber().then(captureBlockRange); | |
//This will contain an ordered list of all transactions | |
var transactionList = new Array(); | |
var ethTransferred = 0; | |
var addressRecords = [];//{address:"0x0",sent:0,received:0,contract:false} | |
function captureBlockRange(finalBlock){ | |
console.log("Final Block #", finalBlock); | |
var startingBlock = finalBlock - 2; | |
var promiseForBlocks = []; | |
for(var i = startingBlock; i <= finalBlock; i += 1 ){ | |
let blockPromise = web3.eth.getBlock(i, true); | |
promiseForBlocks.push( blockPromise ); | |
} | |
Promise.all(promiseForBlocks).then( handlePromiseForBlocks ); | |
} | |
function handlePromiseForBlocks(resultsArray){ | |
resultsArray.forEach( (block) => { | |
//this will add the transactions to the transaction list | |
if(block.transactions) | |
transactionList = transactionList.concat(block.transactions); | |
}); | |
transactionList.forEach( recordTransaction ); | |
var promisesForContractDetection = []; | |
var ii = 0; | |
addressRecords.forEach( (address,i)=>{ | |
if(address.address){ | |
let contractDetection = web3.eth.getCode(address.address).then( (result)=>{ | |
address.isContract = (result.length>2); | |
var isContract = 'No'; | |
if(address.isContract){ | |
isContract = 'Yes'; | |
} | |
console.log(address.address+"\t Contract["+isContract+"]\nSent: "+address.sent+" Received:"+address.received); | |
}).catch( ()=>{console.log("Couldn't get this one")} ); | |
promisesForContractDetection.push(contractDetection); | |
} | |
}); | |
Promise.all(promisesForContractDetection).then( ()=>{ | |
console.log("All requests complete"); | |
//a report document would be created here. | |
}); | |
} | |
function recordTransaction(transaction){ | |
var addressRecord_ofSender = getAddressRecord(transaction.from); | |
var addressRecord_ofReceiver = getAddressRecord(transaction.to); | |
var transactionValue = parseInt(transaction.value); | |
addressRecord_ofSender.sent += transactionValue; | |
addressRecord_ofReceiver.received += transactionValue; | |
ethTransferred += transactionValue; | |
} | |
function getAddressRecord(address){ | |
var addressRecord = addressRecords.find( (addressRecord)=>{ return addressRecord.address === address } ); | |
if (addressRecord === undefined){ | |
addressRecords.push({ | |
address: address, | |
sent:0, | |
received:0, | |
isContract: false | |
}); | |
//console.log("new address record: \t" + address); | |
return addressRecords[addressRecords.length-1]; | |
}else{ | |
//console.log("existing address:\t " + address); | |
return addressRecord; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment