From da5b8d94c9731cfeb3b235c119f5a422515b1a0e Mon Sep 17 00:00:00 2001 From: Mostafa Date: Wed, 1 Jul 2026 17:36:54 +0800 Subject: [PATCH 1/2] docs: add example for block decoding --- examples/example_decode_block.py | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/example_decode_block.py diff --git a/examples/example_decode_block.py b/examples/example_decode_block.py new file mode 100644 index 0000000..37073af --- /dev/null +++ b/examples/example_decode_block.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +""" +Pactus SDK Example: Fetch and Decode a Block +============================================= + +This example demonstrates how to: +1. Connect to the Pactus testnet via JSON-RPC +2. Fetch blockchain info and pick a random block height +3. Retrieve a block data and decode the block using pactus-sdk +4. Print the block's details, including header, previous certificate, and transactions. + +Usage: + python sdk_example.py +""" + +import asyncio +import random + +from pactus.block import Block +from pactus_jsonrpc.client import PactusOpenRPCClient + +TESTNET_RPC = "https://testnet1.pactus.org/jsonrpc" + +async def fetch_and_decode_block(): + client = PactusOpenRPCClient( + headers={}, + timeout=30, + client_url=TESTNET_RPC, + ) + + # print("Connecting to Pactus testnet...") + # info = await client.pactus.blockchain.get_blockchain_info() + # latest_height = info["last_block_height"] + # print(f" Latest height: {latest_height}") + + height = random.randint(10_000, 1_000_000) + print(f"\nFetching block at height {height}...") + + res = await client.pactus.blockchain.get_block(height=height, verbosity=0) + block = Block.decode(bytes.fromhex(res["data"])) + + print(f" - Hash: {block.id}") + print(f" - Version: {block.header.version}") + print(f" - Prev Hash: {block.header.prev_block_hash}") + print(f" - Time: {block.header.unix_time}") + print(f" - State Root: {block.header.state_root}") + print(f" - Proposer: {block.header.proposer_address.string()}") + + print(" - Previous Certificate:") + print(f" - Committers: {block.prev_cert.committers}") + print(f" - Absentees: {block.prev_cert.absentees}") + print(f" - Signature: {block.prev_cert.signature.string()}") + + + print(" - Transactions:") + for i, tx in enumerate(block.transactions): + print(f" - {i}: {tx.id()}") + +if __name__ == "__main__": + asyncio.run(fetch_and_decode_block()) From 414efca21dcff3f8fd2da7dc3c122c66a3126336 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Wed, 1 Jul 2026 17:39:34 +0800 Subject: [PATCH 2/2] docs: add example for block decoding --- examples/example_decode_block.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/example_decode_block.py b/examples/example_decode_block.py index 37073af..d172f05 100644 --- a/examples/example_decode_block.py +++ b/examples/example_decode_block.py @@ -8,9 +8,6 @@ 2. Fetch blockchain info and pick a random block height 3. Retrieve a block data and decode the block using pactus-sdk 4. Print the block's details, including header, previous certificate, and transactions. - -Usage: - python sdk_example.py """ import asyncio