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

104 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"
)
// A really cool msg type, these fields are can be entirely arbitrary and
// custom to your message
2018-03-12 17:37:50 -07:00
type SetTrendMsg struct {
2018-03-12 17:23:33 -07:00
Sender sdk.Address
Cool string
2018-03-03 09:30:08 -08:00
}
// New cool message
2018-03-12 17:37:50 -07:00
func NewSetTrendMsg(sender sdk.Address, cool string) SetTrendMsg {
return SetTrendMsg{
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-03-12 17:37:50 -07:00
var _ sdk.Msg = SetTrendMsg{}
2018-03-03 09:30:08 -08:00
// nolint
2018-03-12 17:37:50 -07:00
func (msg SetTrendMsg) Type() string { return "cool" }
func (msg SetTrendMsg) Get(key interface{}) (value interface{}) { return nil }
func (msg SetTrendMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
func (msg SetTrendMsg) String() string {
return fmt.Sprintf("SetTrendMsg{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-03-12 17:37:50 -07:00
func (msg SetTrendMsg) 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-03-12 17:37:50 -07:00
func (msg SetTrendMsg) 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
type QuizMsg struct {
Sender sdk.Address
CoolAnswer string
2018-03-03 09:30:08 -08:00
}
// New cool message
2018-03-12 17:37:50 -07:00
func NewQuizMsg(sender sdk.Address, coolerthancool string) QuizMsg {
return QuizMsg{
Sender: sender,
CoolAnswer: coolerthancool,
2018-03-03 09:30:08 -08:00
}
}
// enforce the msg type at compile time
2018-03-12 17:37:50 -07:00
var _ sdk.Msg = QuizMsg{}
2018-03-03 09:30:08 -08:00
// nolint
2018-03-12 17:37:50 -07:00
func (msg QuizMsg) Type() string { return "cool" }
func (msg QuizMsg) Get(key interface{}) (value interface{}) { return nil }
func (msg QuizMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
func (msg QuizMsg) String() string {
return fmt.Sprintf("QuizMsg{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-03-12 17:37:50 -07:00
func (msg QuizMsg) 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-03-12 17:37:50 -07:00
func (msg QuizMsg) GetSignBytes() []byte {
2018-03-03 09:30:08 -08:00
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}