-
Notifications
You must be signed in to change notification settings - Fork 4
/
price.go
48 lines (42 loc) · 1.04 KB
/
price.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
package dirtoracle
import (
"fmt"
"github.com/fox-one/pando/pkg/mtg"
"github.com/fox-one/pando/pkg/uuid"
"github.com/shopspring/decimal"
)
type (
PriceData struct {
Timestamp int64 `json:"t,omitempty"`
AssetID string `json:"a,omitempty"`
Price decimal.Decimal `json:"p,omitempty"`
Signature *CosiSignature `json:"s,omitempty"`
}
)
func (p PriceData) Payload() []byte {
return []byte(fmt.Sprintf("%d:%s:%v", p.Timestamp, p.AssetID, p.Price))
}
func (p *PriceData) MarshalBinary() (data []byte, err error) {
asset, err := uuid.FromString(p.AssetID)
if err != nil {
return nil, err
}
return mtg.Encode(p.Timestamp, asset, p.Price, p.Signature)
}
func (p *PriceData) UnmarshalBinary(data []byte) error {
var (
timestamp int64
price decimal.Decimal
signature CosiSignature
asset uuid.UUID
)
_, err := mtg.Scan(data, ×tamp, &asset, &price, &signature)
if err != nil {
return err
}
p.Timestamp = timestamp
p.AssetID = asset.String()
p.Price = price
p.Signature = &signature
return nil
}