AssetIdMap

class cometa.assets.asset_id_map.AssetIdMap(ptr=None)[source]

Bases: Mapping[AssetId, int]

Represents a map of asset IDs to their quantities.

This collection type maps asset identifiers to coin amounts, useful for tracking asset balances across multiple tokens.

Example

>>> asset_map = AssetIdMap()
>>> asset_map.insert(AssetId.new_lovelace(), 1000000)
>>> asset_map.get(AssetId.new_lovelace())
1000000
__init__(ptr=None)[source]
Return type:

None

__enter__()[source]
Return type:

AssetIdMap

__exit__(exc_type, exc_val, exc_tb)[source]
Return type:

None

__repr__()[source]

Return repr(self).

Return type:

str

insert(key, value)[source]

Inserts or updates an asset ID with its quantity.

Parameters:
  • key (AssetId) – The asset ID.

  • value (int) – The quantity (can be negative for burning).

Raises:

CardanoError – If insertion fails.

Return type:

None

get(key, default=None)[source]

Retrieves the quantity for a given asset ID.

Parameters:
  • key (AssetId) – The asset ID to look up.

  • default (int | None) – Value to return if key is not found. Defaults to None.

Returns:

The quantity associated with the asset ID, or default if not found.

Return type:

int | None

get_keys()[source]

Retrieves all keys from the map.

Returns:

An AssetIdList containing all asset IDs in the map.

Raises:

CardanoError – If retrieval fails.

Return type:

AssetIdList

get_key_at(index)[source]

Retrieves the asset ID at a specific index.

Parameters:

index (int) – The index of the asset ID to retrieve.

Returns:

The AssetId at the specified index.

Raises:
  • CardanoError – If retrieval fails.

  • IndexError – If index is out of bounds.

Return type:

AssetId

get_value_at(index)[source]

Retrieves the quantity at a specific index.

Parameters:

index (int) – The index of the quantity to retrieve.

Returns:

The quantity at the specified index.

Raises:
  • CardanoError – If retrieval fails.

  • IndexError – If index is out of bounds.

Return type:

int

get_key_value_at(index)[source]

Retrieves the key-value pair at a specific index.

Parameters:

index (int) – The index of the key-value pair to retrieve.

Returns:

A tuple containing the AssetId and quantity at the specified index.

Raises:
  • CardanoError – If retrieval fails.

  • IndexError – If index is out of bounds.

Return type:

tuple[AssetId, int]

__len__()[source]

Returns the number of entries in the map.

Return type:

int

__iter__()[source]

Iterates over all keys (like Python dict).

Return type:

Iterator[AssetId]

__contains__(item)[source]

Checks if an asset ID is in the map.

Parameters:

item (AssetId)

Return type:

bool

__bool__()[source]

Returns True if the map is not empty.

Return type:

bool

__getitem__(key)[source]

Gets a quantity by asset ID using bracket notation.

Parameters:

key (AssetId)

Return type:

int

__setitem__(key, value)[source]

Sets a quantity by asset ID using bracket notation.

Parameters:
Return type:

None

keys()[source]

Returns an iterator over keys (like Python dict).

Return type:

Iterator[AssetId]

values()[source]

Returns an iterator over values (like Python dict).

Return type:

Iterator[int]

items()[source]

Returns an iterator over (key, value) pairs (like Python dict).

Return type:

Iterator[Tuple[AssetId, int]]

__eq__(other)[source]

Checks equality with another AssetIdMap.

Parameters:

other (object)

Return type:

bool

__add__(other)[source]

Adds two asset ID maps together.

Creates a new map where quantities for matching keys are summed.

Parameters:

other (AssetIdMap) – The map to add.

Returns:

A new AssetIdMap with combined quantities.

Raises:

CardanoError – If the operation fails.

Return type:

AssetIdMap

__hash__ = None
__sub__(other)[source]

Subtracts one asset ID map from another.

Creates a new map where quantities for matching keys are subtracted.

Parameters:

other (AssetIdMap) – The map to subtract.

Returns:

A new AssetIdMap with subtracted quantities.

Raises:

CardanoError – If the operation fails.

Return type:

AssetIdMap