How to get information about an ERC-20 transaction with JavaScript web3
In this tutorial, we are going to learn how to get information about an ERC-20 transaction with JavaScript and the Web3.js library.
In this tutorial, we are going to learn how to get information about an ERC-20 transaction with JavaScript and the Web3.js library.
Note that this also works for all EVM blockchains. That means that you can use the same code for the Binance Smart Chain, Fantom, Polygon or Avalanche networks.
The only thing that will determine that is the the network of the provider you're using. If it's an HTTP provider (an API) then it's the network that the API is connected to that matters. If you're connected to a user's wallet, it's the selected wallet on that network that matters.
Here you can find a list of all EVM blockchains: https://chainlist.org/
First of all, install the libraries:
npm install web3 abi-decoder
You will need to instantiate Web3
with a provider. It can be an HTTP provider (like Infura or another node provider API) or it can be a wallet that the user connected to your website.
Learn how to connect a wallet to your website here
In this tutorial, we use Infura as it supports Ethereum:
import Web3 from 'web3';
const web3 = new Web3(new Web3.providers.HttpProvider("INFURA_URL_HERE"));
Next, you will need to define an ABI, it is like an interface that explains how the contract methods are created. In our case, the ABI interfaces the transfer
method of an ERC-20 token smart contract:
const ABI = [
{
constant: false,
inputs: [
{
name: 'to',
type: 'address',
},
{
name: 'value',
type: 'uint256',
},
],
name: 'transfer',
outputs: [
{
name: '',
type: 'bool',
},
],
type: 'function',
},
];
Next, you need to grab the transaction hash of the transaction you want to get information for and then call the getTransaction
method:
import abiDecoder from 'abi-decoder'
const transactionHash = "0xd40abef1e838b89493d5c0af098c93303b9b37e44d55b75be183895aab1dad41"
// this is a Rinkeby transaction (it is an Ethereum Testnet)
web3.eth.getTransaction(transactionHash)
.then((tx) => {
console.log(tx);
abiDecoder.addABI(ABI);
const decodedData = abiDecoder.decodeMethod(tx.input);
console.log(decodedData);
});
In the getTransaction
method's .then
block, the returned data is a transaction object that has this format (to learn more about that format, check out our guide on how to get a transaction in Web3):
{
blockHash: '0x6a33387fb37fdfa7bfae4a3f6a7bb1349b56e737c75cc8a3c3e54799bde9a9b6',
blockNumber: 10679851,
from: '0x5F70Ddd9908B04f952b9cB2A6F8E4D451725ceDC',
gas: 200000,
gasPrice: '2310817425',
hash: '0xd40abef1e838b89493d5c0af098c93303b9b37e44d55b75be183895aab1dad41',
input: '0xa9059cbb0000000000000000000000006a8fcbdcfeeeb2b4426f92cbf5b34951e66eaf6b00000000000000000000000000000000000000000000000000b1a2bc2ec50000',
nonce: 37,
r: '0xa6ea83e8311cbb3bc597bb48ef823863dbc852c2a1208013486c35c2d8ee5243',
s: '0x6a73245cef31a35de09f410cfea50ea13ab3ddb94626116bad0dfc835c36026a',
to: '0x01BE23585060835E02B77ef475b0Cc51aA1e0709',
transactionIndex: 39,
type: 0,
v: '0x2b',
value: '0'
}
However when you send an ERC-20 transfer transaction, there is no value
in the transaction as the amount sent is passed in the contract's value parameter and not in the transaction value directly. So you can't know what's the transferred amount directly. The to
property is the address of the contract so you can't know where the tokens went.
To get that information, you need to decode the input
of the transaction. It will show you the parameters you sent to the contract to make the transfer.
That's what we do with the ABI decoder in the .then
block which gives us this data:
{
name: 'transfer',
params: [
{
name: 'to',
value: '0x6a8fcbdcfeeeb2b4426f92cbf5b34951e66eaf6b',
type: 'address'
},
{ name: 'value', value: '50000000000000000', type: 'uint256' }
]
}
name
is the method name in the smart contractparams
is the list of parameters that were passed to the method, as defined in the ABI above. In our case we have:- the
to
parameter with the value being the address that the transaction was sent to - the
value
parameter which is the amount of tokens that were sent in the transfer
And that's it! 👏
Thanks for reading this article!