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-03-12 17:23:33 -07:00
|
|
|
Sender sdk.Address
|
|
|
|
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
|
|
|
|
func NewMsgSetTrend(sender sdk.Address, cool string) MsgSetTrend {
|
|
|
|
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-04-18 21:49:24 -07:00
|
|
|
func (msg MsgSetTrend) Type() string { return "cool" }
|
|
|
|
func (msg MsgSetTrend) Get(key interface{}) (value interface{}) { return nil }
|
|
|
|
func (msg MsgSetTrend) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
|
|
|
|
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 {
|
2018-03-17 19:42:54 -07:00
|
|
|
return sdk.ErrUnknownAddress(msg.Sender.String()).Trace("")
|
2018-03-03 09:30:08 -08:00
|
|
|
}
|
2018-03-12 17:23:33 -07:00
|
|
|
if strings.Contains(msg.Cool, "hot") {
|
2018-03-03 09:30:08 -08:00
|
|
|
return sdk.ErrUnauthorized("").Trace("hot is not cool")
|
|
|
|
}
|
2018-03-12 17:23:33 -07:00
|
|
|
if strings.Contains(msg.Cool, "warm") {
|
2018-03-03 09:30:08 -08:00
|
|
|
return sdk.ErrUnauthorized("").Trace("warm is not very cool")
|
|
|
|
}
|
|
|
|
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 b
|
|
|
|
}
|
|
|
|
|
|
|
|
//_______________________________________________________________________
|
|
|
|
|
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-03-12 17:37:50 -07:00
|
|
|
Sender sdk.Address
|
|
|
|
CoolAnswer string
|
2018-03-03 09:30:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New cool message
|
2018-04-18 21:49:24 -07:00
|
|
|
func NewMsgQuiz(sender sdk.Address, coolerthancool string) MsgQuiz {
|
|
|
|
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-04-18 21:49:24 -07:00
|
|
|
func (msg MsgQuiz) Type() string { return "cool" }
|
|
|
|
func (msg MsgQuiz) Get(key interface{}) (value interface{}) { return nil }
|
|
|
|
func (msg MsgQuiz) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
|
|
|
|
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 {
|
2018-03-17 19:42:54 -07:00
|
|
|
return sdk.ErrUnknownAddress(msg.Sender.String()).Trace("")
|
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 b
|
|
|
|
}
|