/
utils.go
76 lines (64 loc) · 2.65 KB
/
utils.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
package utils
import (
"encoding/json"
"io/ioutil"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/params"
)
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 ParameterChangeProposal 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.SysCoins `json:"deposit" yaml:"deposit"`
Height uint64 `json:"height" yaml:"height"`
}
// 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.SysCoins `json:"deposit" yaml:"deposit"`
Height uint64 `json:"height" yaml:"height"`
}
)
// ToParamChange converts a ParamChangeJSON object to ParamChange
func (pcj ParamChangeJSON) ToParamChange() params.ParamChange {
return params.NewParamChange(pcj.Subspace, pcj.Key, string(pcj.Value))
}
// ToParamChanges converts a slice of ParamChangeJSON objects to a slice of ParamChange
func (pcj ParamChangesJSON) ToParamChanges() []params.ParamChange {
res := make([]params.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) {
var 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
}