1

I am trying to get a list of which accounts sent ether to 1 specific account (or wallet contract), and how much they sent. Is there any function in web3 that can do this. I assume it would look something like:

var transactions = web3.eth.getTransactionHistory();
console.log("sender: " + transactions.sender[0]);
console.log("amount: " + transactions.amount[0]);

These are just madeup function names that I'm using to explain what I'm basically looking for. Would anyone know how I might be able to do this?

1

You could do something like this (modified from this answer):

function getSendersToAccount(myaccount, startBlockNumber) {
  let senders = [];
  let values = [];
  if (endBlockNumber == null) {
    console.log("Using endBlockNumber: " + endBlockNumber);
  }
  if (startBlockNumber == null) {
    startBlockNumber = endBlockNumber - 1000;
    console.log("Using startBlockNumber: " + startBlockNumber);
  }
  console.log("Searching for transactions to \"" + myaccount + "\" within blocks "  + startBlockNumber + " and " + endBlockNumber);

  for (var i = startBlockNumber; i <= endBlockNumber; i++) {
    if (i % 1000 == 0) {
      console.log("Searching block " + i);
    }
    var block = web3.eth.getBlock(i, true);
    if (block != null && block.transactions != null) {
      block.transactions.forEach( function(e) {
        if (myaccount == e.to) {
          senders.push(e.from);
          values.push(e.value);
        }
      })
    }
  }
  return [senders, values];
}

Shorter code

You could remove the block limits (would make it slower since it has to go from a much earlier time), and get something like this:

function getSendersToAccount(myaccount, startBlockNumber) {
  let senders = [];
  let values = [];
  let endBlockNumber = web3.eth.blockNumber;

  for (var i = 1; i <= endBlockNumber; i++) {
    let block = web3.eth.getBlock(i, true);
    if (block != null && block.transactions != null) {
      block.transactions.forEach( function(e) {
        if (myaccount == e.to) {
          senders.push(e.from);
          values.push(e.value);
        }
      })
    }
  }
  return [senders, values];
}

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.