bitstamp

package
v1.3.2 Latest Latest
Warning

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

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

README

Bitstamp Provider

Overview

Bitstamp is a cryptocurrency exchange that provides a free API for fetching cryptocurrency data. Bitstamp is a primary data source for the oracle. It supports connecting to a websocket without authentication. Once you open a connection via websocket handshake (using HTTP upgrade header), you can subscribe to desired channels. After this is accomplished, you will start to receive a stream of live events for every channel you are subscribed to. Maximum connection age is 90 days from the time the connection is established. When that period of time elapses, you will be automatically disconnected and will need to re-connect.

To see the supported set of markets, you can reference the websocket documentation here.

Documentation

Index

Constants

View Source
const (
	// TickerChannel is the ticker channel. A subscription to this channel
	// will provide real-time trade data, updated every time a trade occurs.
	//
	// ref: https://www.bitstamp.net/websocket/v2/
	TickerChannel ChannelType = "live_trades_"

	// HeartbeatEvent is the heartbeat event. This event is sent to the server
	// to keep the connection alive.
	//
	// ref: https://www.bitstamp.net/websocket/v2/
	HeartbeatEvent EventType = "bts:heartbeat"

	// SubscriptionEvent is the subscription event. This event is sent to the
	// server to subscribe to a channel.
	//
	// ref: https://www.bitstamp.net/websocket/v2/
	SubscriptionEvent EventType = "bts:subscribe"

	// SubscriptionSucceededEvent is the subscription succeeded event. This
	// event is received after a subscription request is made to the ticker
	// channel.
	//
	// ref: https://www.bitstamp.net/websocket/v2/
	SubscriptionSucceededEvent EventType = "bts:subscription_succeeded"

	// ReconnectEvent is the reconnect event. After you receive this request,
	// you will have a few seconds to reconnect. Without doing so, you will
	// automatically be disconnected. If you send reconnection request, you
	// will be placed to a new server. Consequently, you can continue without
	// any message loss.
	//
	// ref: https://www.bitstamp.net/websocket/v2/
	ReconnectEvent EventType = "bts:request_reconnect"

	// TradeEvent is the trade event. This event is received after a subscription
	// request is made to the ticker channel. It contains the relevant ticker data.
	//
	// ref: https://www.bitstamp.net/websocket/v2/
	TradeEvent EventType = "trade"
)
View Source
const (
	// ExpectedTickerLength is the expected length of the ID field.
	// This field contains the channel name and the currency pair.
	ExpectedTickerLength = 2

	// TickerChannelIndex is the index of the ticker channel in the ID field.
	TickerChannelIndex = 0

	// TickerCurrencyPairIndex is the index of the ticker currency pair in the
	// ID field.
	TickerCurrencyPairIndex = 1
)
View Source
const (
	// Name is the name of the bitstamp provider.
	Name = "bitstamp_ws"

	// WSS is the bitstamp websocket address.
	WSS = "wss://ws.bitstamp.net"

	// DefaultPingInterval is the default ping interval for the bitstamp websocket.
	DefaultPingInterval = 10 * time.Second
)

Variables

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

	MaxSubscriptionsPerBatch: config.DefaultMaxSubscriptionsPerBatch,
}

DefaultWebSocketConfig returns the default websocket config for bitstamp.

Functions

func NewHeartbeatRequestMessage

func NewHeartbeatRequestMessage() ([]handlers.WebsocketEncodedMessage, error)

NewHeartbeatRequestMessage returns a new heartbeat request message.

func NewReconnectRequestMessage

func NewReconnectRequestMessage() ([]handlers.WebsocketEncodedMessage, error)

NewReconnectRequestMessage returns a new reconnect request message.

func NewSubscriptionRequestMessage

func NewSubscriptionRequestMessage(channel string) (handlers.WebsocketEncodedMessage, error)

NewSubscriptionRequestMessage returns a new subscription request message.

func NewSubscriptionRequestMessages

func NewSubscriptionRequestMessages(channels []string) ([]handlers.WebsocketEncodedMessage, error)

NewSubscriptionRequestMessages returns a new subscription request message for a given set of channels.

func NewWebSocketDataHandler

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

NewWebSocketDataHandler returns a new Bitstamp PriceWebSocketDataHandler.

