AI agent documentation index: llms.txt. Raw markdown for any page is available by appending .md to the URL. Full content snapshot: llms-full.txt.
llms.txt — complete Cartesi documentation index. Append .md to any page URL for raw Markdown (e.g. /cartesi-rollups/2.0/development/building-an-application.md).
Skip to main content

Ledger reference

This page lists every ledger operation with its signature per language. For a guided walk through, see Managing balances.

Lifecycle

Create, persist, reset and release a ledger.

The ledger comes in two flavors. The multi asset ledger tracks many assets at once. The single asset ledger tracks one fixed asset, Ether or one ERC20, chosen when the store is created and immutable after that. It keeps 64 bit balances and stores each account as a 32 byte record, which is the layout the default emergency withdrawal tooling reads. See Managing balances for when to use each one.

MethodDescription
Ledger()Creates an in memory ledger
Ledger(memory_filename, offset, mem_length, n_accounts, n_assets, n_balances)Creates or opens a file backed multi asset ledger. The file is created and sized when missing, and validated when it already exists
Ledger(memory_filename, offset, mem_length, n_accounts, n_assets, n_balances, single_asset_account_drive=True, account_drive_token=None)Creates or opens a file backed single asset ledger. Leave account_drive_token unset for Ether, or pass an ERC20 address for a token drive. n_assets and n_balances are unused here but must still be non zero
reset()Clears all accounts, assets and balances

Retrieving accounts and assets

These calls find an entry and return its internal ID. With the find or create operation they also create missing entries, which is the common case when handling deposits.

In a single asset ledger the asset is fixed, so the asset retrieve calls always return id 0, and balances are 64 bit values.

MethodDescription
retrieve_account(account_id=None, account=None)Finds or creates an account. account is a hex string: a 20 byte wallet address or a 32 byte account ID. Returns a dict with account_id and account
retrieve_asset(asset_id=None, token=None, token_id=None, token_id_with_amount=None, base_token=None, force_find=None)Finds or creates an asset. Use base_token=True for Ether, token for ERC20, token plus token_id for ERC721, and add token_id_with_amount=True for ERC1155. force_find=True fails instead of creating. Returns a dict with asset_id, token, token_id and total_supply

State changes

Each operation either completes fully or fails with an error, for example when funds are insufficient.

MethodDescription
deposit(asset_id, account_id, amount)Adds amount of the asset to the account and raises the total supply
withdraw(asset_id, account_id, amount)Removes amount from the account and lowers the total supply
transfer(asset_id, from_account_id, to_account_id, amount)Moves amount between two accounts

Queries

Read only calls. They never change the ledger, so they are safe to use in inspect handlers.

MethodDescription
balance(asset_id, account_id)Returns the account's balance of the asset as an int
supply(asset_id)Returns the total supply of the asset as an int

Errors

Every operation reports failures with a typed error: an exception in Python, a LedgerError in Rust, and a negative code in C and C++. The full list, including insufficient funds, missing accounts and storage limits, is in Types and selectors.

We use cookies to ensure that we give you the best experience on our website. By using the website, you agree to the use of cookies.