-
Notifications
You must be signed in to change notification settings - Fork 568
/
marshal.go
118 lines (99 loc) · 3.18 KB
/
marshal.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package types
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
"gopkg.in/yaml.v2"
)
type poolAssetPretty struct {
Token sdk.Coin `json:"token" yaml:"token"`
Weight sdk.Dec `json:"weight" yaml:"weight"`
}
func (asset PoolAsset) prettify() poolAssetPretty {
return poolAssetPretty{
Weight: sdk.NewDecFromInt(asset.Weight).QuoInt64(GuaranteedWeightPrecision),
Token: asset.Token,
}
}
// D: at name
// func (asset poolAssetPretty) uglify() PoolAsset {
// return PoolAsset{
// Weight: asset.Weight.MulInt64(GuaranteedWeightPrecision).RoundInt(),
// Token: asset.Token,
// }
// }
// MarshalYAML returns the YAML representation of a PoolAsset.
// This is assumed to not be called on a stand-alone instance, so it removes the first marshalled line.
func (pa PoolAsset) MarshalYAML() (interface{}, error) {
bz, err := yaml.Marshal(pa.prettify())
if err != nil {
return nil, err
}
s := string(bz)
return s, nil
}
type poolPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
Id uint64 `json:"id" yaml:"id"`
PoolParams PoolParams `json:"pool_params" yaml:"pool_params"`
FuturePoolGovernor string `json:"future_pool_governor" yaml:"future_pool_governor"`
TotalWeight sdk.Dec `json:"total_weight" yaml:"total_weight"`
TotalShares sdk.Coin `json:"total_shares" yaml:"total_shares"`
PoolAssets []PoolAsset `json:"pool_assets" yaml:"pool_assets"`
}
func (pa Pool) String() string {
out, _ := pa.MarshalYAML()
return out.(string)
}
// MarshalYAML returns the YAML representation of a Pool.
func (pa Pool) MarshalYAML() (interface{}, error) {
accAddr, err := sdk.AccAddressFromBech32(pa.Address)
if err != nil {
return nil, err
}
decTotalWeight := sdk.NewDecFromInt(pa.TotalWeight).QuoInt64(GuaranteedWeightPrecision)
bz, err := yaml.Marshal(poolPretty{
Address: accAddr,
Id: pa.Id,
PoolParams: pa.PoolParams,
FuturePoolGovernor: pa.FuturePoolGovernor,
TotalWeight: decTotalWeight,
TotalShares: pa.TotalShares,
PoolAssets: pa.PoolAssets,
})
if err != nil {
return nil, err
}
return string(bz), nil
}
// MarshalJSON returns the JSON representation of a Pool.
func (pa Pool) MarshalJSON() ([]byte, error) {
accAddr, err := sdk.AccAddressFromBech32(pa.Address)
if err != nil {
return nil, err
}
decTotalWeight := sdk.NewDecFromInt(pa.TotalWeight)
return json.Marshal(poolPretty{
Address: accAddr,
Id: pa.Id,
PoolParams: pa.PoolParams,
FuturePoolGovernor: pa.FuturePoolGovernor,
TotalWeight: decTotalWeight,
TotalShares: pa.TotalShares,
PoolAssets: pa.PoolAssets,
})
}
// UnmarshalJSON unmarshals raw JSON bytes into a Pool.
func (pa *Pool) UnmarshalJSON(bz []byte) error {
var alias poolPretty
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
pa.Address = alias.Address.String()
pa.Id = alias.Id
pa.PoolParams = alias.PoolParams
pa.FuturePoolGovernor = alias.FuturePoolGovernor
pa.TotalWeight = alias.TotalWeight.RoundInt()
pa.TotalShares = alias.TotalShares
pa.PoolAssets = alias.PoolAssets
return nil
}