Parsing an ERC20 transfer with JavaScript from the ETH API
I didn’t find any concise information how to parse ERC20 tokens transfers from the ETH API (Infura for example). This is a short guide how to do it with JavaScript.
Step 1: Understanding ERC20 Standard and encoding
ERC20 is a contract standard that enables the creation of tokens on the ETH blockchain. It basically consists of a map of addresses to amounts to keep the balances, some token informations and some methods.
One of the most central methods is transfer
. The full definition always looks like this: transfer(address recipient, unit256 amount)
. This is the kind of event we want to parse.
If we take a look at the input data of this TXL token transfer on etherscan we’ll see following:
We have three hex values. The Method ID 0xa9059cbb
which will always stay the same for all ERC20 contracts and two fields with more hex data, which map to the recipient address and the amount transferred.
Step 2: Taking a closer look at the data
If we remove the first 24 characters (zeros) of the hex representation of the first value and prefix it with 0x
we will get the recipient address. We can compare the address on Etherscan to the To
address in “Tokens Transferred”.
If we convert the hex value of the second value (64 characters as hex) to decimal we’ll get 500000000000000000000
(a 5 with 20 zeros). Most ERC20 contracts have 18 decimals and so does this contract, so if we remove 18 zeros we’re left with 500
which is the amount TXL shown on Etherscan.
Great, now we know how to read the data. Let’s do it programatically.
Step 3: Getting the data from the ETH API
ETH API providers like Infura make it easy to get the data from the ETH blockchain. Create an account and get your API key. We will use the getTransactionByHash
RPC method to get the data of the transaction by hash. The request looks like this:
{
"id": 0,
"jsonrpc": "2.0",
"method": "eth_getTransactionByHash",
"params": [
"0xd1a2033b3a81d130241e097468554d7d626947c146c8ecd9a4c1b36fc6e45e54"
]
}
And we get following response:
The fields we’re interested in are:
from:
This is the sender of the transaction, the ERC20 tokens are deducted here.to:
⚠️ Careful, this is not the receiver of the tokens, but the smart contract. We’re interacting with a smart contract so theto
of the transaction is the smart contract address.input:
This is the data we’ve seen above, but only concatenated. Remember the0xa9059
the string starts with?
Step 4: Extracting the data programatically with JavaScript
I used ethjs
to interact with the ETH API and hex2dec
to parse hex data to decimal.
In line 6 we get the transaction data from Infura. We do some error handling if the transaction was not found or the data in the transaction doesn’t match our ERC20 transfer expectations.
Then we just parse the receiver and the amount (not caring for the decimals) from the input and return it all.
Executing the code while passing INFURA
url as environment variable we will get:
You can find the code here as a gist.