Skip to content

Commit

Permalink
simplify _transaction_balance
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelsoup42 committed Sep 10, 2023
1 parent be92861 commit 2c256d1
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/cardano_account_pandas_dumper/cardano_account_pandas_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
FrozenSet,
List,
Mapping,
MutableMapping,
Optional,
Set,
Tuple,
)
import pandas as pd
import numpy as np
Expand Down Expand Up @@ -290,7 +292,7 @@ def _format_message(self, tx_obj: Namespace) -> str:
]
):
result = ["(internal)"]
return " ".join(result)
return " ".join(result).removeprefix("Message : ")

def _format_script(self, script: str) -> str:
return self.known_dict[self.SCRIPTS_KEY].get(
Expand All @@ -314,7 +316,9 @@ def _decimals_for_asset(self, asset: str) -> int:
return 0

def _transaction_balance(self, transaction: Namespace) -> Any:
result = {}
result: MutableMapping[Tuple[str, str], np.longlong] = defaultdict(
lambda: np.longlong(0)
)
result[(self.data.LOVELACE_ASSET, "fees")] = np.longlong(transaction.fees)
result[(self.data.LOVELACE_ASSET, "deposit")] = np.longlong(transaction.deposit)
result[(self.data.LOVELACE_ASSET, "rewards")] = (
Expand All @@ -328,23 +332,16 @@ def _transaction_balance(self, transaction: Namespace) -> Any:
if not transaction.reward_amount
else np.longlong(transaction.reward_amount)
)
balance_result: Dict = {}
for utxo in transaction.utxos.nonref_inputs:
if not utxo.collateral or not transaction.valid_contract:
for amount in utxo.amount:
key = (amount.unit, utxo.address)
if key not in balance_result:
balance_result[key] = np.longlong(0)
balance_result[key] -= np.longlong(amount.quantity)
result[(amount.unit, utxo.address)] -= np.longlong(amount.quantity)

for utxo in transaction.utxos.outputs:
for amount in utxo.amount:
key = (amount.unit, utxo.address)
if key not in balance_result:
balance_result[key] = np.longlong(0)
balance_result[key] += np.longlong(amount.quantity)
result.update({k: v for k, v in balance_result.items() if v != np.longlong(0)})
return result
result[(amount.unit, utxo.address)] += np.longlong(amount.quantity)

return dict([i for i in result.items() if i[1] != np.longlong(0)])

def _make_hash_frame(self) -> pd.DataFrame:
tx_hash = pd.DataFrame(
Expand Down

0 comments on commit 2c256d1

Please sign in to comment.