4

I want to get the list of all the transactions which have been performed by/on an account in my private blockchain. I tried using the function

getTransactionsByAccount(accountAddress, startBlockNumber, endBlockNumber) 

given on this link. The problem with this method is that for transactions which include transfer (of amount) from one account to another, it gives the correct address in "from" field, but incorrect one for "to" field.

So, I'd like to ask if there is some other method by which we can get the transaction history???

2 Answers 2

6
contractAddress = "0x00.." web3.eth.filter({ address: contractAddress from: 1, to: 'latest' }).get(function (err, result) { // callback code here }) 

You can read the docs here

5
  • 2
    This works for contract's address it will not work for external owned accounts. Commented Aug 29, 2017 at 15:54
  • 1
    I don't know what difference it makes, but on storing contract address in a variable and then passing it into the filter doesn't work, whereas passing the contract address directly into the filter yields the correct result. Also, 'from' and 'to' should be replaced by 'fromBlock' and 'toBlock'. Commented Aug 31, 2017 at 11:30
  • Can someone please update this answer for web3.js 1.x? web3.eth.filter is no longer available and i couldn't find anything in the docs to achieve what was originally being asked for. Commented Jul 27, 2018 at 15:14
  • Also this only works if the contract has generated an event. Commented Oct 13, 2018 at 22:03
  • The link to the doc doesnt exist. Does it work for the current web3? Commented Jul 14, 2021 at 13:46
1

Yeah, the above mentioned approach works for Contract Addresses. Unfortunately, there is not any method in Web3js which will help us achieve this task. But, the way I did it was going from the 0 block-number to Latest. While going through I got all the transactions that happened in the block along with the details of the transaction.

Below is the code:

I have used web3.eth.getBlock, to get all transactions from the block.

function getAccountTransactions(accAddress, startBlockNumber, endBlockNumber) { // You can do a NULL check for the start/end blockNumber console.log("Searching for transactions to/from account \"" + accAddress + "\" within blocks " + startBlockNumber + " and " + endBlockNumber); for (var i = startBlockNumber; i <= endBlockNumber; i++) { var block = eth.getBlock(i, true); if (block != null && block.transactions != null) { block.transactions.forEach( function(e) { if (accAddress == "*" || accAddress == e.from || accAddress == 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" + " gasPrice : " + e.gasPrice + "\n" + " gas : " + e.gas + "\n" + " input : " + e.input); } }) } } } 

Example Transaction Details on Ropsten Etherscan

Find transactions to/from eth.accounts[0] address:

getTransactionsByAccount(eth.accounts[0]) endBlockNumber: 1575620 startBlockNumber: 1575619 Searching for transactions to/from account "0x87bae5d55603cfe2703062cea8bf76263fa0ca95" within blocks 1575619 and 1575620 tx hash : 0xd55f196cd4a41f518c84804ac6286972db1d0fa8e51d137e00299fb4906a51ab nonce : 12044 blockHash : 0xdac37667bca025e8107cea79b6b9bc5c1d86ddbec5a8b13615a2ce70bec63e60 blockNumber : 1575620 transactionIndex: 0 from : 0x87bae5d55603cfe2703062cea8bf76263fa0ca95 to : 0x60b634cf459c6e88dfb2a52311199a8473ed85b5 value : 0.000909211685644988 gasPrice : 20000000000 gas : 21000 input : 0x 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.