/
price_level.go
62 lines (50 loc) · 1.26 KB
/
price_level.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
59
60
61
62
package orderbook
import (
"encoding/json"
"fmt"
"github.com/shopspring/decimal"
)
var _ json.Marshaler = (*PriceLevel)(nil)
var _ json.Unmarshaler = (*PriceLevel)(nil)
// PriceLevel takes a count of how many assets have that price.
type PriceLevel struct {
price decimal.Decimal
amount decimal.Decimal
}
// NewPriceLevel creates a new price level.
func NewPriceLevel(price, amount decimal.Decimal) *PriceLevel {
return &PriceLevel{price, amount}
}
// Price returns the price.
func (p *PriceLevel) Price() decimal.Decimal {
return p.price
}
// Amount returns the amount.
func (p *PriceLevel) Amount() decimal.Decimal {
return p.amount
}
// MarshalJSON implements json.Marshaler.
func (p *PriceLevel) MarshalJSON() ([]byte, error) {
return json.Marshal(
&struct {
Amount decimal.Decimal `json:"amount"`
Price decimal.Decimal `json:"price"`
}{
p.amount,
p.price,
},
)
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *PriceLevel) UnmarshalJSON(data []byte) error {
obj := struct {
Amount decimal.Decimal `json:"amount"`
Price decimal.Decimal `json:"price"`
}{}
if err := json.Unmarshal(data, &obj); err != nil {
return fmt.Errorf("PriceLevel.Unmarshal(%s): %w", data, err)
}
p.amount = obj.Amount
p.price = obj.Price
return nil
}