ws

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WsEndpoint     = "wss://ws.blockchain.info/mercury-gateway/v1/ws"
	WsTestEndpoint = "wss://ws.staging.blockchain.info/mercury-gateway/v1/ws"
)

Endpoints

View Source
const (
	Granularity60    Granularity = 60
	Granularity300   Granularity = 300
	Granularity900   Granularity = 900
	Granularity3600  Granularity = 3600
	Granularity21600 Granularity = 21600
	Granularity86400 Granularity = 86400

	GTC TimeInForce = "GTC" // Good Till Cancel. The order will rest on the order book until it is cancelled or filled
	GTD TimeInForce = "GTD" // Good Till Date. The order will reset on the order book until it is cancelled, filled, or expired
	FOK TimeInForce = "FOK" // Fill or Kill. The order is either completely filled or cancelled. No Partial Fills are permitted.
	IOC TimeInForce = "IOC" // Immediate or Cancel. The order is either a) completely filled, b) partially filled and the remaining quantity canceled, or c) the order is canceled.

	LIMIT      OrderType = "limit"     // order which has a price limit
	MARKET     OrderType = "market"    // order that will match at any price available in the market, starting from the best prices and filling up to the available balance
	STOP       OrderType = "stop"      // order which has a stop/trigger price, and when that price is reached, it triggers a market order
	STOP_LIMIT OrderType = "stopLimit" // order which has a stop price and limit price, and when the stop price is reached, it triggers a limit order at the limit price

	ORDER_STATUS_PENDING   OrderStatus = "pending"   // is pending acceptance. Only applicable to stop and stop-limit orders
	ORDER_STATUS_OPEN      OrderStatus = "open"      // has been accepted
	ORDER_STATUS_REJECTED  OrderStatus = "rejected"  // has been rejected	Limit and market orders can get rejected if you have no balance to fill the order even partially.
	ORDER_STATUS_CANCELLED OrderStatus = "cancelled" //  has been cancelled	A market order might get in state cancelled if you don’t have enough balance to fill it at market price. Both market orders and limit orders with IOC can have ordStatus ‘cancelled’ if there is no market for them, even without the user requesting the cancellation.
	ORDER_STATUS_FILLED    OrderStatus = "filled"    // has been filled	A limit order get in state cancelled after the user requested a cancellation.
	ORDER_STATUS_PARTIAL   OrderStatus = "partial"   // has been partially filled
	ORDER_STATUS_EXPIRED   OrderStatus = "expired"   // has been expired

	BUY  OrderSide = "buy"
	SELL OrderSide = "sell"

	ALO ExecInst = "ALO"

	ExecutionReport     MsgType = "8"
	OrderCancelRejected MsgType = "9"

	EXEC_TYPE_NEW          ExecType = "0"
	EXEC_TYPE_CANCELLED    ExecType = "4"
	EXEC_TYPE_EXPIRED      ExecType = "C"
	EXEC_TYPE_REJECTED     ExecType = "8"
	EXEC_TYPE_PARTIAL_FILL ExecType = "F"
	EXEC_TYPE_PENDING      ExecType = "A"
	EXEC_TYPE_TRADE_BREAK  ExecType = "H"
	EXEC_TYPE_ORDER_STATUS ExecType = "I"
)

Variables

View Source
var (
	WsHeaders = http.Header{
		"Origin": {"https://exchange.blockchain.com"},
	}

	PingFrequency = time.Second * 5
)
View Source
var (
	ErrAlreadySubscribed = errors.New("already subscribed")
	ErrInvalidRequest    = errors.New("invalid request")
)
View Source
var UseTestnet = false

UseTestnet switch all the API endpoints from production to the testnet

Functions

This section is empty.

Types

type BalanceMsg

type BalanceMsg struct {
	Currency       string  `json:"currency"`
	Balance        float64 `json:"balance"`
	Available      float64 `json:"available"`
	BalanceLocal   float64 `json:"balance_local"`
	AvailableLocal float64 `json:"available_local"`
	Rate           float64 `json:"rate"`
}

type BalancesSnapshot

type BalancesSnapshot struct {
	Seqnum              int          `json:"seqnum"`
	Event               string       `json:"event"`
	Channel             string       `json:"channel"`
	Balances            []BalanceMsg `json:"balances"`
	TotalAvailableLocal float64      `json:"total_available_local"`
	TotalBalanceLocal   float64      `json:"total_balance_local"`
}

type Configuration

type Configuration struct {
	Host      string
	ApiKey    string
	Env       Env
	Timeout   time.Duration
	Keepalive bool
	IsSecure  bool
}

type Env

