pawkanarek commited on
Commit
81a9ded
·
1 Parent(s): c55570e

use bittensor python api

Browse files
Files changed (1) hide show
  1. app.py +37 -39
app.py CHANGED
@@ -9,48 +9,45 @@ g_cached_data: pd.DataFrame | None = None
9
  g_last_fetch_time = 0.0
10
 
11
 
12
- def get_incentive_percent_for_key(key, sn, incentive, substrate) -> tuple[float, int]:
13
- uid = substrate.query('SubtensorModule', 'Uids', [sn, key])
14
- if uid and uid.value is not None:
15
- owner_incentive = int(incentive[uid.value])
16
- if owner_incentive > 0:
17
- return (owner_incentive / 65535) * 100, uid.value
18
- return -1, -1
19
-
20
-
21
  def fetch_incentive_data() -> pd.DataFrame:
22
- print("Starting data fetch...")
23
  data = []
24
  subtensor = bt.subtensor(network="finney")
25
- substrate = subtensor.substrate
26
-
27
- for sn in range(128):
28
- incentive = substrate.query("SubtensorModule", "Incentive", [sn])
29
- owner_ck = substrate.query('SubtensorModule', 'SubnetOwner', [sn])
30
- assert incentive, "WTF"
31
- assert owner_ck, "WTF"
32
- sn_link = f"[{sn}](https://taostats.io/subnets/{sn})"
 
 
 
 
 
 
33
 
34
- ck_incentive, ck_uid = get_incentive_percent_for_key(owner_ck, sn, incentive, substrate)
35
- if ck_incentive > 0:
36
- print(f"Found incentive for SN {sn} owner coldkey.")
37
- address_link = f"[{owner_ck}](https://taostats.io/coldkey/{owner_ck})"
38
- data.append([sn_link, f"{ck_incentive:.2f}%", ck_uid, "Coldkey", address_link])
39
- continue
40
 
41
- owner_hks = substrate.query("SubtensorModule", "OwnedHotkeys", [owner_ck])
42
- if owner_hks and owner_hks.value:
43
- for byte_array in owner_hks.value:
44
- owner_hk = Keypair(public_key=bytes(byte_array[0]), ss58_format=42).ss58_address
45
- hk_incentive, hk_uid = get_incentive_percent_for_key(owner_hk, sn, incentive, substrate)
46
- if hk_incentive > 0:
47
- print(f"Found incentive for SN {sn} owner hotkey.")
48
- address_link = f"[{owner_hk}](https://taostats.io/hotkey/{owner_hk})"
49
- data.append([sn_link, f"{hk_incentive:.2f}%", hk_uid, "Hotkey", address_link])
50
- break
 
 
51
 
52
  data = [(i+1, *d) for i, d in enumerate(data)]
53
  df = pd.DataFrame(data, columns=["#", "Subnet", "Burn", "UID", "Key", "Address"]) # type: ignore
 
54
  return df
55
 
56
 
@@ -59,7 +56,9 @@ def get_cached_data() -> pd.DataFrame:
59
  if g_cached_data is None or (time.time() - g_last_fetch_time) > 1200: # 20 min
60
  g_last_fetch_time = time.time()
61
  g_cached_data = fetch_incentive_data()
62
- return g_cached_data
 
 
63
 
64
 
65
  with gr.Blocks(title="Bittensor Subnet Incentives") as demo:
@@ -69,13 +68,12 @@ with gr.Blocks(title="Bittensor Subnet Incentives") as demo:
69
  This dashboard displays the burn percentage set by subnet owners for miners.
70
  """
71
  )
72
-
73
  output_df = gr.DataFrame(
74
  datatype=["number", "markdown", "str", "number", "str", "markdown"],
75
  label="Subnet Burn Data",
76
  interactive=False,
77
- row_count=(60, "dynamic"),
78
- col_count=(6, "fixed")
79
  )
80
- demo.load(get_cached_data, None, output_df)
81
  demo.launch()
 
9
  g_last_fetch_time = 0.0
10
 
11
 
 
 
 
 
 
 
 
 
 
12
  def fetch_incentive_data() -> pd.DataFrame:
 
13
  data = []
14
  subtensor = bt.subtensor(network="finney")
15
+ print("connected to subtensor")
16
+ subnets = subtensor.all_subnets()
17
+ print("fetched all subnets")
18
+ metagraphs = subtensor.get_all_metagraphs_info()
19
+ print("fetched all metagraphs")
20
+
21
+ assert subnets, "WTF"
22
+ assert metagraphs, "WTF"
23
+
24
+ for sn in range(1, 129):
25
+ subnet = subnets[sn]
26
+ metagraph = metagraphs[sn]
27
+ address_to_uid = {hk: i for i, hk in enumerate(metagraph.hotkeys)}
28
+ addresses = [("coldkey", subnet.owner_coldkey), ("hotkey", subnet.owner_hotkey)]
29
 
30
+ for key_type, address in addresses:
31
+ uid = address_to_uid.get(address, None)
32
+ if uid is None:
33
+ continue
 
 
34
 
35
+ incentive = metagraph.incentives[uid]
36
+ if incentive <= 0:
37
+ continue
38
+
39
+ data.append([
40
+ f"[{sn}](https://taostats.io/subnets/{sn})",
41
+ f"{incentive*100:.2f}%",
42
+ uid,
43
+ key_type,
44
+ f"[{address}](https://taostats.io/{key_type}/{address})"
45
+ ])
46
+ break
47
 
48
  data = [(i+1, *d) for i, d in enumerate(data)]
49
  df = pd.DataFrame(data, columns=["#", "Subnet", "Burn", "UID", "Key", "Address"]) # type: ignore
50
+ print(f"{len(data)} subnets burn")
51
  return df
52
 
53
 
 
56
  if g_cached_data is None or (time.time() - g_last_fetch_time) > 1200: # 20 min
57
  g_last_fetch_time = time.time()
58
  g_cached_data = fetch_incentive_data()
59
+
60
+ time_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(g_last_fetch_time + 1200))
61
+ return time_str, g_cached_data
62
 
63
 
64
  with gr.Blocks(title="Bittensor Subnet Incentives") as demo:
 
68
  This dashboard displays the burn percentage set by subnet owners for miners.
69
  """
70
  )
71
+ next_process_text = gr.Textbox(label="Next refresh time", interactive=False)
72
  output_df = gr.DataFrame(
73
  datatype=["number", "markdown", "str", "number", "str", "markdown"],
74
  label="Subnet Burn Data",
75
  interactive=False,
76
+ max_height=1000000
 
77
  )
78
+ demo.load(get_cached_data, None, [next_process_text, output_df])
79
  demo.launch()