Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
update readme, fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
larscom committed Nov 27, 2023
1 parent c78d6a5 commit 693e417
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 93 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Go Bitvavo

[![Go Report Card](https://goreportcard.com/badge/github.com/larscom/go-bitvavo)](https://goreportcard.com/report/github.com/larscom/go-bitvavo)
[![Go Report Card](https://goreportcard.com/badge/github.com/larscom/go-bitvavo/v2)](https://goreportcard.com/report/github.com/larscom/go-bitvavo/v2)
[![Go Reference](https://pkg.go.dev/badge/github.com/larscom/go-bitvavo.svg)](https://pkg.go.dev/github.com/larscom/go-bitvavo)

> Go **thread safe** client library for Bitvavo v2 (https://docs.bitvavo.com)
Expand All @@ -10,7 +10,7 @@ Go Bitvavo is a **thread safe** client written in GO to interact with the Bitvav
## 📒 Features

- [x] WebSockets
-- Read Only
-- Read only
- [ ] REST (_soon_)
-- Read / Write
- [ ] ...
Expand Down
26 changes: 13 additions & 13 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ func (o *OrderEvent) UnmarshalJSON(data []byte) error {
Status: status,
Side: side,
OrderType: orderType,
Amount: util.IfOrElse(len(amount) > 0, func() float64 { return util.MustFloat64(amount) }, ZERO),
AmountRemaining: util.IfOrElse(len(amountRemaining) > 0, func() float64 { return util.MustFloat64(amountRemaining) }, ZERO),
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, ZERO),
OnHold: util.IfOrElse(len(onHold) > 0, func() float64 { return util.MustFloat64(onHold) }, ZERO),
Amount: util.IfOrElse(len(amount) > 0, func() float64 { return util.MustFloat64(amount) }, zerof),
AmountRemaining: util.IfOrElse(len(amountRemaining) > 0, func() float64 { return util.MustFloat64(amountRemaining) }, zerof),
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof),
OnHold: util.IfOrElse(len(onHold) > 0, func() float64 { return util.MustFloat64(onHold) }, zerof),
OnHoldCurrency: onHoldCurrency,
TriggerPrice: util.IfOrElse(len(triggerPrice) > 0, func() float64 { return util.MustFloat64(triggerPrice) }, ZERO),
TriggerAmount: util.IfOrElse(len(triggerAmount) > 0, func() float64 { return util.MustFloat64(triggerAmount) }, ZERO),
TriggerPrice: util.IfOrElse(len(triggerPrice) > 0, func() float64 { return util.MustFloat64(triggerPrice) }, zerof),
TriggerAmount: util.IfOrElse(len(triggerAmount) > 0, func() float64 { return util.MustFloat64(triggerAmount) }, zerof),
TriggerType: triggerType,
TriggerReference: triggerReference,
TimeInForce: timeInForce,
Expand Down Expand Up @@ -223,11 +223,11 @@ func (f *FillEvent) UnmarshalJSON(data []byte) error {
OrderId: orderId,
FillId: fillId,
Timestamp: int64(timestamp),
Amount: util.IfOrElse(len(amount) > 0, func() float64 { return util.MustFloat64(amount) }, ZERO),
Amount: util.IfOrElse(len(amount) > 0, func() float64 { return util.MustFloat64(amount) }, zerof),
Side: side,
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, ZERO),
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof),
Taker: taker,
Fee: util.IfOrElse(len(fee) > 0, func() float64 { return util.MustFloat64(fee) }, ZERO),
Fee: util.IfOrElse(len(fee) > 0, func() float64 { return util.MustFloat64(fee) }, zerof),
FeeCurrency: feeCurrency,
}

Expand Down Expand Up @@ -299,7 +299,7 @@ func (t *accountEventHandler) Subscribe(market string) (AccountSub, error) {
}

if err := t.withAuth(func() {
t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameAccount, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameAccount, market)
}); err != nil {
return nil, err
}
Expand All @@ -317,7 +317,7 @@ func (t *accountEventHandler) Unsubscribe(market string) error {

if exist {
if err := t.withAuth(func() {
t.writechn <- newWebSocketMessage(ActionUnsubscribe, ChannelNameBook, market)
t.writechn <- newWebSocketMessage(actionUnsubscribe, channelNameBook, market)
}); err != nil {
return err
}
Expand Down Expand Up @@ -377,7 +377,7 @@ func (t *accountEventHandler) handleAuthMessage(bytes []byte) {
func newWebSocketAuthMessage(apiKey string, apiSecret string, windowTimeMs uint64) WebSocketMessage {
timestamp := time.Now().UnixMilli()
return WebSocketMessage{
Action: ActionAuthenticate.Value,
Action: actionAuthenticate.Value,
Key: apiKey,
Signature: createSignature(timestamp, apiSecret),
Timestamp: timestamp,
Expand All @@ -402,7 +402,7 @@ func (t *accountEventHandler) reconnect() {
for sub := range t.subs.IterBuffered() {
market := sub.Key
if err := t.withAuth(func() {
t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameAccount, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameAccount, market)
}); err != nil {
log.Logger().Error("Failed to reconnect the account websocket", "market", market)
}
Expand Down
14 changes: 7 additions & 7 deletions book.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (b *BookEvent) UnmarshalJSON(bytes []byte) error {
size := bidEvents[i].([]any)[1].(string)

bids[i] = Page{
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, ZERO),
Size: util.IfOrElse(len(size) > 0, func() float64 { return util.MustFloat64(size) }, ZERO),
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof),
Size: util.IfOrElse(len(size) > 0, func() float64 { return util.MustFloat64(size) }, zerof),
}
}

Expand All @@ -74,8 +74,8 @@ func (b *BookEvent) UnmarshalJSON(bytes []byte) error {
size := askEvents[i].([]any)[1].(string)

asks[i] = Page{
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, ZERO),
Size: util.IfOrElse(len(size) > 0, func() float64 { return util.MustFloat64(size) }, ZERO),
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof),
Size: util.IfOrElse(len(size) > 0, func() float64 { return util.MustFloat64(size) }, zerof),
}
}

Expand Down Expand Up @@ -107,7 +107,7 @@ func (t *bookEventHandler) Subscribe(market string, buffSize uint64) (<-chan Boo
return nil, fmt.Errorf("subscription already active for market: %s", market)
}

t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameBook, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameBook, market)

chn := make(chan BookEvent, buffSize)
t.subs.Set(market, chn)
Expand All @@ -119,7 +119,7 @@ func (t *bookEventHandler) Unsubscribe(market string) error {
sub, exist := t.subs.Get(market)

if exist {
t.writechn <- newWebSocketMessage(ActionUnsubscribe, ChannelNameBook, market)
t.writechn <- newWebSocketMessage(actionUnsubscribe, channelNameBook, market)
close(sub)
t.subs.Remove(market)
return nil
Expand Down Expand Up @@ -156,6 +156,6 @@ func (t *bookEventHandler) handleMessage(bytes []byte) {
func (t *bookEventHandler) reconnect() {
for sub := range t.subs.IterBuffered() {
market := sub.Key
t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameBook, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameBook, market)
}
}
8 changes: 4 additions & 4 deletions candles.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func newCandleWebSocketMessage(action Action, market string, interval string) We
Action: action.Value,
Channels: []Channel{
{
Name: ChannelNameCandles.Value,
Name: channelNameCandles.Value,
Markets: []string{market},
Intervals: []string{interval},
},
Expand All @@ -102,7 +102,7 @@ func (c *candlesEventHandler) Subscribe(market string, interval string, buffSize
return nil, fmt.Errorf("subscription already active for market: %s with interval: %s", market, interval)
}

c.writechn <- newCandleWebSocketMessage(ActionSubscribe, market, interval)
c.writechn <- newCandleWebSocketMessage(actionSubscribe, market, interval)

chn := make(chan CandlesEvent, buffSize)
c.subs.Set(key, chn)
Expand All @@ -115,7 +115,7 @@ func (c *candlesEventHandler) Unsubscribe(market string, interval string) error
sub, exist := c.subs.Get(key)

if exist {
c.writechn <- newCandleWebSocketMessage(ActionUnsubscribe, market, interval)
c.writechn <- newCandleWebSocketMessage(actionUnsubscribe, market, interval)
close(sub)
c.subs.Remove(key)
return nil
Expand Down Expand Up @@ -157,7 +157,7 @@ func (c *candlesEventHandler) handleMessage(bytes []byte) {
func (c *candlesEventHandler) reconnect() {
for sub := range c.subs.IterBuffered() {
market, interval := getMapKeyValue(sub.Key)
c.writechn <- newCandleWebSocketMessage(ActionSubscribe, market, interval)
c.writechn <- newCandleWebSocketMessage(actionSubscribe, market, interval)
}
}

Expand Down
16 changes: 8 additions & 8 deletions ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func (t *TickerEvent) UnmarshalJSON(data []byte) error {

t.Market = market
t.Ticker = Ticker{
BestBid: util.IfOrElse(len(bestBid) > 0, func() float64 { return util.MustFloat64(bestBid) }, ZERO),
BestBidSize: util.IfOrElse(len(bestBidSize) > 0, func() float64 { return util.MustFloat64(bestBidSize) }, ZERO),
BestAsk: util.IfOrElse(len(bestAsk) > 0, func() float64 { return util.MustFloat64(bestAsk) }, ZERO),
BestAskSize: util.IfOrElse(len(bestAskSize) > 0, func() float64 { return util.MustFloat64(bestAskSize) }, ZERO),
LastPrice: util.IfOrElse(len(lastPrice) > 0, func() float64 { return util.MustFloat64(lastPrice) }, ZERO),
BestBid: util.IfOrElse(len(bestBid) > 0, func() float64 { return util.MustFloat64(bestBid) }, zerof),
BestBidSize: util.IfOrElse(len(bestBidSize) > 0, func() float64 { return util.MustFloat64(bestBidSize) }, zerof),
BestAsk: util.IfOrElse(len(bestAsk) > 0, func() float64 { return util.MustFloat64(bestAsk) }, zerof),
BestAskSize: util.IfOrElse(len(bestAskSize) > 0, func() float64 { return util.MustFloat64(bestAskSize) }, zerof),
LastPrice: util.IfOrElse(len(lastPrice) > 0, func() float64 { return util.MustFloat64(lastPrice) }, zerof),
}

return nil
Expand All @@ -84,7 +84,7 @@ func (t *tickerEventHandler) Subscribe(market string, buffSize uint64) (<-chan T
return nil, fmt.Errorf("subscription already active for market: %s", market)
}

t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameTicker, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameTicker, market)

chn := make(chan TickerEvent, buffSize)
t.subs.Set(market, chn)
Expand All @@ -96,7 +96,7 @@ func (t *tickerEventHandler) Unsubscribe(market string) error {
sub, exist := t.subs.Get(market)

if exist {
t.writechn <- newWebSocketMessage(ActionUnsubscribe, ChannelNameTicker, market)
t.writechn <- newWebSocketMessage(actionUnsubscribe, channelNameTicker, market)
close(sub)
t.subs.Remove(market)
return nil
Expand Down Expand Up @@ -133,6 +133,6 @@ func (t *tickerEventHandler) handleMessage(bytes []byte) {
func (t *tickerEventHandler) reconnect() {
for sub := range t.subs.IterBuffered() {
market := sub.Key
t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameTicker, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameTicker, market)
}
}
26 changes: 13 additions & 13 deletions ticker24h.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ func (t *Ticker24hEvent) UnmarshalJSON(bytes []byte) error {
t.Event = event
t.Market = market
t.Ticker24h = Ticker24h{
Open: util.IfOrElse(len(open) > 0, func() float64 { return util.MustFloat64(open) }, ZERO),
High: util.IfOrElse(len(high) > 0, func() float64 { return util.MustFloat64(high) }, ZERO),
Low: util.IfOrElse(len(low) > 0, func() float64 { return util.MustFloat64(low) }, ZERO),
Last: util.IfOrElse(len(last) > 0, func() float64 { return util.MustFloat64(last) }, ZERO),
Volume: util.IfOrElse(len(volume) > 0, func() float64 { return util.MustFloat64(volume) }, ZERO),
VolumeQuote: util.IfOrElse(len(volumeQuote) > 0, func() float64 { return util.MustFloat64(volumeQuote) }, ZERO),
Bid: util.IfOrElse(len(bid) > 0, func() float64 { return util.MustFloat64(bid) }, ZERO),
BidSize: util.IfOrElse(len(bidSize) > 0, func() float64 { return util.MustFloat64(bidSize) }, ZERO),
Ask: util.IfOrElse(len(ask) > 0, func() float64 { return util.MustFloat64(ask) }, ZERO),
AskSize: util.IfOrElse(len(askSize) > 0, func() float64 { return util.MustFloat64(askSize) }, ZERO),
Open: util.IfOrElse(len(open) > 0, func() float64 { return util.MustFloat64(open) }, zerof),
High: util.IfOrElse(len(high) > 0, func() float64 { return util.MustFloat64(high) }, zerof),
Low: util.IfOrElse(len(low) > 0, func() float64 { return util.MustFloat64(low) }, zerof),
Last: util.IfOrElse(len(last) > 0, func() float64 { return util.MustFloat64(last) }, zerof),
Volume: util.IfOrElse(len(volume) > 0, func() float64 { return util.MustFloat64(volume) }, zerof),
VolumeQuote: util.IfOrElse(len(volumeQuote) > 0, func() float64 { return util.MustFloat64(volumeQuote) }, zerof),
Bid: util.IfOrElse(len(bid) > 0, func() float64 { return util.MustFloat64(bid) }, zerof),
BidSize: util.IfOrElse(len(bidSize) > 0, func() float64 { return util.MustFloat64(bidSize) }, zerof),
Ask: util.IfOrElse(len(ask) > 0, func() float64 { return util.MustFloat64(ask) }, zerof),
AskSize: util.IfOrElse(len(askSize) > 0, func() float64 { return util.MustFloat64(askSize) }, zerof),
Timestamp: int64(timestamp),
StartTimestamp: int64(startTimestamp),
OpenTimestamp: int64(openTimestamp),
Expand All @@ -137,7 +137,7 @@ func (t *ticker24hEventHandler) Subscribe(market string, buffSize uint64) (<-cha
return nil, fmt.Errorf("subscription already active for market: %s", market)
}

t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameTicker24h, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameTicker24h, market)

chn := make(chan Ticker24hEvent, buffSize)
t.subs.Set(market, chn)
Expand All @@ -149,7 +149,7 @@ func (t *ticker24hEventHandler) Unsubscribe(market string) error {
sub, exist := t.subs.Get(market)

if exist {
t.writechn <- newWebSocketMessage(ActionUnsubscribe, ChannelNameTicker24h, market)
t.writechn <- newWebSocketMessage(actionUnsubscribe, channelNameTicker24h, market)
close(sub)
t.subs.Remove(market)
return nil
Expand Down Expand Up @@ -186,6 +186,6 @@ func (t *ticker24hEventHandler) handleMessage(bytes []byte) {
func (t *ticker24hEventHandler) reconnect() {
for sub := range t.subs.IterBuffered() {
market := sub.Key
t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameTicker24h, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameTicker24h, market)
}
}
10 changes: 5 additions & 5 deletions trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func (t *TradesEvent) UnmarshalJSON(bytes []byte) error {
t.Market = market
t.Trade = Trade{
Id: id,
Amount: util.IfOrElse(len(amount) > 0, func() float64 { return util.MustFloat64(amount) }, ZERO),
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, ZERO),
Amount: util.IfOrElse(len(amount) > 0, func() float64 { return util.MustFloat64(amount) }, zerof),
Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof),
Side: side,
Timestamp: int64(timestamp),
}
Expand All @@ -87,7 +87,7 @@ func (t *tradesEventHandler) Subscribe(market string, buffSize uint64) (<-chan T
return nil, fmt.Errorf("subscription already active for market: %s", market)
}

t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameTrades, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameTrades, market)

chn := make(chan TradesEvent, buffSize)
t.subs.Set(market, chn)
Expand All @@ -99,7 +99,7 @@ func (t *tradesEventHandler) Unsubscribe(market string) error {
sub, exist := t.subs.Get(market)

if exist {
t.writechn <- newWebSocketMessage(ActionUnsubscribe, ChannelNameTrades, market)
t.writechn <- newWebSocketMessage(actionUnsubscribe, channelNameTrades, market)
close(sub)
t.subs.Remove(market)
return nil
Expand Down Expand Up @@ -136,6 +136,6 @@ func (t *tradesEventHandler) handleMessage(bytes []byte) {
func (t *tradesEventHandler) reconnect() {
for sub := range t.subs.IterBuffered() {
market := sub.Key
t.writechn <- newWebSocketMessage(ActionSubscribe, ChannelNameTrades, market)
t.writechn <- newWebSocketMessage(actionSubscribe, channelNameTrades, market)
}
}
49 changes: 20 additions & 29 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,42 @@ package bitvavo

import "github.com/orsinium-labs/enum"

const ZERO = float64(0)
const zerof = float64(0)

type WsEvent enum.Member[string]

var (
WsEventSubscribed = WsEvent{"subscribed"}
WsEventUnsubscribed = WsEvent{"unsubscribed"}
WsEventCandles = WsEvent{"candle"}
WsEventTicker = WsEvent{"ticker"}
WsEventTicker24h = WsEvent{"ticker24h"}
WsEventTrades = WsEvent{"trade"}
WsEventBook = WsEvent{"book"}
WsEventAuth = WsEvent{"authenticate"}
WsEventAccount = WsEvent{"account"}
WsEventOrder = WsEvent{"order"}
WsEventFill = WsEvent{"fill"}
wsEventSubscribed = WsEvent{"subscribed"}
wsEventUnsubscribed = WsEvent{"unsubscribed"}
wsEventCandles = WsEvent{"candle"}
wsEventTicker = WsEvent{"ticker"}
wsEventTicker24h = WsEvent{"ticker24h"}
wsEventTrades = WsEvent{"trade"}
wsEventBook = WsEvent{"book"}
wsEventAuth = WsEvent{"authenticate"}
wsEventOrder = WsEvent{"order"}
wsEventFill = WsEvent{"fill"}
)

type Action enum.Member[string]

var (
ActionSubscribe = Action{"subscribe"}
ActionUnsubscribe = Action{"unsubscribe"}
ActionAuthenticate = Action{"authenticate"}
actionSubscribe = Action{"subscribe"}
actionUnsubscribe = Action{"unsubscribe"}
actionAuthenticate = Action{"authenticate"}
)

type ChannelName enum.Member[string]

var (
ChannelNameCandles = ChannelName{"candles"}
ChannelNameTicker = ChannelName{"ticker"}
ChannelNameTicker24h = ChannelName{"ticker24h"}
ChannelNameTrades = ChannelName{"trades"}
ChannelNameBook = ChannelName{"book"}
ChannelNameAccount = ChannelName{"account"}
channelNameCandles = ChannelName{"candles"}
channelNameTicker = ChannelName{"ticker"}
channelNameTicker24h = ChannelName{"ticker24h"}
channelNameTrades = ChannelName{"trades"}
channelNameBook = ChannelName{"book"}
channelNameAccount = ChannelName{"account"}
)

type SubscribedEvent struct {
// Describes the returned event over the socket.
Event string `json:"event"`

// Subscriptions map[event][]markets
Subscriptions map[string][]string `json:"subscriptions"`
}

type AuthEvent struct {
// Describes the returned event over the socket.
Event string `json:"event"`
Expand Down
Loading

0 comments on commit 693e417

Please sign in to comment.