-
Notifications
You must be signed in to change notification settings - Fork 556
/
marshal.go
63 lines (53 loc) · 1.75 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
package balancer
import (
"encoding/json"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type balancerPoolPretty 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, err := pa.MarshalJSON()
if err != nil {
panic(err)
}
return string(out)
}
// 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(balancerPoolPretty{
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 balancerPoolPretty
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
}