0xMagnus commited on
Commit
a796e10
·
verified ·
1 Parent(s): 21e3f4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -37,34 +37,39 @@ def get_current_time_in_timezone(timezone: str) -> str:
37
  def get_hype_price() -> str:
38
  """A tool that fetches the current HYPE/USDC price from Hyperliquid Spot market."""
39
  try:
40
- # API endpoint
41
  url = "https://api.hyperliquid.xyz/info"
 
42
 
43
- # Request headers
44
- headers = {
45
- "Content-Type": "application/json"
46
- }
47
 
48
- # Request payload for spot market data
49
- payload = {
50
- "type": "spotMetaAndAssetCtxs"
51
- }
52
-
53
- # Make the POST request
54
- response = requests.post(url, headers=headers, json=payload)
55
-
56
- if response.status_code == 200:
57
- data = response.json()
58
 
59
- # Extract HYPE price from the response
60
- for market in data[1]:
61
- if market["coin"] == "HYPE":
62
- price = float(market["markPx"])
63
- return f"Current HYPE/USDC price on Hyperliquid is: {price} USD"
64
 
65
- return "Error: HYPE market not found on Hyperliquid"
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  else:
67
- return f"Error: API request failed with status code {response.status_code}"
68
 
69
  except Exception as e:
70
  return f"Error fetching Hyperliquid price: {str(e)}"
 
37
  def get_hype_price() -> str:
38
  """A tool that fetches the current HYPE/USDC price from Hyperliquid Spot market."""
39
  try:
 
40
  url = "https://api.hyperliquid.xyz/info"
41
+ headers = {"Content-Type": "application/json"}
42
 
43
+ # First, get spot meta to find HYPE's index
44
+ meta_payload = {"type": "spotMeta"}
45
+ meta_response = requests.post(url, headers=headers, json=meta_payload)
 
46
 
47
+ if meta_response.status_code == 200:
48
+ meta_data = meta_response.json()
49
+ hype_index = None
 
 
 
 
 
 
 
50
 
51
+ # Find HYPE's index in the universe
52
+ for token in meta_data["universe"]:
53
+ if token["name"] == "HYPE":
54
+ hype_index = token["index"]
55
+ break
56
 
57
+ if hype_index is not None:
58
+ # Now get all mids
59
+ mids_payload = {"type": "allMids"}
60
+ mids_response = requests.post(url, headers=headers, json=mids_payload)
61
+
62
+ if mids_response.status_code == 200:
63
+ mids_data = mids_response.json()
64
+ token_key = f"@{hype_index}"
65
+
66
+ if token_key in mids_data:
67
+ price = mids_data[token_key]
68
+ return f"Current HYPE/USDC price on Hyperliquid is: {price} USDC"
69
+
70
+ return "Error: HYPE market not found in current mids"
71
  else:
72
+ return f"Error: API request failed with status code {meta_response.status_code}"
73
 
74
  except Exception as e:
75
  return f"Error fetching Hyperliquid price: {str(e)}"