/** * What the action button says, and whether it can be pressed. * * The label is the part of a transaction form that is usually wrong. A button * that reads "Swap" and then fails has told somebody their transaction was * ready when it was not; a button that reads "Not enough USDC" has told them * what to do next. So no screen writes a label. Each states the facts it knows * as conditions, and the first one that does not hold is what a person reads. * * None of this touches a DOM, which is why it is here rather than beside the * component that renders it: the ordering is the part worth being sure about. * * Every constructor takes its sentence as the first argument. This package * ships no copy — the words belong to whoever holds the catalogue, and a * boolean here would put an English string in a module that cannot know the * reader's language. */ /** * What pressing the button does. A hash means the wallet took a transaction; * nothing means the press moved the world some other way — connecting, most * often — and the screen's own props are what say so afterwards. * * The hash is written out rather than imported, so the ordering below does not * drag a chain client into a package of components. */ export type Act = () => Promise<`0x${string}` | void> /** * What can stand between a person and the verb, in the order they meet it. * * The order is a property of the kind rather than of the array a screen writes, * so no screen can put approval before funds by listing it first. A screen's * own condition — a destination network, a price band, a closed poll — takes * its place by saying which kind of obstacle it is. */ export type Kind = 'read' | 'wallet' | 'input' | 'quote' | 'funds' | 'allowance' export interface Condition { kind: Kind /** Whether it holds. */ met: boolean /** What the button says while it does not. */ label: string /** * What pressing the button does about it. Absent, the button is inert and * the person supplies what is missing themselves — there is no press that * would help, and offering one would be a lie about what the software can do. */ act?: Act } const ORDER: readonly Kind[] = ['read', 'wallet', 'input', 'quote', 'funds', 'allowance'] /** * Whether pressing this hands the work to somebody else and waits. * * The wallet rung is cleared on the page: its press opens a chooser here, and * nothing has been given to anybody to confirm. Every other press — an * approval, the verb itself — goes out to a wallet and is waited on. The * distinction belongs to the kind, so a form knows which sentence its button * takes without knowing what any one act does. */ export const waits = (of: Condition | null): boolean => of?.kind !== 'wallet' /** * The first condition that does not hold. Null when every one does, which is * the only state in which the verb appears on the button. */ export function first(conditions: readonly Condition[]): Condition | null { for (const kind of ORDER) { const unmet = conditions.find((c) => c.kind === kind && !c.met) if (unmet) return unmet } return null } /** * Something about the chain, rather than about the person, standing between * them and the verb. * * First in the order, and the placement is the point. A screen that asks for a * wallet, an amount and an approval before saying that the position cannot be * read has spent somebody's afternoon on a transaction it was never going to * build. What cannot be prepared is the first thing to say, not the last. * * It carries no `act` for the same reason a shortfall does not: no press this * software could offer would supply what is missing. The label is the whole of * what it can honestly do, and the screen writes it — the other constructors * each name one recurring obstacle, and this one stands for every obstacle that * is nobody's fault, where a general sentence would tell a person nothing they * could act on. */ export const known = (label: string, met: boolean): Condition => ({ kind: 'read', met, label }) /** * There is nobody on this end yet. * * The press is the way in, so this is the one obstacle a screen can clear for * the person standing at it — which is why the act is required rather than * optional. An accurate sentence somebody cannot act on is still a dead end. */ export const connected = (label: string, account: unknown, connect: Act): Condition => ({ kind: 'wallet', met: account !== null && account !== undefined, label, act: connect, }) /** * Something the person has yet to supply. * * Both spellings a screen has are accepted, because a quantity is a string * while somebody is typing it and a bigint by the time it reaches a * transaction, and asking every screen to convert before it can say "empty" is * how a zero ends up counted as entered. * * A string is tested for emptiness and nothing else. `'0'` and `'0.00'` are * answers somebody typed, so this holds on them; whether the amount is one the * software can act on is a question about a number, and the screen that parses * it is the only place that knows the separator its reader uses. Pass the * bigint once it exists — `0n` is unentered — or state the amount rule as a * condition of the screen's own. */ export const entered = (label: string, value: string | bigint | null | undefined): Condition => ({ kind: 'input', met: value !== null && value !== undefined && value !== '' && value !== 0n, label, }) /** * A price from whoever is on the other side, for what the person has said they * want. * * After the input rungs and before the funds one, and the placement is the * whole of what this adds. A quote is an answer to a stated order, so there is * nothing to ask for until the order is stated, and nothing to cover until a * price has come back. Asked earlier it greets a reader with a refusal before * they have said anything. * * No `act`, for the reason a shortfall carries none: no press this software * could offer produces a price somebody else has to publish. */ export const quoted = (label: string, met: boolean): Condition => ({ kind: 'quote', met, label }) /** * What is being spent is covered by what is held. * * An unread balance holds rather than blocks. A shortfall announced from a * number nobody read is a fabrication, and it is the expensive kind: the person * is told they cannot afford something they can, and believes it. `held` is * undefined until a screen has actually read it. */ export const covered = (label: string, need: bigint | null, held: bigint | undefined): Condition => ({ kind: 'funds', met: need === null || held === undefined || held >= need, label, }) /** * The contract that will move the asset is already allowed to. * * Undefined holds, for the same reason an unread balance does: an approval * demanded on a suspicion costs real gas for nothing. A chain's own coin needs * no allowance at all, so those screens leave this condition out rather than * pass one that is permanently true. */ export const allowed = ( label: string, need: bigint | null, allowance: bigint | undefined, approve: Act, ): Condition => ({ kind: 'allowance', met: need === null || allowance === undefined || allowance >= need, label, act: approve, })