How to get events from block to block
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 events from block to block
In order to get events from block to block, Blocklens provides you a getEventsFromBlockToBlock endpoint to do so.
Here you'll need parameters: from_block
, to_block
.
Once you've obtained all the from_block
, to_block
, 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.APTOS_MAINNET.getEventsFromBlockToBlock({"from_block":18246150,"to_block":18246151});
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:
[
{
"cursor": "string",
"page": 0,
"limit": 0,
"total_items": 0,
"data": [
{
"block_height": 48569766,
"block_time": "2023-06-13 18:17:09.819",
"timestamp": 1686655029819390,
"transaction_version": 125033617,
"transaction_hash": "0xdf4b0d8b621cd39e3d3dc4e24f82f0ad045014653aa0ccee9256810e1db3c8ef",
"transaction_type": "block_metadata_transaction",
"creation_number": 3,
"account_address": "0xb723f7fb05992065f97b0562bf5a6eac95f6044134efd2ee2bf95607eb1942c7",
"sequence_number": 17,
"type": "0x1::coin::WithdrawEvent",
"event_index": 1,
"amount": 4441891,
"module_name": "0x6173736574",
"struct_name": "0x6173736574",
"data": "{\"amount\": \"11385\"}",
"block_hash": "0xa9f9ea4852444a91a1d259175f4e51a9c13715391dbf91e16992c130eb31f3c5",
"success": true,
"vm_status": "Executed successfully"
}
]
}
]
Congratulations 🥳 you just found the get events from block to block 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.