ws

package
v0.0.0-...-e86409c Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2024 License: MIT Imports: 15 Imported by: 0

README

ws

okx api v5 client using websocket protocol

Documentation

Index

Constants

View Source
const (
	PublicChannelType   = "public"
	PrivateChannelType  = "private"
	BusinessChannelType = "business"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Channel

type Channel interface {
	// get channel name
	GetChannel() (string, error)
}

Channel channel for subscribing

type ChannelRawMessage

type ChannelRawMessage struct {
	json.RawMessage
}

ChannelRawMessage

func (ChannelRawMessage) GetChannel

func (m ChannelRawMessage) GetChannel() (string, error)

GetChannel get channel name

type ChannelType

type ChannelType string

ChannelType

type Client

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

Client okx api v5 client using websocket protocol

func NewClient

func NewClient(url string, fns ...OptFn) (*Client, error)

NewClient create okx api v5 client using websocket protocol

func (*Client) Business

func (c *Client) Business() *Client

func (*Client) Clone

func (c *Client) Clone() *Client

func (*Client) Close

func (c *Client) Close() (err error)

Close close related resource

func (*Client) Private

func (c *Client) Private() *Client

func (*Client) Public

func (c *Client) Public() *Client

func (*Client) Run

func (c *Client) Run(ctx context.Context) (err error)

Run run websocket

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, channels ...Channel) error

Subscribe subscribe channels

func (*Client) Unsubscribe

func (c *Client) Unsubscribe(ctx context.Context, channels ...Channel) error

Unsubscribe

https://www.okx.com/docs-v5/en/#overview-websocket-unsubscribe

type MessageHandler

type MessageHandler interface {
	// HandleUnknownMessage
	HandleUnknownMessage(ctx context.Context, msg []byte)
	// HandlePushData
	HandlePushData(context.Context, PushData[ChannelRawMessage, json.RawMessage])
	// HandleResponse
	HandleResponse(context.Context, Response[ChannelRawMessage])
}

MessageHandler message handler

type NoopMessageHandler

type NoopMessageHandler struct{}

NoopMessageHandler implement MessageHandler but do nonthing

func (NoopMessageHandler) HandlePushData

func (NoopMessageHandler) HandleResponse

func (h NoopMessageHandler) HandleResponse(ctx context.Context, resp Response[ChannelRawMessage])

func (NoopMessageHandler) HandleUnknownMessage

func (h NoopMessageHandler) HandleUnknownMessage(ctx context.Context, msg []byte)

type OptFn

type OptFn func(*options)

OptFn client functional options

func WithMessageHandler

func WithMessageHandler(handler MessageHandler) OptFn

WithMessageHandler set message's handler

type PushData

type PushData[Arg Channel, Data any] struct {
	// Successfully subscribed channel
	Arg Arg `json:"arg,omitempty"`
	// Subscribed data
	Data []Data `json:"data,omitempty"`
	// contains filtered or unexported fields
}

PushData Push data parameters

func MapPushData

func MapPushData[Arg Channel, Data any](input PushData[ChannelRawMessage, json.RawMessage]) (output PushData[Arg, Data], err error)

MapPushData convert input to output

func (PushData[A, D]) GetRawMessage

func (d PushData[A, D]) GetRawMessage() []byte

GetRawMessage get raw websocket message

type Request

type Request[T any] struct {
	// Unique identifier of the messageProvided by client.
	// It will be returned in response message for identifying the corresponding request.
	// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters.
	Id string `json:"id,omitempty"`
	// Operation
	Op string `json:"op,omitempty"`
	// Request Parameters
	Args []T `json:"args,omitempty"`
}

Request

type Response

type Response[T Channel] struct {
	// Operation
	// login
	// subscribe
	// unsubscribe
	// error
	Event string `json:"event"`

	Arg T `json:"arg"`
	// Error code
	Code string `json:"code"`
	// Error message
	Msg string `json:"msg"`
	// WebSocket connection ID
	ConnId string `json:"connId"`
	// contains filtered or unexported fields
}

Response Operation result

func MapResponse

func MapResponse[T Channel](input Response[ChannelRawMessage]) (output Response[T], err error)

MapResponse map input to output

func (Response[T]) GetError

func (r Response[T]) GetError() error

func (Response[T]) GetRawMessage

func (r Response[T]) GetRawMessage() []byte

GetRawMessage get raw websocket message

type TickersChannel

type TickersChannel struct {
	// Channel name
	Channel string `json:"channel,omitempty"`
	// Instrument ID
	InstId string `json:"instId"`
}

TickersChannel

https://www.okx.com/docs-v5/en/#order-book-trading-market-data-ws-tickers-channel

func (TickersChannel) GetChannel

func (t TickersChannel) GetChannel() (string, error)

GetChannel implement Channel interface

type TickersData

type TickersData struct {
	// Instrument type
	InstType string `json:"instType"`
	// Instrument ID
	InstID string `json:"instId"`
	// Last traded price
	Last string `json:"last"`
	// Last traded size
	LastSz string `json:"lastSz"`
	// Best ask price
	AskPx string `json:"askPx"`
	// Best ask size
	AskSz string `json:"askSz"`
	// Best bid price
	BidPx string `json:"bidPx"`
	// Best bid size
	BidSz string `json:"bidSz"`
	// Open price in the past 24 hours
	Open24H string `json:"open24h"`
	// Highest price in the past 24 hours
	High24H string `json:"high24h"`
	// Lowest price in the past 24 hours
	Low24H string `json:"low24h"`
	// Open price in the UTC 0
	SodUtc0 string `json:"sodUtc0"`
	// Open price in the UTC 8
	SodUtc8 string `json:"sodUtc8"`
	// 24h trading volume, with a unit of currency.
	// If it is a derivatives contract, the value is the number of base currency.
	// If it is SPOT/MARGIN, the value is the quantity in quote currency.
	VolCcy24H string `json:"volCcy24h"`
	// 24h trading volume, with a unit of contract.
	// If it is a derivatives contract, the value is the number of contracts.
	// If it is SPOT/MARGIN, the value is the quantity in base currency.
	Vol24H string `json:"vol24h"`
	// Ticker data generation time, Unix timestamp format in milliseconds, e.g. 1597026383085
	Ts string `json:"ts"`
}

TickersData

Jump to

Keyboard shortcuts

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