type Env string
var (
	PROD    Env = "PROD"
	STAGING Env = "STAGING"
)

type ErrHandler

type ErrHandler func(err error)

ErrHandler handles errors

type ExecInst

type ExecInst string

type ExecType

type ExecType string

type Granularity

type Granularity int

type HeartbeatMsg

type HeartbeatMsg struct {
	Event     eventType `json:"event"`
	Channel   channel   `json:"channel"`
	SeqNum    int64     `json:"seqNum"`
	Timestamp time.Time `json:"timestamp"`
}

type L2Msg

type L2Msg struct {
	Seqnum  int     `json:"seqnum"`
	Event   string  `json:"event"`
	Channel string  `json:"channel"`
	Symbol  string  `json:"symbol"`
	Bids    []Level `json:"bids"`
	Asks    []Level `json:"asks"`
}

type L3Msg

type L3Msg struct {
	Seqnum  int     `json:"seqnum"`
	Event   string  `json:"event"`
	Channel string  `json:"channel"`
	Symbol  string  `json:"symbol"`
	Bids    []Level `json:"bids"`
	Asks    []Level `json:"asks"`
}

type Level

type Level struct {
	Px  float64 `json:"px"`
	Qty float64 `json:"qty"`
	Num int     `json:"num"`
}

type MsgType

type MsgType string

type NewOrderSingleMsg

type NewOrderSingleMsg struct {
	ClOrdID     string      `json:"clOrdID"`
	Symbol      Symbol      `json:"symbol"`
	OrdType     OrderType   `json:"ordType"`
	TimeInForce TimeInForce `json:"timeInForce"`
	Side        OrderSide   `json:"side"`
	OrderQty    float64     `json:"orderQty"`
	Price       float64     `json:"price"`
	ExecInst    ExecInst    `json:"execInst"`
}

type Order

type Order struct {
	OrderID      string    `json:"orderID"`
	ClOrdID      string    `json:"clOrdID"`
	Symbol       string    `json:"symbol"`
	Side         string    `json:"side"`
	OrdType      string    `json:"ordType"`
	OrderQty     float64   `json:"orderQty"`
	LeavesQty    float64   `json:"leavesQty"`
	CumQty       float64   `json:"cumQty"`
	AvgPx        float64   `json:"avgPx"`
	OrdStatus    string    `json:"ordStatus"`
	TimeInForce  string    `json:"timeInForce"`
	Text         string    `json:"text"`
	ExecType     string    `json:"execType"`
	ExecID       string    `json:"execID"`
	TransactTime time.Time `json:"transactTime"`
	MsgType      int       `json:"msgType"`
	LastPx       float64   `json:"lastPx"`
	LastShares   float64   `json:"lastShares"`
	TradeID      string    `json:"tradeId"`
	Price        float64   `json:"price"`
}

type OrderSide

type OrderSide string

type OrderStatus

type OrderStatus string

type OrderType

type OrderType string

type PricesMsg

type PricesMsg struct {
	Seqnum  int       `json:"seqnum"`
	Event   string    `json:"event"`
	Channel string    `json:"channel"`
	Symbol  string    `json:"symbol"`
	Price   []float64 `json:"price"`
}

type RejectMsg

type RejectMsg struct {
	Seqnum  int    `json:"seqnum"`
	Event   string `json:"event"`
	Channel string `json:"channel"`
	Text    string `json:"text"`
}

type SubscriptionError

type SubscriptionError struct {
	SubscriptionName string
	ErrorString      string
}

type Symbol

type Symbol string
const (
	BTCUSD Symbol = "BTC-USD"
	BTCEUR Symbol = "BTC-EUR"
	LTCUSD Symbol = "LTC-USD"
	LTCBTC Symbol = "LTC-BTC"
	ETHUSD Symbol = "ETH-USD"
	ETHBTC Symbol = "ETH-BTC"
	ETHEUR Symbol = "ETH-EUR"
	ETHGBP Symbol = "ETH-GBP"
)

type SymbolMsg

type SymbolMsg struct {
	Name                   Symbol  `json:"name"`
	BaseCurrency           string  `json:"base_currency"`
	BaseCurrencyScale      int     `json:"base_currency_scale"`
	CounterCurrency        string  `json:"counter_currency"`
	CounterCurrencyScale   int     `json:"counter_currency_scale"`
	MinPriceIncrement      int     `json:"min_price_increment"`
	MinPriceIncrementScale int     `json:"min_price_increment_scale"`
	MinOrderSize           int     `json:"min_order_size"`
	MinOrderSizeScale      int     `json:"min_order_size_scale"`
	MaxOrderSize           int     `json:"max_order_size"`
	MaxOrderSizeScale      int     `json:"max_order_size_scale"`
	LotSize                int     `json:"lot_size"`
	LotSizeScale           int     `json:"lot_size_scale"`
	Status                 string  `json:"status"`
	ID                     int     `json:"id"`
	AuctionPrice           float64 `json:"auction_price"`
	AuctionSize            float64 `json:"auction_size"`
	AuctionTime            string  `json:"auction_time"`
	Imbalance              float64 `json:"imbalance"`
}

