|
| 1 | +// Package client provides a REST client for the Hyperliquid API (via QuickNode builder API). |
| 2 | +// |
| 3 | +// No SDK required -- just net/http + go-ethereum. |
| 4 | +package client |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "crypto/ecdsa" |
| 9 | + "encoding/hex" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | + "strconv" |
| 16 | + "strings" |
| 17 | + |
| 18 | + "github.com/ethereum/go-ethereum/crypto" |
| 19 | +) |
| 20 | + |
| 21 | +const ( |
| 22 | + apiURL = "https://send.hyperliquidapi.com" |
| 23 | + hlInfoURL = "https://api.hyperliquid.xyz/info" |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + privateKey *ecdsa.PrivateKey |
| 28 | + Address string |
| 29 | + httpClient = &http.Client{} |
| 30 | +) |
| 31 | + |
| 32 | +func init() { |
| 33 | + pk := os.Getenv("PRIVATE_KEY") |
| 34 | + if pk == "" { |
| 35 | + fmt.Println("Set PRIVATE_KEY environment variable (hex, with or without 0x)") |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + |
| 39 | + pk = strings.TrimPrefix(pk, "0x") |
| 40 | + key, err := crypto.HexToECDSA(pk) |
| 41 | + if err != nil { |
| 42 | + fmt.Printf("Invalid PRIVATE_KEY: %v\n", err) |
| 43 | + os.Exit(1) |
| 44 | + } |
| 45 | + |
| 46 | + privateKey = key |
| 47 | + Address = crypto.PubkeyToAddress(key.PublicKey).Hex() |
| 48 | + fmt.Printf("Wallet: %s\n", Address) |
| 49 | +} |
| 50 | + |
| 51 | +// Exchange sends a POST to /exchange -- build (no signature) or send (with signature). |
| 52 | +func Exchange(body map[string]interface{}) map[string]interface{} { |
| 53 | + jsonBody, _ := json.Marshal(body) |
| 54 | + resp, err := httpClient.Post(apiURL+"/exchange", "application/json", bytes.NewReader(jsonBody)) |
| 55 | + if err != nil { |
| 56 | + fmt.Printf("HTTP request failed: %v\n", err) |
| 57 | + os.Exit(1) |
| 58 | + } |
| 59 | + defer resp.Body.Close() |
| 60 | + |
| 61 | + raw, _ := io.ReadAll(resp.Body) |
| 62 | + var data map[string]interface{} |
| 63 | + if err := json.Unmarshal(raw, &data); err != nil { |
| 64 | + fmt.Printf("Invalid JSON response: %v\n", err) |
| 65 | + os.Exit(1) |
| 66 | + } |
| 67 | + |
| 68 | + if errVal, ok := data["error"]; ok && errVal != nil { |
| 69 | + fmt.Printf("\nError (%d):\n", resp.StatusCode) |
| 70 | + fmt.Printf(" error: %v\n", data["error"]) |
| 71 | + fmt.Printf(" message: %v\n", data["message"]) |
| 72 | + if guidance, ok := data["guidance"]; ok && guidance != nil { |
| 73 | + fmt.Printf(" guidance: %v\n", guidance) |
| 74 | + } |
| 75 | + os.Exit(1) |
| 76 | + } |
| 77 | + |
| 78 | + return data |
| 79 | +} |
| 80 | + |
| 81 | +// GetApproval checks builder fee approval status via GET /approval?user=<addr>. |
| 82 | +func GetApproval(user string) map[string]interface{} { |
| 83 | + resp, err := httpClient.Get(fmt.Sprintf("%s/approval?user=%s", apiURL, user)) |
| 84 | + if err != nil { |
| 85 | + fmt.Printf("HTTP request failed: %v\n", err) |
| 86 | + os.Exit(1) |
| 87 | + } |
| 88 | + defer resp.Body.Close() |
| 89 | + |
| 90 | + var data map[string]interface{} |
| 91 | + json.NewDecoder(resp.Body).Decode(&data) |
| 92 | + return data |
| 93 | +} |
| 94 | + |
| 95 | +// GetMarkets lists all available markets via GET /markets. |
| 96 | +func GetMarkets() map[string]interface{} { |
| 97 | + resp, err := httpClient.Get(apiURL + "/markets") |
| 98 | + if err != nil { |
| 99 | + fmt.Printf("HTTP request failed: %v\n", err) |
| 100 | + os.Exit(1) |
| 101 | + } |
| 102 | + defer resp.Body.Close() |
| 103 | + |
| 104 | + var data map[string]interface{} |
| 105 | + json.NewDecoder(resp.Body).Decode(&data) |
| 106 | + return data |
| 107 | +} |
| 108 | + |
| 109 | +// PostEndpoint sends a POST to a utility endpoint (e.g. /openOrders, /orderStatus). |
| 110 | +func PostEndpoint(path string, body map[string]interface{}) map[string]interface{} { |
| 111 | + jsonBody, _ := json.Marshal(body) |
| 112 | + resp, err := httpClient.Post(apiURL+path, "application/json", bytes.NewReader(jsonBody)) |
| 113 | + if err != nil { |
| 114 | + fmt.Printf("HTTP request failed: %v\n", err) |
| 115 | + os.Exit(1) |
| 116 | + } |
| 117 | + defer resp.Body.Close() |
| 118 | + |
| 119 | + var data map[string]interface{} |
| 120 | + json.NewDecoder(resp.Body).Decode(&data) |
| 121 | + return data |
| 122 | +} |
| 123 | + |
| 124 | +// SignHash signs a 32-byte hash and returns {r, s, v}. |
| 125 | +func SignHash(hashHex string) map[string]interface{} { |
| 126 | + hashHex = strings.TrimPrefix(hashHex, "0x") |
| 127 | + hashBytes, err := hex.DecodeString(hashHex) |
| 128 | + if err != nil { |
| 129 | + fmt.Printf("Invalid hash hex: %v\n", err) |
| 130 | + os.Exit(1) |
| 131 | + } |
| 132 | + |
| 133 | + sig, err := crypto.Sign(hashBytes, privateKey) |
| 134 | + if err != nil { |
| 135 | + fmt.Printf("Signing failed: %v\n", err) |
| 136 | + os.Exit(1) |
| 137 | + } |
| 138 | + |
| 139 | + // sig is 65 bytes: R (32) || S (32) || V (1) |
| 140 | + r := hex.EncodeToString(sig[:32]) |
| 141 | + s := hex.EncodeToString(sig[32:64]) |
| 142 | + v := int(sig[64]) + 27 |
| 143 | + |
| 144 | + return map[string]interface{}{ |
| 145 | + "r": "0x" + r, |
| 146 | + "s": "0x" + s, |
| 147 | + "v": v, |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// GetMid gets the current mid price for a coin from Hyperliquid. |
| 152 | +func GetMid(coin string) float64 { |
| 153 | + body, _ := json.Marshal(map[string]string{"type": "allMids"}) |
| 154 | + resp, err := httpClient.Post(hlInfoURL, "application/json", bytes.NewReader(body)) |
| 155 | + if err != nil { |
| 156 | + return 0 |
| 157 | + } |
| 158 | + defer resp.Body.Close() |
| 159 | + |
| 160 | + var data map[string]interface{} |
| 161 | + json.NewDecoder(resp.Body).Decode(&data) |
| 162 | + |
| 163 | + if val, ok := data[coin]; ok { |
| 164 | + if s, ok := val.(string); ok { |
| 165 | + f, err := strconv.ParseFloat(s, 64) |
| 166 | + if err == nil { |
| 167 | + return f |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + return 0 |
| 172 | +} |
| 173 | + |
| 174 | +// GetHip3Mid gets the mid price for a HIP-3 market (requires dex parameter). |
| 175 | +func GetHip3Mid(coin string) float64 { |
| 176 | + dex := strings.Split(coin, ":")[0] |
| 177 | + body, _ := json.Marshal(map[string]interface{}{"type": "allMids", "dex": dex}) |
| 178 | + resp, err := httpClient.Post(hlInfoURL, "application/json", bytes.NewReader(body)) |
| 179 | + if err != nil { |
| 180 | + return 0 |
| 181 | + } |
| 182 | + defer resp.Body.Close() |
| 183 | + |
| 184 | + var data map[string]interface{} |
| 185 | + json.NewDecoder(resp.Body).Decode(&data) |
| 186 | + |
| 187 | + if val, ok := data[coin]; ok { |
| 188 | + if s, ok := val.(string); ok { |
| 189 | + f, err := strconv.ParseFloat(s, 64) |
| 190 | + if err == nil { |
| 191 | + return f |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + return 0 |
| 196 | +} |
| 197 | + |
| 198 | +// PrettyJSON marshals a value to indented JSON string. |
| 199 | +func PrettyJSON(v interface{}) string { |
| 200 | + b, err := json.MarshalIndent(v, "", " ") |
| 201 | + if err != nil { |
| 202 | + return fmt.Sprintf("%v", v) |
| 203 | + } |
| 204 | + return string(b) |
| 205 | +} |
0 commit comments