|
| 1 | +from shared.clickhouse.batch_insert import buffer_insert |
| 2 | +from shared.shovel_base_class import ShovelBaseClass |
| 3 | +from shared.substrate import get_substrate_client |
| 4 | +from shared.clickhouse.utils import ( |
| 5 | + get_clickhouse_client, |
| 6 | + table_exists, |
| 7 | +) |
| 8 | +from shared.exceptions import DatabaseConnectionError, ShovelProcessingError |
| 9 | +from shared.block_metadata import get_block_metadata |
| 10 | +import logging |
| 11 | + |
| 12 | + |
| 13 | +logging.basicConfig(level=logging.INFO, |
| 14 | + format="%(asctime)s %(process)d %(message)s") |
| 15 | + |
| 16 | + |
| 17 | +class AlphaToTaoShovel(ShovelBaseClass): |
| 18 | + table_name = "shovel_alpha_to_tao" |
| 19 | + skip_interval = 7200 |
| 20 | + |
| 21 | + def __init__(self, name): |
| 22 | + super().__init__(name, skip_interval=self.skip_interval) |
| 23 | + self.starting_block = 3638246 |
| 24 | + |
| 25 | + def process_block(self, n): |
| 26 | + do_process_block(self, n) |
| 27 | + |
| 28 | + |
| 29 | +def do_process_block(self, n): |
| 30 | + try: |
| 31 | + substrate = get_substrate_client() |
| 32 | + |
| 33 | + try: |
| 34 | + if not table_exists(self.table_name): |
| 35 | + query = f""" |
| 36 | + CREATE TABLE IF NOT EXISTS {self.table_name} ( |
| 37 | + block_number UInt64 CODEC(Delta, ZSTD), |
| 38 | + timestamp DateTime CODEC(Delta, ZSTD), |
| 39 | + netuid UInt8 CODEC(Delta, ZSTD), |
| 40 | + alpha_to_tao Float64 CODEC(ZSTD) |
| 41 | + ) ENGINE = ReplacingMergeTree() |
| 42 | + PARTITION BY toYYYYMM(timestamp) |
| 43 | + ORDER BY (block_number, netuid) |
| 44 | + """ |
| 45 | + get_clickhouse_client().execute(query) |
| 46 | + except Exception as e: |
| 47 | + raise DatabaseConnectionError(f"Failed to create/check table: {str(e)}") |
| 48 | + |
| 49 | + try: |
| 50 | + block_timestamp, block_hash = get_block_metadata(n) |
| 51 | + if block_timestamp == 0 and n != 0: |
| 52 | + raise ShovelProcessingError(f"Invalid block timestamp (0) for block {n}") |
| 53 | + except Exception as e: |
| 54 | + raise ShovelProcessingError(f"Failed to get block metadata: {str(e)}") |
| 55 | + |
| 56 | + try: |
| 57 | + # Get list of active subnets |
| 58 | + networks_added = substrate.query_map( |
| 59 | + 'SubtensorModule', |
| 60 | + 'NetworksAdded', |
| 61 | + block_hash=block_hash |
| 62 | + ) |
| 63 | + networks = [int(net[0].value) for net in networks_added] |
| 64 | + |
| 65 | + # Process each subnet |
| 66 | + for netuid in networks: |
| 67 | + subnet_tao = substrate.query( |
| 68 | + 'SubtensorModule', |
| 69 | + 'SubnetTAO', |
| 70 | + [netuid], |
| 71 | + block_hash=block_hash |
| 72 | + ).value / 1e9 |
| 73 | + |
| 74 | + subnet_alpha_in = substrate.query( |
| 75 | + 'SubtensorModule', |
| 76 | + 'SubnetAlphaIn', |
| 77 | + [netuid], |
| 78 | + block_hash=block_hash |
| 79 | + ).value / 1e9 |
| 80 | + |
| 81 | + # Calculate exchange rate (TAO per Alpha) |
| 82 | + alpha_to_tao = subnet_tao / subnet_alpha_in if subnet_alpha_in > 0 else 0 |
| 83 | + |
| 84 | + buffer_insert( |
| 85 | + self.table_name, |
| 86 | + [n, block_timestamp, netuid, alpha_to_tao] |
| 87 | + ) |
| 88 | + |
| 89 | + except Exception as e: |
| 90 | + raise DatabaseConnectionError(f"Failed to insert data into buffer: {str(e)}") |
| 91 | + |
| 92 | + except (DatabaseConnectionError, ShovelProcessingError): |
| 93 | + # Re-raise these exceptions to be handled by the base class |
| 94 | + raise |
| 95 | + except Exception as e: |
| 96 | + # Convert unexpected exceptions to ShovelProcessingError |
| 97 | + raise ShovelProcessingError(f"Unexpected error processing block {n}: {str(e)}") |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + AlphaToTaoShovel(name="alpha_to_tao").start() |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + main() |
0 commit comments