|
| 1 | +import ast |
| 2 | +import configparser |
| 3 | +import logging |
| 4 | +import os |
| 5 | +from collections import defaultdict |
| 6 | +from typing import Any, Dict, List, Tuple |
| 7 | + |
| 8 | +from indexer.domain.log import Log |
| 9 | +from indexer.executors.batch_work_executor import BatchWorkExecutor |
| 10 | +from indexer.jobs import FilterTransactionDataJob |
| 11 | +from indexer.modules.custom import common_utils |
| 12 | +from indexer.modules.custom.staking_fbtc import utils |
| 13 | +from indexer.modules.custom.staking_fbtc.domain.feature_staked_fbtc_detail import ( |
| 14 | + StakedFBTCCurrentStatus, |
| 15 | + StakedFBTCDetail, |
| 16 | +) |
| 17 | +from indexer.specification.specification import TopicSpecification, TransactionFilterByLogs |
| 18 | +from indexer.utils.abi import decode_log |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class ExportLockedFBTCDetailJob(FilterTransactionDataJob): |
| 24 | + dependency_types = [Log] |
| 25 | + output_types = [StakedFBTCDetail, StakedFBTCCurrentStatus] |
| 26 | + able_to_reorg = True |
| 27 | + |
| 28 | + def __init__(self, **kwargs): |
| 29 | + super().__init__(**kwargs) |
| 30 | + self._batch_work_executor = BatchWorkExecutor( |
| 31 | + kwargs["batch_size"], |
| 32 | + kwargs["max_workers"], |
| 33 | + job_name=self.__class__.__name__, |
| 34 | + ) |
| 35 | + self._is_batch = kwargs["batch_size"] > 1 |
| 36 | + self._batch_size = kwargs["batch_size"] |
| 37 | + self._max_worker = kwargs["max_workers"] |
| 38 | + self._chain_id = common_utils.get_chain_id(self._web3) |
| 39 | + self._load_config("config.ini", self._chain_id) |
| 40 | + self._service = kwargs["config"].get("db_service") |
| 41 | + self._current_holdings = utils.get_staked_fbtc_status( |
| 42 | + self._service, list(self.staked_abi_dict.keys()), int(kwargs["config"].get("start_block")) |
| 43 | + ) |
| 44 | + |
| 45 | + def _load_config(self, filename, chain_id): |
| 46 | + base_path = os.path.dirname(os.path.abspath(__file__)) |
| 47 | + full_path = os.path.join(base_path, filename) |
| 48 | + config = configparser.ConfigParser() |
| 49 | + config.read(full_path) |
| 50 | + |
| 51 | + try: |
| 52 | + staked_protocol_dict_str = config.get(str(chain_id), "STAKED_PROTOCOL_DICT") |
| 53 | + self.staked_protocol_dict = ast.literal_eval(staked_protocol_dict_str) |
| 54 | + |
| 55 | + staked_topic0_dict_str = config.get(str(chain_id), "STAKED_TOPIC0_DICT") |
| 56 | + self.staked_topic0_dict = ast.literal_eval(staked_topic0_dict_str) |
| 57 | + |
| 58 | + staked_abi_dict_str = config.get(str(chain_id), "STAKED_ABI_DICT") |
| 59 | + self.staked_abi_dict = ast.literal_eval(staked_abi_dict_str) |
| 60 | + |
| 61 | + except (configparser.NoOptionError, configparser.NoSectionError) as e: |
| 62 | + raise ValueError(f"Missing required configuration in {filename}: {str(e)}") |
| 63 | + except (SyntaxError, ValueError) as e: |
| 64 | + raise ValueError(f"Error parsing configuration in {filename}: {str(e)}") |
| 65 | + |
| 66 | + def get_filter(self): |
| 67 | + return TransactionFilterByLogs( |
| 68 | + [ |
| 69 | + TopicSpecification(topics=list(self.staked_abi_dict.keys())), |
| 70 | + ] |
| 71 | + ) |
| 72 | + |
| 73 | + def _collect(self, **kwargs): |
| 74 | + logs = self._data_buff[Log.type()] |
| 75 | + staked_details, current_status_list, current_holdings = collect_detail( |
| 76 | + logs, self._current_holdings, self.staked_abi_dict, self.staked_protocol_dict |
| 77 | + ) |
| 78 | + for data in staked_details: |
| 79 | + self._collect_item(StakedFBTCDetail.type(), data) |
| 80 | + for data in current_status_list: |
| 81 | + self._collect_item(StakedFBTCCurrentStatus.type(), data) |
| 82 | + self._current_holdings = current_holdings |
| 83 | + |
| 84 | + def _process(self, **kwargs): |
| 85 | + self._data_buff[StakedFBTCDetail.type()].sort(key=lambda x: x.block_number) |
| 86 | + self._data_buff[StakedFBTCCurrentStatus.type()].sort(key=lambda x: x.block_number) |
| 87 | + |
| 88 | + |
| 89 | +def collect_detail( |
| 90 | + logs: List[Log], |
| 91 | + current_holdings: Dict[str, Dict[str, StakedFBTCCurrentStatus]], |
| 92 | + staked_abi_dict: Dict[str, Any], |
| 93 | + staked_protocol_dict: Dict[str, str], |
| 94 | +) -> Tuple[List[StakedFBTCDetail], List[StakedFBTCCurrentStatus], Dict[str, Dict[str, StakedFBTCCurrentStatus]]]: |
| 95 | + current_status = defaultdict( |
| 96 | + lambda: defaultdict( |
| 97 | + lambda: StakedFBTCCurrentStatus( |
| 98 | + vault_address="", |
| 99 | + protocol_id="", |
| 100 | + wallet_address="", |
| 101 | + amount=0, |
| 102 | + changed_amount=0, |
| 103 | + block_number=0, |
| 104 | + block_timestamp=0, |
| 105 | + ) |
| 106 | + ) |
| 107 | + ) |
| 108 | + for contract_address, wallet_dict in current_holdings.items(): |
| 109 | + for wallet_address, status in wallet_dict.items(): |
| 110 | + current_status[contract_address][wallet_address] = status |
| 111 | + |
| 112 | + logs_by_address = defaultdict(lambda: defaultdict(list)) |
| 113 | + for log in logs: |
| 114 | + if log.topic0 in staked_abi_dict and log.address in staked_protocol_dict: |
| 115 | + logs_by_address[log.address][log.block_number].append(log) |
| 116 | + |
| 117 | + staked_details = [] |
| 118 | + |
| 119 | + for address, blocks in logs_by_address.items(): |
| 120 | + protocol_id = staked_protocol_dict[address] |
| 121 | + |
| 122 | + for block_number in sorted(blocks.keys()): |
| 123 | + block_logs = blocks[block_number] |
| 124 | + block_changes = defaultdict(int) |
| 125 | + block_timestamp = block_logs[0].block_timestamp |
| 126 | + |
| 127 | + for log in block_logs: |
| 128 | + decode_staked = decode_log(staked_abi_dict[log.topic0], log) |
| 129 | + wallet_address = decode_staked["user"] |
| 130 | + amount = decode_staked["amount"] |
| 131 | + block_changes[wallet_address] += amount |
| 132 | + |
| 133 | + for wallet_address, change in block_changes.items(): |
| 134 | + current_amount = current_status[address][wallet_address].amount |
| 135 | + new_amount = current_amount + change |
| 136 | + |
| 137 | + current_status[address][wallet_address] = StakedFBTCCurrentStatus( |
| 138 | + vault_address=address, |
| 139 | + protocol_id=protocol_id, |
| 140 | + wallet_address=wallet_address, |
| 141 | + amount=new_amount, |
| 142 | + changed_amount=change, |
| 143 | + block_number=block_number, |
| 144 | + block_timestamp=block_timestamp, |
| 145 | + ) |
| 146 | + |
| 147 | + staked_details.append( |
| 148 | + StakedFBTCDetail( |
| 149 | + vault_address=address, |
| 150 | + protocol_id=protocol_id, |
| 151 | + wallet_address=wallet_address, |
| 152 | + amount=new_amount, |
| 153 | + changed_amount=change, |
| 154 | + block_number=block_number, |
| 155 | + block_timestamp=block_timestamp, |
| 156 | + ) |
| 157 | + ) |
| 158 | + |
| 159 | + current_status_list = [status for address_dict in current_status.values() for status in address_dict.values()] |
| 160 | + |
| 161 | + updated_current_holdings = {} |
| 162 | + for contract_address, wallet_dict in current_status.items(): |
| 163 | + updated_current_holdings[contract_address] = {} |
| 164 | + for wallet_address, status in wallet_dict.items(): |
| 165 | + updated_current_holdings[contract_address][wallet_address] = status |
| 166 | + |
| 167 | + return staked_details, current_status_list, updated_current_holdings |
0 commit comments