15

I'm sure this question was asked before, but I did not find any result that helped my problem.

How can I get the transactions (in and out) for a given account? I use nodejs and web3?

With this line I get the number of sent transactions:

web3.eth.getTransactionCount(accounts[i])

So at least web3 should know something about the account activity.

Is there an easy way to get the transactions of my accounts? Or did I miss something about it? Do I have to crawl the entire blockchain, block by block, to get my transactions?

  • 3
    There's actually an important distinction between knowing the number of transactions and actually fetching those txs. The "transaction count" is also the nonce for the account, which is needed to create new transactions and is part of the account's state, just like the balance. Past transactions are not part of the current state, so you need to actually filter through all of the past blocks to find them – Tjaden Hess Sep 29 '16 at 13:44
  • I have the same issue. Does anyone have snippet code of Java, using web3j? Thanks – Kiem Duong Nov 2 '18 at 5:10
  • The best Answer could be the following: ethereum.stackexchange.com/a/86426/55270 – Ank_247shbm Jul 31 at 13:46
10

This script has a getTransactionsByAccount. You need to modify it by prepending web3, for example use web3.eth.blockNumber instead of just eth.blockNumber (which works in the Geth console since the Geth console includes the web3 object).

Above will give the easy transactions, and more work will be needed for How to get contract internal transactions.

In addition to @Tjaden's comment, web3.eth.getTransactionCount(account) only gives the number of transactions originating from account.

| improve this answer | |
  • Thank you. Before I asked, I found the script and pasted it into my editor. It didn't work and I hoped there is an easy way around finding why. No time to try now, but is it done when I add web3 or do have to define variables etc.? – Christoph Bergmann Sep 29 '16 at 18:31
  • 2
    web3 should be the only object you need; the other variables are passed into the getTransactionsByAccount function. – eth Oct 7 '16 at 1:15
  • If you're running the script in node, you must await each web3 call – arshbot Jun 26 '18 at 14:39
7

Simple transactions are not indexed. So you either need to iterate over all the blocks, use some kind of an off-chain index (e.g. etherscan.io), or build one up yourself and use it.

For example, you can use this heuristic approach without having to process the entire chain

var myAddr = '0xbb9bc244d798123fde783fcc1c72d3bb8c189413';
var currentBlock = eth.blockNumber;
var n = eth.getTransactionCount(myAddr, currentBlock);
var bal = eth.getBalance(myAddr, currentBlock);
for (var i=currentBlock; i >= 0 && (n > 0 || bal > 0); --i) {
    try {
        var block = eth.getBlock(i, true);
        if (block && block.transactions) {
            block.transactions.forEach(function(e) {
                if (myAddr == e.from) {
                    if (e.from != e.to)
                        bal = bal.plus(e.value);
                    console.log(i, e.from, e.to, e.value.toString(10));
                    --n;
                }
                if (myAddr == e.to) {
                    if (e.from != e.to)
                        bal = bal.minus(e.value);
                    console.log(i, e.from, e.to, e.value.toString(10));
                }
            });
        }
    } catch (e) { console.error("Error in block " + i, e); }
}

How does it work?

Using the available information about the current state (number of "from" transactions and the current balance), it goes back in time until at least so many "from" transactions have been found, and then continues going back until the balance reaches 0.

The inherent limitation is that 0-value transactions before the account was funded will not be found.


Contract events on the other hand, are indexed. So if you're developing a smart contract, you can make use of that.
Using web3 1.0.0 API:

web3.eth.getPastLogs({fromBlock:'0x0',address:'0x9e3319636e2126e3c0bc9e3134AEC5e1508A46c7'})
.then(res => {
  res.forEach(rec => {
    console.log(rec.blockNumber, rec.transactionHash, rec.topics);
  });
}).catch(err => console.log("getPastLogs failed", err));

Or via JSON RPC eth_getLogs.

| improve this answer | |
4

check etherscan.io api you can get a list of all transactions easily as an alternative

https://etherscan.io/apis

http://api.etherscan.io/api?module=account&action=txlist&address=0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken

| improve this answer | |
1

I know it's too late to answer this question. But I hope it could help others.

You can not list transactions directly using web3 and JSON-RPC. In order to have a list of translations involving an address you need to scan Whole(or a part of) blocks of ethereum blockchain to find them.

You could guess! It's pretty time intensive!

