github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/feesplit/types/proposal.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     8  	"github.com/fibonacci-chain/fbc/libs/tendermint/global"
     9  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    10  	govtypes "github.com/fibonacci-chain/fbc/x/gov/types"
    11  )
    12  
    13  const (
    14  	// proposalTypeFeeSplitShares defines the type for a FeeSplitProposalShares
    15  	proposalTypeFeeSplitShares = "FeeSplit"
    16  )
    17  
    18  func init() {
    19  	govtypes.RegisterProposalType(proposalTypeFeeSplitShares)
    20  	govtypes.RegisterProposalTypeCodec(FeeSplitSharesProposal{}, "fbexchain/feesplit/SharesProposal")
    21  }
    22  
    23  var (
    24  	_ govtypes.Content = (*FeeSplitSharesProposal)(nil)
    25  )
    26  
    27  type FeeSplitSharesProposal struct {
    28  	Title       string   `json:"title" yaml:"title"`
    29  	Description string   `json:"description" yaml:"description"`
    30  	Shares      []Shares `json:"shares" yaml:"shares"`
    31  }
    32  
    33  type Shares struct {
    34  	ContractAddr string  `json:"contract_addr" yaml:"contract_addr"`
    35  	Share        sdk.Dec `json:"share" yaml:"share"`
    36  }
    37  
    38  func NewFeeSplitSharesProposal(title, description string, shares []Shares) FeeSplitSharesProposal {
    39  	return FeeSplitSharesProposal{title, description, shares}
    40  }
    41  
    42  func (fp FeeSplitSharesProposal) GetTitle() string       { return fp.Title }
    43  func (fp FeeSplitSharesProposal) GetDescription() string { return fp.Description }
    44  func (fp FeeSplitSharesProposal) ProposalRoute() string  { return RouterKey }
    45  func (fp FeeSplitSharesProposal) ProposalType() string   { return proposalTypeFeeSplitShares }
    46  func (fp FeeSplitSharesProposal) ValidateBasic() sdk.Error {
    47  	if global.GetGlobalHeight() > 0 && !tmtypes.HigherThanVenus3(global.GetGlobalHeight()) {
    48  		return ErrNotFeesplitHeight
    49  	}
    50  
    51  	if len(strings.TrimSpace(fp.Title)) == 0 {
    52  		return govtypes.ErrInvalidProposalContent("title is required")
    53  	}
    54  	if len(fp.Title) > govtypes.MaxTitleLength {
    55  		return govtypes.ErrInvalidProposalContent("title length is longer than the max")
    56  	}
    57  
    58  	if len(fp.Description) == 0 {
    59  		return govtypes.ErrInvalidProposalContent("description is required")
    60  	}
    61  
    62  	if len(fp.Description) > govtypes.MaxDescriptionLength {
    63  		return govtypes.ErrInvalidProposalContent("description length is longer than the max")
    64  	}
    65  
    66  	if fp.ProposalType() != proposalTypeFeeSplitShares {
    67  		return govtypes.ErrInvalidProposalType(fp.ProposalType())
    68  	}
    69  
    70  	if len(fp.Shares) == 0 {
    71  		return govtypes.ErrInvalidProposalContent("fee split shares is required")
    72  	}
    73  
    74  	for _, share := range fp.Shares {
    75  		if err := ValidateNonZeroAddress(share.ContractAddr); err != nil {
    76  			return govtypes.ErrInvalidProposalContent("invalid contract address")
    77  		}
    78  
    79  		if share.Share.IsNil() || share.Share.IsNegative() || share.Share.GT(sdk.OneDec()) {
    80  			return govtypes.ErrInvalidProposalContent("invalid share: nil, negative, greater than 1")
    81  		}
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  func (fp FeeSplitSharesProposal) String() string {
    88  	var b strings.Builder
    89  
    90  	b.WriteString(fmt.Sprintf(`Fee Split Shares Proposal:
    91    Title:       %s
    92    Description: %s
    93    Shares:      
    94  `, fp.Title, fp.Description))
    95  
    96  	for _, share := range fp.Shares {
    97  		b.WriteString("\t\t\t\t\t\t")
    98  		b.WriteString(fmt.Sprintf("%s: %s", share.ContractAddr, share.Share))
    99  		b.Write([]byte{'\n'})
   100  	}
   101  
   102  	return b.String()
   103  }