Skip to content

Commit

Permalink
Helper functions for UTxO data
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoura committed Jun 29, 2022
1 parent f512a2c commit 80a6854
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cardano_clusterlib/clusterlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@
from cardano_clusterlib.structs import TxRawOutput
from cardano_clusterlib.structs import UTXOData
from cardano_clusterlib.structs import Value
from cardano_clusterlib.txtools import calculate_utxos_balance
from cardano_clusterlib.txtools import filter_utxo_with_highest_amount
from cardano_clusterlib.types import FileType
12 changes: 9 additions & 3 deletions cardano_clusterlib/clusterlib_klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,25 +222,29 @@ def get_utxo(
self,
address: str = "",
txin: str = "",
utxo: Optional[structs.UTXOData] = None,
coins: UnpackableSequence = (),
) -> List[structs.UTXOData]:
"""Return UTxO info for payment address.
Args:
address: A payment address.
txin: A transaction input (TxId#TxIx).
coins: A list (iterable) of coin names (asset IDs).
utxo: A representation of UTxO data (`structs.UTXOData`).
coins: A list (iterable) of coin names (asset IDs, optional).
Returns:
List[structs.UTXOData]: A list of UTxO data.
"""
cli_args = ["utxo", "--out-file", "/dev/stdout"]
if address:
cli_args.extend(["--address", address])
elif txin: # noqa: SIM106
elif txin:
cli_args.extend(["--tx-in", txin])
elif utxo: # noqa: SIM106
cli_args.extend(["--tx-in", f"{utxo.utxo_hash}#{utxo.utxo_ix}"])
else:
raise AssertionError("Either `address` or `txin` need to be specified.")
raise AssertionError("Either `address`, `txin` or `utxo` need to be specified.")

utxo_dict = json.loads(self.query_cli(cli_args))
return txtools.get_utxo(utxo_dict=utxo_dict, address=address, coins=coins)
Expand Down Expand Up @@ -1265,6 +1269,7 @@ def get_address_balance(self, address: str, coin: str = consts.DEFAULT_COIN) ->
Args:
address: A payment address string.
coin: A coin name (asset IDs).
Returns:
int: A total balance.
Expand All @@ -1280,6 +1285,7 @@ def get_utxo_with_highest_amount(
Args:
address: A payment address string.
coin: A coin name (asset IDs).
Returns:
structs.UTXOData: An UTxO record with the highest amount.
Expand Down
33 changes: 33 additions & 0 deletions cardano_clusterlib/txtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,39 @@ def get_utxo( # noqa: C901
return utxo


def calculate_utxos_balance(utxos: List[structs.UTXOData], coin: str = consts.DEFAULT_COIN) -> int:
"""Calculate sum of UTxO balances.
Args:
utxos: A list of UTxO data.
coin: A coin name (asset IDs).
Returns:
int: A total balance.
"""
filtered_utxos = [u for u in utxos if u.coin == coin]
address_balance = functools.reduce(lambda x, y: x + y.amount, filtered_utxos, 0)
return int(address_balance)


def filter_utxo_with_highest_amount(
utxos: List[structs.UTXOData],
coin: str = consts.DEFAULT_COIN,
) -> structs.UTXOData:
"""Return data for UTxO with highest amount.
Args:
utxos: A list of UTxO data.
coin: A coin name (asset IDs).
Returns:
structs.UTXOData: An UTxO record with the highest amount.
"""
filtered_utxos = [u for u in utxos if u.coin == coin]
highest_amount_rec = max(filtered_utxos, key=lambda x: x.amount)
return highest_amount_rec


def _get_script_args( # noqa: C901
script_txins: structs.OptionalScriptTxIn,
mint: structs.OptionalMint,
Expand Down

0 comments on commit 80a6854

Please sign in to comment.