@pwngh/economy-lab
    Preparing search index...

    Interface Economy

    Public surface of a running economy: submit operations that change money, read balances and statements, run the integrity check, shut down. Built by createEconomy in economy.ts.

    The Economy for the submit/read/close surface and its construction.

    interface Economy {
        read: {
            accounts(options?: CallOptions): AsyncIterable<AccountRef>;
            balance(account: AccountRef, options?: CallOptions): Promise<Amount>;
            capacity(options?: CallOptions): Promise<CapacityReport>;
            checkpoint(options?: CallOptions): Promise<Checkpoint | null>;
            entitled(
                userId: string,
                sku: string,
                options?: CallOptions,
            ): Promise<boolean>;
            export(options?: CallOptions): AsyncIterable<string>;
            health(options?: CallOptions): Promise<ProveReport>;
            lineage(
                account: AccountRef,
                options?: CallOptions,
            ): AsyncIterable<StoredLink>;
            payouts(
                options?: CallOptions & {
                    states?: readonly (
                        "SUBMITTED"
                        | "SETTLED"
                        | "FAILED"
                        | "REQUESTED"
                        | "RESERVED"
                    )[];
                },
            ): AsyncIterable<Saga>;
            posting(txnId: string, options?: CallOptions): Promise<Posting | null>;
            postings(options?: CallOptions): AsyncIterable<Posting>;
            saga(id: string, options?: CallOptions): Promise<Saga | null>;
            statement(
                account: AccountRef,
                range: Range,
                options?: CallOptions,
            ): Promise<Statement>;
            status(): EconomyStatus;
        };
        close(): Promise<void>;
        submit(operation: Operation, options?: CallOptions): Promise<Outcome>;
        submitBatch(
            operations: readonly Operation[],
            options?: CallOptions,
        ): Promise<readonly BatchOutcome[]>;
    }
    Index
    read: {
        accounts(options?: CallOptions): AsyncIterable<AccountRef>;
        balance(account: AccountRef, options?: CallOptions): Promise<Amount>;
        capacity(options?: CallOptions): Promise<CapacityReport>;
        checkpoint(options?: CallOptions): Promise<Checkpoint | null>;
        entitled(
            userId: string,
            sku: string,
            options?: CallOptions,
        ): Promise<boolean>;
        export(options?: CallOptions): AsyncIterable<string>;
        health(options?: CallOptions): Promise<ProveReport>;
        lineage(
            account: AccountRef,
            options?: CallOptions,
        ): AsyncIterable<StoredLink>;
        payouts(
            options?: CallOptions & {
                states?: readonly (
                    "SUBMITTED"
                    | "SETTLED"
                    | "FAILED"
                    | "REQUESTED"
                    | "RESERVED"
                )[];
            },
        ): AsyncIterable<Saga>;
        posting(txnId: string, options?: CallOptions): Promise<Posting | null>;
        postings(options?: CallOptions): AsyncIterable<Posting>;
        saga(id: string, options?: CallOptions): Promise<Saga | null>;
        statement(
            account: AccountRef,
            range: Range,
            options?: CallOptions,
        ): Promise<Statement>;
        status(): EconomyStatus;
    }

    The read surface: balances, statements, postings, sagas, entitlements, status, capacity, the streamed enumerations, and the export. Reads never mutate; balance alone consults the optional cache, every other reader goes to the store.

    Type Declaration

    • accounts: function
      • Every account that has a balance row, streamed. A real ledger can hold many, so iterate and stop when you've seen enough rather than collecting them all. Lets a reader enumerate accounts (and derive users) without tracking them itself. This is the prover's own enumeration.

        Parameters

        Returns AsyncIterable<AccountRef>

    • balance: function
      • The account's current balance in its own currency, from the maintained running total — one read, not a sum over history; an account never posted to reads as zero. When a cache port is present this is a read-through, and any cache failure logs and falls back to the ledger, so a degraded cache can never fail the read. A bare sharded platform account resolves to its logical balance: the sum over its shard rows.

        Parameters

        Returns Promise<Amount>

    • capacity: function
    • checkpoint: function
      • The latest signed checkpoint (the Merkle root over all account heads, its signature, and the count it covers), or null before the worker has sealed one. The read side of the periodic seal a UI verifies against the live heads. Delegates to CheckpointStore.latest.

        Parameters

        Returns Promise<Checkpoint | null>

    • entitled: function
      • Whether a user currently owns an entitlement (a SKU: an item or feature), true or false. Ownership is a record, not a balance, so it has its own reader. This is the readable side of grantEntitlement/revokeEntitlement that a UI gates access on.

        Parameters

        • userId: string
        • sku: string
        • Optionaloptions: CallOptions

        Returns Promise<boolean>

    • export: function
      • Streams the ledger as canonical JSONL for offline verification: a header line, every account's chain links in lineage order, then the latest checkpoint. scripts/ledger-verify.ts re-proves the chain and checks the checkpoint signature from the file alone, with no store access.

        Parameters

        Returns AsyncIterable<string>

    • health: function
    • lineage: function
      • One account's hash chain, oldest first: every posting that touched it, each carrying the head hash before and after, so a reader can walk the tamper-evident chain a link at a time. The first link's prevHash is the fixed genesis; each later prevHash equals the prior hash. Delegates to Ledger.lineage.

        Parameters

        Returns AsyncIterable<StoredLink>

    • payouts: function
      • Every payout saga, newest first, streamed (a busy economy can have many). Includes settled and failed payouts, not only the due ones the worker claims. Lets a UI render payout status without tracking minted payout ids itself. Delegates to SagaStore.list.

        Parameters

        • Optionaloptions: CallOptions & {
              states?: readonly (
                  "SUBMITTED"
                  | "SETTLED"
                  | "FAILED"
                  | "REQUESTED"
                  | "RESERVED"
              )[];
          }

        Returns AsyncIterable<Saga>

    • posting: function
      • One committed posting by transaction id (its legs and meta), or null if unknown. Lets a reader resolve a posting without reaching past read into the raw Store.

        Parameters

        Returns Promise<Posting | null>

    • postings: function
      • Every committed posting, newest first, streamed (a busy ledger can have many). Includes user and worker postings alike, every account touched, not only the ones a given reader minted. Each posting carries its full legs, so a UI renders a row without a second lookup. Delegates to Ledger.list.

        Parameters

        Returns AsyncIterable<Posting>

    • saga: function
      • One payout saga by id (state, provider ref, attempts), or null if unknown. The background worker advances these; a UI reads them to render payout status.

        Parameters

        Returns Promise<Saga | null>

    • statement: function
      • One page of the account's entries inside range (half-open, epoch milliseconds: from included, to not). Paging is by narrowing the range window by window; the returned cursor is reserved and always null.

        Parameters

        Returns Promise<Statement>

    • status: function
    • Shuts the economy down by closing its store — the SQL engines close their connection pools; the memory store has nothing to release. The economy itself holds no other state. Call it once, after in-flight submits and the worker's sweeps have finished; a submit after close gets whatever the engine does on a closed pool.

      Returns Promise<void>

    • Runs one operation through the full pipeline: validate, authorize, then one all-or-nothing transaction that claims the idempotency key, screens risk and funds, posts the legs, and queues the outbound event. Resolves committed, duplicate (the idempotency key was seen before — the original transaction returns unchanged, nothing posts twice), or rejected (a normal decline returned as data); a genuine fault throws an EconomyError instead. A rejected operation commits nothing and leaves its idempotency key unclaimed, so a corrected retry under the same key can still succeed.

      Parameters

      Returns Promise<Outcome>

    • Submits several independent operations through the full pipeline for one database commit (one fsync) on the clean path — the aggregate-throughput lever — while each operation keeps exactly submit's semantics: a rejection or fault rolls back that operation alone, and its batch-mates still commit. The isolation strategy is the store engine's own (see Store.batchTransaction). Slots are index-aligned with the input. Operations in one batch must carry distinct idempotency keys; a duplicate key's slot faults. On a store without batch support, this degrades to sequential submits.

      Parameters

      Returns Promise<readonly BatchOutcome[]>