Files
observer/web/src/components/Payload.tsx
T
grenadeandClaude Opus 5 edce856263
deploy / build (push) Successful in 7m17s
deploy / deploy-web (push) Successful in 4s
deploy / deploy-api (push) Successful in 15s
feat: decode extrinsics, and show them
An event is the chain's account of what happened; an extrinsic is the request
that caused it, and carries what no event does — who signed, what they called,
the nonce and tip, and whether it worked. A history built from events alone
omits every failed attempt and never names a submitter.

The same oracle does both. The metadata's extrinsic type carries four
parameters and the registry names all of them, including
`qp_dilithium_crypto::types::DilithiumSignatureScheme` — the part that looked
like it would need hand-written post-quantum knowledge and does not. The chain
describes its own signature scheme, so `decode_extrinsic` reads a Quantus
transaction without a line of code that knows Quantus exists.

Two facts about this chain's extrinsics, both now in CLAUDE.md. The first byte
is not the version: the top two bits are a type tag, and mainnet carries `0x84`
(signed, v4) and `0x05` (bare, v5) in the same block while the metadata declares
version 4 — check the byte against the metadata and you reject every timestamp
inherent on the chain. And a signature is 5.3 KiB, two orders of magnitude
larger than the call it authorises; it is decoded to find where the call begins
and then discarded, keeping only the scheme's name and the byte count.

Both halves of a block index in one pass, off calls already being made. Whether
a dispatch succeeded comes from the `ExtrinsicSuccess`/`ExtrinsicFailed` events
in that same decoded list — read, used, not stored.

The block page grows a table below the facts: call, signer, arguments, and the
signature's size. The account page merges extrinsics into the history it already
had, ordered by block with the request above the results it caused. `source` is
1 for an extrinsic and 0 for an event so that every part of the sort key
descends, which lets the paging cursor be a plain row comparison rather than a
mixed-direction one Postgres cannot express.

Nested calls are why this had to be generic. A `Utility::batch_all` carries whole
calls in its arguments, so a transfer's recipient can be three levels down — and
because the decoder collects account ids wherever they appear, that batched
transfer shows on the recipient's page tagged *received* even though they signed
nothing. Verified on mainnet 12,616: the batch renders both its transfers with
destinations and values, above the two `Balances::Transfer` events they produced.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jp6a8EDar9ueEhAxzep4V5
2026-09-09 18:27:54 +03:00

134 lines
4.9 KiB
TypeScript

/**
* Rendering a decoded call's arguments or an event's fields.
*
* One component for both because they are the same thing: a JSON object whose
* keys are names the runtime chose, whose account ids are `0x` hex, and whose
* integers past 2^53 are strings. Neither the block page nor the account page
* knows what a transfer is, and neither should — a pallet added next year
* renders here without a line of code.
*
* `AMOUNTS` is presentation only. A field it does not recognise still appears,
* as its key and its value; the list decides which of them get the token
* treatment rather than which of them exist. That is the difference between a
* page that shows what happened and one that shows the subset somebody
* remembered to write a case for.
*/
import { Link } from 'react-router-dom'
import { exactTokens, shortAddress, tokens } from '../lib/format'
import { href } from '../lib/routes'
/** Field names that carry a balance, and so want the token treatment. */
const AMOUNTS = new Set(['reward', 'amount', 'value', 'fee', 'actual_fee', 'tip'])
/** A 32-byte account id, which is what the decoder renders accounts as. */
function isAccount(value: unknown): value is string {
return typeof value === 'string' && /^0x[0-9a-f]{64}$/.test(value)
}
export interface PayloadContext {
chain: string
/** `0x hex → SS58` for every account the payload mentions. */
addresses: Record<string, string>
decimals: number
symbol: string
/** Folded out of the rendering: the account whose page this is. Every row
* involves it by definition, and repeating it buries the half that differs. */
focus?: string
}
export function Payload({ value, ctx }: { value: unknown; ctx: PayloadContext }) {
const shown = Object.entries((value ?? {}) as Record<string, unknown>).filter(
([, v]) => !(ctx.focus && isAccount(v) && v === ctx.focus),
)
if (shown.length === 0) return <span className="event-fields">—</span>
return (
<span className="event-fields">
{shown.map(([key, v]) => (
<span key={key} className="event-field">
{/* The key only when there is something to tell apart. A reward has
one field left once the page's own account is folded out, and
"REWARD 0.31 QTC" in a column headed Detail says it twice. */}
{shown.length > 1 && <span className="event-key">{key}</span>}
<Value name={key} value={v} ctx={ctx} />
</span>
))}
</span>
)
}
function Value({ name, value, ctx }: { name: string; value: unknown; ctx: PayloadContext }) {
if (AMOUNTS.has(name) && (typeof value === 'string' || typeof value === 'number')) {
const raw = String(value)
return (
<span className="numeral" title={`${exactTokens(raw, ctx.decimals)} ${ctx.symbol}`}>
{tokens(raw, ctx.decimals)} {ctx.symbol}
</span>
)
}
if (isAccount(value)) {
// The counterparty in the spelling they would recognise, and as a way to
// their own page. The raw id stays in the title, because it is what the
// chain stored and what anyone checking our working needs.
const address = ctx.addresses[value]
if (!address) {
return (
<span className="numeral" title={value}>
{shortAddress(value)}
</span>
)
}
return (
<Link
className="numeral"
to={href({ chain: ctx.chain, account: address })}
title={`${address}\n${value}`}
>
{shortAddress(address)}
</Link>
)
}
// A nested structure: a `MultiAddress::Id` wrapper, or a whole call inside a
// `Utility::batch_all`. Recursing rather than stringifying is what lets a
// batched transfer show its recipient instead of a wall of JSON.
if (value && typeof value === 'object') {
const entries = Object.entries(value as Record<string, unknown>)
// A single-key object is a variant wrapper — `{Id: "0x…"}`,
// `{Balances: {…}}` — and its key is the name, not a field.
if (entries.length === 1) {
const [only, inner] = entries[0]!
if (isAccount(inner) || typeof inner !== 'object') {
return <Value name={name} value={inner} ctx={ctx} />
}
return (
<span className="event-nested">
<span className="event-key">{only}</span>
<Payload value={inner} ctx={ctx} />
</span>
)
}
if (Array.isArray(value)) {
return (
<span className="event-nested">
{value.map((item, i) => (
<Value key={i} name={name} value={item} ctx={ctx} />
))}
</span>
)
}
return (
<span className="event-nested">
<Payload value={value} ctx={ctx} />
</span>
)
}
// Anything the cases above do not know. Shown rather than dropped: the index
// holds every field the runtime emitted, and a page that hides the ones it
// has no styling for is lying by omission.
return <span className="numeral">{String(value)}</span>
}