/** * Balance accounting. * * The whole recharge story lives in this module. A top-up raises the wallet, and * the rule that keeps it harmless is simple: **only decreases are ever summed**. * Every balance drop is one observed spend event; an increase is a recharge (or a * refund) and contributes nothing. That single rule means * * - a recharge never corrupts the correction factor, * - a recharge never invalidates a calibration window (so the earlier "reset the * anchor when the balance rises" rule is unnecessary), * - a recharge arriving mid-window is simply invisible to `observed / predicted`, * which therefore still equals the true average price over that window, * - chunky settlement (a 0.05 jump covering several minutes of usage) is counted * exactly once, in whichever pair reports it. * * Each drop is attributed to the **usage-clock** interval `(fromUsageClockAt, * toUsageClockAt]`, not to the interval between fetches: a reading taken at `T` * only reflects usage up to `T - settlementLagMs`, so using fetch times would * double-count the last `lag` of usage against the next reading. * * Drops with no corresponding local token consumption are classified as * adjustments (grant expiry, platform corrections) and are reported separately * instead of being charged to the user as spend. * * Everything here is per-currency: the platform reports several wallets at once, * and summing across them would be meaningless. * * @module dsh-balance/ledger */ /** One wallet reading, reduced to what the ledger needs. */ export interface LedgerReading { /** When the reading was fetched, epoch ms. */ readonly at: number; /** `at - settlementLagMs`: the usage cutoff this reading reflects. */ readonly usageClockAt: number; /** Total balance in scaled integer units. */ readonly totalUnits: number; } /** One observed decrease between two adjacent readings. */ export interface LedgerDrop { readonly fromAt: number; readonly toAt: number; readonly fromUsageClockAt: number; readonly toUsageClockAt: number; /** Decrease in scaled integer units (always positive). */ readonly units: number; } /** Drops split by cause. */ export interface ClassifiedDrops { /** Decreases with matching local token consumption: real spend. */ readonly usage: readonly LedgerDrop[]; /** Decreases with no matching consumption: grant expiry or a platform correction. */ readonly adjustment: readonly LedgerDrop[]; } /** * Sort readings and drop anything that does not advance the clock, so a rolled-back * system clock or a duplicated poll cannot produce a bogus drop. * * @param readings - readings in append order. * @returns readings with strictly increasing `at`. */ export declare function normalizeReadings(readings: readonly LedgerReading[]): readonly LedgerReading[]; /** * Extract every decrease from a reading series. * @param readings - normalized readings for one currency. * @returns the drops, in ascending time order. Increases yield no entry. */ export declare function findDrops(readings: readonly LedgerReading[]): readonly LedgerDrop[]; /** * Split drops into consumption and non-consumption causes. * * Three cases, in order: * 1. the interval shows **any** local token consumption — the drop is explained by * this machine's own usage, however small; * 2. the drop is no larger than one quantization step (`minPredictedSpend`, which * defaults to the platform's 0.01 resolution) — money really did leave the * account, so it counts as spend even though nothing can be attributed to it; * 3. a material drop with no local consumption at all — a grant expiry or a platform * correction. Reported separately, never charged as spend, and never allowed to * move the correction factor. * * @param drops - drops from {@link findDrops}. * @param consumedBetween - local predicted consumption over a usage-clock interval. * @param minPredictedSpend - quantization step and minimum material adjustment size. * @param hasLocalUsage - whether any local token consumption is observable at all. * Without it, "we spent nothing yet money left" is not evidence of anything, so * every drop stays spend rather than being misfiled as a correction. * @returns the classification. */ export declare function classifyDrops(drops: readonly LedgerDrop[], consumedBetween: (fromUsageClockAt: number, toUsageClockAt: number) => number, minPredictedSpend: number, hasLocalUsage?: boolean): ClassifiedDrops; /** * Sum drops whose usage cutoff falls inside a half-open window. * * Windowing on the usage clock rather than the fetch time keeps the totals aligned * with the lag-shifted projection. * * @param drops - drops to sum. * @param fromUsageClockAt - exclusive lower bound. * @param toUsageClockAt - inclusive upper bound. * @returns the summed amount in currency units. */ export declare function sumDropAmount(drops: readonly LedgerDrop[], fromUsageClockAt: number, toUsageClockAt: number): number; /** * Total a drop set, independent of any window. * @param drops - drops to total. * @returns the summed amount in currency units. */ export declare function totalDropAmount(drops: readonly LedgerDrop[]): number;