forked from scorum/bitshares-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
price.go
53 lines (44 loc) · 1.21 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
49
50
51
52
53
package types
import (
"encoding/json"
"github.com/youthonline/bitshares-go/encoding/transaction"
"strconv"
)
type Price struct {
Base AssetAmount `json:"base"`
Quote AssetAmount `json:"quote"`
}
type AssetAmount struct {
Amount uint64 `json:"amount"`
AssetID ObjectID `json:"asset_id"`
}
func (aa AssetAmount) MarshalTransaction(encoder *transaction.Encoder) error {
enc := transaction.NewRollingEncoder(encoder)
enc.EncodeLittleEndianUInt64(aa.Amount)
enc.Encode(aa.AssetID)
return enc.Err()
}
// RPC client might return asset amount as uint64 or string,
// therefore a custom unmarshaller is used
func (aa *AssetAmount) UnmarshalJSON(b []byte) (err error) {
stringCase := struct {
Amount string `json:"amount"`
AssetID ObjectID `json:"asset_id"`
}{}
uint64Case := struct {
Amount uint64 `json:"amount"`
AssetID ObjectID `json:"asset_id"`
}{}
if err = json.Unmarshal(b, &uint64Case); err == nil {
aa.AssetID = uint64Case.AssetID
aa.Amount = uint64Case.Amount
return nil
}
// failed on uint64, try string
if err = json.Unmarshal(b, &stringCase); err == nil {
aa.AssetID = stringCase.AssetID
aa.Amount, err = strconv.ParseUint(stringCase.Amount, 10, 64)
return err
}
return err
}