client

package
v0.20260723.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAPIError deprecated

func IsAPIError(err error) bool

IsAPIError reports whether err is an OKX *APIError.

Deprecated: this is a bare type assertion and does not unwrap. Prefer AsAPIError, which walks the error chain.

func IsCode added in v0.20260623.1

func IsCode(err error, code string) bool

IsCode reports whether err carries an OKX APIError with the given code (e.g. "51400" for "order does not exist"). It unwraps via AsAPIError, so it works on wrapped errors — the typical use is collapsing an idempotent failure (cancel of an already-gone order) to a no-op.

Types

type APIError

type APIError struct {
	Code    string `json:"code"`
	Message string `json:"msg"`
}

APIError is the error envelope OKX returns when a request fails. OKX codes are strings; "0" means success and anything else is an error. For batch order endpoints the top-level code can also be "1" (all failed) or "2" (partial), with the real per-order status carried in each data item's sCode/sMsg.

func AsAPIError added in v0.20260623.1

func AsAPIError(err error) (*APIError, bool)

AsAPIError extracts the OKX APIError carried by err, walking the error chain (errors.As). It matches whether the chain holds a *APIError (as the SDK's request layer returns) or a value APIError (as a caller's %w-wrapping may embed), so it is robust to either wrapping style. Returns (nil, false) when err carries no APIError.

func (APIError) Error

func (e APIError) Error() string

Error returns the error code and message.

func (APIError) IsValid

func (e APIError) IsValid() bool

IsValid reports whether e represents an actual API-level error (a non-empty, non-success code).

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the shared, product-agnostic REST core. OKX's v5 API is a single unified account, so every business line (trading account, funding, sub-account, earn, ...) is just a set of request paths layered on top of this same signing + transport machinery; the core carries no product-specific state.

func NewClient

func NewClient(options ...Options) *Client

func (*Client) GetAPIKey

func (c *Client) GetAPIKey() string

func (*Client) GetAPISecret

func (c *Client) GetAPISecret() string

func (*Client) GetHttpClient

func (c *Client) GetHttpClient() *resty.Client

func (*Client) GetLogger

func (c *Client) GetLogger() log.Logger

func (*Client) GetPassphrase

func (c *Client) GetPassphrase() string

func (*Client) GetSignFn

func (c *Client) GetSignFn() SignFn

func (*Client) GetTimeOffsetMs

func (c *Client) GetTimeOffsetMs() int64

func (*Client) IsDemoTrading

func (c *Client) IsDemoTrading() bool

func (*Client) Now

func (c *Client) Now() time.Time

Now returns the current request time adjusted by the configured client/server clock offset. The REST layer formats it as an ISO-8601 millisecond string for the OK-ACCESS-TIMESTAMP header; the WebSocket login uses its Unix epoch seconds. OKX rejects requests whose timestamp drifts more than 30s from its own clock, so the offset (set via SyncServerTime) keeps signed requests valid.

func (*Client) SetTimeOffset

func (c *Client) SetTimeOffset(offsetMs int64)

type Option

type Option struct {
	// contains filtered or unexported fields
}

type Options

type Options func(*Option)

func WithAuth

func WithAuth(apiKey, apiSecret, passphrase string) Options

WithAuth sets the API credentials used to sign private requests. All three values come from the OKX API-management page; passphrase is the one set when the key was created.

func WithBaseURL

func WithBaseURL(baseURL string) Options

WithBaseURL overrides the REST base URL derived from WithNetwork. Use it to point the client at a custom or proxied endpoint (e.g. an OKX regional domain). An empty value is ignored.

func WithDemoTrading

func WithDemoTrading(enabled bool) Options

WithDemoTrading routes requests to OKX's demo (paper) trading environment by attaching the "x-simulated-trading: 1" header. The REST domain is unchanged.

func WithHTTPClient

func WithHTTPClient(client *resty.Client) Options

WithHTTPClient supplies a pre-configured resty client (custom transport, timeouts, TLS, etc.). The JSON (un)marshalers and base URL are still set by the SDK afterwards.

func WithLogger

func WithLogger(logger log.Logger) Options

func WithNetwork

func WithNetwork(network okxCommon.Network) Options

WithNetwork selects the OKX environment. OKX exposes a single production domain, so this currently only accepts common.Mainnet; it exists for forward symmetry with sibling SDKs.

func WithProxy

func WithProxy(proxyURL string) Options

WithProxy routes all REST traffic through the given proxy. Supported schemes: http, https, socks5, socks5h. Pass userinfo in the URL for authenticated proxies. Invalid URLs are logged and skipped.

func WithSignFn

func WithSignFn(signFn SignFn) Options

WithSignFn replaces the default HMAC-SHA256 signer. Use it for RSA-signed keys or to delegate signing to an HSM / remote signer.

func WithTimeOffset

func WithTimeOffset(timeOffsetMs int64) Options

