'use client' import { createContext, createElement, useContext, type ReactNode } from 'react' /** * What an asset looks like, answered once for the whole surface. * * Two questions were being answered in two places by two copies of the same * idea: a token list kept a set of token marks and a chain panel kept a set of * chain marks, each with its own rule for what to draw when the file is not * there. Two lists are two chances for a screen to draw a frame it cannot fill, * and the second list had already drifted. * * So there is one resolver, and it answers with a whole identity rather than a * path: where the thing lives, what it is called, the mark this build serves * for it, the chain badge that goes on it, and the letters to draw when there * is no mark. A caller cannot ask for half of that and cannot supply a path of * its own. * * Which files a build serves is the build's own fact and not this package's, so * an app states it once at its root and every asset below resolves against it. * A build that states nothing draws letters, which always resolve and never * fetch. */ /** * Where a build serves its marks from, by name. * * Both answer null for a name they have no file for, and null is the ordinary * answer rather than the exception: an indexer returns id, symbol, name and * decimals and no image, so most of a chain's tokens have no mark at all. * Asking the network instead would leave a broken frame where something * readable would have stood. */ export interface Marks { /** A token's mark, by its symbol, lower-cased. */ token?(symbol: string): string | null /** A chain's mark, by the venue key an asset states. */ chain?(key: string): string | null /** A region's flag, by its ISO 3166 code, lower-cased. Drawn beside the * currency a sum is stated in. */ region?(code: string): string | null } const Served = createContext({}) /** * The marks a tree draws with. One at an app's root. * * A provider rather than a prop on every asset: which files a build serves is * one fact stated once, and repeating it down fifteen screens is fifteen * chances for one of them to disagree. */ export const Marks = ({ value, children }: { value: Marks; children: ReactNode }) => createElement(Served.Provider, { value }, children) /** Everything drawing an asset needs, and nothing a caller has to look up. */ export interface Identity { /** Where the asset is, lower-cased. Absent for a thing that has no address — * a chain's own coin before it is wrapped, a wallet. */ address?: string symbol?: string /** Where the mark is served from, or null where this build serves none. */ icon: string | null /** The chain's own mark, drawn on the asset's, where the caller named a chain * and the build serves one for it. It is what tells two tokens sharing a * symbol on two chains apart. */ badge: string | null /** The letters drawn in place of a mark. Always resolves. */ fallback: string } /** What a caller knows about an asset before this answers. */ export interface Of { symbol?: string address?: string /** The venue key, where the caller knows which chain it is reading. */ chain?: string } /** * The letters an asset is known by, at most three. * * Two is not enough on a chain whose assets are mostly one letter plus a name: * LUX and LUSD both reduce to `LU`, and letters that collide mislead faster * than none would. Three separates them. * * Three does not separate everything — USDC and USDT still meet — and it is not * meant to. The letters are an aid to scanning, not an identifier: the symbol * is in the row beside them, and the shade comes from the address, so two * assets sharing letters still differ on the page. */ export function letters(symbol?: string, address?: string): string { const said = (symbol ?? '').replace(/[^A-Za-z0-9]/g, '') if (said.length >= 2) return said.slice(0, 3).toUpperCase() if (said.length === 1) return said.toUpperCase() // No symbol at all: the first two hex digits after 0x, which at least differ // between assets and match what the address column shows. return (address ?? '').replace(/^0x/i, '').slice(0, 2).toUpperCase() || '??' } /** One asset, as a build can draw it. */ export function identify({ symbol, address, chain }: Of, marks: Marks = {}): Identity { const key = symbol?.trim().toLowerCase() return { address: address?.toLowerCase(), symbol, icon: (key && marks.token?.(key)) || null, badge: (chain && marks.chain?.(chain)) || null, fallback: letters(symbol, address), } } /** The marks the tree around this component draws with. */ export const useMarks = (): Marks => useContext(Served) /** The same answer, for a component drawing inside a tree that states its own. */ export const useIdentity = (of: Of): Identity => identify(of, useMarks())