2019-04-30 09:31:38 -07:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2019-05-02 11:21:35 -07:00
|
|
|
"encoding/json"
|
2019-04-30 09:31:38 -07:00
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
2020-02-19 03:52:20 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params/types/proposal"
|
2019-04-30 09:31:38 -07:00
|
|
|
)
|
|
|
|
|
2019-05-02 11:21:35 -07:00
|
|
|
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 {
|
2019-07-05 16:25:56 -07:00
|
|
|
Subspace string `json:"subspace" yaml:"subspace"`
|
|
|
|
Key string `json:"key" yaml:"key"`
|
|
|
|
Value json.RawMessage `json:"value" yaml:"value"`
|
2019-05-02 11:21:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParamChangeProposalJSON defines a ParameterChangeProposal with a deposit used
|
|
|
|
// to parse parameter change proposals from a JSON file.
|
|
|
|
ParamChangeProposalJSON struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
Title string `json:"title" yaml:"title"`
|
|
|
|
Description string `json:"description" yaml:"description"`
|
|
|
|
Changes ParamChangesJSON `json:"changes" yaml:"changes"`
|
2020-03-11 09:45:02 -07:00
|
|
|
Deposit string `json:"deposit" yaml:"deposit"`
|
2019-05-02 11:21:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParamChangeProposalReq defines a parameter change proposal request body.
|
|
|
|
ParamChangeProposalReq struct {
|
2019-07-05 16:25:56 -07:00
|
|
|
BaseReq rest.BaseReq `json:"base_req" yaml:"base_req"`
|
2019-05-02 11:21:35 -07:00
|
|
|
|
2019-07-05 16:25:56 -07:00
|
|
|
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.Coins `json:"deposit" yaml:"deposit"`
|
2019-05-02 11:21:35 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-12-10 08:48:57 -08:00
|
|
|
func NewParamChangeJSON(subspace, key string, value json.RawMessage) ParamChangeJSON {
|
|
|
|
return ParamChangeJSON{subspace, key, value}
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
2019-05-02 11:21:35 -07:00
|
|
|
// ToParamChange converts a ParamChangeJSON object to ParamChange.
|
2020-02-19 03:52:20 -08:00
|
|
|
func (pcj ParamChangeJSON) ToParamChange() proposal.ParamChange {
|
|
|
|
return proposal.NewParamChange(pcj.Subspace, pcj.Key, string(pcj.Value))
|
2019-05-02 11:21:35 -07:00
|
|
|
}
|
2019-04-30 09:31:38 -07:00
|
|
|
|
2019-05-02 11:21:35 -07:00
|
|
|
// ToParamChanges converts a slice of ParamChangeJSON objects to a slice of
|
|
|
|
// ParamChange.
|
2020-02-19 03:52:20 -08:00
|
|
|
func (pcj ParamChangesJSON) ToParamChanges() []proposal.ParamChange {
|
|
|
|
res := make([]proposal.ParamChange, len(pcj))
|
2019-05-02 11:21:35 -07:00
|
|
|
for i, pc := range pcj {
|
|
|
|
res[i] = pc.ToParamChange()
|
|
|
|
}
|
|
|
|
return res
|
2019-04-30 09:31:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseParamChangeProposalJSON reads and parses a ParamChangeProposalJSON from
|
|
|
|
// file.
|
2020-04-09 07:23:17 -07:00
|
|
|
func ParseParamChangeProposalJSON(cdc codec.JSONMarshaler, proposalFile string) (ParamChangeProposalJSON, error) {
|
2019-04-30 09:31:38 -07:00
|
|
|
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
|
|
|
|
}
|