AssetNameMap
- class cometa.assets.asset_name_map.AssetNameMap(ptr=None)[source]
Bases:
Mapping[AssetName, int]Represents a map of asset names to their quantities.
This collection type is used within Cardano’s multi-asset structure to associate asset names with their amounts under a specific policy ID. The amounts can be positive or negative (for minting/burning).
Example
>>> asset_map = AssetNameMap() >>> asset_map.insert(AssetName.from_string("Token1"), 1000) >>> asset_map.get(AssetName.from_string("Token1")) 1000
- classmethod from_cbor(reader)[source]
Deserializes an AssetNameMap from CBOR data.
- Parameters:
reader (CborReader) – A CborReader positioned at the asset name map data.
- Returns:
A new AssetNameMap deserialized from the CBOR data.
- Raises:
CardanoError – If deserialization fails.
- Return type:
- insert(key, value)[source]
Inserts or updates an asset name with its quantity.
- Parameters:
key (AssetName) – The asset name.
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 name.
- Parameters:
key (AssetName) – The asset name to look up.
default (int | None) – Value to return if key is not found. Defaults to None.
- Returns:
The quantity associated with the asset name, or default if not found.
- Return type:
int | None
- get_key_at(index)[source]
Retrieves the asset name at a specific index.
- Parameters:
index (int) – The index of the asset name to retrieve.
- Returns:
The AssetName at the specified index.
- Raises:
CardanoError – If retrieval fails.
IndexError – If index is out of bounds.
- Return type:
- 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 AssetName and quantity at the specified index.
- Raises:
CardanoError – If retrieval fails.
IndexError – If index is out of bounds.
- Return type:
tuple[AssetName, int]
- get_keys()[source]
Retrieves all keys (asset names) from the map.
- Returns:
An AssetNameList containing all asset names in the map.
- Raises:
CardanoError – If retrieval fails.
- Return type:
- to_cbor(writer)[source]
Serializes the asset name map to CBOR format.
- Parameters:
writer (CborWriter) – A CborWriter to write the serialized data to.
- Raises:
CardanoError – If serialization fails.
- Return type:
None
- to_cip116_json(writer)[source]
Serializes this asset name map to CIP-116 compliant JSON.
- Parameters:
writer (JsonWriter) – The JsonWriter to write the JSON to.
- Raises:
CardanoError – If serialization fails.
- Return type:
None
- add(other)[source]
Combines two asset name maps by adding their quantities.
- Parameters:
other (AssetNameMap) – The other asset name map to add.
- Returns:
A new AssetNameMap with combined quantities.
- Raises:
CardanoError – If the operation fails.
- Return type:
- subtract(other)[source]
Subtracts another asset name map from this one.
- Parameters:
other (AssetNameMap) – The asset name map to subtract.
- Returns:
A new AssetNameMap with subtracted quantities.
- Raises:
CardanoError – If the operation fails.
- Return type:
- __getitem__(key)[source]
Gets a value by key using bracket notation.
- Parameters:
key (AssetName)
- Return type:
int
- __setitem__(key, value)[source]
Sets a value by key using bracket notation.
- Parameters:
key (AssetName)
value (int)
- Return type:
None
- __contains__(item)[source]
Checks if an asset name is in the map.
- Parameters:
item (AssetName)
- Return type:
bool
- __eq__(other)[source]
Checks equality with another AssetNameMap.
- Parameters:
other (object)
- Return type:
bool
- __add__(other)[source]
Adds two asset name maps using the + operator.
- Parameters:
other (AssetNameMap)
- Return type:
- __sub__(other)[source]
Subtracts an asset name map using the - operator.
- Parameters:
other (AssetNameMap)
- Return type:
- __hash__ = None