4

I'm currently developing a Java application which communicates with Geth using JSON-RPC.

I managed to implement something like @rustyx suggested on this thread using an HTTP connection, but it's too slow.
On my machine, the time to retrieve all the transactions for an account in a 1000 blocks range is approximately 100 seconds.

I know Etherscan's API is able to return the last 10'000 transactions made by an account, but you will need a API key and you have limited requests per day. Using an indexer could do the trick, but it's heavy and quite complex to implement.

So, i was wondering, is there any efficient way to retrieve all the transactions for a given account through a series of JSON-RPC requests? Is it possible to implement by myself a eth_listTransactions method? Could using a lower level programming language (like C) reduce the processing time?

Note: With 'addresses' I'm referring to wallet addresses, not contracts addresses.

3 Answers 3

1

This is not a "native" RPC method but Alchemy does the job with the endpoint alchemy_getAssetTransfers. If you want the raw transactions (logs) you can get the txn hashes from that endpoint and later call eth_getTransactionByHash and/or eth_getTransactionReceipt for each txn hash. You will need to register but the free account is still pretty good and you won't need to iterate over blocks. The speed will depend on your internet connection and if you use concurrent requests. It's not CPU/memmory intensive so I don't think that using C will improve the performance.

1

Disclosure: I work with Moralis

We've just launched support for extended RPC APIs, and we now have an extended RPC method that does exactly this!

With our RPC method eth_getTransactions, you can get all transactions for a given wallet address. You also have the ability to query in batches with a cursor.

Here's a sample Node.js script that lets you do this

// Dependencies to install:
// $ npm install node-fetch --save
// add "type": "module" to package.json

import fetch from 'node-fetch';

const options = {
  method: 'POST',
  headers: {
    accept: 'application/json',
    'content-type': 'application/json'
  },
  body: JSON.stringify({
    "jsonrpc": "2.0",
    "id": 1,
    "method": "eth_getTransactions",
    "params": [
      {
        "address": "0x357C52eBaDAd41B68C0950E2c103576e38D06BcC",
        "fromBlock": 19943515,
        "toBlock": 19943536,
        "limit": 100,
        "cursor": "eyJhbGciOiJIUzI1NiJ9..."
      }
    ]
  })
};

fetch('https://site1.moralis-nodes.com/eth/YOUR_API_KEY', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

Here's a link to a tutorial that covers how you can fetch transactions using RPC - https://youtu.be/HJMI8R-aEVI?si=1aXe7VVCX1KHPlMT

0

you can use getPastEvents for the Transfer event and also specify block range from first block to latest

js version:

      const latest = await web3.eth.getBlockNumber(),
      contract = new web3.eth.Contract(abi, contractAddress),
      logs = await contract.getPastEvents(
                "Transfer", 
                {
                  fromBlock: 0,
                  toBlock: latest,
                  filter:
                     eventType === EventType.send ?
                                { from: address } : { to: address },
                 }
              );
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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