Types

type BaseMessage

type BaseMessage struct {
	// Event is the event type.
	Event string `json:"event"`
}

BaseMessage is utilized to define the base message structure for the Bitstamp websocket feed.

type ChannelType

type ChannelType string

ChannelType defines the channel types that can be subscribed to.

type EventType

type EventType string

EventType defines the event types that can be sent to the server.

type SubscriptionRequestMessage

type SubscriptionRequestMessage struct {
	BaseMessage

	// Data is the subscription data.
	Data SubscriptionRequestMessageData `json:"data"`
}

SubscriptionRequestMessage represents a subscription request message. Once an initial connection is established, clients can send a subscription request message to receive updates for a specific topic.

{
    "event": "bts:subscribe",
    "data": {
        "channel": "[channel_name]"
    }
}

ref: https://www.bitstamp.net/websocket/v2/

type SubscriptionRequestMessageData

type SubscriptionRequestMessageData struct {
	// Channel is the channel to subscribe to.
	Channel string `json:"channel"`
}

SubscriptionRequestMessageData is the data field of the SubscriptionRequestMessage.

type SubscriptionResponseMessage

type SubscriptionResponseMessage struct {
	BaseMessage

	// Channel is the channel that was subscribed to.
	Channel string `json:"channel"`
}

SubscriptionResponseMessage represents a subscription response message. This message is received after a subscription request is made to the ticker channel.

{
		"event" : "bts:subscription_succeeded",
		"channel" : "live_trades_btcusd",
		"data":{}
	}

ref: https://www.bitstamp.net/websocket/v2/

type TickerData

type TickerData struct {
	// PriceStr is the price represented in string format.
	PriceStr string `json:"price_str"`

	// Channel is the channel that was subscribed to.
	Channel string `json:"channel"`
}

TickerData is the data field of the TickerResponseMessage.

type TickerResponseMessage

type TickerResponseMessage struct {
	BaseMessage

	// Channel is the channel that was subscribed to.
	Channel string `json:"channel"`

	// Data is the ticker data.
	Data TickerData `json:"data"`
}

TickerResponseMessage represents a ticker response message. This message is received after a subscription request is made to the ticker channel. It contains the relevant ticker data.

id: Trade unique ID. amount: Trade amount. amount_str: Trade amount represented in string format. price: Trade price. price_str: Trade price represented in string format. type: Trade type (0 - buy; 1 - sell). timestamp: Trade timestamp. microtimestamp: Trade microtimestamp. buy_order_id: Trade buy order ID. sell_order_id: Trade sell order ID.

example:

{
	"channel":"live_trades_ethusd",
	"event":"trade"
	"data": {
			"id":317319339,
			"timestamp":"1706302442",
			"amount":0.062,
			"amount_str":"0.06200000",
			"price":2253.7,
			"price_str":"2253.7",
			"type":0,
			"microtimestamp":"1706302442078000",
			"buy_order_id":1709946746556417,
			"sell_order_id":1709946744614913
		}
}

ref: https://www.bitstamp.net/websocket/v2/

type WebSocketHandler

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

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

func (*WebSocketHandler) Copy

Copy creates a copy of the WebSocketHandler.

func (*WebSocketHandler) CreateMessages

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

CreateMessages creates the messages to send to the Bitstamp websocket API. The messages are used to subscribe to the live trades channel for the specified tickers.

func (*WebSocketHandler) HandleMessage

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

HandleMessage handles a message received from the Bitstamp websocket API. There are four types of messages that can be received:

  1. HeartbeatEvent: This is a heartbeat event. This event is sent from the server to the client letting the client know that the connection is still alive.
  2. ReconnectEvent: This is a reconnect event. This event is sent from the server to the client letting the client know that the server is about to restart.
  3. SubscriptionSucceededEvent: This is a subscription succeeded event. This event is sent from the server to the client letting the client know that the subscription was successful.
  4. TradeEvent: This is a trade event. This event is sent from the server to the client letting the client know that a trade has occurred. This event contains the price information for the trade.

func (*WebSocketHandler) HeartBeatMessages

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

HeartBeatMessages is used to create the heartbeat messages to send to the Bitstamp websocket API.

Jump to

Keyboard shortcuts

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