Polkadot - Getting Extrinsics from a Block
Getting the Extrinsics from a block requires a few steps
Steps
Step_1: Get Block Hash
RPC call. We need the block number to pass it as a parameter.
Example request:
curl --location 'https://api.tatum.io/v3/blockchain/node/dot-mainnet/' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": [21453172],
"id": 1
}'
//Response:
{
"jsonrpc": "2.0",
"result": "0xe45599f512e6487668031b0ef0d1df79ccdfc3eea4f7b6987eb2e64cb5acd5e1",
"id": 1
}
Step_2: Get Block
RPC call. Take the block hash from Step_1 and pass it as a parameter
Example request:
curl --location 'https://api.tatum.io/v3/blockchain/node/dot-mainnet/' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
"jsonrpc": "2.0",
"method": "chain_getBlock",
"params": ["0xe45599f512e6487668031b0ef0d1df79ccdfc3eea4f7b6987eb2e64cb5acd5e1"],
"id": 1
}'
//Response:
{
"jsonrpc": "2.0",
"result": {
"block": {
"header": {
"parentHash": "0x49cec4687315e7aa0b3e20926dfb5b4103fcd66678de5a0e2774b5250974c85e",
"number": "0x1475974",
"stateRoot": "0x20eea15eacd5b52af0398b5894eb40c02d44c94aae2e918c6b821cfdbffbe56f",
"extrinsicsRoot": "0x480781608d53a81287a876ca2ea57e013764f786c31f8606cbeb212b146d14aa",
"digest": {
"logs": [
"0x0642414245b501030b0000008eb6151100000000e65b7d0b72ef8b189d0215e158dd5c76588e474b82a17e51f24ec581deed764313f0531a714c511935b34d3c54df07e67d346a60ef59f117f6ce77125cf0bf0eee40269abadc70052796c7259a9f7f9fd64996c0a9affd2ff1709f5324909909",
"0x044245454684039950151e4e3cf24d3059738b0dfd470a702030dbac21eab570475746b8b38043",
"0x0542414245010136b35069302a31e006977220857f48cadb93a8722df8a106336a2b1d2003ed3ad11b0624a907c2bfa6300f7a97b6bfaef55cc9918f9af27a4067d1557256d385"
]
}
},
"extrinsics": [
"0x280403000b20a0e66c9001",
"0x228402000
...
Step_3: Automatize data gathering
Here's an example code file in Python that can help you out.
Tatum staff does not troubleshoot 3rd party code.
Example code:
# import os
# import json
import requests
# TTM_URL = os.getenv("TTM_URL")
TTM_URL = "https://api.tatum.io"
API_URL = f"{TTM_URL}/v3/blockchain/node/dot-mainnet/"
print(f"API_URL: {API_URL}")
def get_block_hash(block_number):
payload = {
"jsonrpc": "2.0",
"method": "chain_getBlockHash",
"params": [block_number],
"id": 1
}
try:
response = requests.post(API_URL, json=payload)
response.raise_for_status()
result = response.json()
if 'error' in result:
raise Exception(result['error'])
return result.get("result")
except requests.exceptions.RequestException as e:
print(f"HTTP Request failed: {e}")
except Exception as e:
print(f"Error retrieving block hash: {e}")
return None
def get_block_extrinsics(block_hash):
payload = {
"jsonrpc": "2.0",
"method": "chain_getBlock",
"params": [block_hash],
"id": 1
}
try:
response = requests.post(API_URL, json=payload)
response.raise_for_status()
result = response.json()
# save to file as json dump
# with open("block.json", "w") as f:
# f.write(json.dumps(result, indent=4))
if 'error' in result:
raise Exception(result['error'])
return result.get("result", {}).get("block", {}).get("extrinsics", [])
except requests.exceptions.RequestException as e:
print(f"HTTP Request failed: {e}")
except Exception as e:
print(f"Error retrieving block extrinsics: {e}")
return []
def main():
block_number = 21453172
extrinsic_index = 2 # Specific extrinsic index we are looking for
block_hash = get_block_hash(block_number)
print(f"Block hash for block {block_number}: {block_hash}")
if block_hash:
extrinsics = get_block_extrinsics(block_hash)
if extrinsics:
if extrinsic_index < len(extrinsics):
print(f"Extrinsic at index {extrinsic_index}:", extrinsics[extrinsic_index])
else:
print(f"Extrinsic at index {extrinsic_index} not found in block {block_number}")
else:
print(f"No extrinsics found for block hash {block_hash}")
else:
print(f"Block hash for block {block_number} not found")
if __name__ == "__main__":
main()
Updated 5 months ago