cosmos-sdk/examples/democoin/x/cool/types.go

107 lines
2.7 KiB
Go
Raw Normal View History

2018-03-03 09:30:08 -08:00
package cool
import (
"encoding/json"
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
2018-04-18 21:49:24 -07:00
// a really cool msg type, these fields are can be entirely arbitrary and
2018-03-03 09:30:08 -08:00
// custom to your message
2018-04-18 21:49:24 -07:00
type MsgSetTrend struct {
2018-07-06 00:06:53 -07:00
Sender sdk.AccAddress
2018-03-12 17:23:33 -07:00
Cool string
2018-03-03 09:30:08 -08:00
}
2018-04-18 21:49:24 -07:00
// genesis state - specify genesis trend
type Genesis struct {
2018-04-05 06:16:54 -07:00
Trend string `json:"trend"`
}
2018-04-18 21:49:24 -07:00
// new cool message
2018-07-06 00:06:53 -07:00
func NewMsgSetTrend(sender sdk.AccAddress, cool string) MsgSetTrend {
2018-04-18 21:49:24 -07:00
return MsgSetTrend{
2018-03-12 17:23:33 -07:00
Sender: sender,
Cool: cool,
2018-03-03 09:30:08 -08:00
}
}
// enforce the msg type at compile time
2018-04-18 21:49:24 -07:00
var _ sdk.Msg = MsgSetTrend{}
2018-03-03 09:30:08 -08:00
// nolint
2018-07-06 00:06:53 -07:00
func (msg MsgSetTrend) Type() string { return "cool" }
func (msg MsgSetTrend) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Sender} }
2018-04-18 21:49:24 -07:00
func (msg MsgSetTrend) String() string {
return fmt.Sprintf("MsgSetTrend{Sender: %v, Cool: %v}", msg.Sender, msg.Cool)
2018-03-03 09:30:08 -08:00
}
// Validate Basic is used to quickly disqualify obviously invalid messages quickly
2018-04-18 21:49:24 -07:00
func (msg MsgSetTrend) ValidateBasic() sdk.Error {
2018-03-03 09:30:08 -08:00
if len(msg.Sender) == 0 {
return sdk.ErrUnknownAddress(msg.Sender.String()).TraceSDK("")
2018-03-03 09:30:08 -08:00
}
2018-03-12 17:23:33 -07:00
if strings.Contains(msg.Cool, "hot") {
return sdk.ErrUnauthorized("").TraceSDK("hot is not cool")
2018-03-03 09:30:08 -08:00
}
2018-03-12 17:23:33 -07:00
if strings.Contains(msg.Cool, "warm") {
return sdk.ErrUnauthorized("").TraceSDK("warm is not very cool")
2018-03-03 09:30:08 -08:00
}
return nil
}
// Get the bytes for the message signer to sign on
2018-04-18 21:49:24 -07:00
func (msg MsgSetTrend) GetSignBytes() []byte {
2018-03-03 09:30:08 -08:00
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
2018-03-03 09:30:08 -08:00
}
//_______________________________________________________________________
2018-03-12 17:37:50 -07:00
// A message type to quiz how cool you are. these fields are can be entirely
// arbitrary and custom to your message
2018-04-18 21:49:24 -07:00
type MsgQuiz struct {
2018-07-06 00:06:53 -07:00
Sender sdk.AccAddress
2018-03-12 17:37:50 -07:00
CoolAnswer string
2018-03-03 09:30:08 -08:00
}
// New cool message
2018-07-06 00:06:53 -07:00
func NewMsgQuiz(sender sdk.AccAddress, coolerthancool string) MsgQuiz {
2018-04-18 21:49:24 -07:00
return MsgQuiz{
2018-03-12 17:37:50 -07:00
Sender: sender,
CoolAnswer: coolerthancool,
2018-03-03 09:30:08 -08:00
}
}
// enforce the msg type at compile time
2018-04-18 21:49:24 -07:00
var _ sdk.Msg = MsgQuiz{}
2018-03-03 09:30:08 -08:00
// nolint
2018-07-06 00:06:53 -07:00
func (msg MsgQuiz) Type() string { return "cool" }
func (msg MsgQuiz) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.Sender} }
2018-04-18 21:49:24 -07:00
func (msg MsgQuiz) String() string {
return fmt.Sprintf("MsgQuiz{Sender: %v, CoolAnswer: %v}", msg.Sender, msg.CoolAnswer)
2018-03-03 09:30:08 -08:00
}
// Validate Basic is used to quickly disqualify obviously invalid messages quickly
2018-04-18 21:49:24 -07:00
func (msg MsgQuiz) ValidateBasic() sdk.Error {
2018-03-03 09:30:08 -08:00
if len(msg.Sender) == 0 {
return sdk.ErrUnknownAddress(msg.Sender.String()).TraceSDK("")
2018-03-03 09:30:08 -08:00
}
return nil
}
// Get the bytes for the message signer to sign on
2018-04-18 21:49:24 -07:00
func (msg MsgQuiz) GetSignBytes() []byte {
2018-03-03 09:30:08 -08:00
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return sdk.MustSortJSON(b)
2018-03-03 09:30:08 -08:00
}