To solve this problem you need a block indexer or a block explorer. They index all blockchain data in different shape and orders. You make use of a block explorer like "Etherscan" or deploy an opensource indexer like "Blockscout".

They provide you with a more helpful API(in comparison with ETH RPC). You need to interact with "txlist" method in "account" module.

| improve this answer | |
0

I did not find this method 'web3.eth.getTransactionsByAccount' in my console. as i see, there is no direct way to get transactions by account address, maybe you should crawl the entire blockchain by your way or you can use third party api.

| improve this answer | |
  • That method doesn't exist in web3. What @eth was referring to was a script called getTransactionsByAccount. – arshbot Jun 26 '18 at 14:38
0
 web3.eth.getTransactionCount("0x8fFcABE2D92A76286b58f6C286980877C3C2C5C6")
 .then((b=console.log)=>{
    console.log(b)
    for(var i=0;i<b;i++){
            web3.eth.getBlock(b-i).then((Block)=>
            {

            a =[
                            Block.hash
                 ]
                     console.log(a);
                 var  iterator =a.values()
                 for (let elements of iterator) { 
             web3.eth.getTransactionFromBlock(elements).then(console.log)


                } 
             });
         }
         });
| improve this answer | |
0

The suitable answer to the date:

class TransactionChecker {
    constructor(address) {
        this.address = address.toLowerCase();
        this.web3 = new Web3("https://mainnet.infura.io/v3/60968ff3b2f84a0ebdff7a993f4d080b");
}

async checkBlock() {
    let block = await this.web3.eth.getBlock('latest');
    let number = block.number;
    let transactions = block.transactions;
    //console.log('Search Block: ' + transactions);

    if (block != null && block.transactions != null) {
        for (let txHash of block.transactions) {
            let tx = await this.web3.eth.getTransaction(txHash);
            if (this.address == tx.to.toLowerCase()) {
                console.log("from: " + tx.from.toLowerCase() + " to: " + tx.to.toLowerCase() + " value: " + tx.value);
            }
        }
    }
  }
}

 var transactionChecker = new  TransactionChecker('0x69fb2a80542721682bfe8daa8fee847cddd1a267');
 transactionChecker.checkBlock();

Output:

 from: 0x240946ca5418ba362e9011d6454ff858adc0e0ae to: 0x2c334d73c68bbc45dd55b13c5dea3a8f84ea053c value: 2000000000000000000
 from: 0x410282b6e53c0d48007ace4874b6afa6754425f7 to: 0xe6c46e42256703de29e37b01a246e11a5b102c6b value: 450000000000000000
 from: 0xffc5e446d5f32f7c8479b5eae4271751dacd45b8 to: 0x7a250d5630b4cf539739df2c5dacb4c659f2488d value: 1000000000000000000
 from: 0xd8376d071c27582fb15c6a9a46eaf23e6adaf76e to: 0x7a250d5630b4cf539739df2c5dacb4c659f2488d value: 2000000000000000000
 from: 0x7f5896c91a1bf9e94d4d81b142daa61d50c8eecb to: 0xba2eac592746fc7427247b591507dac51c51a500 value: 0
 from: 0x02314d41f62e62c02b8cbd036334573db1334f11 to: 0x4d62c46898bdcd6b14118646ed1401336c9a47b3 value: 0
 from: 0x9cc53c6f67332f7c4aa5a56f83121e51d30c5923 to: 0x0a331b3de09c986eaaf967d8b493b816cb7902e8 value: 296699999999999940
 from: 0xc09e7e05da6b021d881b0e3ec580307c40102e2d to: 0xaf4afb03963f3f5f87a3e20a0f9191270c7b3436 value: 638368162441200000
 from: 0x32be343b94f860124dc4fee278fdcbd38c102d88 to: 0xa910f92acdaf488fa6ef02174fb86208ad7722ba value: 1487418725338565767
 from: 0x343b18bf65d9d2a64bb763246e207d2d303328bf to: 0xdac17f958d2ee523a2206206994597c13d831ec7 value: 0
 from: 0x343b18bf65d9d2a64bb763246e207d2d303328bf to: 0xdac17f958d2ee523a2206206994597c13d831ec7 value: 0
 from: 0xcb0249c85ba9b6e32d79adaae2f9660ae0dfbefc to: 0x239220734954c95c852f0fc059ec783a3f0e283c value: 1000000000000000
| improve this answer | |

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.