bitfinex

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: 10 Imported by: 0

README

BitFinex Provider

Overview

The BitFinex provider is used to fetch the ticker price from the BitFinex websocket API. The total amount of subscriptions per connection is 30.

BitFinex provides public and private (authenticated) channels.

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

The exact channel that is used to subscribe to the ticker price is the Tickers. This pushes data regularly regarding a ticker status.

To retrieve all supported tickers, please run the following command:

curl https://api-pub.bitfinex.com/v2/conf/pub:list:currency

Documentation

Index

Constants

View Source
const (
	// EventSubscribe indicates a subscribe action.
	EventSubscribe Event = "subscribe"
	// EventSubscribed indicates that a subscription was successful.
	EventSubscribed Event = "subscribed"
	// EventError indicates that an error occurred.
	EventError Event = "error"
	// ChannelTicker is the channel name for the ticker channel.
	ChannelTicker Channel = "ticker"
	// IDHeartbeat is the id always used for a heartbeat.
	IDHeartbeat = "hb"
	// ExpectedStreamPayloadLength is the expected length of the payload of a data stream.
	ExpectedStreamPayloadLength = 10
	// ExpectedBaseStreamLength is the expected length of a stream base message.
	ExpectedBaseStreamLength = 2

	// ErrorUnknownEvent indicates an unknown event.
	ErrorUnknownEvent ErrorCode = 10000
	// ErrorUnknownPair indicates an unknown pari.
	ErrorUnknownPair ErrorCode = 10001
	// ErrorLimitOpenChannels indicates the limit of open channels has been exceeded.
	ErrorLimitOpenChannels ErrorCode = 10305
	// ErrorSubscriptionFailed indicates a subscription failed.
	ErrorSubscriptionFailed ErrorCode = 10400
	// ErrorNotSubscribed indicates you are not subscribed to the given topic.
	ErrorNotSubscribed ErrorCode = 104001
)
View Source
const (
	// Name is the name of the BitFinex provider.
	Name = "bitfinex_ws"

	// URLProd is the public BitFinex Websocket URL.
	URLProd = "wss://api-pub.bitfinex.com/ws/2"

	// DefaultMaxSubscriptionsPerConnection is the default maximum number of subscriptions
	// per connection. By default, BitFinex accepts up to 30 subscriptions per connection.
	// However, we limit this to 20 to prevent overloading the connection.
	DefaultMaxSubscriptionsPerConnection = 20
)

Variables

View Source
var DefaultWebSocketConfig = config.WebSocketConfig{
	Name:                          Name,
	Enabled:                       true,
	MaxBufferSize:                 1000,
	ReconnectionTimeout:           config.DefaultReconnectionTimeout,
	PostConnectionTimeout:         config.DefaultPostConnectionTimeout,
	Endpoints:                     []config.Endpoint{{URL: URLProd}},
	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: DefaultMaxSubscriptionsPerConnection,

	MaxSubscriptionsPerBatch: config.DefaultMaxSubscriptionsPerBatch,
}

DefaultWebSocketConfig is the default configuration for the BitFinex Websocket.

Functions

func NewSubscribeMessage

func NewSubscribeMessage(symbol string) (handlers.WebsocketEncodedMessage, error)

NewSubscribeMessage creates a new subscribe message given the ticker symbol.

func NewSubscribeMessages

func NewSubscribeMessages(symbols []string) ([]handlers.WebsocketEncodedMessage, error)

NewSubscribeMessages creates subscription messages for the given tickers.

func NewWebSocketDataHandler

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

NewWebSocketDataHandler returns a new BitFinex PriceWebSocketDataHandler.

Types

type BaseMessage

type BaseMessage struct {
	Event string `json:"event" validate:"required"`
}

BaseMessage is the base message structure for subscription requests and responses in the BitFinex websocket API.

type Channel

type Channel string

Channel is the channel of a message sent over the BitFinex websocket API.

type ErrorCode

type ErrorCode int64

ErrorCode is a type alias for an error code sent from the BitFinex websocket API.

func (ErrorCode) Error

func (e ErrorCode) Error() error

Error returns the error representation of the ErrorCode.

type ErrorMessage

type ErrorMessage struct {
	BaseMessage
	Msg  string `json:"msg" validate:"required"`
	Code int64  `json:"code" validate:"required"`
}

ErrorMessage represent an error returned by the peer.

Ex.

{
  "event": "error",
  "msg": ERROR_MSG,
  "code": ERROR_CODE
}

ref: https://docs.bitfinex.com/reference/ws-public-ticker

type Event

type Event string

Event is the event type of message sent over the BitFinex websocket API.

type SubscribeMessage

type SubscribeMessage struct {
	BaseMessage
	Channel string `json:"channel" validate:"required"`
	Symbol  string `json:"symbol" validate:"required"`
}

SubscribeMessage is a base message used to make a subscription request.

Ex:

{
 event: "subscribe",
 channel: "ticker",
 symbol: SYMBOL
}

ref: https://docs.bitfinex.com/reference/ws-public-ticker

type SubscribedMessage

type SubscribedMessage struct {
	BaseMessage
	Channel   string `json:"channel" validate:"required"`
	ChannelID int    `json:"chanId" validate:"required"`
	Pair      string `json:"pair" validate:"required"`
}

SubscribedMessage is message indicating the status of a subscription request.

Ex:

{
  event: "subscribed",
  channel: "ticker",
  chanId: CHANNEL_ID,
  symbol: SYMBOL,
  pair: PAIR
}

ref: https://docs.bitfinex.com/reference/ws-public-ticker

type WebSocketHandler

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

WebSocketHandler implements the WebSocketDataHandler interface. This is used to handle messages received from the BitFinex 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 index 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 BitFinex provider sends four types of messages:

  1. Subscribed response message. The subscribe response message is used to determine if the subscription was successful. If successful, the channel ID is saved.
  2. Error response messages. These messages provide info about errors from requests sent to the BitFinex websocket API.
  3. Ticker stream message. This is sent when a ticker update is received from the BitFinex websocket API.
  4. Heartbeat stream messages. These are sent every 15 seconds by the BitFinex API.

func (*WebSocketHandler) HeartBeatMessages

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

HeartBeatMessages is not used for BitFinex.

Jump to

Keyboard shortcuts

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