mirror of
https://github.com/autistic-symposium/blockchain-data-engineering-toolkit.git
synced 2025-12-31 07:20:24 -05:00
first commit
This commit is contained in:
commit
bb17a2a56e
29 changed files with 1238 additions and 0 deletions
36
token-scanner-api/src/utils/data_processing.py
Normal file
36
token-scanner-api/src/utils/data_processing.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
# utils/data_processing.py
|
||||
# Data processing for token transfers.
|
||||
|
||||
import collections
|
||||
from decimal import Decimal
|
||||
|
||||
import src.utils.os_utils as os_utils
|
||||
|
||||
|
||||
def process_balances(filepath) -> list:
|
||||
"""Return a list of balances for each address."""
|
||||
|
||||
data = os_utils.open_json(filepath)
|
||||
balances = collections.defaultdict(Decimal)
|
||||
|
||||
for _, block_data in data.items():
|
||||
for _, tx_data in block_data.items():
|
||||
for _, event_data in tx_data.items():
|
||||
balances[event_data["from"]] -= Decimal(event_data["value"])
|
||||
balances[event_data["to"]] += Decimal(event_data["value"])
|
||||
|
||||
|
||||
balances = {key: float(value) for key, value in balances.items() if value >= Decimal('0')}
|
||||
return dict(sorted(balances.items(), key=lambda x: x[1]))
|
||||
|
||||
|
||||
def run_data_processing(filepath) -> None:
|
||||
"""Run data processing."""
|
||||
|
||||
balance_data = process_balances(filepath)
|
||||
balance_output_file = os_utils.create_result_file("balances")
|
||||
balance_output_filepath = os_utils.set_output(balance_output_file)
|
||||
|
||||
os_utils.log_info(f' Writing balances to {balance_output_filepath}')
|
||||
os_utils.save_output(balance_output_filepath, balance_data)
|
||||
Loading…
Add table
Add a link
Reference in a new issue