Utils

A collection of utility functions.

pycardano.utils.fee(context: ChainContext, length: int, exec_steps: int = 0, max_mem_unit: int = 0) int

Calculate fee based on the length of a transaction’s CBOR bytes and script execution.

Parameters:
  • context (ChainConext) – A chain context.

  • length (int) – The length of CBOR bytes, which could usually be derived by len(tx.to_cbor()).

  • exec_steps (Optional[int]) – Number of execution steps run by plutus scripts in the transaction.

  • max_mem_unit (Optional[int]) – Max numer of memory units run by plutus scripts in the transaction.

Returns:

Minimum acceptable transaction fee.

Return type:

int

pycardano.utils.max_tx_fee(context: ChainContext) int

Calculate the maximum possible transaction fee based on protocol parameters.

Parameters:

context (ChainContext) – A chain context.

Returns:

Maximum possible tx fee in lovelace.

Return type:

int

pycardano.utils.bundle_size(multi_asset: MultiAsset) int

Calculate size of a multi-asset in words. (1 word = 8 bytes)

Parameters:

multi_asset (MultiAsset) – Input multi asset.

Returns:

Number of words.

Return type:

int

pycardano.utils.min_lovelace(context: ChainContext, output: Optional[TransactionOutput] = None, amount: Optional[Union[int, Value]] = None, has_datum: bool = False) int

Calculate minimum lovelace a transaction output needs to hold.

More info could be found in this page.

Parameters:
  • context (ChainContext) – A chain context.

  • output (TransactionOutput) – A transaction output (for post-alonzo transactions).

  • amount (Union[int, Value]) – Amount from a transaction output (for pre-alonzo transactions).

  • has_datum (bool) – Whether the transaction output contains datum hash (for pre-alonzo transactions).

Returns:

Minimum required lovelace amount for this transaction output.

Return type:

int

pycardano.utils.min_lovelace_pre_alonzo(amount: Optional[Union[int, Value]], context: ChainContext, has_datum: bool = False) int

Calculate minimum lovelace a transaction output needs to hold.

More info could be found in this page.

Parameters:
  • amount (Union[int, Value]) – Amount from a transaction output.

  • context (ChainContext) – A chain context.

  • has_datum (bool) – Whether the transaction output contains datum hash.

Returns:

Minimum required lovelace amount for this transaction output.

Return type:

int

pycardano.utils.min_lovelace_post_alonzo(output: TransactionOutput, context: ChainContext) int

Calculate minimum lovelace a transaction output needs to hold post alonzo.

This implementation is copied from the origianl Haskell implementation: https://github.com/input-output-hk/cardano-ledger/blob/eb053066c1d3bb51fb05978eeeab88afc0b049b2/eras/babbage/impl/src/Cardano/Ledger/Babbage/Rules/Utxo.hs#L242-L265

Parameters:
Returns:

Minimum required lovelace amount for this transaction output.

Return type:

int

pycardano.utils.script_data_hash(redeemers: List[Redeemer], datums: List[Union[PlutusData, dict, int, bytes, IndefiniteList, RawCBOR, RawPlutusData]], cost_models: Optional[Union[CostModels, Dict]] = None) ScriptDataHash

Calculate plutus script data hash

Parameters:
  • redeemers (List[Redeemer]) – Redeemers to include.

  • datums (List[Datum]) – Datums to include.

  • cost_models (Optional[CostModels]) – Cost models.

Returns:

Plutus script data hash

Return type:

ScriptDataHash