/** * @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 { AccountRef } from './accounts.js'; import type { Digest, Ledger, Leg, CallOptions, Posting } from './ports.js'; import type { Transaction } from './contract.js'; /** * Previous-hash placeholder for the start of an account's chain: 32 zero bytes. An * account's first posting links to this. */ export declare const GENESIS: Uint8Array; /** {@link GENESIS} in lowercase hex — the one spelling every stored head compares against. */ export declare const GENESIS_HEX: string; /** * Builds a debit leg for one account. Pass the amount positive; it is stored positive. A debit * lowers credit-normal accounts, such as a user's spendable balance, and raises debit-normal * ones. Pairing a debit with a {@link credit} of the same amount yields stored amounts that sum * to zero: a balanced posting. * * @example * const price = toAmount('CREDIT', 70_000n); * const legs = [ * debit(spendable('usr_buyer'), price), // stored +70_000: buyer's balance falls * credit(earned('usr_seller'), price), // stored -70_000: seller's balance rises * ]; // sums to zero, so the posting balances */ export declare function debit(account: AccountRef, amount: Amount): Leg; /** * Builds a credit leg for one account. Pass the amount positive; it is stored negated, following * the ledger's debit-positive convention. A credit raises credit-normal accounts, such as a * user's spendable balance, and lowers debit-normal ones. See {@link debit} for the balanced-pair * example. */ export declare function credit(account: AccountRef, amount: Amount): Leg; /** * Returns the signed amount by which a leg moves its account's balance. Leg amounts are * debit-positive, so this flips the sign for credit-normal accounts and leaves debit-normal * ones unchanged. */ export declare function balanceDelta(leg: Leg): Amount; /** * Locks each account in one global order: `.sort()` by raw character code, identical on every * machine unlike a locale-aware comparison. Two operations that share an account acquire its lock * in the same order, so neither can deadlock waiting on a lock the other holds. Every lock-set goes * through here, so the fixed-order discipline lives in one place. * * @see {@link https://economy-lab-docs.pages.dev/economy/concepts/concurrency/ Concurrency} for the * deadlock-free lock ordering and the no-fork constraint that backs it. */ export declare function lockAll(ledger: Ledger, accounts: ReadonlyArray, options?: CallOptions): Promise; /** * Validates a posting, then writes it. Four pre-write checks (currency match, balanced, * accounts exist, no user overdraft) are a redundant second line of defense; the database * enforces them too. Only the write advances each account's hash chain and running balances. * * @see {@link https://economy-lab-docs.pages.dev/economy/concepts/accounts-and-double-entry/ * Accounts & double-entry} for the posting model these checks enforce. */ export declare function postEntry(ledger: Ledger, posting: Posting, options?: CallOptions): Promise; /** * Posts several entries with `postEntry`'s exact checks each, fused into one engine round trip * when the ledger offers `appendAll` and the postings share no account. Overlapping postings * keep the sequential path, so every overdraft check sees its predecessor's effect; the fused * path's checks are independent by construction. */ export declare function postEntries(ledger: Ledger, postings: ReadonlyArray, options?: CallOptions): Promise; /** * Builds the bytes hashed to extend one account's chain. Each link commits to the account's prior * head, so altering a past entry stops the chain re-deriving. The hash covers four parts: the prior * link hash, the transaction id, this account's legs, and the posting metadata. The layout is fixed * (amounts via `encodeAmount`, metadata keys sorted, parts joined via `lengthPrefixed`) so the same * posting reproduces the same bytes, and hash, on later verification. * * @see {@link https://economy-lab-docs.pages.dev/economy/concepts/integrity/ Integrity} for the * hash-chain design. */ export declare function chainPreimage(input: { accountPrevHash: Uint8Array; txnId: string; account: AccountRef; legs: ReadonlyArray; meta: Record; }): Uint8Array; /** The account's new chain head: the `chainPreimage` bytes through the digest, as lowercase hex. */ export declare function chainHash(digest: Digest, input: { accountPrevHash: Uint8Array; txnId: string; account: AccountRef; legs: ReadonlyArray; meta: Record; }): Promise;