/** * 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 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).filter( ([, v]) => !(ctx.focus && isAccount(v) && v === ctx.focus), ) if (shown.length === 0) return — return ( {shown.map(([key, v]) => ( {/* 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 && {key}} ))} ) } 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 ( {tokens(raw, ctx.decimals)} {ctx.symbol} ) } 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 ( {shortAddress(value)} ) } return ( {shortAddress(address)} ) } // 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) // 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 } return ( {only} ) } if (Array.isArray(value)) { return ( {value.map((item, i) => ( ))} ) } return ( ) } // 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 {String(value)} }