docs/examples: templates for more examples

This commit is contained in:
Ethan Buchman 2018-06-25 22:43:57 -04:00
parent 7e50e7d125
commit 234d7498de
4 changed files with 195 additions and 9 deletions

View File

@ -14,17 +14,17 @@ import (
)
const (
appName = "MyApp"
app1Name = "App1"
)
func NewApp(logger log.Logger, db dbm.DB) *bapp.BaseApp {
func NewApp1(logger log.Logger, db dbm.DB) *bapp.BaseApp {
// TODO: make this an interface or pass in
// a TxDecoder instead.
cdc := wire.NewCodec()
// Create the base application object.
app := bapp.NewBaseApp(appName, cdc, logger, db)
app := bapp.NewBaseApp(app1Name, cdc, logger, db)
// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
@ -35,7 +35,7 @@ func NewApp(logger log.Logger, db dbm.DB) *bapp.BaseApp {
// Register message routes.
// Note the handler gets access to the account store.
app.Router().
AddRoute("bank", NewHandler(keyAccount))
AddRoute("bank", NewApp1Handler(keyAccount))
// Mount stores and load the latest state.
app.MountStoresIAVL(keyAccount)
@ -99,7 +99,7 @@ func (msg MsgSend) GetSigners() []sdk.Address {
//------------------------------------------------------------------
// Handler for the message
func NewHandler(keyAcc *sdk.KVStoreKey) sdk.Handler {
func NewApp1Handler(keyAcc *sdk.KVStoreKey) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgSend:
@ -142,23 +142,23 @@ type acc struct {
// Tx
// Simple tx to wrap the Msg.
type tx struct {
type app1Tx struct {
MsgSend
}
// This tx only has one Msg.
func (tx tx) GetMsgs() []sdk.Msg {
func (tx app1Tx) GetMsgs() []sdk.Msg {
return []sdk.Msg{tx.MsgSend}
}
// TODO: remove the need for this
func (tx tx) GetMemo() string {
func (tx app1Tx) GetMemo() string {
return ""
}
// JSON decode MsgSend.
func txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) {
var tx tx
var tx app1Tx
err := json.Unmarshal(txBytes, &tx)
if err != nil {
return nil, sdk.ErrTxDecode(err.Error())

View File

@ -0,0 +1,87 @@
package app
import (
"reflect"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
bapp "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
)
const (
app2Name = "App2"
)
func NewCodec() *wire.Codec {
// TODO register
return nil
}
func NewApp2(logger log.Logger, db dbm.DB) *bapp.BaseApp {
cdc := NewCodec()
// Create the base application object.
app := bapp.NewBaseApp(app2Name, cdc, logger, db)
// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
keyIssuer := sdk.NewKVStoreKey("issuer")
// Register message routes.
// Note the handler gets access to the account store.
app.Router().
AddRoute("bank", NewApp2Handler(keyAccount, keyIssuer))
// Mount stores and load the latest state.
app.MountStoresIAVL(keyAccount, keyIssuer)
err := app.LoadLatestVersion(keyAccount)
if err != nil {
cmn.Exit(err.Error())
}
return app
}
//------------------------------------------------------------------
// Msgs
// TODO: MsgIssue
//------------------------------------------------------------------
// Handler for the message
func NewApp1Handler(keyAcc *sdk.KVStoreKey) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgSend:
return handleMsgSend(ctx, keyAcc, msg)
case MsgIssue:
// TODO
default:
errMsg := "Unrecognized bank Msg type: " + reflect.TypeOf(msg).Name()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
//------------------------------------------------------------------
// Tx
// Simple tx to wrap the Msg.
type app2Tx struct {
sdk.Msg
}
// This tx only has one Msg.
func (tx app2Tx) GetMsgs() []sdk.Msg {
return []sdk.Msg{tx.Msg}
}
// TODO: remove the need for this
func (tx app2Tx) GetMemo() string {
return ""
}

View File

@ -0,0 +1,50 @@
package app
import (
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
bapp "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
app3Name = "App3"
)
func NewApp3(logger log.Logger, db dbm.DB) *bapp.BaseApp {
cdc := NewCodec()
// Create the base application object.
app := bapp.NewBaseApp(app3Name, cdc, logger, db)
// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
keyIssuer := sdk.NewKVStoreKey("issuer")
// TODO: accounts, ante handler
// Register message routes.
// Note the handler gets access to the account store.
app.Router().
AddRoute("bank", NewApp2Handler(keyAccount, keyIssuer))
// Mount stores and load the latest state.
app.MountStoresIAVL(keyAccount, keyIssuer)
err := app.LoadLatestVersion(keyAccount)
if err != nil {
cmn.Exit(err.Error())
}
return app
}
//------------------------------------------------------------------
// StdTx
//------------------------------------------------------------------
// Account
//------------------------------------------------------------------
// Ante Handler

View File

@ -0,0 +1,49 @@
package app
import (
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
bapp "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
app4Name = "App4"
)
func NewApp4(logger log.Logger, db dbm.DB) *bapp.BaseApp {
cdc := NewCodec()
// Create the base application object.
app := bapp.NewBaseApp(app4Name, cdc, logger, db)
// Create a key for accessing the account store.
keyAccount := sdk.NewKVStoreKey("acc")
keyIssuer := sdk.NewKVStoreKey("issuer")
// TODO: accounts, ante handler
// TODO: AccountMapper, CoinKeepr
// Register message routes.
// Note the handler gets access to the account store.
app.Router().
AddRoute("bank", NewApp2Handler(keyAccount, keyIssuer))
// Mount stores and load the latest state.
app.MountStoresIAVL(keyAccount, keyIssuer)
err := app.LoadLatestVersion(keyAccount)
if err != nil {
cmn.Exit(err.Error())
}
return app
}
//------------------------------------------------------------------
// AccountMapper
//------------------------------------------------------------------
// CoinsKeeper