BC9: Apps that talk to the chain
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
- Reading state: call view functions such as
balanceOf(address); free and immediate. - Exact amounts: results come back as BigInt wei. Format with
formatEtherfor people; do maths in wei. -
Events for history:
queryFilter(fund.filters.Deposited(), from, to)in chunks within the provider's range limit, oldest first, starting from the deployment block. -
Retries and limits: providers fail or throttle sometimes. Retry with a delay and a time limit.
- Indexers: for big histories, an indexer (your own database fed by events, or services such as The Graph) answers queries faster than asking nodes each time.
📝A worked example
Input: SakhiKosh's deposit history over 500 blocks, with a provider limit of 50 blocks per query.
Steps:
- Start at the deployment block saved by the deploy script.
- Ask for blocks 1–50, then 51–100, and so on, retrying any query that fails.
- 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
- Dashboards and explorers.
- Accounting exports from events.
- Indexers behind most large apps.
📝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
- One query for everything. Sign: range errors. Fix: chunks.
- Floating-point formatting. Sign: rounded balances. Fix:
formatEtheron BigInt. - Starting at block 0. Sign: slow, wasteful queries. Fix: the deployment block.
🛠️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.
