Răsfoiți Sursa

fetch_leaderboard加入,但是不行,他们的加密太牛逼了。

skyfffire 1 săptămână în urmă
părinte
comite
1920967702
3 a modificat fișierele cu 178 adăugiri și 1 ștergeri
  1. 2 1
      requirements.txt
  2. 118 0
      src/lighter/fetch_leaderboard.py
  3. 58 0
      src/lighter/leaderboard_results.json

+ 2 - 1
requirements.txt

@@ -1 +1,2 @@
-ijson
+ijson
+requests

+ 118 - 0
src/lighter/fetch_leaderboard.py

@@ -0,0 +1,118 @@
+import argparse
+import json
+import os
+import time
+from typing import Any, Dict, List
+
+import requests
+
+DEFAULT_AUTH = (
+    "1761464345:272589:0:77f74abf549ad0394e88323b73c56d09cdc66f400a4df9fc52a53887471e0cf2caaa77ac7891aa5b6f0ac8f854c3abe06d2853fd7d5bc74d6b6a8e0fef1240f473d97b07cf43300ddedf4e79ec671039"
+)
+BASE_URL = "https://mainnet.zklighter.elliot.ai/api/v1/leaderboard"
+
+
+def load_filtered_accounts(path: str) -> List[Dict[str, Any]]:
+    with open(path, "r", encoding="utf-8") as f:
+        return json.load(f)
+
+
+def extract_addresses(accounts: List[Dict[str, Any]]) -> List[str]:
+    seen = set()
+    addrs: List[str] = []
+    for acc in accounts:
+        addr = acc.get("l1_address")
+        if isinstance(addr, str) and addr and addr not in seen:
+            seen.add(addr)
+            addrs.append(addr)
+    return addrs
+
+
+def read_existing_output(path: str) -> Dict[str, Any]:
+    if os.path.exists(path):
+        try:
+            with open(path, "r", encoding="utf-8") as f:
+                return json.load(f)
+        except Exception:
+            return {}
+    return {}
+
+
+def save_output(path: str, data: Dict[str, Any]) -> None:
+    tmp_path = path + ".tmp"
+    with open(tmp_path, "w", encoding="utf-8") as f:
+        json.dump(data, f, ensure_ascii=False, indent=2)
+    os.replace(tmp_path, path)
+
+
+def try_json(resp: requests.Response) -> Any:
+    try:
+        return resp.json()
+    except ValueError:
+        return {"text": resp.text}
+
+
+def fetch(session: requests.Session, address: str, auth: str, timeout: int = 15) -> Dict[str, Any]:
+    headers = {"authorization": auth}
+    params = {"type": "weekly", "l1_address": address}
+    print(params)
+    print(headers)
+    try:
+        resp = session.get(BASE_URL, headers=headers, params=params, timeout=timeout)
+        return {"status_code": resp.status_code, "body": try_json(resp)}
+    except requests.RequestException as e:
+        return {"error": str(e)}
+
+
+def run(input_path: str, output_path: str, period: float, auth: str) -> None:
+    accounts = load_filtered_accounts(input_path)
+    addrs = extract_addresses(accounts)
+    results = read_existing_output(output_path)
+    session = requests.Session()
+
+    count = 0
+    for addr in addrs:
+        if addr in results:
+            continue
+        data = fetch(session, addr, auth)
+        results[addr] = data
+        save_output(output_path, results)
+        count += 1
+        print(f"Fetched {addr}: {'OK' if 'body' in data else 'ERR'}, {data}")
+        time.sleep(period)
+
+    print(f"Done. Total fetched: {count}. Output: {output_path}")
+
+
+def main() -> None:
+    parser = argparse.ArgumentParser(
+        description="Fetch leaderboard data for l1_addresses from filtered_accounts.json"
+    )
+    parser.add_argument(
+        "--input",
+        default=os.path.join("src", "lighter", "filtered_accounts.json"),
+        help="Input filtered accounts JSON path",
+    )
+    parser.add_argument(
+        "--output",
+        default=os.path.join("src", "lighter", "leaderboard_results.json"),
+        help="Output JSON path",
+    )
+    parser.add_argument(
+        "--period",
+        type=float,
+        default=2.0,
+        help="Seconds between requests (default: 2.0)",
+    )
+    parser.add_argument(
+        "--auth",
+        default=DEFAULT_AUTH,
+        help="Authorization header value",
+    )
+    args = parser.parse_args()
+
+    run(args.input, args.output, args.period, args.auth)
+
+
+if __name__ == "__main__":
+    main()

+ 58 - 0
src/lighter/leaderboard_results.json

@@ -0,0 +1,58 @@
+{
+  "0x3fBBA2b0e07895ae8638D17fd83d72338954D272": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  },
+  "0x621cbC6159D2BbA808F15dF8947D2F42b0fEBdBE": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  },
+  "0x800a656095BC2d884EC08E601C918c73c20690f8": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  },
+  "0x580e0B94A79E2CADEBb30d06ADFb710c08194458": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  },
+  "0x631e44e538D9298e1cebd18C14AE4D6F80f0436b": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  },
+  "0x72cCad3332aD2f100ba8472740Bb4aFF3Bd8d1b4": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  },
+  "0x767DDa2382DEA024304CAEad7f177b8396E671F1": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  },
+  "0x1024582045589710AA9488555ca655333cCd5653": {
+    "status_code": 401,
+    "body": {
+      "code": 20013,
+      "message": "invalid auth: invalid account index"
+    }
+  }
+}