File size: 2,335 Bytes
714cdab
 
 
 
 
 
 
0587701
714cdab
 
 
 
 
 
fd8c150
 
 
714cdab
fd8c150
714cdab
 
 
fd8c150
714cdab
0587701
fd8c150
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714cdab
 
 
 
 
 
 
 
 
 
 
 
 
 
0587701
 
714cdab
 
0587701
 
 
 
714cdab
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import requests
import time
from typing import Any
from communex.client import CommuneClient
from communex.balance import from_nano

netuid = 1
node_url = "wss://commune.api.onfinality.io/public-ws"

def get_com_price() -> Any:
    retries = 5
    for i in range(0, retries):
        try:
            price = float(requests.get("https://api.mexc.com/api/v3/avgPrice?symbol=COMAIUSDT").json()["price"])
            print(f"Fetched COM price: {price}")
            return price
        except Exception as e:
            print(f"Error fetching COM price: {e}")
            if i == retries - 1:
                raise
            time.sleep(retries)
    raise RuntimeError("Failed to fetch COM price")

def get_leaderboard_data():
    try:
        com_price = get_com_price()
        blocks_in_day = 10_800
        client = CommuneClient(node_url)
        #emission = client.query("Emission", params=[netuid])
        emission = [99999999, 99999, 99999999999999999, 11122]
        scores = {}
        for uid, emi in enumerate(emission):
            scores[uid] = ((emi / 10**11) * blocks_in_day) * com_price
        
        sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
        leaderboard_data = []
        for rank, (uid, score) in enumerate(sorted_scores, start=1):
            leaderboard_data.append({"rank": rank, "uid": uid, "score": score})
        
        print(f"Leaderboard data: {leaderboard_data}")
        return leaderboard_data
    except Exception as e:
        print(f"Error fetching leaderboard data: {e}")
        return []

with gr.Blocks() as demo:
    gr.Markdown("# Commune Leaderboard")
    
    leaderboard_table = gr.components.DataFrame(
        headers=["rank", "uid", "score"],
        datatype=["number", "number", "number"],
        interactive=False,
        visible=True
    )
    
    refresh_button = gr.Button("Refresh Leaderboard")
    
    def refresh_leaderboard():
        leaderboard_data = get_leaderboard_data()
        return gr.components.DataFrame.update(value=leaderboard_data)
    
    refresh_button.click(fn=refresh_leaderboard, outputs=leaderboard_table)
    
    # Initial load of leaderboard data
    leaderboard_data = get_leaderboard_data()
    leaderboard_table.value = leaderboard_data

if __name__ == "__main__":
    demo.launch()