1

This question is a follow up question based on the answer I got to the question How many transfer events are there in Ethereum?

The answer mentioned that:

There is no event emitted when an account transfers Ether to another account as events are accessible solely by declaring them in smart contracts and then calling them.

How then can normal transaction activities involving just transfer of ethers tracked?

If I am given an EOA account,

  • how can one retrieve all transaction history programmatically?
  • and how can one listen to real-time transfers happening to/from that account?

2 Answers 2

2

Since there isn't event for ETH transfer that you can follow, you can enter the block or blocks in the specific period that you want to monitor, then get msg.value from all of transactions and calculate them. Transfer transactions shouldn't have any log, but if someone pays ETH for interacting with a contract, almost always there is log.and also in the transfer transactions there is a From and To to track address both of sender and receiver. for example this is a ETH transaction: look at this

0

Following your questions:

How can one retrieve all transaction history programmatically?

You can get the transactions of a wallet using Moralis Transaction API.

Here is an example of getting the transactions using moralis

const Moralis = require("moralis").default;

const runApp = async () => {
  const { EvmChain } = Moralis.EvmUtils;

  await Moralis.start({
    apiKey: "YOUR_API_KEY",
    // ...and any other configuration
  });

  const address = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";

  const chain = EvmChain.ETHEREUM;

  const response = await Moralis.EvmApi.transaction.getWalletTransactions({
    address,
    chain,
  });
  console.log(response.raw);
};

runApp();

And this is what the results will look like.

{
  "total": "2000",
  "page": "2",
  "page_size": "100",
  "result": [
    {
      "hash": "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
      "nonce": 326595425,
      "transaction_index": 25,
      "from_address": "0xd4a3BebD824189481FC45363602b83C9c7e9cbDf",
      "to_address": "0xa71db868318f0a0bae9411347cd4a6fa23d8d4ef",
      "value": 650000000000000000,
      "gas": 6721975,
      "gas_price": 20000000000,
      "input": "string",
      "receipt_cumulative_gas_used": 1340925,
      "receipt_gas_used": 1340925,
      "receipt_contract_address": "0x1d6a4cf64b52f6c73f201839aded7379ce58059c",
      "receipt_root": "string",
      "receipt_status": 1,
      "block_timestamp": "2021-04-02T10:07:54.000Z",
      "block_number": 12526958,
      "block_hash": "0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86"
    }
  ]
}

You can find a guide here: How to get transactions by wallet

For the second question: How can one listen to real-time transfers happening to/from that account?

You can use moralis streams API to listen to real-time blockchains events, including native eth transactions

Set up a stream

const options = {
  chains: [EvmChain.ETHEREUM], // list of blockchains to monitor
  description: "Listen to transfers", // your description
  tag: "transfers", // give it a tag
  includeContractLogs: false,
  includeNativeTxs: true,
  webhookUrl: "https://YOUR_WEBHOOK_URL", // webhook url to receive events,
};

const stream = await Moralis.Streams.add(options);

const { id } = stream.toJSON(); // { id: 'YOUR_STREAM_ID', ...stream }

// Attach the address to the stream
await Moralis.Streams.addAddress({
  id,
  address: "0x0.........2e1e", // Wallet address to monitor
});

Feel free to check the docs for more details, and to check some examples https://docs.moralis.io/streams-api

You can check a more detailed guide if you want to try out streams: https://youtu.be/pnmVhxdUBao

Disclosure: I work at Moralis

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.