1- """Thin RPC client for Hyperliquid API builder API."""
1+ """REST client for Hyperliquid API (via QuickNode builder API).
2+
3+ No SDK required -- just requests + eth_account.
4+ """
25
36import json
47import os
710import requests
811from eth_account import Account
912
10- ENDPOINT = "https://send.hyperliquidapi.com"
13+ API_URL = "https://send.hyperliquidapi.com"
14+ HL_INFO_URL = "https://api.hyperliquid.xyz/info"
1115
1216_pk = os .environ .get ("PRIVATE_KEY" )
1317if not _pk :
1418 print ("Set PRIVATE_KEY environment variable (hex, with or without 0x)" )
1519 sys .exit (1 )
1620
1721wallet = Account .from_key (_pk )
18- print (f"Wallet: { wallet .address } " )
19-
20- _req_id = 0
22+ address = wallet .address
23+ print (f"Wallet: { address } " )
2124
2225
23- def rpc (method , params = None ):
24- global _req_id
25- _req_id += 1
26- r = requests .post (ENDPOINT , json = {
27- "jsonrpc" : "2.0" ,
28- "method" : method ,
29- "params" : params or {},
30- "id" : _req_id ,
31- })
26+ def exchange (body ):
27+ """POST /exchange -- build (no signature) or send (with signature)."""
28+ r = requests .post (f"{ API_URL } /exchange" , json = body )
3229 data = r .json ()
3330 if data .get ("error" ):
34- err = data ["error" ]
35- print (f"\n RPC error ({ method } ):" )
36- print (f" code: { err .get ('code' )} " )
37- print (f" message: { err .get ('message' )} " )
38- guidance = err .get ("data" , {}).get ("guidance" )
31+ print (f"\n Error ({ r .status_code } ):" )
32+ print (f" error: { data .get ('error' )} " )
33+ print (f" message: { data .get ('message' )} " )
34+ guidance = data .get ("guidance" )
3935 if guidance :
4036 print (f" guidance: { guidance } " )
4137 sys .exit (1 )
4238 return data
4339
4440
41+ def get_approval (user ):
42+ """GET /approval?user=<addr> -- check builder fee approval status."""
43+ r = requests .get (f"{ API_URL } /approval" , params = {"user" : user })
44+ return r .json ()
45+
46+
47+ def get_markets ():
48+ """GET /markets -- list all available markets."""
49+ r = requests .get (f"{ API_URL } /markets" )
50+ return r .json ()
51+
52+
53+ def post_endpoint (path , body ):
54+ """POST to a utility endpoint (e.g. /openOrders, /orderStatus, /preflight)."""
55+ r = requests .post (f"{ API_URL } { path } " , json = body )
56+ return r .json ()
57+
58+
4559def sign_hash (hash_hex ):
4660 h = bytes .fromhex (hash_hex .removeprefix ("0x" ))
4761 s = wallet .unsafe_sign_hash (h )
@@ -50,5 +64,12 @@ def sign_hash(hash_hex):
5064
5165def get_mid (coin ):
5266 """Get the current mid price for a coin from Hyperliquid."""
53- r = requests .post ("https://api.hyperliquid.xyz/info" , json = {"type" : "allMids" })
67+ r = requests .post (HL_INFO_URL , json = {"type" : "allMids" })
68+ return float (r .json ().get (coin , 0 ))
69+
70+
71+ def get_hip3_mid (coin ):
72+ """Get mid price for a HIP-3 market (requires dex parameter)."""
73+ dex = coin .split (":" )[0 ]
74+ r = requests .post (HL_INFO_URL , json = {"type" : "allMids" , "dex" : dex })
5475 return float (r .json ().get (coin , 0 ))
0 commit comments