Blockchain: Getting transactionsWe can use JavaScript to find all the transactions related to a given account. An overview of the operation of blocks and transactions is: We can use this script to mine across given block numbers and for a specified account: function getTransactionsByAccount(myaccount, startBlockNumber, endBlockNumber) { if (endBlockNumber == null) { endBlockNumber = eth.blockNumber; console.log("Using endBlockNumber: " + endBlockNumber); } if (startBlockNumber == null) { startBlockNumber = endBlockNumber - 1000; console.log("Using startBlockNumber: " + startBlockNumber); } console.log("Searching for transactions to/from account \"" + myaccount + "\" within blocks " + startBlockNumber + " and " + endBlockNumber); for (var i = startBlockNumber; i <= endBlockNumber; i++) { if (i % 1000 == 0) { console.log("Searching block " + i); } var block = eth.getBlock(i, true); if (block != null && block.transactions != null) { block.transactions.forEach( function(e) { if (myaccount == "*" || myaccount == e.from || myaccount == e.to) { console.log(" tx hash : " + e.hash + "\n" + " nonce : " + e.nonce + "\n" + " blockHash : " + e.blockHash + "\n" + " blockNumber : " + e.blockNumber + "\n" + " transactionIndex: " + e.transactionIndex + "\n" + " from : " + e.from + "\n" + " to : " + e.to + "\n" + " value : " + e.value + "\n" + " time : "+block.timestamp +" "+new Date(block.timestamp * 1000).toGMTString() + "\n" + " gasPrice : " + e.gasPrice + "\n" + " gas : " + e.gas + "\n" + " input : " + e.input); } }) } } } We can now load the JavaScript file and determine transactions for a given user on blocks 1 to 20: > loadScript('trans.js') true > getTransactionsByAccount(eth.accounts[0],1,20) Searching for transactions to/from account "0xc7552f45deb093cafb47286a0bc9415845ca3735" within blocks 1 and 20 tx hash : 0xcc943bcd0210882febc3bce1c2c118967976ea86449d3f9e5a0233af8bf5e696 nonce : 0 blockHash : 0xde3fb2117453b32a4d9403a3268b2ba27398a06f62a1c86955d986a735f563b4 blockNumber : 1 transactionIndex: 0 from : 0xc7552f45deb093cafb47286a0bc9415845ca3735 to : 0x0851db3e133a15cd1c32531ffff96b4526e3cbcd value : 100000 time : 1498553568 Tue, 27 Jun 2017 08:52:48 GMT gasPrice : 18000000000 gas : 90000 input : 0x Now let's try blocks 1 to 40: > getTransactionsByAccount(eth.accounts[0],1,40) Searching for transactions to/from account "0xc7552f45deb093cafb47286a0bc9415845ca3735" within blocks 1 and 40 tx hash : 0xcc943bcd0210882febc3bce1c2c118967976ea86449d3f9e5a0233af8 bf5e696 nonce : 0 blockHash : 0xde3fb2117453b32a4d9403a3268b2ba27398a06f62a1c86955d986a735f563b4 blockNumber : 1 transactionIndex: 0 from : 0xc7552f45deb093cafb47286a0bc9415845ca3735 to : 0x0851db3e133a15cd1c32531ffff96b4526e3cbcd value : 100000 time : 1498553568 Tue, 27 Jun 2017 08:52:48 GMT gasPrice : 18000000000 gas : 90000 input : 0x undefined > getTransactionsByAccount(eth.accounts[0],1,60) Searching for transactions to/from account "0xc7552f45deb093cafb47286a0bc9415845 ca3735" within blocks 1 and 60 tx hash : 0xcc943bcd0210882febc3bce1c2c118967976ea86449d3f9e5a0233af8 bf5e696 nonce : 0 blockHash : 0xde3fb2117453b32a4d9403a3268b2ba27398a06f62a1c86955d986a735f563b4 blockNumber : 1 transactionIndex: 0 from : 0xc7552f45deb093cafb47286a0bc9415845ca3735 to : 0x0851db3e133a15cd1c32531ffff96b4526e3cbcd value : 100000 time : 1498553568 Tue, 27 Jun 2017 08:52:48 GMT gasPrice : 18000000000 gas : 90000 input : 0x tx hash : 0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861 nonce : 1 blockHash : 0xcd416ab6a3fb87eb88ce5d830d78f888db48d31d5b3eef200241ba5aeb46b377 blockNumber : 45 transactionIndex: 0 from : 0xc7552f45deb093cafb47286a0bc9415845ca3735 to : 0x0851db3e133a15cd1c32531ffff96b4526e3cbcd value : 100000 time : 1498553643 Tue, 27 Jun 2017 08:54:03 GMT gasPrice : 18000000000 gas : 90000 input : 0x undefined We can now use some JavaScript to view the transaction based on its hash: function printTransaction(txHash) { var tx = eth.getTransaction(txHash); if (tx != null) { console.log(" tx hash : " + tx.hash + "\n" + " nonce : " + tx.nonce + "\n" + " blockHash : " + tx.blockHash + "\n" + " blockNumber : " + tx.blockNumber + "\n" + " transactionIndex: " + tx.transactionIndex + "\n" + " from : " + tx.from + "\n" + " to : " + tx.to + "\n" + " value : " + tx.value + "\n" + " gasPrice : " + tx.gasPrice + "\n" + " gas : " + tx.gas + "\n" + " input : " + tx.input); } } And now print the transaction:
> printTransaction("0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861")
tx hash : 0xbfe234697a506bfb7b2c19202bdeb9938e53eb9ae78104b22f3ff95477547861
nonce : 1
blockHash : 0xcd416ab6a3fb87eb88ce5d830d78f888db48d31d5b3eef200241ba5aeb46b377
blockNumber : 45
transactionIndex: 0
from : 0xc7552f45deb093cafb47286a0bc9415845ca3735
to : 0x0851db3e133a15cd1c32531ffff96b4526e3cbcd
value : 100000
gasPrice : 18000000000
gas : 90000
input : 0x
Now we will find all the transactions in blocks. We can use the JavaScript of: function checkTransactionCount(startBlockNumber, endBlockNumber) { console.log("Searching for non-zero transaction counts between blocks " + startBlockNumber + " and " + endBlockNumber); for (var i = startBlockNumber; i <= endBlockNumber; i++) { var block = eth.getBlock(i); if (block != null) { if (block.transactions != null && block.transactions.length != 0) { console.log("Block #" + i + " has " + block.transactions.length + " transactions") } } } } Let's search for transactions between Block 0 and Block 1000:
> checkTransactionCount(0,1000)
Searching for non-zero transaction counts between blocks 0 and 1000
Block #1 has 1 transactions
Block #45 has 1 transactions
Block #68 has 2 transactions
Block #82 has 2 transactions
Block #120 has 10 transactions
Block #126 has 10 transactions
Block #155 has 1 transactions
Block #156 has 1 transactions
Block #872 has 2 transactions
Block #873 has 1 transactions
|