gradium-js
v1.0.0
Published
A JavaScript library for fetching detailed block information from Polkadot-based blockchains
Maintainers
Readme
Gradium.js
A JavaScript library for fetching detailed block information from Polkadot-based blockchains.
Installation
npm install gradium-jsUsage
import { Gradium } from 'gradium-js';
async function main() {
// Initialize the Gradium client
const gradium = new Gradium('wss://rpc.polkadot.io');
await gradium.init();
try {
// Fetch block information using a block hash
const blockInfo = await gradium.fetchBlockInfo('0x123...'); // Replace with actual block hash
console.log('Block Number:', blockInfo.number);
console.log('Block Hash:', blockInfo.hash);
console.log('Timestamp:', blockInfo.timestamp);
// Access extrinsics
blockInfo.extrinsics.forEach(ext => {
console.log(`Extrinsic: ${ext.section}.${ext.method}`);
});
// Access events
blockInfo.events.forEach(event => {
console.log(`Event: ${event.section}.${event.method}`);
});
} catch (error) {
console.error('Error:', error);
}
}API Reference
Gradium Class
Constructor
constructor(wsUrl: string)Creates a new Gradium instance with the specified WebSocket URL.
Methods
init(): Promise<void>- Initializes the connection to the blockchainfetchBlockInfo(hash: string): Promise<BlockInfo>- Fetches detailed block informationfetchFinalizedHead(): Promise<Hash>- Gets the latest finalized block hashfetchHeader(hash: string): Promise<Header>- Fetches block headerfetchBlockHash(number: number): Promise<Hash>- Gets block hash by numberfetchBlock(hash: string): Promise<Block>- Fetches raw block datafetchCurrentSetId(atHash: string): Promise<string>- Gets current GRANDPA set IDfetchAuthorities(atHash: string): Promise<any>- Fetches GRANDPA authoritieshashSha3(data: string): string- Computes SHA3 hash of input data
Block Information Structure
{
hash: string; // Block hash
number: number; // Block number
parentHash: string; // Parent block hash
stateRoot: string; // State root hash
extrinsicsRoot: string;// Extrinsics root hash
timestamp: number; // Block timestamp
extrinsics: [ // Array of extrinsics
{
index: number;
section: string;
method: string;
signer: string | null;
nonce: number | null;
isSigned: boolean;
tip: number | null;
}
];
events: [ // Array of events
{
index: number;
section: string;
method: string;
phase: string;
data: any;
}
];
}Development
- Clone the repository
- Install dependencies:
npm install - Build the project:
npm run build
License
MIT
