Transactions – How to Get Type and ERC20 Tokens Involved

etherhashtransactionswallets

I', getting to know the Ethereum technology (so rookie level). By far, with the free APIs (etherscan.io, ethplorer.io, coingecko) I have been able to build a "watch wallet". It looks like this:
My App

But I can't seem to find the API which gives me more detail about all the Transactions in the given wallet. API does give me the hash, block, gas, status, address (from/to), etc… But I don't know the tokens (assets) that were involved in those transactions and I don't know which action took place (Sent/ Received/Swapped/Approved …)

Here's the raw info I get from the api (image 2), can someone guide me to the next step of how to extract those detail data?

Image 2

P/s: What I'm trying to achieve looks like this (Image 3: Zerion app)
enter image description here

Best Answer

First, you are not querying the right endpoint. If you want to fetch ERC-20 events, you must do the following query :

https://api.etherscan.io/api?module=account&action=tokentx&address=0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a&startblock=0&endblock=99999999&sort=asc&apikey=YourApiKeyToken

Notice that unfortunately, the input field value is deprecated. This is what you should have used to determine the event (Trade, Approval, Send ...)

Fortunately, we can call another method to get this data. Let's get the input data for the last transaction, so transaction with hash 0xdceb6a76cc83802ddabf8efc0b0458dfa9150866df3ad826686e47b303259cd8

https://api.etherscan.io/api?module=proxy&action=eth_getTransactionByHash&txhash=0xdceb6a76cc83802ddabf8efc0b0458dfa9150866df3ad826686e47b303259cd8&apikey=YourApiKeyToken

You can then get the encoded input data : 0xa9059cbb000000000000000000000000ddbd2b932c763ba5b1b7ae3b362eac3e8d40121a00000000000000000000000000000000000000000000000000005af3107a4000

To decode it, you can use abi-decoder. You must provide to abi-decoder the abi of ERC-20 contract :

const erc20abistr = '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]'

Then do something like this :

import abiDecoder from "abi-decoder"
const erc20Abi = JSON.parse(erc20abistr);
let decodedData = abiDecoder.decodeMethod(input);
console.log(input);

Note : instead of getting your hands dirty with etherscan api, you should try using web3js instead

Related Topic