Utils
A collection of utility functions.
- pycardano.utils.fee(context: pycardano.backend.base.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(“bytes”)).
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: pycardano.backend.base.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: pycardano.transaction.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: pycardano.backend.base.ChainContext, output: Optional[pycardano.transaction.TransactionOutput] = None, amount: Optional[Union[int, pycardano.transaction.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, pycardano.transaction.Value]], context: pycardano.backend.base.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: pycardano.transaction.TransactionOutput, context: pycardano.backend.base.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
output (TransactionOutput) – A transaction output.
context (ChainContext) – A chain context.
- Returns
Minimum required lovelace amount for this transaction output.
- Return type
int
- pycardano.utils.script_data_hash(redeemers: List[pycardano.plutus.Redeemer], datums: List[Union[pycardano.plutus.PlutusData, dict, pycardano.serialization.IndefiniteList, int, bytes, pycardano.serialization.RawCBOR, pycardano.plutus.RawPlutusData]], cost_models: Optional[Union[pycardano.plutus.CostModels, Dict]] = None) pycardano.hash.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