Overview
Theank-accounting module provides minimal accounting primitives used across the engine and protocols. All amounts are represented as 1e18-scaled integers (wei) unless otherwise stated.
Core Types
Timestamp
UserId
TokenId
The numeric token identifier
Amount
Balances
TokenId to Amount. This is a thin wrapper over IndexMap<TokenId, Amount> with helper methods for balance management.
When the ts-bindings feature is enabled, this maps to Record<number, string> (amounts serialized as decimal strings).
Methods
get
Amount for token t, or 0 if absent.
The token identifier to query
The current balance for the token, or 0 if not present
set
Amount for token t (overwrites any previous value).
The token identifier to set
The new balance amount (1e18-scaled)
apply_delta
BalancesDelta to these balances.
- Positive deltas are added using saturating addition
- Negative deltas are subtracted using saturating subtraction (no underflow)
The balance changes to apply
Always returns
Ok(()) (reserved for future validation/errors)BalancesDelta
TokenId to i128 delta.
- Positive deltas add to balances
- Negative deltas subtract from balances
- Subtractions saturate at zero (no underflow panic)
ts-bindings feature is enabled, this maps to Record<number, string> (deltas serialized as decimal strings).
Usage Examples
Apply a delta to balances
Managing user balances
Conventions
- Amounts are represented as 1e18-scaled integers unless otherwise stated
- Applying a negative delta that exceeds the current balance saturates at zero (no panic)
- All operations use saturating arithmetic to prevent overflows and underflows