Skip to content

Commit

Permalink
Fix: book checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Jul 31, 2020
1 parent b39edb4 commit f346a3e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/trades/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
log.Print("----------------")
log.Printf("Price: %f", trade.Price)
log.Printf("Volume: %f", trade.Volume)
log.Printf("Time: %s", trade.Time.String())
log.Printf("Time: %f", trade.Time)
log.Printf("Pair: %s", trade.Pair)
log.Printf("Order type: %s", trade.OrderType)
log.Printf("Side: %s", trade.Side)
Expand Down
58 changes: 36 additions & 22 deletions websocket/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package websocket

import (
"fmt"
"strings"
)

type tickerFactory struct{}
Expand Down Expand Up @@ -135,33 +136,46 @@ func (f *bookFactory) Parse(data interface{}, pair string) (interface{}, error)
}

for k, v := range body {
items := make([]OrderBookItem, 0)
updates := v.([]interface{})
for _, item := range updates {
entity := item.([]interface{})
orderBookItem := OrderBookItem{
Price: valToFloat64(entity[0]),
Volume: valToFloat64(entity[1]),
Time: valToFloat64(entity[2]),
switch k {
case "c":
checkSum, ok := v.(string)
if !ok {
return nil, fmt.Errorf("[bookFactory] Invalid checkSum type: %v %T", v, v)
}
result.CheckSum = checkSum
default:
items, err := f.parseItems(v)
if err != nil {
return nil, err
}
result.IsSnapshot = len(k) == 2 && strings.HasSuffix(k, "s")
if strings.HasPrefix(k, "a") {
result.Asks = items
} else {
result.Bids = items
}
orderBookItem.Republish = (len(entity) == 4 && entity[3] == "r")
items = append(items, orderBookItem)
}
}
return result, nil
}

switch k {
case "as":
result.IsSnapshot = true
result.Asks = items
case "a":
result.Asks = items
case "bs":
result.IsSnapshot = true
result.Bids = items
case "b":
result.Bids = items
func (f *bookFactory) parseItems(value interface{}) ([]OrderBookItem, error) {
items := make([]OrderBookItem, 0)
updates, ok := value.([]interface{})
if !ok {
return nil, fmt.Errorf("[bookFactory] Invalid items type: %v %T", value, value)
}
for _, item := range updates {
entity := item.([]interface{})
orderBookItem := OrderBookItem{
Price: valToFloat64(entity[0]),
Volume: valToFloat64(entity[1]),
Time: valToFloat64(entity[2]),
}
orderBookItem.Republish = (len(entity) == 4 && entity[3] == "r")
items = append(items, orderBookItem)
}
return result, nil
return items, nil
}

type ownTradesFactory struct{}
Expand Down
1 change: 1 addition & 0 deletions websocket/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ type OrderBookUpdate struct {
Bids []OrderBookItem
IsSnapshot bool
Pair string
CheckSum string
}

// AuthDataRequest - data structure for private subscription request
Expand Down

0 comments on commit f346a3e

Please sign in to comment.