gate

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

Gate.io Provider

Overview

The Gate.io provider is used to fetch the ticker price from the Gate.io websocket API.

Gate.io provides public and authenticated channels.

The Gate.io provider uses protocol-level ping-pong, so no handlers need to be specifically implemented.

Application level ping messages can be sent which should be responded to with pong messages.

  • Public channels -- No authentication is required, include tickers topic, K-Line topic, limit price topic, order book topic, and mark price topic etc.
  • Private channels -- including account topic, order topic, and position topic, etc. -- require log in.

Users can choose to subscribe to one or more topic. This provider is implemented assuming that the user is only subscribing to public topics.

The exact topic that is used to subscribe to the ticker price is the Tickers. This pushes data every 1000ms.

To retrieve all supported spot markets, please run the following command:

curl -X GET https://api.gateio.ws/api/v4/spot/currency_pairs \
  -H 'Accept: application/json'

Documentation

Index

Constants

View Source
const (
	// ChannelTickers is the tickers channel to subscribe to.
	ChannelTickers Channel = "spot.tickers"

	// EventSubscribe is the event for subscribing to a topic.
	EventSubscribe Event = "subscribe"

	// EventUpdate is the event indicating an update.
	EventUpdate Event = "update"

	// StatusSuccess is the status indicating a subscription was successful.
	StatusSuccess Status = "success"

	// ErrorInvalidRequestBody is returned for an invalid body in the request.
	ErrorInvalidRequestBody ErrorCode = 1
	// ErrorInvalidArgument is returned for an invalid argument in the request.
	ErrorInvalidArgument ErrorCode = 2
	// ErrorServer is returned when there is a server side error.
	ErrorServer ErrorCode = 3
)
View Source
const (
	// Name is the name of the Gate.io provider.
	Name = "gate_ws"
	// URL is the base url of for the Gate.io websocket API.
	URL = "wss://api.gateio.ws/ws/v4/"
)

Variables

View Source
var DefaultWebSocketConfig = config.WebSocketConfig{
	Name:                          Name,
	Enabled:                       true,
	MaxBufferSize:                 1000,
	ReconnectionTimeout:           10 * time.Second,
	PostConnectionTimeout:         config.DefaultPostConnectionTimeout,
	Endpoints:                     []config.Endpoint{{URL: URL}},
	ReadBufferSize:                config.DefaultReadBufferSize,
	WriteBufferSize:               config.DefaultWriteBufferSize,
	HandshakeTimeout:              config.DefaultHandshakeTimeout,
	EnableCompression:             config.DefaultEnableCompression,
	ReadTimeout:                   config.DefaultReadTimeout,
	WriteTimeout:                  config.DefaultWriteTimeout,
	PingInterval:                  config.DefaultPingInterval,
	WriteInterval:                 config.DefaultWriteInterval,
	MaxReadErrorCount:             config.DefaultMaxReadErrorCount,
	MaxSubscriptionsPerConnection: config.DefaultMaxSubscriptionsPerConnection,
	MaxSubscriptionsPerBatch:      config.DefaultMaxSubscriptionsPerBatch,
}

DefaultWebSocketConfig is the default configuration for the Gate.io Websocket.

Functions

func NewWebSocketDataHandler

func NewWebSocketDataHandler(
	logger *zap.Logger,
	ws config.WebSocketConfig,
) (types.PriceWebSocketDataHandler, error)

NewWebSocketDataHandler returns a new Gate.io PriceWebSocketDataHandler.

Types

type BaseMessage

type BaseMessage struct {
	// Time is the time of the message.
	Time int `json:"time"`
	// Channel is the channel to subscribe to.
	Channel string `json:"channel"`
	// Event is the event the request/response is taking.
	Event string `json:"event"`
}

BaseMessage is a base message for a request/response from a peer.

type Channel

type Channel string

Channel is a type alias for a channel identifier.

type ErrorCode

type ErrorCode int

ErrorCode is a type alias for an int error code.

func (ErrorCode) Error

func (e ErrorCode) Error() error

Error returns the error representation of the ErrorCode.

type ErrorMessage

type ErrorMessage struct {
	// Code is the integer representation of the error code.
	Code int `json:"code"`
	// Message is the accompanying error message.
	Message string `json:"message"`
}

