Skip to content

Instantly share code, notes, and snippets.

@adyshimony
Created August 29, 2018 21:14
Show Gist options
  • Save adyshimony/687c17be1e5016ce0513c00252d3a9f3 to your computer and use it in GitHub Desktop.
Save adyshimony/687c17be1e5016ce0513c00252d3a9f3 to your computer and use it in GitHub Desktop.
Fetch transactions receipt
const ipcPath = process.env['HOME'] + '/.local/share/io.parity.ethereum/jsonrpc.ipc'
const net = require('net')
const Web3 = require('web3')
const web3 = new Web3()
web3.setProvider(new web3.providers.IpcProvider(ipcPath, net))
function parseHrtimeToSeconds (hrtime) {
var seconds = (hrtime[0] + (hrtime[1] / 1e9)).toFixed(3)
return seconds
}
async function getBlockTransactionsAsync () {
let transactions = []
try {
let blocksPromises = []
let index
let startTime = process.hrtime()
// fetch all blocks async
for (index = 4832000; index < 4832100; index++) {
// fetch full block, include transactions
blocksPromises.push(web3.eth.getBlock(index, true))
}
console.info(`getBlockTransactionsAsync waiting for blocks 4832000 - 4832099 requests`)
// wait for blocks
let resBlocks = await Promise.all(blocksPromises)
let elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime))
console.info(`getBlockTransactionsAsync got blocks, duration in sec:, ${elapsedSeconds}`)
// for every block, handle transactions
let startTimeTransactions = process.hrtime()
let transactionCount = 0
for (let blockIndex = 0; blockIndex < resBlocks.length;) {
let block = resBlocks[blockIndex]
if (block) {
let txstartTime = process.hrtime()
let transactionReceiptPromises = []
// fetch all transactions async
for (let index = 0; index < block.transactions.length; index++) {
transactionReceiptPromises.push(web3.eth.getTransactionReceipt(block.transactions[index].hash))
}
console.info(`wait for ${block.transactions.length} transactions receipt requests .`)
let resTransactionReceipt = await Promise.all(transactionReceiptPromises)
let elapsedTxSeconds = parseHrtimeToSeconds(process.hrtime(txstartTime))
console.info(`getTransactionsFromBlockAsync for block: ${block.number}, txs: ${resTransactionReceipt.length}, duration in sec:, ${elapsedTxSeconds}`)
transactionCount += block.transactions.length
blockIndex++
}
}
let elapsedSecondsTransactions = parseHrtimeToSeconds(process.hrtime(startTimeTransactions))
console.info(`getting ${transactionCount} txs, duration in sec:, ${elapsedSecondsTransactions}`)
} catch (error) {
console.error(error)
}
}
getBlockTransactionsAsync().then(async () => {
console.info('done')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment