0

I'd like to have a better understanding of events and logs data, more specifically when it comes to interacting with ERC20 tokens smart contracts and their Transfer events.

I'm looking for a way to extract the transfer value/amount from the returned (transaction) Log struct as described in the rust-web3 documentation.

I understand that from the event log, the topics field has 3 values:

  • topics[0] is the keccak-256 of the Transfer(address,address,uint256) canonical signature.
  • topics[1] is the value of the _from address.
  • topics[2] is the value of the _to address.

However, I cannot seem to find the ERC20 token transaction value in the returned Log struct or the log or input field as described in this thread and this one.

I suspect I can extract the transfer amount in an ERC20 token transaction from the data field in the events log but I haven’t figured how to do that yet. data is of type Bytes, i.e. a Vec<u8>, a vector of 32 u8.

Thanks.

khangle27
  • 11
  • 2

1 Answers1

1

Found the answer to my question:

The data field from the returned Log struct contains indeed the transferred token amount value:

The Bytes' vector needs to be converted into a hexadecimal figure (by looping through the vector and converting its value at each index into a hex and concatenating all the converted values together) to look like the log's data shown in the first linked thread.

eg:

data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 141, 126, 164, 198, 128, 0])

is equivalent to:

data: 0000000000000000000000000000000000000000000000000de0b6b3a7640000

which converted back to a single decimal is:

1000000000000000000 = 10^(18) => 1 ERC20 transferred token

khangle27
  • 11
  • 2