Value

class cometa.transaction_body.value.Value(ptr)[source]

Bases: object

Represents a value in Cardano containing both ADA (in lovelace) and multi-assets.

A Value can represent: - Pure ADA amounts (coin only) - Multi-asset bundles (native tokens) - Combined ADA and multi-assets

__init__(ptr)[source]
Return type:

None

__enter__()[source]
Return type:

Value

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

None

__repr__()[source]

Return repr(self).

Return type:

str

classmethod new(coin, multi_asset=None)[source]

Creates a new Value with the given coin amount and optional multi-assets.

Parameters:
  • coin (int) – The amount in lovelace.

  • multi_asset (MultiAsset | None) – Optional multi-asset bundle.

Returns:

A new Value instance.

Raises:

CardanoError – If creation fails.

Return type:

Value

classmethod zero()[source]

Creates a zero Value.

Returns:

A new Value with zero coin and no assets.

Raises:

CardanoError – If creation fails.

Return type:

Value

classmethod from_coin(lovelace)[source]

Creates a Value from only ADA amount.

Parameters:

lovelace (int) – The amount in lovelace.

Returns:

A new Value instance.

Raises:

CardanoError – If creation fails.

Return type:

Value

classmethod from_asset_map(asset_map)[source]

Creates a Value from an asset ID map.

Parameters:

asset_map (AssetIdMap) – The asset ID map containing asset quantities.

Returns:

A new Value instance.

Raises:

CardanoError – If creation fails.

Return type:

Value

classmethod from_cbor(reader)[source]

Deserializes a Value from CBOR data.

Parameters:

reader (CborReader) – A CborReader positioned at the value data.

Returns:

A new Value deserialized from the CBOR data.

Raises:

CardanoError – If deserialization fails.

Return type:

Value

classmethod from_dict(data)[source]

Creates a Value from a Python dict representation.

Parameters:

data (int | List[int | Dict[bytes, Dict[bytes, int]]]) – Either: - An integer representing lovelace amount (ADA only) - A list of [lovelace, {policy_id: {asset_name: amount}}]

Returns:

A new Value instance.

Raises:
  • CardanoError – If creation fails.

  • ValueError – If the format is invalid.

Return type:

Value

Example

>>> # ADA only
>>> value = Value.from_dict(1500000)
>>> # ADA with native tokens
>>> value = Value.from_dict([
...     1500000,
...     {
...         bytes.fromhex("57fca08..."): {
...             b"CHOC": 2000
...         }
...     }
... ])
to_dict()[source]

Converts this Value to a Python dict representation.

Returns:

Either an integer (if ADA only) or a list of [lovelace, multi_asset_dict].

Return type:

int | List[int | Dict[bytes, Dict[bytes, int]]]

Example

>>> value = Value.from_coin(1500000)
>>> value.to_dict()
1500000
>>> value.add_asset(policy_id, b"TOKEN", 100)
>>> value.to_dict()
[1500000, {policy_id: {b"TOKEN": 100}}]
to_cbor(writer)[source]

Serializes the value 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 value to CIP-116 JSON format.

CIP-116 defines a standard JSON representation for Cardano values.

Parameters:

writer – A JsonWriter to write the serialized data to.

Raises:

CardanoError – If serialization fails.

Return type:

None

Example

>>> from cometa.json import JsonWriter
>>> value = Value.from_coin(1000000)
>>> writer = JsonWriter()
>>> value.to_cip116_json(writer)
>>> json_str = writer.encode()
property coin: int

The ADA amount in lovelace.

Returns:

The coin amount.

property multi_asset: MultiAsset | None

The multi-asset bundle.

Returns:

The MultiAsset if present, None otherwise.

add_coin(coin)[source]

Adds coin to this value (with overflow check).

Parameters:

coin (int) – The amount to add in lovelace.

Raises:

CardanoError – If addition fails or would overflow.

Return type:

None

subtract_coin(coin)[source]

Subtracts coin from this value (with underflow check).

Parameters:

coin (int) – The amount to subtract in lovelace.

Raises:

CardanoError – If subtraction fails or would underflow.

Return type:

None

add_multi_asset(assets)[source]

Adds multi-assets to this value.

Parameters:

assets (MultiAsset) – The multi-assets to add.

Raises:

CardanoError – If addition fails.

Return type:

None

subtract_multi_asset(assets)[source]

Subtracts multi-assets from this value.

Parameters:

assets (MultiAsset) – The multi-assets to subtract.

Raises:

CardanoError – If subtraction fails.

Return type:

None

add_asset(policy_id, asset_name, quantity)[source]

Adds a specific asset to this value.

Parameters:
  • policy_id (bytes) – The policy ID (28 bytes).

  • asset_name (bytes) – The asset name.

  • quantity (int) – The quantity to add.

Raises:

CardanoError – If addition fails.

Return type:

None

add_asset_with_id(asset_id, quantity)[source]

Adds a specific asset to this value using an AssetId.

Parameters:
  • asset_id (AssetId) – The asset ID identifying the asset.

  • quantity (int) – The quantity to add.

Raises:

CardanoError – If addition fails.

Return type:

None

as_asset_map()[source]

Returns this value as a flattened asset ID map.

Returns:

An AssetIdMap containing all assets including ADA.

Return type:

AssetIdMap

property asset_count: int

The number of distinct assets in this value (including ADA if non-zero).

Returns:

The asset count.

property is_zero: bool

Whether this value is zero.

Returns:

True if both coin and all assets are zero.

__add__(other)[source]

Adds two values together, returning a new value.

Parameters:

other (Value)

Return type:

Value

__sub__(other)[source]

Subtracts another value from this one, returning a new value.

Parameters:

other (Value)

Return type:

Value

get_intersection(other)[source]

Returns a list of assets that are present in both Values.

Parameters:

other (Value) – The other Value to intersect with.

Returns:

An AssetIdList containing asset IDs present in both Values.

Raises:

CardanoError – If the operation fails.

Return type:

AssetIdList

get_intersection_count(other)[source]

Returns the count of assets present in both Values.

Parameters:

other (Value) – The other Value to intersect with.

Returns:

The number of assets present in both Values.

Raises:

CardanoError – If the operation fails.

Return type:

int

__eq__(other)[source]

Checks equality with another Value.

Parameters:

other (object)

Return type:

bool

__hash__ = None
__bool__()[source]

Returns True if this value is non-zero.

Return type:

bool