How to get information about a transaction that calls a smart contract method Web3 JS
In this tutorial, we are going to learn how to get information about a transaction that calls a smart contract method with Web3 JavaScript.
In this tutorial, we are going to learn how to get information about a transaction that calls a smart contract method with Web3 JavaScript.
Note that you can use the same code for the Binance Smart Chain, Fantom, Polygon or Avalanche networks and all EVM networks. The selected network will depend on the provider you use.
Here you can find a list of all EVM blockchains: https://chainlist.org/
As an example, here we will learn how to get information about an NFT transfer (ERC-721 contract).
Here is the transaction we will use: https://etherscan.io/tx/0x495a5ed35ecd4f994e781fdc320c05ebccae530f07e22b295c73e9f455e86491
First of all, install the 2 libraries we will need:
npm install web3 abi-decoder
Now, you will need to instantiate Web3
with a provider, either an HTTP provider (like Infura or another node provider API) or a wallet that the user connected to your website.
Learn how to connect a wallet to your website here
Here, we use an Infura URL 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's an interface that explains how the contract methods are work. It defines the name of the method and the name of the parameters and their type.
In our case, the ABI interfaces the transferFrom
method of an ERC-721 NFT smart contract:
const ABI = [
{
constant: false,
inputs: [
{
name: 'from',
type: 'address',
},
{
name: 'to',
type: 'address',
},
{
name: 'tokenId',
type: 'uint256',
},
],
name: 'transferFrom',
outputs: [],
type: 'function',
},
];
Next, take the transaction hash of the transaction you want to get information for and call getTransaction
:
import abiDecoder from 'abi-decoder'
const transactionHash = "0x495a5ed35ecd4f994e781fdc320c05ebccae530f07e22b295c73e9f455e86491"
web3.eth.getTransaction(transactionHash)
.then((tx) => {
console.log(tx);
abiDecoder.addABI(ABI);
const decodedData = abiDecoder.decodeMethod(tx.input);
console.log(decodedData);
});
The getTransaction
method's returned data is a transaction object:
{
blockHash: '0x99c45cee060189095127c04077d186945f6912ef300cf5f1e24a3092de696782',
blockNumber: 12330202,
from: '0xaBA7161A7fb69c88e16ED9f455CE62B791EE4D03',
gas: 124587,
gasPrice: '115687500000',
hash: '0x495a5ed35ecd4f994e781fdc320c05ebccae530f07e22b295c73e9f455e86491',
input: '0x23b872dd000000000000000000000000aba7161a7fb69c88e16ed9f455ce62b791ee4d03000000000000000000000000dff71a881a17737b6942fe1542f4b88128ea57d80000000000000000000000000000000000000000000000000000000000000014',
nonce: 28,
r: '0x9140d8176152c4f17e9254adff1e1ceef9015d55879798ef098ec98aeecfc8b',
s: '0x4dec5ea7a9a6997df1a86946f7cbbb6469fa0608f0de3b760527541ac8a78700',
to: '0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D',
transactionIndex: 61,
type: 0,
v: '0x26',
value: '0'
}
To learn more about that format, check out our guide on how to get a transaction in Web3
However with that result, you can't see the parameters that were sent to the contract's method and you can't see which method was used. To do see that, we need to decode the input property.
That's what we do in the .then
block of the getTransaction
function using the ABI we created above. This is what we get:
{
name: 'transferFrom',
params: [
{
name: 'from',
value: '0xaba7161a7fb69c88e16ed9f455ce62b791ee4d03',
type: 'address'
},
{
name: 'to',
value: '0xdff71a881a17737b6942fe1542f4b88128ea57d8',
type: 'address'
},
{ name: 'tokenId', value: '20', type: 'uint256' }
]
}
Now we can see the name of the function that was called and the value of every parameter.
If you don't know which function was called, you can add all the functions of the smart contract in the ABI, decode the transaction input and the name
property will tell you which one was used and the params
will tell you what was passed to it.
To see all the methods of a smart contract, you can go to the etherscan page of that contract, in the contract tab and see all the function in the "Write Contract" section:
https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#writeContract
You can also get the whole contract's ABI in the "Code" section of the "Contract" tab:
https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#code
And that's it! 👏
Thanks for reading this article!