working new cool module in basecoin
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package coolmodule
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
)
|
||||
|
||||
// This is just an example to demonstrate a functional custom module
|
||||
// with full feature set functionality.
|
||||
//
|
||||
// /$$$$$$$ /$$$$$$ /$$$$$$ /$$
|
||||
// /$$_____/ /$$__ $$ /$$__ $$| $$
|
||||
//| $$ | $$ \ $$| $$ \ $$| $$
|
||||
//| $$ | $$ | $$| $$ | $$| $$
|
||||
//| $$$$$$$| $$$$$$/| $$$$$$/| $$$$$$$
|
||||
// \_______/ \______/ \______/ |______/
|
||||
|
||||
// Handle all "coolmodule" type objects
|
||||
func NewHandler(ck bank.CoinKeeper) sdk.Handler {
|
||||
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
|
||||
switch msg := msg.(type) {
|
||||
case CoolMsg:
|
||||
return handleCoolMsg(ctx, ck, msg)
|
||||
case CoolerThanCoolMsg:
|
||||
return handleMeltMsg(ctx, ck, msg)
|
||||
default:
|
||||
errMsg := "Unrecognized bank Msg type: " + reflect.TypeOf(msg).Name()
|
||||
return sdk.ErrUnknownRequest(errMsg).Result()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle CoolMsg This is the engine of your module
|
||||
func handleCoolMsg(ctx sdk.Context, ck CoinKeeper, msg CoolMsg) sdk.Result {
|
||||
|
||||
if msg.coolerthancool == "icecold" {
|
||||
|
||||
bonusCoins := sdk.Coins{{"icecold", 69}}
|
||||
_, err := ck.AddCoins(ctx, msg.Address, bonusCoins)
|
||||
if err != nil {
|
||||
return err.Result()
|
||||
}
|
||||
}
|
||||
|
||||
return sdk.Result{}
|
||||
}
|
||||
|
||||
// Handle CoolMsg This is the engine of your module
|
||||
func handleMeltMsg(ctx sdk.Context, ck CoinKeeper, msg CoolMsg) sdk.Result {
|
||||
|
||||
// checks for existence should already have occured
|
||||
if strings.Prefix(msg.what, "ice") {
|
||||
return bank.ErrInvalidInput("only frozen coins can use the blow dryer")
|
||||
}
|
||||
|
||||
bonusCoins := sdk.Coins{{"icecold", 69}}
|
||||
_, err := ck.SubtractCoins(ctx, msg.Address, bonusCoins)
|
||||
if err != nil {
|
||||
return err.Result()
|
||||
}
|
||||
return sdk.Result{}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package coolmodule
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// A really cool msg type, these fields are can be entirely arbitrary and
|
||||
// custom to your message
|
||||
type CoolMsg struct {
|
||||
Sender sdk.Address
|
||||
Coolerthancool string
|
||||
}
|
||||
|
||||
// New cool message
|
||||
func NewCoolMsg(sender sdk.Address, coolerthancool string) CoolMsg {
|
||||
return CoolMsg{
|
||||
Sender: sender,
|
||||
Coolerthancool: coolerthancool,
|
||||
}
|
||||
}
|
||||
|
||||
// enforce the msg type at compile time
|
||||
var _ CoolMsg = sdk.Msg
|
||||
|
||||
// nolint
|
||||
func (msg CoolMsg) Type() string { return "cool" }
|
||||
func (msg CoolMsg) Get(key interface{}) (value interface{}) { return nil }
|
||||
func (msg CoolMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
|
||||
func (msg CoolMsg) String() string { return fmt.Sprintf("CoolMsg{%v}", msg) }
|
||||
|
||||
// Validate Basic is used to quickly disqualify obviously invalid messages quickly
|
||||
func (msg CoolMsg) ValidateBasic() sdk.Error {
|
||||
if msg.Signer.Empty() {
|
||||
return ErrNoOutputs().Trace("")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the bytes for the message signer to sign on
|
||||
func (msg CoolMsg) GetSignBytes() []byte {
|
||||
b, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
//_______________________________________________________________________
|
||||
|
||||
// A really cool msg type, these fields are can be entirely arbitrary and
|
||||
// custom to your message
|
||||
type BlowDryMsg struct {
|
||||
Sender sdk.Address
|
||||
What sdk.Coin
|
||||
}
|
||||
|
||||
// New cool message
|
||||
func NewBlowDryMsg(sender sdk.Address, what sdk.Coin) BlowDryMsg {
|
||||
return BlowDryMsg{
|
||||
Sender: sender,
|
||||
What: what,
|
||||
}
|
||||
}
|
||||
|
||||
// enforce the msg type at compile time
|
||||
var _ BlowDryMsg = sdk.Msg
|
||||
|
||||
// nolint
|
||||
func (msg BlowDryMsg) Type() string { return "cool" }
|
||||
func (msg BlowDryMsg) Get(key interface{}) (value interface{}) { return nil }
|
||||
func (msg BlowDryMsg) GetSigners() []sdk.Address { return []sdk.Address{msg.Sender} }
|
||||
func (msg BlowDryMsg) String() string { return fmt.Sprintf("BlowDryMsg{%v}", msg) }
|
||||
|
||||
// Validate Basic is used to quickly disqualify obviously invalid messages quickly
|
||||
func (msg BlowDryMsg) ValidateBasic() sdk.Error {
|
||||
if msg.Signer.Empty() {
|
||||
return ErrNoOutputs().Trace("")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the bytes for the message signer to sign on
|
||||
func (msg BlowDryMsg) GetSignBytes() []byte {
|
||||
b, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
Reference in New Issue
Block a user