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

     1  package types
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/ethereum/go-ethereum/common"
     7  
     8  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    10  	"github.com/fibonacci-chain/fbc/libs/tendermint/global"
    11  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    12  )
    13  
    14  var (
    15  	_ sdk.Msg = &MsgRegisterFeeSplit{}
    16  	_ sdk.Msg = &MsgCancelFeeSplit{}
    17  	_ sdk.Msg = &MsgUpdateFeeSplit{}
    18  )
    19  
    20  const (
    21  	TypeMsgRegisterFeeSplit = "register_fee_split"
    22  	TypeMsgCancelFeeSplit   = "cancel_fee_split"
    23  	TypeMsgUpdateFeeSplit   = "update_fee_split"
    24  )
    25  
    26  // MsgRegisterFeeSplit defines a message that registers a FeeSplit
    27  type MsgRegisterFeeSplit struct {
    28  	// contract hex address
    29  	ContractAddress string `json:"contract_address,omitempty"`
    30  	// bech32 address of message sender, must be the same as the origin EOA
    31  	// sending the transaction which deploys the contract
    32  	DeployerAddress string `json:"deployer_address,omitempty"`
    33  	// bech32 address of account receiving the transaction fees
    34  	WithdrawerAddress string `json:"withdrawer_address,omitempty"`
    35  	// array of nonces from the address path, where the last nonce is the nonce
    36  	// that determines the contract's address - it can be an EOA nonce or a
    37  	// factory contract nonce
    38  	Nonces []uint64 `json:"nonces,omitempty"`
    39  }
    40  
    41  // NewMsgRegisterFeeSplit creates new instance of MsgRegisterFeeSplit
    42  func NewMsgRegisterFeeSplit(
    43  	contract common.Address,
    44  	deployer,
    45  	withdrawer sdk.AccAddress,
    46  	nonces []uint64,
    47  ) MsgRegisterFeeSplit {
    48  	withdrawerAddress := ""
    49  	if withdrawer != nil {
    50  		withdrawerAddress = withdrawer.String()
    51  	}
    52  
    53  	return MsgRegisterFeeSplit{
    54  		ContractAddress:   contract.String(),
    55  		DeployerAddress:   deployer.String(),
    56  		WithdrawerAddress: withdrawerAddress,
    57  		Nonces:            nonces,
    58  	}
    59  }
    60  
    61  // Route returns the name of the module
    62  func (msg MsgRegisterFeeSplit) Route() string { return RouterKey }
    63  
    64  // Type returns the the action
    65  func (msg MsgRegisterFeeSplit) Type() string { return TypeMsgRegisterFeeSplit }
    66  
    67  // ValidateBasic runs stateless checks on the message
    68  func (msg MsgRegisterFeeSplit) ValidateBasic() error {
    69  	if global.GetGlobalHeight() > 0 && !tmtypes.HigherThanVenus3(global.GetGlobalHeight()) {
    70  		return ErrNotFeesplitHeight
    71  	}
    72  
    73  	if _, err := sdk.AccAddressFromBech32(msg.DeployerAddress); err != nil {
    74  		return sdkerrors.Wrapf(err, "invalid deployer address %s", msg.DeployerAddress)
    75  	}
    76  
    77  	if err := ValidateNonZeroAddress(msg.ContractAddress); err != nil {
    78  		return sdkerrors.Wrapf(err, "invalid contract address %s", msg.ContractAddress)
    79  	}
    80  
    81  	if msg.WithdrawerAddress != "" {
    82  		if _, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress); err != nil {
    83  			return sdkerrors.Wrapf(err, "invalid withdraw address %s", msg.WithdrawerAddress)
    84  		}
    85  	}
    86  
    87  	if len(msg.Nonces) < 1 {
    88  		return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid nonces - empty array")
    89  	}
    90  
    91  	if len(msg.Nonces) > 20 {
    92  		return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid nonces - array length must be less than 20")
    93  	}
    94  
    95  	return nil
    96  }
    97  
    98  // GetSignBytes encodes the message for signing
    99  func (msg MsgRegisterFeeSplit) GetSignBytes() []byte {
   100  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
   101  }
   102  
   103  // GetSigners defines whose signature is required
   104  func (msg MsgRegisterFeeSplit) GetSigners() []sdk.AccAddress {
   105  	from := sdk.MustAccAddressFromBech32(msg.DeployerAddress)
   106  	return []sdk.AccAddress{from}
   107  }
   108  
   109  // MsgUpdateFeeSplit defines a message that updates the withdrawer address for a
   110  // registered FeeSplit
   111  type MsgUpdateFeeSplit struct {
   112  	// contract hex address
   113  	ContractAddress string `json:"contract_address,omitempty"`
   114  	// deployer bech32 address
   115  	DeployerAddress string `json:"deployer_address,omitempty"`
   116  	// new withdrawer bech32 address for receiving the transaction fees
   117  	WithdrawerAddress string `json:"withdrawer_address,omitempty"`
   118  }
   119  
   120  // NewMsgUpdateFeeSplit creates new instance of MsgUpdateFeeSplit
   121  func NewMsgUpdateFeeSplit(
   122  	contract common.Address,
   123  	deployer,
   124  	withdraw sdk.AccAddress,
   125  ) MsgUpdateFeeSplit {
   126  	return MsgUpdateFeeSplit{
   127  		ContractAddress:   contract.String(),
   128  		DeployerAddress:   deployer.String(),
   129  		WithdrawerAddress: withdraw.String(),
   130  	}
   131  }
   132  
   133  // Route returns the name of the module
   134  func (msg MsgUpdateFeeSplit) Route() string { return RouterKey }
   135  
   136  // Type returns the the action
   137  func (msg MsgUpdateFeeSplit) Type() string { return TypeMsgUpdateFeeSplit }
   138  
   139  // ValidateBasic runs stateless checks on the message
   140  func (msg MsgUpdateFeeSplit) ValidateBasic() error {
   141  	if global.GetGlobalHeight() > 0 && !tmtypes.HigherThanVenus3(global.GetGlobalHeight()) {
   142  		return ErrNotFeesplitHeight
   143  	}
   144  
   145  	if _, err := sdk.AccAddressFromBech32(msg.DeployerAddress); err != nil {
   146  		return sdkerrors.Wrapf(err, "invalid deployer address %s", msg.DeployerAddress)
   147  	}
   148  
   149  	if err := ValidateNonZeroAddress(msg.ContractAddress); err != nil {
   150  		return sdkerrors.Wrapf(err, "invalid contract address %s", msg.ContractAddress)
   151  	}
   152  
   153  	if _, err := sdk.AccAddressFromBech32(msg.WithdrawerAddress); err != nil {
   154  		return sdkerrors.Wrapf(err, "invalid withdraw address %s", msg.WithdrawerAddress)
   155  	}
   156  
   157  	return nil
   158  }
   159  
   160  // GetSignBytes encodes the message for signing
   161  func (msg MsgUpdateFeeSplit) GetSignBytes() []byte {
   162  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
   163  }
   164  
   165  // GetSigners defines whose signature is required
   166  func (msg MsgUpdateFeeSplit) GetSigners() []sdk.AccAddress {
   167  	from := sdk.MustAccAddressFromBech32(msg.DeployerAddress)
   168  	return []sdk.AccAddress{from}
   169  }
   170  
   171  // MsgCancelFeeSplit defines a message that cancels a registered FeeSplit
   172  type MsgCancelFeeSplit struct {
   173  	// contract hex address
   174  	ContractAddress string `json:"contract_address,omitempty"`
   175  	// deployer bech32 address
   176  	DeployerAddress string `json:"deployer_address,omitempty"`
   177  }
   178  
   179  // NewMsgCancelFeeSplit creates new instance of MsgCancelFeeSplit.
   180  func NewMsgCancelFeeSplit(
   181  	contract common.Address,
   182  	deployer sdk.AccAddress,
   183  ) MsgCancelFeeSplit {
   184  	return MsgCancelFeeSplit{
   185  		ContractAddress: contract.String(),
   186  		DeployerAddress: deployer.String(),
   187  	}
   188  }
   189  
   190  // Route returns the message route for a MsgCancelFeeSplit.
   191  func (msg MsgCancelFeeSplit) Route() string { return RouterKey }
   192  
   193  // Type returns the message type for a MsgCancelFeeSplit.
   194  func (msg MsgCancelFeeSplit) Type() string { return TypeMsgCancelFeeSplit }
   195  
   196  // ValidateBasic runs stateless checks on the message
   197  func (msg MsgCancelFeeSplit) ValidateBasic() error {
   198  	if global.GetGlobalHeight() > 0 && !tmtypes.HigherThanVenus3(global.GetGlobalHeight()) {
   199  		return ErrNotFeesplitHeight
   200  	}
   201  
   202  	if _, err := sdk.AccAddressFromBech32(msg.DeployerAddress); err != nil {
   203  		return sdkerrors.Wrapf(err, "invalid deployer address %s", msg.DeployerAddress)
   204  	}
   205  
   206  	if err := ValidateNonZeroAddress(msg.ContractAddress); err != nil {
   207  		return sdkerrors.Wrapf(err, "invalid contract address %s", msg.ContractAddress)
   208  	}
   209  
   210  	return nil
   211  }
   212  
   213  // GetSignBytes encodes the message for signing
   214  func (msg MsgCancelFeeSplit) GetSignBytes() []byte {
   215  	return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
   216  }
   217  
   218  // GetSigners defines whose signature is required
   219  func (msg MsgCancelFeeSplit) GetSigners() []sdk.AccAddress {
   220  	funder := sdk.MustAccAddressFromBech32(msg.DeployerAddress)
   221  	return []sdk.AccAddress{funder}
   222  }
   223  
   224  // isZeroAddress returns true if the address corresponds to an empty ethereum hex address.
   225  func isZeroAddress(address string) bool {
   226  	return bytes.Equal(common.HexToAddress(address).Bytes(), common.Address{}.Bytes())
   227  }
   228  
   229  // validateAddress returns an error if the provided string is either not a hex formatted string address
   230  func validateAddress(address string) error {
   231  	if !common.IsHexAddress(address) {
   232  		return sdkerrors.Wrapf(
   233  			sdkerrors.ErrInvalidAddress, "address '%s' is not a valid ethereum hex address",
   234  			address,
   235  		)
   236  	}
   237  	return nil
   238  }
   239  
   240  // ValidateNonZeroAddress returns an error if the provided string is not a hex
   241  // formatted string address or is equal to zero
   242  func ValidateNonZeroAddress(address string) error {
   243  	if isZeroAddress(address) {
   244  		return sdkerrors.Wrapf(
   245  			sdkerrors.ErrInvalidAddress, "address '%s' must not be zero",
   246  			address,
   247  		)
   248  	}
   249  	return validateAddress(address)
   250  }