Documentation
¶
Overview ¶
Package trading defines order intents and validates server-built Hyperliquid actions against them before anything is signed.
Index ¶
- Constants
- func MarginUnits(amount string) (int64, error)
- func ValidIntentID(id string) bool
- func ValidateAction(in *Intent, actionJSON string) (string, error)
- func ValidateLeverage(leverage, maxLeverage int, isCross, onlyIsolated bool) error
- func WorstPrice(reference string, isBuy bool, bps int, maxDecimals int) (string, error)
- type Intent
- type Mismatch
- type Outcome
Constants ¶
const ( KindOrder = "order" KindCancel = "cancel" // KindLeverage changes a market's leverage and cross/isolated mode // (Hyperliquid updateLeverage). It opens no position by itself. KindLeverage = "leverage" // KindMargin adds (positive) or removes (negative) isolated margin on an // existing isolated position (Hyperliquid updateIsolatedMargin). KindMargin = "margin" )
Kinds of intents.
const MarginDecimals = 6
MarginDecimals is the fixed-point scale of updateIsolatedMargin ntli.
Variables ¶
This section is empty.
Functions ¶
func MarginUnits ¶
MarginUnits converts a canonical signed decimal USDC amount to exact 6-decimal integer units. It refuses zero, non-canonical input, more than six decimals (no rounding) and values outside the JS-safe integer range.
func ValidIntentID ¶
ValidIntentID reports whether id is an acceptable intent identifier.
func ValidateAction ¶
ValidateAction checks a server-built actionJson against the intent. Only the order, cancel, updateLeverage and updateIsolatedMargin families are accepted, each only for the matching intent kind; anything else (transfers, approvals, vault/subaccount routing) is rejected. It returns the client order id when present.
func ValidateLeverage ¶
ValidateLeverage checks a leverage request against market metadata before anything is sent: integer in [1, maxLeverage], and cross mode is refused for isolated-only markets.
func WorstPrice ¶
WorstPrice returns reference*(1±bps/10000) rounded toward the reference to a valid Hyperliquid perp price: at most maxDecimals decimals and at most 5 significant figures unless integral. It is the exact IOC limit price submitted for a market order, so it must be exchange-valid.
Types ¶
type Intent ¶
type Intent struct {
Version int `json:"version"`
ID string `json:"intent_id"`
Kind string `json:"kind"`
Network string `json:"network"`
Account string `json:"account"`
APIURL string `json:"api_url"`
Market string `json:"market"`
AssetID int `json:"asset_id"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
// Order fields.
Side string `json:"side,omitempty"` // buy | sell
OrderType string `json:"order_type,omitempty"` // limit | market
Size string `json:"size,omitempty"`
Price string `json:"price,omitempty"` // limit price
TIF string `json:"tif,omitempty"` // GTC | IOC | ALO
ReduceOnly bool `json:"reduce_only,omitempty"`
SlippageBps int `json:"slippage_bps,omitempty"`
// ReferencePx and WorstPx bound market orders. A market order is sent as
// an IOC limit at exactly WorstPx, so the signed price is the approved
// bound and a later market move cannot invalidate it.
ReferencePx string `json:"reference_px,omitempty"`
WorstPx string `json:"worst_px,omitempty"`
// Builder fee policy observed at prepare time.
BuilderAddress string `json:"builder_address,omitempty"`
MaxBuilderFee10 int `json:"max_builder_fee_tenths_bps"`
// Cancel fields.
OrderID int64 `json:"order_id,omitempty"`
// Leverage fields.
Leverage int `json:"leverage,omitempty"`
IsCross bool `json:"is_cross,omitempty"`
// Margin fields: canonical signed USDC decimal, at most 6 decimals.
MarginAmount string `json:"margin_amount,omitempty"`
}
Intent is the exact economic request the user/agent approved. Its digest binds every field; any change requires a new intent.
type Mismatch ¶
type Mismatch struct{ Reason string }
Mismatch is returned when the server action does not match the intent.
type Outcome ¶
type Outcome string
Outcome is the CLI's classification of a backend action status.
func RiskOutcome ¶
RiskOutcome classifies a ConfirmAction status or GetAction state for leverage/margin actions. Only "completed" is success. Risk actions never carry an OID, so an OID is never evidence.
Backend "error" can be a build/validation/workflow failure rather than a venue rejection, so it clears the unknown state only when sentToVenue is false (ConfirmAction was never submitted). Once a signed action may have been submitted, "error" stays unknown until reconciliation yields venue proof. "unresolved" is a terminal workflow state, not exchange evidence, and stays unknown like every other value.