-
Notifications
You must be signed in to change notification settings - Fork 8
/
param_change.go
81 lines (67 loc) · 2.68 KB
/
param_change.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
package utils
import (
"encoding/json"
"io/ioutil"
govtypes "github.com/celer-network/sgn/x/gov/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)
type (
// ParamChangesJSON defines a slice of ParamChangeJSON objects which can be
// converted to a slice of ParamChange objects.
ParamChangesJSON []ParamChangeJSON
// ParamChangeJSON defines a parameter change used in JSON input. This
// allows values to be specified in raw JSON instead of being string encoded.
ParamChangeJSON struct {
Subspace string `json:"subspace" yaml:"subspace"`
Key string `json:"key" yaml:"key"`
Value json.RawMessage `json:"value" yaml:"value"`
}
// ParamChangeProposalJSON defines a ParameterProposal with a deposit used
// to parse parameter change proposals from a JSON file.
ParamChangeProposalJSON struct {
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Changes ParamChangesJSON `json:"changes" yaml:"changes"`
Deposit sdk.Int `json:"deposit" yaml:"deposit"`
}
// ParamChangeProposalReq defines a parameter change proposal request body.
ParamChangeProposalReq struct {
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
Title string `json:"title" yaml:"title"`
Description string `json:"description" yaml:"description"`
Changes ParamChangesJSON `json:"changes" yaml:"changes"`
Proposer sdk.AccAddress `json:"proposer" yaml:"proposer"`
Deposit sdk.Int `json:"deposit" yaml:"deposit"`
}
)
func NewParamChangeJSON(subspace, key string, value json.RawMessage) ParamChangeJSON {
return ParamChangeJSON{subspace, key, value}
}
// ToParamChange converts a ParamChangeJSON object to ParamChange.
func (pcj ParamChangeJSON) ToParamChange() govtypes.ParamChange {
return govtypes.NewParamChange(pcj.Subspace, pcj.Key, string(pcj.Value))
}
// ToParamChanges converts a slice of ParamChangeJSON objects to a slice of
// ParamChange.
func (pcj ParamChangesJSON) ToParamChanges() []govtypes.ParamChange {
res := make([]govtypes.ParamChange, len(pcj))
for i, pc := range pcj {
res[i] = pc.ToParamChange()
}
return res
}
// ParseParamChangeProposalJSON reads and parses a ParamChangeProposalJSON from
// file.
func ParseParamChangeProposalJSON(cdc *codec.Codec, proposalFile string) (ParamChangeProposalJSON, error) {
proposal := ParamChangeProposalJSON{}
contents, err := ioutil.ReadFile(proposalFile)
if err != nil {
return proposal, err
}
if err := cdc.UnmarshalJSON(contents, &proposal); err != nil {
return proposal, err
}
return proposal, nil
}