type SymbolsSnapshot

type SymbolsSnapshot struct {
	Seqnum    int                  `json:"seqnum"`
	Event     string               `json:"event"`
	Channel   string               `json:"channel"`
	Timestamp time.Time            `json:"timestamp"`
	Symbols   map[Symbol]SymbolMsg `json:"symbols"`
}

type TickerMsg

type TickerMsg struct {
	Seqnum         int     `json:"seqnum"`
	Event          string  `json:"event"`
	Channel        string  `json:"channel"`
	Symbol         string  `json:"symbol"`
	Price24H       float64 `json:"price_24h"`
	Volume24H      float64 `json:"volume_24h"`
	LastTradePrice float64 `json:"last_trade_price"`
}

type TimeInForce

type TimeInForce string

type TradesMsg

type TradesMsg struct {
	Seqnum    int       `json:"seqnum"`
	Event     string    `json:"event"`
	Channel   string    `json:"channel"`
	Symbol    string    `json:"symbol"`
	Timestamp time.Time `json:"timestamp"`
	Side      string    `json:"side"`
	Qty       float64   `json:"qty"`
	Price     float64   `json:"price"`
	TradeID   string    `json:"trade_id"`
}

type TradingMsg

type TradingMsg interface {
	IsSnapshot() bool
	IsReject() bool
	IsUpdate() bool
}

type TradingReject

type TradingReject struct {
	Seqnum    int    `json:"seqnum"`
	Event     string `json:"event"`
	Channel   string `json:"channel"`
	Text      string `json:"text"`
	ClOrdID   string `json:"clOrdID"`
	OrdStatus string `json:"ordStatus"`
	Action    string `json:"action"`
}

func (*TradingReject) IsReject

func (t *TradingReject) IsReject() bool

func (*TradingReject) IsSnapshot

func (t *TradingReject) IsSnapshot() bool

func (*TradingReject) IsUpdate

func (t *TradingReject) IsUpdate() bool

type TradingSnapshot

type TradingSnapshot struct {
	Seqnum  int     `json:"seqnum"`
	Event   string  `json:"event"`
	Channel string  `json:"channel"`
	Orders  []Order `json:"orders"`
}

func (*TradingSnapshot) IsReject

func (t *TradingSnapshot) IsReject() bool

func (*TradingSnapshot) IsSnapshot

func (t *TradingSnapshot) IsSnapshot() bool

func (*TradingSnapshot) IsUpdate

func (t *TradingSnapshot) IsUpdate() bool

type TradingUpdated

type TradingUpdated struct {
	Seqnum       int       `json:"seqnum"`
	Event        string    `json:"event"`
	Channel      string    `json:"channel"`
	OrderID      string    `json:"orderID"`
	ClOrdID      string    `json:"clOrdID"`
	Symbol       string    `json:"symbol"`
	Side         string    `json:"side"`
	OrdType      string    `json:"ordType"`
	OrderQty     float64   `json:"orderQty"`
	LeavesQty    float64   `json:"leavesQty"`
	CumQty       float64   `json:"cumQty"`
	AvgPx        float64   `json:"avgPx"`
	OrdStatus    string    `json:"ordStatus"`
	TimeInForce  string    `json:"timeInForce"`
	Text         string    `json:"text"`
	ExecType     string    `json:"execType"`
	ExecID       string    `json:"execID"`
	TransactTime time.Time `json:"transactTime"`
	MsgType      int       `json:"msgType"`
	LastPx       float64   `json:"lastPx"`
	LastShares   float64   `json:"lastShares"`
	TradeID      string    `json:"tradeId"`
	Price        float64   `json:"price"`
}

func (*TradingUpdated) IsReject

func (t *TradingUpdated) IsReject() bool

func (*TradingUpdated) IsSnapshot

func (t *TradingUpdated) IsSnapshot() bool

func (*TradingUpdated) IsUpdate

func (t *TradingUpdated) IsUpdate() bool

type WebSocketClient

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

func NewWebSocketClient

func NewWebSocketClient(configuration Configuration) *WebSocketClient

func (*WebSocketClient) Authenticate

func (ws *WebSocketClient) Authenticate(token string) error

