package protocolpool import ( "context" "encoding/json" "fmt" gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime" "cosmossdk.io/core/appmodule" "cosmossdk.io/x/protocolpool/keeper" "cosmossdk.io/x/protocolpool/types" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/types/module" ) // ConsensusVersion defines the current x/protocolpool module consensus version. const ConsensusVersion = 1 var ( _ module.AppModuleBasic = AppModule{} _ module.AppModule = AppModule{} _ module.AppModuleSimulation = AppModule{} _ module.HasGenesis = AppModule{} ) // AppModuleBasic defines the basic application module used by the pool module. type AppModuleBasic struct { cdc codec.Codec } // Name returns the pool module's name. func (AppModuleBasic) Name() string { return types.ModuleName } // RegisterLegacyAminoCodec registers the pool module's types on the LegacyAmino codec. func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) { if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { panic(err) } } // RegisterInterfaces registers interfaces and implementations of the bank module. func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { types.RegisterInterfaces(registry) } // DefaultGenesis returns default genesis state as raw bytes for the protocolpool // module. func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { return cdc.MustMarshalJSON(types.DefaultGenesisState()) } // ValidateGenesis performs genesis state validation for the protocolpool module. func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { var data types.GenesisState if err := cdc.UnmarshalJSON(bz, &data); err != nil { return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) } return types.ValidateGenesis(&data) } // AppModule implements an application module for the pool module type AppModule struct { AppModuleBasic keeper keeper.Keeper accountKeeper types.AccountKeeper bankKeeper types.BankKeeper } var _ appmodule.AppModule = AppModule{} // IsAppModule implements the appmodule.AppModule interface. func (am AppModule) IsAppModule() {} // RegisterServices registers module services. func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper)) } // NewAppModule creates a new AppModule object func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper, ) AppModule { return AppModule{ AppModuleBasic: AppModuleBasic{cdc: cdc}, keeper: keeper, accountKeeper: accountKeeper, bankKeeper: bankKeeper, } } // InitGenesis performs genesis initialization for the protocolpool module. func (am AppModule) InitGenesis(ctx context.Context, cdc codec.JSONCodec, data json.RawMessage) { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) if err := am.keeper.InitGenesis(ctx, &genesisState); err != nil { panic(err) } } // ExportGenesis returns the exported genesis state as raw bytes for the protocolpool // module. func (am AppModule) ExportGenesis(ctx context.Context, cdc codec.JSONCodec) json.RawMessage { gs, err := am.keeper.ExportGenesis(ctx) if err != nil { panic(err) } return cdc.MustMarshalJSON(gs) } // ConsensusVersion implements AppModule/ConsensusVersion. func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }