fix(hyperliquid): stop SDK init panic from crashing trader creation with 500

go-hyperliquid's NewExchange auto-fetches meta/spotMeta/perpDexs and
panics if any of those API calls fail (NewInfo: panic(err)). The quick
trade flow constructs a probe trader inside POST /api/traders, so any
transient Hyperliquid API hiccup crashed the request with a recovered
panic and a bare 500. Wrap the constructor in initExchangeClient, which
converts the panic into an error that surfaces through the existing
exchange-probe validation path as an honest, retryable message.
This commit is contained in:
tinkle-community
2026-06-11 21:33:55 +08:00
parent bebe51bf89
commit 133ef51de8
3 changed files with 76 additions and 10 deletions

View File

@@ -143,16 +143,23 @@ func NewHyperliquidTrader(privateKeyHex string, walletAddr string, testnet bool,
// v0.36 fixed the spot-meta indexing panic that crashed earlier versions
// when Hyperliquid added new spot tokens whose Tokens[0] index pointed
// past the Tokens array end.
exchange := hyperliquid.NewExchange(
ctx,
privateKey,
apiURL,
nil, // Meta — fetched automatically
"", // vault address (empty for personal account)
walletAddr, // wallet address
nil, // SpotMeta — fetched automatically
nil, // perpDexs — fetched automatically
)
// The constructor still panics if any auto-fetch fails, so it runs inside
// initExchangeClient which converts the panic into a returned error.
exchange, err := initExchangeClient(func() *hyperliquid.Exchange {
return hyperliquid.NewExchange(
ctx,
privateKey,
apiURL,
nil, // Meta — fetched automatically
"", // vault address (empty for personal account)
walletAddr, // wallet address
nil, // SpotMeta — fetched automatically
nil, // perpDexs — fetched automatically
)
})
if err != nil {
return nil, fmt.Errorf("Hyperliquid API is temporarily unavailable, please retry: %w", err)
}
logger.Infof("✓ Hyperliquid trader initialized successfully (testnet=%v, wallet=%s)", testnet, walletAddr)