Skip to content

Go thread safe client library (WebSockets / HTTP) for Bitvavo

License

Notifications You must be signed in to change notification settings

larscom/go-bitvavo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

77 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Bitvavo

Go Report Card Go Reference

Go thread safe client library (WebSockets / HTTP) for Bitvavo v2 (https://docs.bitvavo.com)

Go Bitvavo is a thread-safe client written in GO to interact with the Bitvavo platform. It includes a WebSocket client (for read-only purposes) to listen to all events occurring on the Bitvavo platform (e.g. candles, ticker, orders, fills, etc.) and an HTTP client (for read/write operations). The HTTP client can retrieve the same data as WebSockets but can also perform write operations such as placing orders and withdrawing assets from your account.

πŸ“’ Supported Features

  • WebSocket Client -- Read only
    • Account (orders/fills)
    • Book
    • Candles
    • Trades
    • Ticker
    • Ticker 24h
  • Http Client -- Read / Write
    • Market data endpoints
    • Account endpoints
    • Synchronization endpoints
    • Trading endpoints
    • Transfer endpoints

πŸš€ Installation

go get github.com/larscom/go-bitvavo/v2@latest

πŸ’‘ Usage

import "github.com/larscom/go-bitvavo/v2"

🌐 HTTP client

Every function on the HTTP client has a WithContext variant so you can pass your own context as argument.

client := bitvavo.NewHttpClient()

Public endpoints

func main() {
	client := bitvavo.NewHttpClient()

	time, err := client.GetTime()
	if err != nil {
		log.Fatal(err)
	}
	log.Println(time)

	// provide your own context
	time, err := client.GetTimeWithContext(context.TODO())
	if err != nil {
		log.Fatal(err)
	}
	log.Println(time)

}

Private endpoints

func main() {
	client := bitvavo.NewHttpClient()

	// create a new auth client for authenticated requests
	authClient = client.ToAuthClient("MY_API_KEY", "MY_API_SECRET")

	balance, err := authClient.GetBalance("ETH")
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Balance", balance)
}

πŸ‘‚ WebSocket client

By default, the websocket handler will try to reconnect to the websocket when the connection is lost, you can disable this behaviour in the options.

For each subscription you can set the buffer size for the underlying channel. All channels have a default buffer size of 50 which should be sufficient in most cases.

Public Subscriptions

Public subscriptions requires no authentication and can be used directly.

Candles

Subscribe to candle events for market: ETH-EUR with an interval of 5m

func main() {
	ws, err := bitvavo.NewWsClient()
	if err != nil {
		log.Fatal(err)
	}

	chn, err := ws.Candles().Subscribe([]string{"ETH-EUR"}, "5m")
	if err != nil {
		log.Fatal(err)
	}

	for candlesEvent := range chn {
		log.Println(candlesEvent)
	}
}
View Event
type CandlesEvent struct {
	// Describes the returned event over the socket.
	Event string `json:"event"`

	// The market which was requested in the subscription.
	Market string `json:"market"`

	// The interval which was requested in the subscription.
	Interval string `json:"interval"`

	// The candle in the defined time period.
	Candle Candle `json:"candle"`
}
...

Book

Subscribe to book events for market: ETH-EUR

func main() {
	ws, err := bitvavo.NewWsClient()
	if err != nil {
		log.Fatal(err)
	}

	bookchn, err := ws.Book().Subscribe([]string{"ETH-EUR"})
	if err != nil {
		log.Fatal(err)
	}

	for bookEvent := range bookchn {
		log.Println(bookEvent)
	}
}
View Event
type BookEvent struct {
	// Describes the returned event over the socket.
	Event string `json:"event"`

	// The market which was requested in the subscription.
	Market string `json:"market"`

	// The book containing the bids and asks.
	Book Book `json:"book"`
}
...

Ticker

Subscribe to ticker events for market: ETH-EUR

func main() {
	ws, err := bitvavo.NewWsClient()
	if err != nil {
		log.Fatal(err)
	}

	tickerchn, err := ws.Ticker().Subscribe([]string{"ETH-EUR"})
	if err != nil {
		log.Fatal(err)
	}

	for tickerEvent := range tickerchn {
		log.Println(tickerEvent)
	}
}
View Event
type TickerEvent struct {
	// Describes the returned event over the socket.
	Event string `json:"event"`

	// The market which was requested in the subscription.
	Market string `json:"market"`

	// The ticker containing the prices.
	Ticker Ticker `json:"ticker"`
}
...

Ticker 24H

Subscribe to ticker24h events for market: ETH-EUR

func main() {
	ws, err := bitvavo.NewWsClient()
	if err != nil {
		log.Fatal(err)
	}

	ticker24hchn, err := ws.Ticker24h().Subscribe([]string{"ETH-EUR"})
	if err != nil {
		log.Fatal(err)
	}

	for ticker24hEvent := range ticker24hchn {
		log.Println(ticker24hEvent)
	}
}
View Event
type Ticker24hEvent struct {
	// Describes the returned event over the socket.
	Event string `json:"event"`

	// The market which was requested in the subscription.
	Market string `json:"market"`

	// The ticker24h containing the prices etc.
	Ticker24h Ticker24h `json:"ticker24h"`
}
...

Trades

Subscribe to trades events for market: ETH-EUR

func main() {
	ws, err := bitvavo.NewWsClient()
	if err != nil {
		log.Fatal(err)
	}

	tradeschn, err := ws.Trades().Subscribe([]string{"ETH-EUR"})
	if err != nil {
		log.Fatal(err)
	}

	for tradesEvent := range tradeschn {
		log.Println(tradesEvent)
	}
}
View Event
type TradesEvent struct {
	// Describes the returned event over the socket.
	Event string `json:"event"`

	// The market which was requested in the subscription.
	Market string `json:"market"`

	// The trade containing the price, side etc.
	Trade Trade `json:"trade"`
}
...

Private Subscriptions

Private subscriptions do require authentication in the form of an API key and API secret which you can setup in Bitvavo.

Account :: Orders / Fills

Subscribe to order and fill events for market: ETH-EUR

func main() {
	ws, err := bitvavo.NewWsClient()
	if err != nil {
		log.Fatal(err)
	}

	key := "MY API KEY"
	secret := "MY API SECRET"

	orderchn, fillchn, err := ws.Account(key, secret).Subscribe([]string{"ETH-EUR"})
	if err != nil {
		log.Fatal(err)
	}

	for {
		select {
		case orderEvent := <-orderchn:
			log.Println(orderEvent)

		case fillEvent := <-fillchn:
			log.Println(fillEvent)
		}
	}
}
View Event(s)
type OrderEvent struct {
	// Describes the returned event over the socket.
	Event string `json:"event"`

	// The market which was requested in the subscription.
	Market string `json:"market"`

	// The order itself.
	Order Order `json:"order"`
}

type FillEvent struct {
	// Describes the returned event over the socket
	Event string `json:"event"`
	// The market which was requested in the subscription
	Market string `json:"market"`
	// The fill itself
	Fill Fill `json:"fill"`
}
...

πŸ”§ Options

Auto Reconnect

You can disable auto reconnecting to the websocket by providing an option to the Websocket constructor

ws, err := bitvavo.NewWsClient(ws.WithAutoReconnect(false))

Error Channel

You can provide your own error channel if you wish to receive errors (e.g. reconnect error, auth error, write failed, read failed)

errchn := make(chan error)
ws, _ := bitvavo.NewWsClient(ws.WithErrorChannel(errchn))

for err := range errchn {
	log.Println(err)
}

πŸ“ Debug Logging

You can enable debug logging for the HTTP client and WS client.

bitvavo.EnableDebugLogging()

πŸ‘‰πŸΌ Examples

You can test the performance by cloning this project and running:

make ticker or without make: go run ./examples/ws/ticker/main.go

This command will subscribe to all available trading markets (websocket) and log the received tickers.