MultiAsset
- class cometa.assets.multi_asset.MultiAsset(ptr=None)[source]
Bases:
objectRepresents a collection of native assets in Cardano.
MultiAsset maps policy IDs to asset name maps, allowing representation of multiple tokens across different minting policies. It’s used in transaction outputs to specify token holdings.
Example
>>> multi_asset = MultiAsset() >>> asset_map = AssetNameMap() >>> asset_map.insert(AssetName.from_string("Token"), 100) >>> multi_asset.insert_assets(policy_id, asset_map)
- classmethod from_cbor(reader)[source]
Deserializes a MultiAsset from CBOR data.
- Parameters:
reader (CborReader) – A CborReader positioned at the multi-asset data.
- Returns:
A new MultiAsset deserialized from the CBOR data.
- Raises:
CardanoError – If deserialization fails.
- Return type:
- property policy_count: int
Returns the number of distinct policy IDs in this multi-asset.
- Returns:
The number of unique policy IDs.
- insert_assets(policy_id, assets)[source]
Inserts or updates assets under a specific policy ID.
- Parameters:
policy_id (Blake2bHash) – The minting policy ID.
assets (AssetNameMap) – The asset name map with quantities.
- Raises:
CardanoError – If insertion fails.
- Return type:
None
- get_assets(policy_id)[source]
Retrieves the assets under a specific policy ID.
- Parameters:
policy_id (Blake2bHash) – The minting policy ID to look up.
- Returns:
The AssetNameMap for the specified policy.
- Raises:
CardanoError – If the policy ID is not found or retrieval fails.
- Return type:
- get(policy_id, asset_name)[source]
Retrieves the quantity of a specific asset.
- Parameters:
policy_id (Blake2bHash) – The minting policy ID.
asset_name (AssetName) – The name of the asset.
- Returns:
The quantity of the asset.
- Raises:
CardanoError – If the asset is not found or retrieval fails.
- Return type:
int
- get_with_id(asset_id)[source]
Retrieves the quantity of an asset by its ID.
- Parameters:
asset_id (AssetId) – The asset ID to look up.
- Returns:
The quantity of the asset (0 if not present).
- Raises:
CardanoError – If retrieval fails.
- Return type:
int
- set(policy_id, asset_name, value)[source]
Sets the quantity of a specific asset.
- Parameters:
policy_id (Blake2bHash) – The minting policy ID.
asset_name (AssetName) – The name of the asset.
value (int) – The new quantity.
- Raises:
CardanoError – If the operation fails.
- Return type:
None
- to_cbor(writer)[source]
Serializes the multi-asset 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 multi-asset 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 multi-assets by adding their quantities.
- Parameters:
other (MultiAsset) – The other multi-asset to add.
- Returns:
A new MultiAsset with combined quantities.
- Raises:
CardanoError – If the operation fails.
- Return type:
- subtract(other)[source]
Subtracts another multi-asset from this one.
- Parameters:
other (MultiAsset) – The multi-asset to subtract.
- Returns:
A new MultiAsset with subtracted quantities.
- Raises:
CardanoError – If the operation fails.
- Return type:
- get_positive()[source]
Returns a new multi-asset containing only assets with positive quantities.
- Returns:
A new MultiAsset with only positive quantities.
- Raises:
CardanoError – If the operation fails.
- Return type:
- get_negative()[source]
Returns a new multi-asset containing only assets with negative quantities.
- Returns:
A new MultiAsset with only negative quantities.
- Raises:
CardanoError – If the operation fails.
- Return type:
- __eq__(other)[source]
Checks equality with another MultiAsset.
- Parameters:
other (object)
- Return type:
bool
- __add__(other)[source]
Adds two multi-assets using the + operator.
- Parameters:
other (MultiAsset)
- Return type:
- __sub__(other)[source]
Subtracts a multi-asset using the - operator.
- Parameters:
other (MultiAsset)
- Return type:
- __getitem__(key)[source]
Gets asset map for a policy ID using bracket notation.
- Parameters:
key (Blake2bHash)
- Return type:
- __setitem__(key, value)[source]
Sets assets for a policy ID using bracket notation.
- Parameters:
key (Blake2bHash)
value (AssetNameMap)
- Return type:
None
- __contains__(key)[source]
Checks if a policy ID is in the multi-asset.
- Parameters:
key (Blake2bHash)
- Return type:
bool
- __iter__()[source]
Iterates over all policy IDs (like Python dict).
- Return type:
Iterator[Blake2bHash]
- keys()[source]
Returns an iterator over policy IDs (like Python dict).
- Return type:
Iterator[Blake2bHash]
- values()[source]
Returns an iterator over asset name maps (like Python dict).
- Return type:
Iterator[AssetNameMap]
- __hash__ = None
- items()[source]
Returns an iterator over (policy_id, assets) pairs (like Python dict).
- Return type:
Iterator[tuple[Blake2bHash, AssetNameMap]]