class · Browser and Node
SpendLedger
@toip-tools/mandatesView sourceimport { SpendLedger } from "@toip-tools/mandates";
Constructor
new (saved?: SpendLedgerState): SpendLedger;
Parameters
savedoptionalSpendLedgerState | undefined
Returns
Methods
spent
Spent under mandate in each period: rolling windows ending at now (ms); total is everything.
spent(mandate: string, now: number): Record<Period, Units>;
Parameters
mandaterequiredstringnowrequirednumber
Returns
Record<Period, string>reserve
Counts spend against mandate from at. Call it when the action is allowed, before anything moves.
reserve(mandate: string, actionId: string, spend: Units, at: number): void;
Parameters
mandaterequiredstringactionIdrequiredstringspendrequiredstringatrequirednumber
Returns
voidtryReserve
Checks the mandate's own budgets and reserves in one synchronous step, so two actions racing for
the last of a budget can't both fit. Returns the budgets it would exceed (and reserves nothing), or [].
With actionType, budgets scoped by when to other types are skipped.
tryReserve(mandate: Mandate, actionId: string, spend: Units, at: number, actionType?: string): Reason[];
Parameters
mandaterequiredMandateactionIdrequiredstringspendrequiredstringatrequirednumberactionTypeoptionalstring | undefined
Returns
Reason[]release
Undoes a reservation: the action failed before anything left the wallet.
release(actionId: string): void;
Parameters
actionIdrequiredstring
Returns
voidrefund
A refund gives budget back (agents decision 4): all of it, or units of it.
refund(actionId: string, units?: Units): void;
Parameters
actionIdrequiredstringunitsoptionalstring | undefined
Returns
voidmarkApprovalUsed
markApprovalUsed(id: string): void;
Parameters
idrequiredstring
Returns
voidapprovalUsed
approvalUsed(id: string): boolean;
Parameters
idrequiredstring
Returns
booleansave
save(): SpendLedgerState;
No parameters.