diff --git a/httpc/balance.go b/bitv/balance.go similarity index 98% rename from httpc/balance.go rename to bitv/balance.go index d1d25eb..37ff36b 100644 --- a/httpc/balance.go +++ b/bitv/balance.go @@ -1,7 +1,8 @@ -package httpc +package bitv import ( "github.com/goccy/go-json" + "github.com/larscom/go-bitvavo/v2/util" ) diff --git a/bitv/bitv.go b/bitv/bitv.go new file mode 100644 index 0000000..62710e7 --- /dev/null +++ b/bitv/bitv.go @@ -0,0 +1,3 @@ +package bitv + +const zerof64 = float64(0) diff --git a/bitv/book.go b/bitv/book.go new file mode 100644 index 0000000..c072658 --- /dev/null +++ b/bitv/book.go @@ -0,0 +1,22 @@ +package bitv + +type Page struct { + // Bid / ask price. + Price float64 `json:"price"` + + // Size of 0 means orders are no longer present at that price level, otherwise the returned size is the new total size on that price level. + Size float64 `json:"size"` +} + +type Book struct { + // Integer which is increased by one for every update to the book. Useful for synchronizing. Resets to zero after restarting the matching engine. + Nonce int64 `json:"nonce"` + + // Slice with all bids in the format [price, size], where an size of 0 means orders are no longer present at that price level, + // otherwise the returned size is the new total size on that price level. + Bids []Page `json:"bids"` + + // Slice with all asks in the format [price, size], where an size of 0 means orders are no longer present at that price level, + // otherwise the returned size is the new total size on that price level. + Asks []Page `json:"asks"` +} diff --git a/bitv/candle.go b/bitv/candle.go new file mode 100644 index 0000000..787db1c --- /dev/null +++ b/bitv/candle.go @@ -0,0 +1,41 @@ +package bitv + +import ( + "fmt" + + "github.com/goccy/go-json" + "github.com/larscom/go-bitvavo/v2/util" +) + +type Candle struct { + // Timestamp in unix milliseconds. + Timestamp int64 `json:"timestamp"` + Open float64 `json:"open"` + High float64 `json:"high"` + Low float64 `json:"low"` + Close float64 `json:"close"` + Volume float64 `json:"volume"` +} + +func (c *Candle) UnmarshalJSON(bytes []byte) error { + var event [][]any + + err := json.Unmarshal(bytes, &event) + if err != nil { + return err + } + if len(event) != 1 { + return fmt.Errorf("unexpected length: %d, expected: 1", len(event)) + } + + candle := event[0] + + c.Timestamp = int64(candle[0].(float64)) + c.Open = util.MustFloat64(candle[1].(string)) + c.High = util.MustFloat64(candle[2].(string)) + c.Low = util.MustFloat64(candle[3].(string)) + c.Close = util.MustFloat64(candle[4].(string)) + c.Volume = util.MustFloat64(candle[5].(string)) + + return nil +} diff --git a/bitv/fill.go b/bitv/fill.go new file mode 100644 index 0000000..bb3a748 --- /dev/null +++ b/bitv/fill.go @@ -0,0 +1,23 @@ +package bitv + +type Fill struct { + // The id of the order on which has been filled + OrderId string `json:"orderId"` + // The id of the returned fill + FillId string `json:"fillId"` + // The current timestamp in milliseconds since 1 Jan 1970 + Timestamp int64 `json:"timestamp"` + // The amount in base currency for which the trade has been made + Amount float64 `json:"amount"` + // The side for the taker + // Enum: "buy" | "sell" + Side string `json:"side"` + // The price in quote currency for which the trade has been made + Price float64 `json:"price"` + // True for takers, false for makers + Taker bool `json:"taker"` + // The amount of fee that has been paid. Value is negative for rebates. Only available if settled is true + Fee float64 `json:"fee"` + // Currency in which the fee has been paid. Only available if settled is true + FeeCurrency string `json:"feeCurrency"` +} diff --git a/bitv/order.go b/bitv/order.go new file mode 100644 index 0000000..5140c63 --- /dev/null +++ b/bitv/order.go @@ -0,0 +1,78 @@ +package bitv + +type Order struct { + Guid string `json:"guid"` + + // The order id of the returned order. + OrderId string `json:"orderId"` + + // Is a timestamp in milliseconds since 1 Jan 1970. + Created int64 `json:"created"` + + // Is a timestamp in milliseconds since 1 Jan 1970. + Updated int64 `json:"updated"` + + // The current status of the order. + // Enum: "new" | "awaitingTrigger" | "canceled" | "canceledAuction" | "canceledSelfTradePrevention" | "canceledIOC" | "canceledFOK" | "canceledMarketProtection" | "canceledPostOnly" | "filled" | "partiallyFilled" | "expired" | "rejected" + Status string `json:"status"` + + // Side + // Enum: "buy" | "sell" + Side string `json:"side"` + + // OrderType + // Enum: "limit" | "market" + OrderType string `json:"orderType"` + + // Original amount. + Amount float64 `json:"amount"` + + // Amount remaining (lower than 'amount' after fills). + AmountRemaining float64 `json:"amountRemaining"` + + // The price of the order. + Price float64 `json:"price"` + + // Amount of 'onHoldCurrency' that is reserved for this order. This is released when orders are canceled. + OnHold float64 `json:"onHold"` + + // The currency placed on hold is the quote currency for sell orders and base currency for buy orders. + OnHoldCurrency string `json:"onHoldCurrency"` + + // Only for stop orders: The current price used in the trigger. This is based on the triggerAmount and triggerType. + TriggerPrice float64 `json:"triggerPrice"` + + // Only for stop orders: The value used for the triggerType to determine the triggerPrice. + TriggerAmount float64 `json:"triggerAmount"` + + // Only for stop orders. + // Enum: "price" + TriggerType string `json:"triggerType"` + + // Only for stop orders: The reference price used for stop orders. + // Enum: "lastTrade" | "bestBid" | "bestAsk" | "midPrice" + TriggerReference string `json:"triggerReference"` + + // Only for limit orders: Determines how long orders remain active. + // Possible values: Good-Til-Canceled (GTC), Immediate-Or-Cancel (IOC), Fill-Or-Kill (FOK). + // GTC orders will remain on the order book until they are filled or canceled. + // IOC orders will fill against existing orders, but will cancel any remaining amount after that. + // FOK orders will fill against existing orders in its entirety, or will be canceled (if the entire order cannot be filled). + // Enum: "GTC" | "IOC" | "FOK" + TimeInForce string `json:"timeInForce"` + + // Default: false + PostOnly bool `json:"postOnly"` + + // Self trading is not allowed on Bitvavo. Multiple options are available to prevent this from happening. + // The default ‘decrementAndCancel’ decrements both orders by the amount that would have been filled, which in turn cancels the smallest of the two orders. + // ‘cancelOldest’ will cancel the entire older order and places the new order. + // ‘cancelNewest’ will cancel the order that is submitted. + // ‘cancelBoth’ will cancel both the current and the old order. + // Default: "decrementAndCancel" + // Enum: "decrementAndCancel" | "cancelOldest" | "cancelNewest" | "cancelBoth" + SelfTradePrevention string `json:"selfTradePrevention"` + + // Whether this order is visible on the order book. + Visible bool `json:"visible"` +} diff --git a/bitv/ticker.go b/bitv/ticker.go new file mode 100644 index 0000000..a6cec47 --- /dev/null +++ b/bitv/ticker.go @@ -0,0 +1,18 @@ +package bitv + +type Ticker struct { + // The price of the best (highest) bid offer available, only sent when either bestBid or bestBidSize has changed. + BestBid float64 `json:"bestBid"` + + // The size of the best (highest) bid offer available, only sent when either bestBid or bestBidSize has changed. + BestBidSize float64 `json:"bestBidSize"` + + // The price of the best (lowest) ask offer available, only sent when either bestAsk or bestAskSize has changed. + BestAsk float64 `json:"bestAsk"` + + // The size of the best (lowest) ask offer available, only sent when either bestAsk or bestAskSize has changed. + BestAskSize float64 `json:"bestAskSize"` + + // The last price for which a trade has occurred, only sent when lastPrice has changed. + LastPrice float64 `json:"lastPrice"` +} diff --git a/bitv/ticker24h.go b/bitv/ticker24h.go new file mode 100644 index 0000000..17d9a81 --- /dev/null +++ b/bitv/ticker24h.go @@ -0,0 +1,45 @@ +package bitv + +type Ticker24h struct { + // The open price of the 24 hour period. + Open float64 `json:"open"` + + // The highest price for which a trade occurred in the 24 hour period. + High float64 `json:"high"` + + // The lowest price for which a trade occurred in the 24 hour period. + Low float64 `json:"low"` + + // The last price for which a trade occurred in the 24 hour period. + Last float64 `json:"last"` + + // The total volume of the 24 hour period in base currency. + Volume float64 `json:"volume"` + + // The total volume of the 24 hour period in quote currency. + VolumeQuote float64 `json:"volumeQuote"` + + // The best (highest) bid offer at the current moment. + Bid float64 `json:"bid"` + + // The size of the best (highest) bid offer. + BidSize float64 `json:"bidSize"` + + // The best (lowest) ask offer at the current moment. + Ask float64 `json:"ask"` + + // The size of the best (lowest) ask offer. + AskSize float64 `json:"askSize"` + + // Timestamp in unix milliseconds. + Timestamp int64 `json:"timestamp"` + + // Start timestamp in unix milliseconds. + StartTimestamp int64 `json:"startTimestamp"` + + // Open timestamp in unix milliseconds. + OpenTimestamp int64 `json:"openTimestamp"` + + // Close timestamp in unix milliseconds. + CloseTimestamp int64 `json:"closeTimestamp"` +} diff --git a/bitv/trade.go b/bitv/trade.go new file mode 100644 index 0000000..f6c68d6 --- /dev/null +++ b/bitv/trade.go @@ -0,0 +1,19 @@ +package bitv + +type Trade struct { + // The trade ID of the returned trade (UUID). + Id string `json:"id"` + + // The amount in base currency for which the trade has been made. + Amount float64 `json:"amount"` + + // The price in quote currency for which the trade has been made. + Price float64 `json:"price"` + + // The side for the taker. + // Enum: "buy" | "sell" + Side string `json:"side"` + + // Timestamp in unix milliseconds. + Timestamp int64 `json:"timestamp"` +} diff --git a/httpc/httpclientauth.go b/httpc/httpclientauth.go index 4b6479c..c7f4c1c 100644 --- a/httpc/httpclientauth.go +++ b/httpc/httpclientauth.go @@ -2,11 +2,13 @@ package httpc import ( "fmt" + + "github.com/larscom/go-bitvavo/v2/bitv" ) type HttpClientAuth interface { // GetBalance returns the balance on the account - GetBalance() ([]Balance, error) + GetBalance() ([]bitv.Balance, error) // GetAccount returns generic information about your account GetAccount() (any, error) @@ -30,8 +32,8 @@ func newHttpClientAuth(updateRateLimit func(int), config *authConfig) *httpClien } } -func (c *httpClientAuth) GetBalance() ([]Balance, error) { - return httpGet[[]Balance](fmt.Sprintf("%s/balance", httpUrl), c.updateRateLimit, c.config) +func (c *httpClientAuth) GetBalance() ([]bitv.Balance, error) { + return httpGet[[]bitv.Balance](fmt.Sprintf("%s/balance", httpUrl), c.updateRateLimit, c.config) } func (c *httpClientAuth) GetAccount() (any, error) { diff --git a/wsc/account.go b/wsc/account.go index 25b1ae4..40231e7 100644 --- a/wsc/account.go +++ b/wsc/account.go @@ -5,89 +5,13 @@ import ( "time" "github.com/goccy/go-json" + "github.com/larscom/go-bitvavo/v2/bitv" "github.com/larscom/go-bitvavo/v2/crypto" "github.com/larscom/go-bitvavo/v2/log" "github.com/larscom/go-bitvavo/v2/util" "github.com/smallnest/safemap" ) -type Order struct { - Guid string `json:"guid"` - - // The order id of the returned order. - OrderId string `json:"orderId"` - - // Is a timestamp in milliseconds since 1 Jan 1970. - Created int64 `json:"created"` - - // Is a timestamp in milliseconds since 1 Jan 1970. - Updated int64 `json:"updated"` - - // The current status of the order. - // Enum: "new" | "awaitingTrigger" | "canceled" | "canceledAuction" | "canceledSelfTradePrevention" | "canceledIOC" | "canceledFOK" | "canceledMarketProtection" | "canceledPostOnly" | "filled" | "partiallyFilled" | "expired" | "rejected" - Status string `json:"status"` - - // Side - // Enum: "buy" | "sell" - Side string `json:"side"` - - // OrderType - // Enum: "limit" | "market" - OrderType string `json:"orderType"` - - // Original amount. - Amount float64 `json:"amount"` - - // Amount remaining (lower than 'amount' after fills). - AmountRemaining float64 `json:"amountRemaining"` - - // The price of the order. - Price float64 `json:"price"` - - // Amount of 'onHoldCurrency' that is reserved for this order. This is released when orders are canceled. - OnHold float64 `json:"onHold"` - - // The currency placed on hold is the quote currency for sell orders and base currency for buy orders. - OnHoldCurrency string `json:"onHoldCurrency"` - - // Only for stop orders: The current price used in the trigger. This is based on the triggerAmount and triggerType. - TriggerPrice float64 `json:"triggerPrice"` - - // Only for stop orders: The value used for the triggerType to determine the triggerPrice. - TriggerAmount float64 `json:"triggerAmount"` - - // Only for stop orders. - // Enum: "price" - TriggerType string `json:"triggerType"` - - // Only for stop orders: The reference price used for stop orders. - // Enum: "lastTrade" | "bestBid" | "bestAsk" | "midPrice" - TriggerReference string `json:"triggerReference"` - - // Only for limit orders: Determines how long orders remain active. - // Possible values: Good-Til-Canceled (GTC), Immediate-Or-Cancel (IOC), Fill-Or-Kill (FOK). - // GTC orders will remain on the order book until they are filled or canceled. - // IOC orders will fill against existing orders, but will cancel any remaining amount after that. - // FOK orders will fill against existing orders in its entirety, or will be canceled (if the entire order cannot be filled). - // Enum: "GTC" | "IOC" | "FOK" - TimeInForce string `json:"timeInForce"` - - // Default: false - PostOnly bool `json:"postOnly"` - - // Self trading is not allowed on Bitvavo. Multiple options are available to prevent this from happening. - // The default ‘decrementAndCancel’ decrements both orders by the amount that would have been filled, which in turn cancels the smallest of the two orders. - // ‘cancelOldest’ will cancel the entire older order and places the new order. - // ‘cancelNewest’ will cancel the order that is submitted. - // ‘cancelBoth’ will cancel both the current and the old order. - // Default: "decrementAndCancel" - // Enum: "decrementAndCancel" | "cancelOldest" | "cancelNewest" | "cancelBoth" - SelfTradePrevention string `json:"selfTradePrevention"` - - // Whether this order is visible on the order book. - Visible bool `json:"visible"` -} - type OrderEvent struct { // Describes the returned event over the socket. Event string `json:"event"` @@ -96,7 +20,7 @@ type OrderEvent struct { Market string `json:"market"` // The order itself. - Order Order `json:"order"` + Order bitv.Order `json:"order"` } func (o *OrderEvent) UnmarshalJSON(bytes []byte) error { @@ -135,7 +59,7 @@ func (o *OrderEvent) UnmarshalJSON(bytes []byte) error { o.Market = market o.Event = event - o.Order = Order{ + o.Order = bitv.Order{ Guid: guid, OrderId: orderId, Created: int64(created), @@ -161,35 +85,13 @@ func (o *OrderEvent) UnmarshalJSON(bytes []byte) error { return nil } -type Fill struct { - // The id of the order on which has been filled - OrderId string `json:"orderId"` - // The id of the returned fill - FillId string `json:"fillId"` - // The current timestamp in milliseconds since 1 Jan 1970 - Timestamp int64 `json:"timestamp"` - // The amount in base currency for which the trade has been made - Amount float64 `json:"amount"` - // The side for the taker - // Enum: "buy" | "sell" - Side string `json:"side"` - // The price in quote currency for which the trade has been made - Price float64 `json:"price"` - // True for takers, false for makers - Taker bool `json:"taker"` - // The amount of fee that has been paid. Value is negative for rebates. Only available if settled is true - Fee float64 `json:"fee"` - // Currency in which the fee has been paid. Only available if settled is true - FeeCurrency string `json:"feeCurrency"` -} - 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"` + Fill bitv.Fill `json:"fill"` } func (f *FillEvent) UnmarshalJSON(bytes []byte) error { @@ -217,7 +119,7 @@ func (f *FillEvent) UnmarshalJSON(bytes []byte) error { f.Market = market f.Event = event - f.Fill = Fill{ + f.Fill = bitv.Fill{ OrderId: orderId, FillId: fillId, Timestamp: int64(timestamp), diff --git a/wsc/book.go b/wsc/book.go index 4b9059e..9f757bc 100644 --- a/wsc/book.go +++ b/wsc/book.go @@ -3,6 +3,7 @@ package wsc import ( "fmt" + "github.com/larscom/go-bitvavo/v2/bitv" "github.com/larscom/go-bitvavo/v2/log" "github.com/goccy/go-json" @@ -18,28 +19,7 @@ type BookEvent struct { Market string `json:"market"` // The book containing the bids and asks. - Book Book `json:"book"` -} - -type Page struct { - // Bid / ask price. - Price float64 `json:"price"` - - // Size of 0 means orders are no longer present at that price level, otherwise the returned size is the new total size on that price level. - Size float64 `json:"size"` -} - -type Book struct { - // Integer which is increased by one for every update to the book. Useful for synchronizing. Resets to zero after restarting the matching engine. - Nonce int64 `json:"nonce"` - - // Slice with all bids in the format [price, size], where an size of 0 means orders are no longer present at that price level, - // otherwise the returned size is the new total size on that price level. - Bids []Page `json:"bids"` - - // Slice with all asks in the format [price, size], where an size of 0 means orders are no longer present at that price level, - // otherwise the returned size is the new total size on that price level. - Asks []Page `json:"asks"` + Book bitv.Book `json:"book"` } func (b *BookEvent) UnmarshalJSON(bytes []byte) error { @@ -56,24 +36,24 @@ func (b *BookEvent) UnmarshalJSON(bytes []byte) error { ) bidEvents := bookEvent["bids"].([]any) - bids := make([]Page, len(bidEvents)) + bids := make([]bitv.Page, len(bidEvents)) for i := 0; i < len(bidEvents); i++ { price := bidEvents[i].([]any)[0].(string) size := bidEvents[i].([]any)[1].(string) - bids[i] = Page{ + bids[i] = bitv.Page{ Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof64), Size: util.IfOrElse(len(size) > 0, func() float64 { return util.MustFloat64(size) }, zerof64), } } askEvents := bookEvent["asks"].([]any) - asks := make([]Page, len(askEvents)) + asks := make([]bitv.Page, len(askEvents)) for i := 0; i < len(askEvents); i++ { price := askEvents[i].([]any)[0].(string) size := askEvents[i].([]any)[1].(string) - asks[i] = Page{ + asks[i] = bitv.Page{ Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof64), Size: util.IfOrElse(len(size) > 0, func() float64 { return util.MustFloat64(size) }, zerof64), } @@ -81,7 +61,7 @@ func (b *BookEvent) UnmarshalJSON(bytes []byte) error { b.Event = event b.Market = market - b.Book = Book{ + b.Book = bitv.Book{ Nonce: int64(nonce), Bids: bids, Asks: asks, diff --git a/wsc/candles.go b/wsc/candles.go index 4a0e467..52b1781 100644 --- a/wsc/candles.go +++ b/wsc/candles.go @@ -4,10 +4,10 @@ import ( "fmt" "strings" + "github.com/larscom/go-bitvavo/v2/bitv" "github.com/larscom/go-bitvavo/v2/log" "github.com/goccy/go-json" - "github.com/larscom/go-bitvavo/v2/util" "github.com/smallnest/safemap" ) @@ -22,40 +22,7 @@ type CandlesEvent struct { Interval string `json:"interval"` // The candle in the defined time period. - Candle Candle `json:"candle"` -} - -type Candle struct { - // Timestamp in unix milliseconds. - Timestamp int64 `json:"timestamp"` - Open float64 `json:"open"` - High float64 `json:"high"` - Low float64 `json:"low"` - Close float64 `json:"close"` - Volume float64 `json:"volume"` -} - -func (c *Candle) UnmarshalJSON(bytes []byte) error { - var event [][]any - - err := json.Unmarshal(bytes, &event) - if err != nil { - return err - } - if len(event) != 1 { - return fmt.Errorf("unexpected length: %d, expected: 1", len(event)) - } - - candle := event[0] - - c.Timestamp = int64(candle[0].(float64)) - c.Open = util.MustFloat64(candle[1].(string)) - c.High = util.MustFloat64(candle[2].(string)) - c.Low = util.MustFloat64(candle[3].(string)) - c.Close = util.MustFloat64(candle[4].(string)) - c.Volume = util.MustFloat64(candle[5].(string)) - - return nil + Candle bitv.Candle `json:"candle"` } type CandlesEventHandler interface { diff --git a/wsc/ticker.go b/wsc/ticker.go index 39966cf..df24ad1 100644 --- a/wsc/ticker.go +++ b/wsc/ticker.go @@ -3,6 +3,7 @@ package wsc import ( "fmt" + "github.com/larscom/go-bitvavo/v2/bitv" "github.com/larscom/go-bitvavo/v2/log" "github.com/goccy/go-json" @@ -18,24 +19,7 @@ type TickerEvent struct { Market string `json:"market"` // The ticker containing the prices. - Ticker Ticker `json:"ticker"` -} - -type Ticker struct { - // The price of the best (highest) bid offer available, only sent when either bestBid or bestBidSize has changed. - BestBid float64 `json:"bestBid"` - - // The size of the best (highest) bid offer available, only sent when either bestBid or bestBidSize has changed. - BestBidSize float64 `json:"bestBidSize"` - - // The price of the best (lowest) ask offer available, only sent when either bestAsk or bestAskSize has changed. - BestAsk float64 `json:"bestAsk"` - - // The size of the best (lowest) ask offer available, only sent when either bestAsk or bestAskSize has changed. - BestAskSize float64 `json:"bestAskSize"` - - // The last price for which a trade has occurred, only sent when lastPrice has changed. - LastPrice float64 `json:"lastPrice"` + Ticker bitv.Ticker `json:"ticker"` } func (t *TickerEvent) UnmarshalJSON(bytes []byte) error { @@ -56,7 +40,7 @@ func (t *TickerEvent) UnmarshalJSON(bytes []byte) error { ) t.Market = market - t.Ticker = Ticker{ + t.Ticker = bitv.Ticker{ BestBid: util.IfOrElse(len(bestBid) > 0, func() float64 { return util.MustFloat64(bestBid) }, zerof64), BestBidSize: util.IfOrElse(len(bestBidSize) > 0, func() float64 { return util.MustFloat64(bestBidSize) }, zerof64), BestAsk: util.IfOrElse(len(bestAsk) > 0, func() float64 { return util.MustFloat64(bestAsk) }, zerof64), diff --git a/wsc/ticker24h.go b/wsc/ticker24h.go index e4ee56f..673365a 100644 --- a/wsc/ticker24h.go +++ b/wsc/ticker24h.go @@ -3,6 +3,7 @@ package wsc import ( "fmt" + "github.com/larscom/go-bitvavo/v2/bitv" "github.com/larscom/go-bitvavo/v2/log" "github.com/goccy/go-json" @@ -18,51 +19,7 @@ type Ticker24hEvent struct { Market string `json:"market"` // The ticker24h containing the prices etc. - Ticker24h Ticker24h `json:"ticker24h"` -} - -type Ticker24h struct { - // The open price of the 24 hour period. - Open float64 `json:"open"` - - // The highest price for which a trade occurred in the 24 hour period. - High float64 `json:"high"` - - // The lowest price for which a trade occurred in the 24 hour period. - Low float64 `json:"low"` - - // The last price for which a trade occurred in the 24 hour period. - Last float64 `json:"last"` - - // The total volume of the 24 hour period in base currency. - Volume float64 `json:"volume"` - - // The total volume of the 24 hour period in quote currency. - VolumeQuote float64 `json:"volumeQuote"` - - // The best (highest) bid offer at the current moment. - Bid float64 `json:"bid"` - - // The size of the best (highest) bid offer. - BidSize float64 `json:"bidSize"` - - // The best (lowest) ask offer at the current moment. - Ask float64 `json:"ask"` - - // The size of the best (lowest) ask offer. - AskSize float64 `json:"askSize"` - - // Timestamp in unix milliseconds. - Timestamp int64 `json:"timestamp"` - - // Start timestamp in unix milliseconds. - StartTimestamp int64 `json:"startTimestamp"` - - // Open timestamp in unix milliseconds. - OpenTimestamp int64 `json:"openTimestamp"` - - // Close timestamp in unix milliseconds. - CloseTimestamp int64 `json:"closeTimestamp"` + Ticker24h bitv.Ticker24h `json:"ticker24h"` } func (t *Ticker24hEvent) UnmarshalJSON(bytes []byte) error { @@ -101,7 +58,7 @@ func (t *Ticker24hEvent) UnmarshalJSON(bytes []byte) error { t.Event = event t.Market = market - t.Ticker24h = Ticker24h{ + t.Ticker24h = bitv.Ticker24h{ Open: util.IfOrElse(len(open) > 0, func() float64 { return util.MustFloat64(open) }, zerof64), High: util.IfOrElse(len(high) > 0, func() float64 { return util.MustFloat64(high) }, zerof64), Low: util.IfOrElse(len(low) > 0, func() float64 { return util.MustFloat64(low) }, zerof64), diff --git a/wsc/trades.go b/wsc/trades.go index 40cee33..5504bec 100644 --- a/wsc/trades.go +++ b/wsc/trades.go @@ -3,6 +3,7 @@ package wsc import ( "fmt" + "github.com/larscom/go-bitvavo/v2/bitv" "github.com/larscom/go-bitvavo/v2/log" "github.com/goccy/go-json" @@ -18,25 +19,7 @@ type TradesEvent struct { Market string `json:"market"` // The trade containing the price, side etc. - Trade Trade `json:"trade"` -} - -type Trade struct { - // The trade ID of the returned trade (UUID). - Id string `json:"id"` - - // The amount in base currency for which the trade has been made. - Amount float64 `json:"amount"` - - // The price in quote currency for which the trade has been made. - Price float64 `json:"price"` - - // The side for the taker. - // Enum: "buy" | "sell" - Side string `json:"side"` - - // Timestamp in unix milliseconds. - Timestamp int64 `json:"timestamp"` + Trade bitv.Trade `json:"trade"` } func (t *TradesEvent) UnmarshalJSON(bytes []byte) error { @@ -59,7 +42,7 @@ func (t *TradesEvent) UnmarshalJSON(bytes []byte) error { t.Event = event t.Market = market - t.Trade = Trade{ + t.Trade = bitv.Trade{ Id: id, Amount: util.IfOrElse(len(amount) > 0, func() float64 { return util.MustFloat64(amount) }, zerof64), Price: util.IfOrElse(len(price) > 0, func() float64 { return util.MustFloat64(price) }, zerof64),