[Ethereum] How to track token transactions

internal-transactionstokenstransactions

I'm currently trying to get a deeper understanding of how transactions in the Ethereum Blockchain work. A not so trivial example seems to track token balance of an address.

I thought about the following example: Having nothing but an address and a full node, how can I get all the transactions made by this address, including token transfers and amount of transferred tokens?

I understand that tokens are not more than mapped addresses in a smart contract (correct me if I'm wrong) and so my first thought was to just read the balanceOf() method (assuming its ERC20 standard). But that would just give me the current amount, not all the past transactions.

So what gives me some headaches now is trying to "connect" (like wire the concept in my brain) addresses and smart contracts. How to determine if an address is a smart contract, figure out if the given address is holding (or held) tokens of that contract and if so, get the transaction history of the given address. Furthermore, is there some kind of identifier to transactions?

An example so you get what I mean:
I trade some tokens on Etherdelta (just an example, idk if it is a good one) and the I/O order to my address is not "First in, First out", how can I know which transaction belongs to which trade?

(all tokens are the same)

  • I send 10 tokens(A) to Etherdelta
  • I send 12 more tokens(B) to Etherdelta
  • I send 3 more tokens (C) to Etherdelta
  • I recieve 0.24 Eth from trade B
  • I recieve 0.10 Eth from trade A
  • I recieve 0.03 Eth from trade C

If you didn't already get what I was talking about, I guess you're getting it now. Even if I would be able to track all those transactions from the given address, is it possible to determine transaction pairs, like corresponding A, B and C in the example above?

Thanks for any help!

Best Answer

If we are talking about EtherDelta in particular you could retrieve the data from their even logs.

Here's EtherDelta contract: https://etherscan.io/address/0x8d12a197cb00d4747a1fe03395095ce2a5cc6819#code

As you can see, each transaction being done with the contract has an event log. You can see it by clicking on any completed transaction hash.

EtherDelta fires these events depending on the transaction

  event Order(address tokenGet, uint amountGet, address tokenGive, uint amountGive, uint expires, uint nonce, address user);
  event Cancel(address tokenGet, uint amountGet, address tokenGive, uint amountGive, uint expires, uint nonce, address user, uint8 v, bytes32 r, bytes32 s);
  event Trade(address tokenGet, uint amountGet, address tokenGive, uint amountGive, address get, address give);
  event Deposit(address token, address user, uint amount, uint balance);
  event Withdraw(address token, address user, uint amount, uint balance);

You could get all the transactions associated to this contract using web3 and then filter them, this could prove tricky as these events are not indexed.

Another thing you could do is get the event logs of the tokens themselves. [If they are ERC20 tokens, they have the Transfer() event][1] which would allow you to know how tokens are moving between accounts for a particular token contract.

event Transfer(address indexed _from, address indexed _to, uint256 _value)

In this case, the parameters are indexed so, for example, you could filter _from: your address _to: EtherDelta contract to get all transactions sent by you to them.

Related Topic