Documentation
¶
Index ¶
- func IsAPIError(err error) booldeprecated
- func IsCode(err error, code string) bool
- type APIError
- type Client
- func (c *Client) GetAPIKey() string
- func (c *Client) GetAPISecret() string
- func (c *Client) GetHttpClient() *resty.Client
- func (c *Client) GetLogger() log.Logger
- func (c *Client) GetPassphrase() string
- func (c *Client) GetSignFn() SignFn
- func (c *Client) GetTimeOffsetMs() int64
- func (c *Client) IsDemoTrading() bool
- func (c *Client) Now() time.Time
- func (c *Client) SetTimeOffset(offsetMs int64)
- type Option
- type Options
- func WithAuth(apiKey, apiSecret, passphrase string) Options
- func WithBaseURL(baseURL string) Options
- func WithDemoTrading(enabled bool) Options
- func WithHTTPClient(client *resty.Client) Options
- func WithLogger(logger log.Logger) Options
- func WithNetwork(network okxCommon.Network) Options
- func WithProxy(proxyURL string) Options
- func WithSignFn(signFn SignFn) Options
- func WithTimeOffset(timeOffsetMs int64) Options
- type SignFn
- type WebSocketClient
- func (c *WebSocketClient) GetAPIKey() string
- func (c *WebSocketClient) GetAPISecret() string
- func (c *WebSocketClient) GetBusinessURL() string
- func (c *WebSocketClient) GetDialer() *websocket.Dialer
- func (c *WebSocketClient) GetLogger() log.Logger
- func (c *WebSocketClient) GetPassphrase() string
- func (c *WebSocketClient) GetPrivateURL() string
- func (c *WebSocketClient) GetPublicURL() string
- func (c *WebSocketClient) GetReadTimeout() time.Duration
- func (c *WebSocketClient) GetSignFn() SignFn
- func (c *WebSocketClient) GetWriteTimeout() time.Duration
- func (c *WebSocketClient) IsDemoTrading() bool
- type WebSocketOption
- type WebSocketOptions
- func WithWebSocketAuth(apiKey, apiSecret, passphrase string) WebSocketOptions
- func WithWebSocketBusinessURL(u string) WebSocketOptions
- func WithWebSocketDemoTrading(enabled bool) WebSocketOptions
- func WithWebSocketLogger(logger log.Logger) WebSocketOptions
- func WithWebSocketNetwork(network okxCommon.Network) WebSocketOptions
- func WithWebSocketPrivateURL(u string) WebSocketOptions
- func WithWebSocketProxy(proxyURL string) WebSocketOptions
- func WithWebSocketPublicURL(u string) WebSocketOptions
- func WithWebSocketReadTimeout(d time.Duration) WebSocketOptions
- func WithWebSocketSignFn(signFn SignFn) WebSocketOptions
- func WithWebSocketWriteTimeout(d time.Duration) WebSocketOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsAPIError
deprecated
func IsCode ¶ added in v0.20260623.1
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 ¶
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
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.
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 (*Client) GetAPISecret ¶
func (*Client) GetHttpClient ¶
func (*Client) GetPassphrase ¶
func (*Client) GetTimeOffsetMs ¶
func (*Client) IsDemoTrading ¶
func (*Client) Now ¶
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 ¶
type Options ¶
type Options func(*Option)
func WithAuth ¶
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 ¶
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 ¶
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 ¶
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 WithNetwork ¶
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 ¶
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 ¶
WithSignFn replaces the default HMAC-SHA256 signer. Use it for RSA-signed keys or to delegate signing to an HSM / remote signer.
func WithTimeOffset ¶
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 ¶
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.