forked from jyap808/go-poloniex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
58 lines (51 loc) · 1.24 KB
/
order.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package poloniex
import (
"encoding/json"
"errors"
"strconv"
)
// OrderBook is a list of open orders for a currency
type OrderBook struct {
Asks []*Order `json:"asks"`
Bids []*Order `json:"bids"`
IsFrozen int `json:"isFrozen,string"`
Error string `json:"error"`
}
// Order is an order in an OrderBook
type Order struct {
Rate float64
Amount float64
}
// UnmarshalJSON parses the order as poloniex gives it
func (o *Order) UnmarshalJSON(data []byte) error {
vals := []interface{}{}
if err := json.Unmarshal(data, &vals); err != nil {
return err
}
if len(vals) != 2 {
return errors.New("invalid order data")
}
if rateStr, ok := vals[0].(string); ok {
rate, err := strconv.ParseFloat(rateStr, 64)
if err != nil {
return err
}
o.Rate = rate
} else {
return errors.New("invalid rate value")
}
if amount, ok := vals[1].(float64); ok {
o.Amount = amount
} else {
return errors.New("invalid amount value")
}
return nil
}
// OpenOrder is an open order for the account
type OpenOrder struct {
OrderNumber int64 `json:"orderNumber,string"`
Type string `json:"type"`
Rate float64 `json:"rate,string"`
Amount float64 `json:"amount,string"`
Total float64 `json:"total,string"`
}