0xMagnus commited on
Commit
a9046c3
·
verified ·
1 Parent(s): 77d6138

fix: adjustments to HYPE index ID and information retrieval cleanup

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -35,26 +35,37 @@ def get_current_time_in_timezone(timezone: str) -> str:
35
 
36
  @tool
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
- # Request payload for current prices
44
  payload = {
45
- "type": "allMids"
46
  }
47
 
48
  response = requests.post(url, headers=headers, json=payload)
49
 
50
  if response.status_code == 200:
51
  data = response.json()
52
- # HYPE is at index 150, so we look for "@150" in the response
53
- if "@150" in data:
54
- price = data["@150"]
55
- return f"Current HYPE/USDC price on Hyperliquid is: {price} USDC"
 
 
 
 
 
 
 
 
 
56
 
57
- return "Error: HYPE price not found in current mids"
58
  else:
59
  return f"Error: API request failed with status code {response.status_code}"
60
 
 
35
 
36
  @tool
37
  def get_hype_price() -> str:
38
+ """A tool that fetches the current HYPE/USDC price from Hyperliquid."""
39
  try:
40
  url = "https://api.hyperliquid.xyz/info"
41
+ headers = {
42
+ "Content-Type": "application/json"
43
+ }
44
 
45
+ # Request payload for spot market data
46
  payload = {
47
+ "type": "spotMetaAndAssetCtxs"
48
  }
49
 
50
  response = requests.post(url, headers=headers, json=payload)
51
 
52
  if response.status_code == 200:
53
  data = response.json()
54
+ market_data = data[1] # Market data is in the second array element
55
+
56
+ for market in market_data:
57
+ if market.get("coin") == "@150": # HYPE's index
58
+ mark_price = float(market["markPx"])
59
+ mid_price = float(market["midPx"])
60
+ volume = float(market["dayNtlVlm"])
61
+
62
+ # Format with 2 decimal places as per szDecimals
63
+ return f"HYPE/USDC Price:\n" \
64
+ f"Mark Price: {mark_price:.2f} USDC\n" \
65
+ f"Mid Price: {mid_price:.2f} USDC\n" \
66
+ f"24h Volume: {volume:.2f} USDC"
67
 
68
+ return "Error: HYPE market not found in response"
69
  else:
70
  return f"Error: API request failed with status code {response.status_code}"
71