How to get decoded events by transaction hash
Prerequisites
Before getting started, make sure you have the following ready:
- Node v.14+
- NPM
Step 1: Setup Blocklens
First register your Blocklens account and get your Blocklens API Key.
Once you have your Blocklens API Key, install the Blocklens SDK in your project.
npm i blocklens-sdk
Step 2: Get decoded events by transaction hash
In order to get decoded events by transaction hash, Blocklens provides you a getDecodedEventsByTransactionHash endpoint to do so.
Here you'll need a parameter: transaction_hash
.
Once you've obtained the transaction_hash
, you can copy the following code:
import Blocklens from "blocklens-sdk";
const blocklens = new Blocklens();
blocklens.init({ apiKey: "YOUR_API_KEY" });
const runApis = async () => {
const response = await blocklens.ETH_MAINNET.getDecodedEventsByTransactionHash("0x12259c8b5a0f51320bb1b64dfb2b1b61e24aaa127318b0cf92d209525f0d5447");
console.log(response);
};
runApis();
Step 3: Run the script
ts-node index.ts
In your terminal, you should see the following JSON response with the data: In your terminal, you should see the following JSON response with the data blocks:
{
"page": 1,
"limit": 20,
"cursor": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjB4MDAwMTZkNzU3MDk5MTM4NTdhYmE0ZmE2ZTdiYThmNTRjNDRmMjUzZDk5YWNhMjYwMzdkODk0ODA4YTc4YTEyYV83ODciLCJwYWdlIjoxLCJ0cmFuc2FjdGlvbkhhc2giOiIweDAwMDE2ZDc1NzA5OTEzODU3YWJhNGZhNmU3YmE4ZjU0YzQ0ZjI1M2Q5OWFjYTI2MDM3ZDg5NDgwOGE3OGExMmEiLCJpYXQiOjE2OTcwODg2NTZ9.6ebWzlmBoBbUv1kRej6bTZkSHYQGy_6o05p90eM-iqM",
"total_items": 5,
"data": [
{
"tx_hash": "0x00016d75709913857aba4fa6e7ba8f54c44f253d99aca26037d894808a78a12a",
"contract_address": "0x201F7DD733443b8fe236656DD860Da728db15364",
"block_number": 18222474,
"timestamp": 1695761843,
"block_time": "2023-09-26 20:57:23",
"signature": "Swap(address,uint256,uint256,uint256,uint256,address)",
"index": 791,
"data_decoded": "[\"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D\",{\"type\":\"BigNumber\",\"hex\":\"0x4f4419612ace7d\"},{\"type\":\"BigNumber\",\"hex\":\"0x00\"},{\"type\":\"BigNumber\",\"hex\":\"0x00\"},{\"type\":\"BigNumber\",\"hex\":\"0x0905ae138000\"},\"0xeb847066720351392957E18eC1bd5fB17868B280\"]",
"event_name": "Swap"
}
]
}
Congratulations 🥳 you just found the get decoded events by transaction hash on multiple chains with only a few lines of code using the Blocklens Wallet API!
API Reference
If you want to know more details on the endpoint and optional parameters, check out:
Support
If you face any trouble following the tutorial, feel free to reach out to our community engineers in our Discord or Forum to get 24/7 developer support.