/** * @pwngh/economy-lab * * Copyright (c) Preston Neal * * This source code is licensed under the MIT license found in the * LICENSE.md file in the root directory of this source tree. * * @license MIT */ import type { Amount } from '../money.js'; import type { Transaction, WorkerCtx } from '../contract.js'; import type { CallOptions, Store } from '../ports.js'; /** * Result of one backing check. A credit is backed when it can be redeemed from cash held in * trust at the fixed CREDIT-to-USD "par" rate. */ export type BackingPosition = { custodialCredit: Amount; required: Amount; trustCash: Amount; shortfall: Amount; backed: boolean; }; /** * One treasury sweep run. A run does one backing check, so each list below holds at most one * entry. */ export type TreasurySummary = { position: BackingPosition | null; breaches: ReadonlyArray<{ shortfall: string; required: string; held: string; }>; retrying: ReadonlyArray<{ code: string; }>; failed: ReadonlyArray<{ code: string; }>; }; /** * Check that held USD backs every spendable credit. Measure only: a shortfall is logged and * counted, nothing is posted. Errors are caught into the summary rather than propagated, so * one bad run can't crash the worker loop. * * @see {@link https://economy-lab-docs.pages.dev/economy/reference/background-worker/ Background * worker} for how the treasury sweep checks backing on a schedule. */ export declare function sweepTreasury(store: Store, ctx: WorkerCtx, input: { now: number; }): Promise; /** * Host-implemented read of the USD actually held at the payout provider, for the float-coverage * sweep. It exists because no processor port offers a balance read. The sweep compares the * returned balance against the USD owed by every open payout saga (each valued at its stored * quote) and raises a breach when the float does not cover the obligations. A throw is * normalized as a provider fault and fails only the `floatCoverage` job. */ export type FloatFeed = { balance(options?: CallOptions): Promise; }; export type FloatPosition = { float: Amount; obligations: Amount; shortfall: Amount; covered: boolean; }; export type FloatSummary = { position: FloatPosition | null; breaches: ReadonlyArray<{ shortfall: string; obligations: string; float: string; }>; retrying: ReadonlyArray<{ code: string; }>; failed: ReadonlyArray<{ code: string; }>; }; export declare function sweepFloatCoverage(store: Store, ctx: WorkerCtx, feed: FloatFeed, input: { now: number; }): Promise; /** * Outcome of one fee sweep. A duplicate (an earlier run claimed the key) posts nothing and * reports `swept` as zero; a fresh run reports the CREDIT realized and the posting that did it. */ export type FeeSweepResult = { duplicate: true; swept: Amount; } | { duplicate: false; swept: Amount; transaction: Transaction; }; /** * Realizes earned platform fees as platform cash, the only path that converts accrued fees into * cash the platform keeps. The read-write counterpart to {@link sweepTreasury}, which only checks. * * The surplus check, refund-window cap, and idempotency claim all run inside one DB transaction * with the touched accounts locked, so a concurrent sweep can't move the numbers between check and * post (TOCTOU). REVENUE is CREDIT and trust-cash is USD, and one entry can't mix currencies, so * the move splits into two coupled entries that share a rate id, the same as a payout settle. * * @throws {EconomyError} INVALID_AMOUNT for a non-positive `amount`; COMMINGLING when the * draw would exceed the surplus the platform is allowed to take. * @see {@link https://economy-lab-docs.pages.dev/economy/concepts/solvency/ Solvency} for why a * sweep may take only the surplus. */ export declare function sweepFees(store: Store, ctx: WorkerCtx, input: { amount: Amount; key?: string; }): Promise; /** * Summarizes one fee-realization sweep over a worker cycle. `swept` is the CREDIT realized this * cycle. It reads `'CREDIT:0.00'` when there was no sweepable surplus or when an overlapping run * already claimed the per-cycle key. `skipped` is true when the available surplus was zero, so * nothing was posted or emitted. */ export type FeeRealizationSummary = { swept: string; skipped: boolean; duplicate: boolean; }; /** * Realizes fees on a schedule, the write that {@link sweepTreasury} does not do. Each run takes the * full amount currently allowed (the smaller of cash surplus and matured revenue) and skips cleanly * when there is nothing. * * The amount is read once, then {@link sweepFees} re-checks the surplus and refund-window math under * its own locks at post time. The key is this run's timestamp (`fees:`), so a retry does * nothing. */ export declare function realizeFees(store: Store, ctx: WorkerCtx, input: { now: number; }): Promise;