working new cool module in basecoin

This commit is contained in:
rigelrozanski 2018-03-02 17:21:49 +00:00
parent d3ffd303c4
commit 6dec9353be
6 changed files with 170 additions and 6 deletions

View File

@ -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{}
}

View File

@ -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
}

View File

@ -5,6 +5,7 @@ import (
cmn "github.com/tendermint/tmlibs/common"
)
// Address in go-crypto style
type Address = cmn.HexBytes
// Account is a standard account using a sequence number for replay protection

View File

@ -23,6 +23,8 @@ type Msg interface {
GetSigners() []Address
}
//__________________________________________________________
// Transactions objects must fulfill the Tx
type Tx interface {
@ -64,7 +66,7 @@ func (tx StdTx) GetMsg() Msg { return tx.Msg }
func (tx StdTx) GetFeePayer() Address { return tx.Signatures[0].PubKey.Address() } // XXX but PubKey is optional!
func (tx StdTx) GetSignatures() []StdSignature { return tx.Signatures }
//-------------------------------------
//__________________________________________________________
// Application function variable used to unmarshal transaction bytes
type TxDecoder func(txBytes []byte) (Tx, Error)

View File

@ -15,6 +15,8 @@ type SendMsg struct {
Outputs []Output `json:"outputs"`
}
var _ SendMsg = (nil)(sdk.Msg)
// NewSendMsg - construct arbitrary multi-in, multi-out send msg.
func NewSendMsg(in []Input, out []Output) SendMsg {
return SendMsg{Inputs: in, Outputs: out}
@ -87,7 +89,7 @@ func (msg SendMsg) GetSigners() []sdk.Address {
// IssueMsg - high level transaction of the coin module
type IssueMsg struct {
Banker sdk.Address `json:"banker"`
Outputs []Output `json:"outputs"`
Outputs []Output `json:"outputs"`
}
// NewIssueMsg - construct arbitrary multi-in, multi-out send msg.
@ -96,7 +98,7 @@ func NewIssueMsg(banker sdk.Address, out []Output) IssueMsg {
}
// Implements Msg.
func (msg IssueMsg) Type() string { return "bank" } // TODO: "bank/send"
func (msg IssueMsg) Type() string { return "bank" } // TODO: "bank/issue"
// Implements Msg.
func (msg IssueMsg) ValidateBasic() sdk.Error {
@ -138,10 +140,11 @@ func (msg IssueMsg) GetSigners() []sdk.Address {
//----------------------------------------
// Input
// Transaction Output
type Input struct {
Address sdk.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
Sequence int64 `json:"sequence"`
Coins sdk.Coins `json:"coins"`
Sequence int64 `json:"sequence"`
signature crypto.Signature
}
@ -186,9 +189,10 @@ func NewInputWithSequence(addr sdk.Address, coins sdk.Coins, seq int64) Input {
//----------------------------------------
// Output
// Transaction Output
type Output struct {
Address sdk.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
Coins sdk.Coins `json:"coins"`
}
// ValidateBasic - validate transaction output

View File

@ -4,6 +4,7 @@ import (
"github.com/cosmos/cosmos-sdk/wire"
)
// Register concrete types on wire codec
func RegisterWire(cdc *wire.Codec) {
// TODO: bring this back ...
/*