WithTimeOffset sets a fixed client/server clock offset in milliseconds. The request timestamp is computed as localMillis - timeOffsetMs. Usually set automatically via the client's SyncServerTime helper.

type SignFn

type SignFn = func(secret, prehash string) (signature string, err error)

SignFn produces the OK-ACCESS-SIGN value from the prehash string. The default implementation is base64(HMAC-SHA256(secret, prehash)); supply a custom one via WithSignFn to sign with an RSA private key instead (in which case secret carries the PEM-encoded key).

type WebSocketClient

type WebSocketClient struct {
	// contains filtered or unexported fields
}

WebSocketClient holds the configuration shared by every OKX v5 stream. OKX splits its channels across three gateways: public (market data, no login), private (account/orders/positions, login required) and business (candles, algo/grid/copy-trading/earn, login required for the private business channels). Credentials are only needed for the private and business logins.

func NewWebSocketClient

func NewWebSocketClient(options ...WebSocketOptions) *WebSocketClient

func (*WebSocketClient) GetAPIKey

func (c *WebSocketClient) GetAPIKey() string

func (*WebSocketClient) GetAPISecret

func (c *WebSocketClient) GetAPISecret() string

func (*WebSocketClient) GetBusinessURL

func (c *WebSocketClient) GetBusinessURL() string

func (*WebSocketClient) GetDialer

func (c *WebSocketClient) GetDialer() *websocket.Dialer

func (*WebSocketClient) GetLogger

func (c *WebSocketClient) GetLogger() log.Logger

func (*WebSocketClient) GetPassphrase

func (c *WebSocketClient) GetPassphrase() string

func (*WebSocketClient) GetPrivateURL

func (c *WebSocketClient) GetPrivateURL() string

func (*WebSocketClient) GetPublicURL

func (c *WebSocketClient) GetPublicURL() string

func (*WebSocketClient) GetReadTimeout added in v0.20260623.1

func (c *WebSocketClient) GetReadTimeout() time.Duration

GetReadTimeout is the max idle time a steady-state stream read may block with no inbound frame before the connection is considered dead. Zero means no read deadline is applied.

func (*WebSocketClient) GetSignFn

func (c *WebSocketClient) GetSignFn() SignFn

func (*WebSocketClient) GetWriteTimeout added in v0.20260623.1

func (c *WebSocketClient) GetWriteTimeout() time.Duration

GetWriteTimeout bounds a single WS write (subscribe op, keepalive ping). Zero means no write deadline is applied.

func (*WebSocketClient) IsDemoTrading

func (c *WebSocketClient) IsDemoTrading() bool

type WebSocketOption

type WebSocketOption struct {
	// contains filtered or unexported fields
}

type WebSocketOptions

type WebSocketOptions func(*WebSocketOption)

func WithWebSocketAuth

func WithWebSocketAuth(apiKey, apiSecret, passphrase string) WebSocketOptions

WithWebSocketAuth sets the credentials used to log in to the private and business streams.

func WithWebSocketBusinessURL

func WithWebSocketBusinessURL(u string) WebSocketOptions

WithWebSocketBusinessURL overrides the business stream URL. Empty is ignored.

func WithWebSocketDemoTrading

func WithWebSocketDemoTrading(enabled bool) WebSocketOptions

WithWebSocketDemoTrading points the streams at OKX's demo (paper) trading gateways (wspap.okx.com).

func WithWebSocketLogger

func WithWebSocketLogger(logger log.Logger) WebSocketOptions

func WithWebSocketNetwork

func WithWebSocketNetwork(network okxCommon.Network) WebSocketOptions

func WithWebSocketPrivateURL

func WithWebSocketPrivateURL(u string) WebSocketOptions

WithWebSocketPrivateURL overrides the private stream URL. Empty is ignored.

func WithWebSocketProxy

func WithWebSocketProxy(proxyURL string) WebSocketOptions

WithWebSocketProxy routes the stream dial through the given proxy (http, https, socks5, socks5h). Invalid URLs are logged and skipped.

func WithWebSocketPublicURL

func WithWebSocketPublicURL(u string) WebSocketOptions

WithWebSocketPublicURL overrides the public stream URL. Empty is ignored.

func WithWebSocketReadTimeout added in v0.20260623.1

func WithWebSocketReadTimeout(d time.Duration) WebSocketOptions

WithWebSocketReadTimeout overrides the steady-state read idle deadline (the max time a read may block with no inbound frame before the connection is treated as dead). A non-positive value disables the read deadline.

func WithWebSocketSignFn

func WithWebSocketSignFn(signFn SignFn) WebSocketOptions

WithWebSocketSignFn overrides the default HMAC login signer.

func WithWebSocketWriteTimeout added in v0.20260623.1

func WithWebSocketWriteTimeout(d time.Duration) WebSocketOptions

WithWebSocketWriteTimeout overrides the per-write deadline applied to the subscribe op and keepalive pings. A non-positive value disables it.

Jump to

Keyboard shortcuts

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