chain_subscribeFinalizedHeads - FLOP RPC Method
Subscribe to finalized block headers on FLOP. Real-time WebSocket notifications for blocks that have achieved GRANDPA finality - essential for exchanges, bridges, and applications requiring irreversibility on the Substrate Proof of Inference Layer 1 built for autonomous AI agents.
FLOP RPC endpoint: endpoint URLs, plans and pricing.
Subscribe to receive notifications when blocks are finalized on FLOP. Finalized blocks are guaranteed to never be reverted by the GRANDPA finality gadget, making this the safest way to track confirmed state changes.
Why FLOP? Build on the Substrate Proof of Inference Layer 1 built for autonomous AI agents with Proof of Inference with TOPLOC activation commitments, full historical archive state from genesis, and JSON-RPC v2 archive and chainHead APIs.
When to Use This Method
chain_subscribeFinalizedHeads is critical for AI agent builders, FLOP miners, indexers, and Substrate developers:
- Exchange Deposits - Only credit funds after finalization for Proof of Inference settlement, 1-second BABE blocks with AlephBFT finality, archive state queries, and HTLC atomic swaps
- Bridge Operations - Wait for finality before executing cross-chain transfers
- Critical State Changes - Ensure irreversibility before acting on important transactions
- Compliance Workflows - Record-keeping that requires provably irreversible state
Best Practices
- Requires a WebSocket connection at
wss://api-flop-testnet.n.dwellir.com/YOUR_API_KEY. - FLOP uses AlephBFT finality (not GRANDPA or BEEFY), so finalized headers streamed here represent AlephBFT-confirmed irreversible blocks.
- Prefer this subscription or JSON-RPC v2
chainHead_v1_followover HTTP polling on FLOP's 1-second blocks. - Unsubscribe with
chain_unsubscribeFinalizedHeadswhen finished to free connection resources.
{
"jsonrpc": "2.0",
"method": "chain_subscribeFinalizedHeads",
"params": [],
"id": 1
}Code Examples
# WebSocket subscription (requires wscat or similar tool)
wscat -c wss://api-flop-testnet.n.dwellir.com/YOUR_API_KEY -x '{
"jsonrpc": "2.0",
"method": "chain_subscribeFinalizedHeads",
"params": [],
"id": 1
}'Common Use Cases
1. Exchange Deposit Watcher
Watch for finalized transfers and credit user accounts on FLOP:
async function watchDeposits(api, depositAddresses) {
const unsub = await api.rpc.chain.subscribeFinalizedHeads(async (header) => {
const blockHash = header.hash;
const block = await api.rpc.chain.getBlock(blockHash);
const apiAt = await api.at(blockHash);
const events = await apiAt.query.system.events();
// Check for transfer events in the finalized block
events.forEach((record) => {
const { event } = record;
if (event.section === 'balances' && event.method === 'Transfer') {
const [from, to, amount] = event.data;
if (depositAddresses.includes(to.toString())) {
console.log(`Finalized deposit: ${amount} from ${from} to ${to}`);
// Credit user account - this block will never be reverted
}
}
});
});
return unsub;
}2. Finality Lag Tracker
Monitor the gap between best and finalized blocks:
async function trackFinalityLag(api) {
let bestNumber = 0;
api.rpc.chain.subscribeNewHeads((header) => {
bestNumber = header.number.toNumber();
});
api.rpc.chain.subscribeFinalizedHeads((header) => {
const finalizedNumber = header.number.toNumber();
const lag = bestNumber - finalizedNumber;
console.log(`Best: #${bestNumber} | Finalized: #${finalizedNumber} | Lag: ${lag} blocks`);
if (lag > 10) {
console.warn('WARNING: High finality lag detected - GRANDPA may be stalling');
}
});
}3. Cross-Chain Bridge Relay
Relay finalized headers to a bridge contract:
async function relayFinalizedHeaders(api, bridgeContract) {
const unsub = await api.rpc.chain.subscribeFinalizedHeads(async (header) => {
const headerData = {
number: header.number.toNumber(),
stateRoot: header.stateRoot.toHex(),
extrinsicsRoot: header.extrinsicsRoot.toHex(),
parentHash: header.parentHash.toHex()
};
console.log(`Relaying finalized header #${headerData.number}`);
await bridgeContract.submitHeader(headerData);
});
return unsub;
}Finality Lag
Finalized blocks typically lag behind the best block by a few blocks due to GRANDPA consensus requirements. This lag is normal and ensures Byzantine fault tolerance. The typical lag is 2-3 blocks under healthy network conditions.
Error Handling
| Error Code | Description | Solution |
|---|---|---|
| -32603 | Internal error | Node may be syncing or GRANDPA not running - retry connection |
| -32601 | Method not found | Verify the node supports WebSocket subscriptions |
| -32005 | Rate limit exceeded | Reduce subscription count per connection |
| Connection closed | WebSocket disconnected | Implement automatic reconnection with backoff |
Related Methods
chain_subscribeNewHeads- Subscribe to all new blocks (not just finalized)chain_getFinalizedHead- Get current finalized block hash (one-shot)grandpa_roundState- Monitor GRANDPA consensus progresschain_getBlock- Get full block data for a finalized hash
chain_subscribeNewHeads
Subscribe to new block headers on FLOP. Real-time WebSocket notifications for every new block as it is produced - essential for monitoring, indexing, and event-driven applications on the Substrate Proof of Inference Layer 1 built for autonomous AI agents.
state_getStorage
Read a storage value by key on FLOP. Query account balances, pallet state, and any on-chain data using SCALE-encoded storage keys for Proof of Inference settlement, 1-second BABE blocks with AlephBFT finality, archive state queries, and HTLC atomic swaps.