Help us shape the future of Go! Take the 2026 Go Developer Survey

trading

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package trading defines order intents and validates server-built Hyperliquid actions against them before anything is signed.

Index

Constants

View Source
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.

View Source
const MarginDecimals = 6

MarginDecimals is the fixed-point scale of updateIsolatedMargin ntli.

Variables

This section is empty.

Functions

func MarginUnits

func MarginUnits(amount string) (int64, error)

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

func ValidIntentID(id string) bool

ValidIntentID reports whether id is an acceptable intent identifier.

func ValidateAction

func ValidateAction(in *Intent, actionJSON string) (string, error)

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

func ValidateLeverage(leverage, maxLeverage int, isCross, onlyIsolated bool) error

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

func WorstPrice(reference string, isBuy bool, bps int, maxDecimals int) (string, error)

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.

func (*Intent) Digest

func (in *Intent) Digest() string

Digest is sha256 over the canonical JSON encoding of the intent.

func (*Intent) Expired

func (in *Intent) Expired(now time.Time) bool

Expired reports whether the intent is past its expiry.

func (*Intent) IsBuy

func (in *Intent) IsBuy() bool

IsBuy reports order direction.

type Mismatch

type Mismatch struct{ Reason string }

Mismatch is returned when the server action does not match the intent.

func (*Mismatch) Error

func (m *Mismatch) Error() string

type Outcome

type Outcome string

Outcome is the CLI's classification of a backend action status.

const (
	OutcomeSuccess  Outcome = "success"  // authoritative completion
	OutcomeRejected Outcome = "rejected" // failed without reaching the venue
	OutcomeUnknown  Outcome = "unknown"  // may have executed; never retry, reconcile, gate conflicting exposure
)

func RiskOutcome

func RiskOutcome(status string, sentToVenue bool) Outcome

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL