[Ethereum] ERC20 tokens transactions’ history from Ethereum node

addresseserc-20transactionsweb3js

I have a parity node. I developed a web3.js interface on top of it.

I have APIs whose job is to fetch addresses' balances and transactions.

Example response of transaction API:

{ tx_hash: '0xc4a2db4211686f740885ad72ef5d424f5ba6d953d583ef39382e7c1c594ecab7',
  nonce: 0,
  block_hash: '0xf2b173625cf874387302295f408ef0876c04e5778e6ccacec63fe28ff268f9a3',
  block_number: 5955478,
  transaction_index: 184,
  from: '0xAcc7662daDfCE9dA2C0d8ef7867C15A262c762D7',
  to: '0x3495Ffcee09012AB7D827abF3E3b3ae428a38443',
  value: '0',
  time: '1531467099 Fri, 13 Jul 2018 07:31:39 GMT',
  gas_price: '5000000000',
  gas: 37351,
  input: '0xa9059cbb000000000000000000000000c5c813994f9142d11d73b47e1437c81e27b4dae3000000000000000000000000000000000000000000000000b469471f80140000' }

value key is for ether/wei transfers only.

For ERC20 tokens:

  • I fetch the to address and compare it to a list of supported erc20
    contract addresses.
  • If there is a match, the transaction call was to erc20 contract and it wrote to the blockchain (contract.send())
  • At this point I am lost. How to figure out which function of ERC20 standard contract was called?
  • How to fetch how many tokens were transferred and to which address in this call?

Thanks for reading my query. Any help would be really great. You are free to use the linked git project.

UPDATE

If we use getPastEvents function of web3.js and fetch all events, then a matching can be done against all transactions and I can figure out somehow how many tokens were delivered and when.

But this will be a complicated flow, and will be done per contract basis. Anything that can avoid calling events and figure out the details of the erc20 transactions? what if the contract didn't call any event?

Best Answer

using ethersjs. This will return all transfer logs for all tokens on the network.

Under the hood it takes the signature from the event out of the ABI , which is the first element needed in the topics array (the remaining optional arguments are the indexed arguments from the event)

It then makes an RPC call for these events to the node you supplied when creating the provider.

On succesful response it parses the received hex-encoded data back to human readable data through reading the types from the ABI.

const provider = new providers.JsonRpcProvider('http...')
const event = (new Interface(StandardToken.abi)).events.Transfer
const topics = [event.topics[0]]
let logs = await provider.getLogs({
  fromBlock: 1,
  toBlock: 'latest',
  topics: topics
})

logs =  logs.map(log => event.parse(log.topics, log.data))
Related Topic