func (*WebSocketClient) Balances

func (ws *WebSocketClient) Balances() chan BalancesSnapshot

func (*WebSocketClient) BulkCancel

func (ws *WebSocketClient) BulkCancel(symbol *Symbol) error

func (*WebSocketClient) CancelOrder

func (ws *WebSocketClient) CancelOrder(orderID string) error

func (*WebSocketClient) Errors

func (ws *WebSocketClient) Errors() chan error

func (*WebSocketClient) Heartbeats

func (ws *WebSocketClient) Heartbeats() chan HeartbeatMsg

func (*WebSocketClient) L2Quotes

func (ws *WebSocketClient) L2Quotes() chan L2Msg

func (*WebSocketClient) L3Quotes

func (ws *WebSocketClient) L3Quotes() chan L3Msg

func (*WebSocketClient) NewOrderSingleMessage

func (ws *WebSocketClient) NewOrderSingleMessage(order NewOrderSingleMsg) error

func (*WebSocketClient) Prices

func (ws *WebSocketClient) Prices() chan PricesMsg

func (*WebSocketClient) Start

func (ws *WebSocketClient) Start(authenticate bool) error

func (*WebSocketClient) Stop

func (ws *WebSocketClient) Stop() error

func (*WebSocketClient) SubscribeHeartbeat

func (ws *WebSocketClient) SubscribeHeartbeat() error

func (*WebSocketClient) SubscribeToBalances

func (ws *WebSocketClient) SubscribeToBalances() error

func (*WebSocketClient) SubscribeToL2

func (ws *WebSocketClient) SubscribeToL2(symbol Symbol) error

func (*WebSocketClient) SubscribeToL3

func (ws *WebSocketClient) SubscribeToL3(symbol Symbol) error

func (*WebSocketClient) SubscribeToPrices

func (ws *WebSocketClient) SubscribeToPrices(symbol Symbol, granularity Granularity) error

func (*WebSocketClient) SubscribeToSymbols

func (ws *WebSocketClient) SubscribeToSymbols() error

func (*WebSocketClient) SubscribeToTicker

func (ws *WebSocketClient) SubscribeToTicker(symbol Symbol) error

func (*WebSocketClient) SubscribeToTrades

func (ws *WebSocketClient) SubscribeToTrades(symbol Symbol) error

func (*WebSocketClient) SubscribeToTrading

func (ws *WebSocketClient) SubscribeToTrading() error

func (*WebSocketClient) Symbols

func (ws *WebSocketClient) Symbols() chan SymbolMsg

func (*WebSocketClient) Ticker

func (ws *WebSocketClient) Ticker() chan TickerMsg

func (*WebSocketClient) Trades

func (ws *WebSocketClient) Trades() chan TradesMsg

func (*WebSocketClient) Trading

func (ws *WebSocketClient) Trading() chan TradingMsg

func (*WebSocketClient) WsL2Serve

func (ws *WebSocketClient) WsL2Serve(symbol string, handler WsL2MsgHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error)

WsL2Serve serve websocket l2 handler with a symbol

func (*WebSocketClient) WsL2ServeCombined

func (ws *WebSocketClient) WsL2ServeCombined(symbols []string, handler WsL2MsgHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error)

WsL2ServeCombined serve websocket l2 handler for a list of symbols

func (*WebSocketClient) WsL3Serve

func (ws *WebSocketClient) WsL3Serve(symbol string, handler WsL3MsgHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error)

WsL3Serve serve websocket l3 handler with a symbol

func (*WebSocketClient) WsPriceServe

func (ws *WebSocketClient) WsPriceServe(symbol string, granularity Granularity, handler WsPriceMsgHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error)

WsPriceServe serve websocket ticker handler with a symbol

func (*WebSocketClient) WsTickerServe

func (ws *WebSocketClient) WsTickerServe(symbol string, handler WsTickerMsgHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error)

WsTickerServe serve websocket ticker handler with a symbol

type WsHandler

type WsHandler func(message []byte)

WsHandler handle raw websocket message

type WsL2MsgHandler

type WsL2MsgHandler func(event *L2Msg)

WsL2MsgHandler handle websocket l2 event

type WsL3MsgHandler

type WsL3MsgHandler func(event *L3Msg)

WsL2MsgHandler handle websocket l3 event

type WsPriceMsgHandler

type WsPriceMsgHandler func(event *PricesMsg)

WsPriceMsgHandler handle websocket price event

type WsTickerMsgHandler

type WsTickerMsgHandler func(event *TickerMsg)

WsTickerMsgHandler handle websocket ticker event

Jump to

Keyboard shortcuts

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