feat: read chain state, decoded against the runtime
The observer could read headers, events, extrinsics, metadata and difficulty, and not a single pallet storage item. Answering "which address is this chain's treasury" meant going outside it and hand-deriving twox128 prefixes, which is a gap in the tool rather than an answer. The treasury is the case that shows why. `set_treasury_account` reads *never* on the call index and `TreasuryAccountUpdated` reads *never* on the event index — both true, because the address was set at genesis, so no extrinsic ever carried it and no event ever announced it. It exists only in state. It is qzjsuLN7Nhu4bjvmUbjSTr2ZTeZ7oRxXpQP9fdv6PcHUCRrVR, and it has never been funded. `Runtime::storage_key` builds a key entirely from the runtime's own description — the pallet's storage prefix, the item name, and each key's declared hasher — because a wrong hasher yields a key that reads as *absent* rather than as an error, and nothing would catch it. Its test pins two keys against ones read off the live chain by hand. Keys are SCALE-encoded against the type the entry declares, so a map on an account, a u32 or a tuple all work without this knowing which it is. Absent is not zero, and only the modifier knows which: an `optional` entry holding nothing means nothing, a `default` entry means the runtime's default, and the state page marks the latter rather than passing it off as something the chain wrote. `read_balance` deliberately does not apply the default — `AccountInfo` zeroes, Substrate reaps empty accounts, and falling back would turn "does not exist" into "holds nothing", which is the treasury's exact case. /:chain/state lists all 40 keyless entries decoded, and an account page now carries a real balance beside the flows it already summed. Those are different numbers: rewards say what an account was paid, a balance says what it has, and they differ by every transfer out. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jp6a8EDar9ueEhAxzep4V5
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
import type { AccountEvent } from "./AccountEvent";
|
||||
import type { BigUintDec } from "./BigUintDec";
|
||||
import type { ChainId } from "./ChainId";
|
||||
import type { MinerId } from "./MinerId";
|
||||
import type { RewardSummary } from "./RewardSummary";
|
||||
@@ -35,6 +36,24 @@ miner: MinerId | null,
|
||||
* The telemetry name held for that miner, if one is.
|
||||
*/
|
||||
display: string | null,
|
||||
/**
|
||||
* The account's free balance right now, read from chain state rather than
|
||||
* inferred from the flows below.
|
||||
*
|
||||
* `null` when the node would not answer, and — importantly — also when the
|
||||
* account does not exist. On Substrate an account with nothing in it is
|
||||
* not an account with zero, it is absent, and those read differently: the
|
||||
* treasury address this chain has configured has never been funded, and
|
||||
* showing it a zero would imply it had been and spent.
|
||||
*/
|
||||
balance: BigUintDec | null,
|
||||
/**
|
||||
* The whole `System::Account` entry as the runtime describes it — nonce,
|
||||
* consumers, providers, and the balance breakdown. Carried alongside the
|
||||
* single number above because the number is what a reader came for and the
|
||||
* rest is what makes it checkable.
|
||||
*/
|
||||
state: Record<string, unknown> | null,
|
||||
/**
|
||||
* Mining rewards, over the whole indexed record.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
import type { ChainId } from "./ChainId";
|
||||
import type { StateEntry } from "./StateEntry";
|
||||
|
||||
/**
|
||||
* Every storage entry readable without a key, decoded.
|
||||
*/
|
||||
export type ChainState = {
|
||||
/**
|
||||
* Which chain.
|
||||
*/
|
||||
chain: ChainId,
|
||||
/**
|
||||
* The runtime the entries were described by.
|
||||
*/
|
||||
spec_version: number,
|
||||
/**
|
||||
* The block this state was read at.
|
||||
*/
|
||||
height: number,
|
||||
/**
|
||||
* The entries, in pallet declaration order.
|
||||
*/
|
||||
entries: Array<StateEntry>, };
|
||||
@@ -0,0 +1,44 @@
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
|
||||
/**
|
||||
* One storage entry, read and decoded.
|
||||
*/
|
||||
export type StateEntry = {
|
||||
/**
|
||||
* Owning pallet, as the runtime names it.
|
||||
*/
|
||||
pallet: string,
|
||||
/**
|
||||
* The entry, as the runtime names it.
|
||||
*/
|
||||
name: string,
|
||||
/**
|
||||
* The value's type, rendered from the registry.
|
||||
*/
|
||||
type_name: string,
|
||||
/**
|
||||
* `optional` or `default`.
|
||||
*/
|
||||
modifier: string,
|
||||
/**
|
||||
* The first line of the runtime's documentation for it.
|
||||
*/
|
||||
docs: string | null,
|
||||
/**
|
||||
* The decoded value. Accounts as `0x` hex, big integers as decimal
|
||||
* strings — the same normalisation events and call arguments get, so one
|
||||
* renderer serves all three.
|
||||
*/
|
||||
value: unknown,
|
||||
/**
|
||||
* Whether the chain held nothing there.
|
||||
*
|
||||
* Not the same as `value` being null: for a `default` entry the runtime
|
||||
* supplies a value when storage is empty, and this says the value shown is
|
||||
* that default rather than something the chain wrote.
|
||||
*/
|
||||
is_default: boolean,
|
||||
/**
|
||||
* Why it could not be read, when it could not.
|
||||
*/
|
||||
error: string | null, };
|
||||
@@ -14,6 +14,7 @@ import type { BlockDetail } from './generated/BlockDetail'
|
||||
import type { BlockExtrinsic } from './generated/BlockExtrinsic'
|
||||
import type { CallIndex } from './generated/CallIndex'
|
||||
import type { ChainSeries } from './generated/ChainSeries'
|
||||
import type { ChainState } from './generated/ChainState'
|
||||
import type { RecentBlock } from './generated/RecentBlock'
|
||||
import type { MinerDetail } from './generated/MinerDetail'
|
||||
import type { RuntimeDetail } from './generated/RuntimeDetail'
|
||||
@@ -147,6 +148,16 @@ export function fetchCallFeed(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Every storage entry readable without a key, decoded.
|
||||
*
|
||||
* Live on every call. State is what is true now, and caching now produces a
|
||||
* copy of some earlier now that is indistinguishable from the real thing.
|
||||
*/
|
||||
export function fetchState(chain: string, signal?: AbortSignal): Promise<ChainState> {
|
||||
return get<ChainState>(`/chains/${encodeURIComponent(chain)}/state`, signal)
|
||||
}
|
||||
|
||||
/** Accounts ranked by what they have been paid, most first. */
|
||||
export function fetchAccounts(chain: string, signal?: AbortSignal): Promise<AccountRow[]> {
|
||||
return get<AccountRow[]>(`/chains/${encodeURIComponent(chain)}/accounts`, signal)
|
||||
|
||||
Reference in New Issue
Block a user