BC9: Apps that talk to the chain

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Reading state and history

🎯You'll be able to

Read balances and events efficiently, format amounts exactly, and know when to use an indexer.

🏠The everyday picture

A clerk who reads the latest balance from the register for one member, and for a history, looks month by month rather than carrying every book at once.

📘What it really is

📝A worked example

Input: SakhiKosh's deposit history over 500 blocks, with a provider limit of 50 blocks per query.

Steps:

  1. Start at the deployment block saved by the deploy script.
  2. Ask for blocks 1–50, then 51–100, and so on, retrying any query that fails.
  3. Collect the events, sort by block and position, and format each amount.

Expected output: the full history, oldest first, with no query over 50 blocks.

🌍Where it's used

📝Case scenario

An app asked its provider for every event since block 0 on each page load. As the chain grew, requests failed with "range too large", and the history page went blank. Chunked queries from the deployment block, with a small cache, brought it back.

🖱️Try it

For a history from block 1,200 to block 3,450 with a limit of 1,000 blocks, list the ranges you'd ask for. Success: 1,200–2,199, 2,200–3,199 and 3,200–3,450.

⚠️Common mistakes

🛠️For your project

Project 3's tests 3 and 4 check exact balances and chunked, oldest-first history.

✅Quick check

Why start event queries at the deployment block?

Show the answer

There are no events before it, so earlier blocks only waste requests.

When does an indexer help?

Show the answer

When histories are large and queries need to be fast.

← PreviousKeys and signers in apps
ProLearners Assist