Skip to content

Commit abe6cfe

Browse files
authored
Merge pull request #12 from opentensor/testnet
Testnet shovels
2 parents 5deaed6 + 81a1c07 commit abe6cfe

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

docker-compose.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ services:
221221
- 'host.docker.internal:host-gateway'
222222
tty: true
223223

224+
shovel_alpha_to_tao:
225+
build:
226+
context: ./scraper_service
227+
dockerfile: ./shovel_alpha_to_tao/Dockerfile
228+
container_name: shovel_alpha_to_tao
229+
depends_on:
230+
clickhouse:
231+
condition: service_started
232+
env_file:
233+
- .env
234+
logging:
235+
driver: 'json-file'
236+
options:
237+
max-size: '10m'
238+
max-file: '3'
239+
networks:
240+
- app_network
241+
restart: on-failure
242+
extra_hosts:
243+
- 'host.docker.internal:host-gateway'
244+
tty: true
245+
224246
# shovel_balance_map:
225247
# build:
226248
# context: ./scraper_service
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM python:3.12-slim
2+
3+
WORKDIR /app
4+
5+
COPY ./requirements.txt /app/requirements.txt
6+
RUN pip install --no-cache-dir -r /app/requirements.txt
7+
8+
COPY ./shared /app/shared
9+
COPY ./shovel_alpha_to_tao /app/shovel_alpha_to_tao
10+
11+
ENV PYTHONPATH="/app:/app/shared"
12+
13+
CMD ["python", "-u", "shovel_alpha_to_tao/main.py"]

scraper_service/shovel_alpha_to_tao/__init__.py

Whitespace-only changes.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)