- msg & query servers - clean up logging, errors - don't use sdk global config
146 lines
4.5 KiB
Go
146 lines
4.5 KiB
Go
package module
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
|
|
// "google.golang.org/grpc"
|
|
|
|
appmodule "cosmossdk.io/core/appmodule/v2"
|
|
"cosmossdk.io/core/registry"
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
|
|
"git.vdb.to/cerc-io/laconicd/x/bond"
|
|
"git.vdb.to/cerc-io/laconicd/x/bond/keeper"
|
|
)
|
|
|
|
var (
|
|
_ appmodule.AppModule = AppModule{}
|
|
_ appmodule.HasConsensusVersion = AppModule{}
|
|
_ appmodule.HasGenesis = AppModule{}
|
|
_ appmodule.HasMsgHandlers = AppModule{}
|
|
_ appmodule.HasQueryHandlers = AppModule{}
|
|
_ appmodule.HasRegisterInterfaces = AppModule{}
|
|
|
|
_ module.HasGRPCGateway = AppModule{}
|
|
// _ module.HasServices = AppModule{}
|
|
_ module.HasInvariants = AppModule{}
|
|
// _ module.HasAminoCodec = AppModule{} // TODO
|
|
)
|
|
|
|
// ConsensusVersion defines the current module consensus version
|
|
const ConsensusVersion = 1
|
|
|
|
type AppModule struct {
|
|
cdc codec.Codec
|
|
keeper *keeper.Keeper
|
|
}
|
|
|
|
// NewAppModule creates a new AppModule object
|
|
func NewAppModule(cdc codec.Codec, keeper *keeper.Keeper) AppModule {
|
|
return AppModule{
|
|
cdc: cdc,
|
|
keeper: keeper,
|
|
}
|
|
}
|
|
|
|
// module.AppModule
|
|
|
|
// Name returns the bond module's name.
|
|
func (AppModule) Name() string { return bond.ModuleName }
|
|
|
|
// module.HasGRPCGateway
|
|
|
|
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the bond module.
|
|
func (AppModule) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) {
|
|
if err := bond.RegisterQueryHandlerClient(context.Background(), mux, bond.NewQueryClient(clientCtx)); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// appmodule.HasRegisterInterfaces
|
|
|
|
// RegisterInterfaces registers interfaces and implementations of the bond module.
|
|
func (AppModule) RegisterInterfaces(registry registry.InterfaceRegistrar) {
|
|
bond.RegisterInterfaces(registry)
|
|
}
|
|
|
|
// appmodule.HasConsensusVersion
|
|
|
|
// ConsensusVersion implements AppModule/ConsensusVersion.
|
|
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
|
|
|
|
// appmodule.HasGenesis
|
|
|
|
// DefaultGenesis returns default genesis state as raw bytes for the module.
|
|
func (am AppModule) DefaultGenesis() json.RawMessage {
|
|
return am.cdc.MustMarshalJSON(bond.DefaultGenesisState())
|
|
}
|
|
|
|
// ValidateGenesis performs genesis state validation for the module.
|
|
func (am AppModule) ValidateGenesis(message json.RawMessage) error {
|
|
var data bond.GenesisState
|
|
if err := am.cdc.UnmarshalJSON(message, &data); err != nil {
|
|
return fmt.Errorf("failed to unmarshal %s genesis state: %w", bond.ModuleName, err)
|
|
}
|
|
|
|
return data.Validate()
|
|
}
|
|
|
|
// InitGenesis performs genesis initialization for the bond module.
|
|
// It returns no validator updates.
|
|
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error {
|
|
var genesisState bond.GenesisState
|
|
am.cdc.MustUnmarshalJSON(data, &genesisState)
|
|
|
|
if err := am.keeper.InitGenesis(ctx, &genesisState); err != nil {
|
|
return fmt.Errorf("failed to initialize %s genesis state: %w", bond.ModuleName, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ExportGenesis returns the exported genesis state as raw bytes for the circuit
|
|
// module.
|
|
func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) {
|
|
gs, err := am.keeper.ExportGenesis(ctx)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("failed to export %s genesis state: %v", bond.ModuleName, err))
|
|
}
|
|
|
|
return am.cdc.MustMarshalJSON(gs), nil
|
|
}
|
|
|
|
// module.HasInvariants
|
|
|
|
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
|
|
keeper.RegisterInvariants(ir, am.keeper)
|
|
}
|
|
|
|
// RegisterMsgHandlers registers the message handlers for the bond module.
|
|
func (am AppModule) RegisterMsgHandlers(router appmodule.MsgRouter) {
|
|
handlers := keeper.NewMsgServerImpl(am.keeper)
|
|
|
|
appmodule.RegisterMsgHandler(router, handlers.CreateBond)
|
|
appmodule.RegisterMsgHandler(router, handlers.RefillBond)
|
|
appmodule.RegisterMsgHandler(router, handlers.WithdrawBond)
|
|
appmodule.RegisterMsgHandler(router, handlers.CancelBond)
|
|
appmodule.RegisterMsgHandler(router, handlers.UpdateParams)
|
|
}
|
|
|
|
// RegisterQueryHandlers registers the query handlers for the bond module.
|
|
func (am AppModule) RegisterQueryHandlers(router appmodule.QueryRouter) {
|
|
handlers := keeper.NewQueryServerImpl(am.keeper)
|
|
|
|
appmodule.RegisterMsgHandler(router, handlers.Params)
|
|
appmodule.RegisterMsgHandler(router, handlers.Bonds)
|
|
appmodule.RegisterMsgHandler(router, handlers.GetBondById)
|
|
appmodule.RegisterMsgHandler(router, handlers.GetBondsByOwner)
|
|
appmodule.RegisterMsgHandler(router, handlers.GetBondModuleBalance)
|
|
}
|