2

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???

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

You can read the docs here

  • 1
    This works for contract's address it will not work for external owned accounts. – Ismael Aug 29 '17 at 15:54
  • 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'. – Anchit Seth Aug 31 '17 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. – rinderwahn Jul 27 '18 at 15:14
  • Also this only works if the contract has generated an event. – Ismael Oct 13 '18 at 22:03
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.