ErrorMessage represents an error returned from the Gate.io websocket API.

type Event

type Event string

Event is a type alias for an event identifier.

type RequestResult

type RequestResult struct {
	// Status is the status of the result.
	Status string `json:"status"`
}

RequestResult is the result message returned in a response from the Gate.io websocket API.

type Status

type Status string

Status is a type alias for a status identifier.

type SubscribeRequest

type SubscribeRequest struct {
	BaseMessage
	// ID is the optional ID for the message.
	ID int `json:"id"`
	// Payload is the argument payload sent for the corresponding request.
	Payload []string `json:"payload"`
}

SubscribeRequest is a subscription request sent to the Gate.io websocket API.

Ex.

{
 "time": 1611541000,
 "id": 123456789,
 "channel": "spot.orders",
 "event": "subscribe",
 "payload": ["BTC_USDT", "GT_USDT"],
}

type SubscribeResponse

type SubscribeResponse struct {
	BaseMessage
	// ID is the optional ID for the message.
	ID int `json:"id"`
	// Error is the error message returned.  Will be empty if no error is returned.
	Error ErrorMessage `json:"error"`
	// Result is the result returned from the server.
	Result RequestResult `json:"result"`
}

SubscribeResponse is a subscription response sent from the Gate.io websocket API.

Ex.

{
 "time": 1611541000,
 "time_ms": 1611541000001,
 "channel": "spot.orders",
 "event": "subscribe",
 "error": null,
 "result": {
   "status": "success"
 }
}

type TickerResult

type TickerResult struct {
	// CurrencyPair is the currency pair for the given data stream.
	CurrencyPair string `json:"currency_pair"`
	// Last is the last price of the pair.
	Last string `json:"last"`
}

TickerResult is the result returned in a TickerStream message.

type TickerStream

type TickerStream struct {
	BaseMessage
	// Result is the result body of the data stream.
	Result TickerResult `json:"result"`
}

TickerStream is the data stream returned for a ticker subscription.

Ex.

{
 "time": 1669107766,
 "time_ms": 1669107766406,
 "channel": "spot.tickers",
 "event": "update",
 "result": {
   "currency_pair": "BTC_USDT",
   "last": "15743.4",
   "lowest_ask": "15744.4",
   "highest_bid": "15743.5",
   "change_percentage": "-1.8254",
   "base_volume": "9110.473081735",
   "quote_volume": "145082083.2535",
   "high_24h": "16280.9",
   "low_24h": "15468.5"
 }
}

type WebSocketHandler

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

WebSocketHandler implements the WebSocketDataHandler interface. This is used to handle messages received from the Gate.io websocket API.

func (*WebSocketHandler) Copy

Copy is used to create a copy of the WebSocketHandler.

func (*WebSocketHandler) CreateMessages

func (h *WebSocketHandler) CreateMessages(
	tickers []types.ProviderTicker,
) ([]handlers.WebsocketEncodedMessage, error)

CreateMessages is used to create an initial subscription message to send to the data provider. Only the tickers that are specified in the config are subscribed to. The only channel that is subscribed to is the tickers channel - which supports spot markets.

func (*WebSocketHandler) HandleMessage

func (h *WebSocketHandler) HandleMessage(
	message []byte,
) (types.PriceResponse, []handlers.WebsocketEncodedMessage, error)

HandleMessage is used to handle a message received from the data provider. The Gate.io provider sends two types of messages:

  1. Subscribe response message. The subscribe response message is used to determine if the subscription was successful.
  2. Ticker stream message. This is sent when a ticker update is received from the Gate.io websocket API.

func (*WebSocketHandler) HeartBeatMessages

func (h *WebSocketHandler) HeartBeatMessages() ([]handlers.WebsocketEncodedMessage, error)

HeartBeatMessages is not used for Gate.io.

func (*WebSocketHandler) NewSubscribeRequest

func (h *WebSocketHandler) NewSubscribeRequest(symbols []string) ([]handlers.WebsocketEncodedMessage, error)

NewSubscribeRequest returns a new SubscribeRequest encoded message for the given symbols.

Jump to

Keyboard shortcuts

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