github.com/Finschia/finschia-sdk@v0.48.1/x/params/types/proposal/proposal.go (about)

     1  package proposal
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	yaml "gopkg.in/yaml.v2"
     8  
     9  	govtypes "github.com/Finschia/finschia-sdk/x/gov/types"
    10  )
    11  
    12  const (
    13  	// ProposalTypeChange defines the type for a ParameterChangeProposal
    14  	ProposalTypeChange = "ParameterChange"
    15  )
    16  
    17  // Assert ParameterChangeProposal implements govtypes.Content at compile-time
    18  var _ govtypes.Content = &ParameterChangeProposal{}
    19  
    20  func init() {
    21  	govtypes.RegisterProposalType(ProposalTypeChange)
    22  }
    23  
    24  func NewParameterChangeProposal(title, description string, changes []ParamChange) *ParameterChangeProposal {
    25  	return &ParameterChangeProposal{title, description, changes}
    26  }
    27  
    28  // GetTitle returns the title of a parameter change proposal.
    29  func (pcp *ParameterChangeProposal) GetTitle() string { return pcp.Title }
    30  
    31  // GetDescription returns the description of a parameter change proposal.
    32  func (pcp *ParameterChangeProposal) GetDescription() string { return pcp.Description }
    33  
    34  // ProposalRoute returns the routing key of a parameter change proposal.
    35  func (pcp *ParameterChangeProposal) ProposalRoute() string { return RouterKey }
    36  
    37  // ProposalType returns the type of a parameter change proposal.
    38  func (pcp *ParameterChangeProposal) ProposalType() string { return ProposalTypeChange }
    39  
    40  // ValidateBasic validates the parameter change proposal
    41  func (pcp *ParameterChangeProposal) ValidateBasic() error {
    42  	err := govtypes.ValidateAbstract(pcp)
    43  	if err != nil {
    44  		return err
    45  	}
    46  
    47  	return ValidateChanges(pcp.Changes)
    48  }
    49  
    50  // String implements the Stringer interface.
    51  func (pcp ParameterChangeProposal) String() string {
    52  	var b strings.Builder
    53  
    54  	b.WriteString(fmt.Sprintf(`Parameter Change Proposal:
    55    Title:       %s
    56    Description: %s
    57    Changes:
    58  `, pcp.Title, pcp.Description))
    59  
    60  	for _, pc := range pcp.Changes {
    61  		b.WriteString(fmt.Sprintf(`    Param Change:
    62        Subspace: %s
    63        Key:      %s
    64        Value:    %X
    65  `, pc.Subspace, pc.Key, pc.Value))
    66  	}
    67  
    68  	return b.String()
    69  }
    70  
    71  func NewParamChange(subspace, key, value string) ParamChange {
    72  	return ParamChange{subspace, key, value}
    73  }
    74  
    75  // String implements the Stringer interface.
    76  func (pc ParamChange) String() string {
    77  	out, _ := yaml.Marshal(pc)
    78  	return string(out)
    79  }
    80  
    81  // ValidateChanges performs basic validation checks over a set of ParamChange. It
    82  // returns an error if any ParamChange is invalid.
    83  func ValidateChanges(changes []ParamChange) error {
    84  	if len(changes) == 0 {
    85  		return ErrEmptyChanges
    86  	}
    87  
    88  	for _, pc := range changes {
    89  		if len(pc.Subspace) == 0 {
    90  			return ErrEmptySubspace
    91  		}
    92  		if len(pc.Key) == 0 {
    93  			return ErrEmptyKey
    94  		}
    95  		if len(pc.Value) == 0 {
    96  			return ErrEmptyValue
    97  		}
    98  	}
    99  
   100  	return nil
   101  }