toip.tools

class · Browser and Node

SpendLedger

@toip-tools/mandatesView source
TypeScriptImport
import { SpendLedger } from "@toip-tools/mandates";

Constructor

TypeScriptConstructor
new (saved?: SpendLedgerState): SpendLedger;

Parameters

savedoptional
SpendLedgerState | undefined

Returns

Methods

spent

Spent under mandate in each period: rolling windows ending at now (ms); total is everything.

TypeScriptSignature
spent(mandate: string, now: number): Record<Period, Units>;

Parameters

mandaterequired
string
nowrequired
number

Returns

Record<Period, string>

reserve

Counts spend against mandate from at. Call it when the action is allowed, before anything moves.

TypeScriptSignature
reserve(mandate: string, actionId: string, spend: Units, at: number): void;

Parameters

mandaterequired
string
actionIdrequired
string
spendrequired
string
atrequired
number

Returns

void

tryReserve

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.

TypeScriptSignature
tryReserve(mandate: Mandate, actionId: string, spend: Units, at: number, actionType?: string): Reason[];

Parameters

mandaterequired
Mandate
actionIdrequired
string
spendrequired
string
atrequired
number
actionTypeoptional
string | undefined

Returns

release

Undoes a reservation: the action failed before anything left the wallet.

TypeScriptSignature
release(actionId: string): void;

Parameters

actionIdrequired
string

Returns

void

refund

A refund gives budget back (agents decision 4): all of it, or units of it.

TypeScriptSignature
refund(actionId: string, units?: Units): void;

Parameters

actionIdrequired
string
unitsoptional
string | undefined

Returns

void

markApprovalUsed

TypeScriptSignature
markApprovalUsed(id: string): void;

Parameters

idrequired
string

Returns

void

approvalUsed

TypeScriptSignature
approvalUsed(id: string): boolean;

Parameters

idrequired
string

Returns

boolean

save

TypeScriptSignature
save(): SpendLedgerState;

No parameters.

Returns