github.com/Finschia/finschia-sdk@v0.48.1/x/collection/module/module.go (about) 1 package module 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 8 "github.com/grpc-ecosystem/grpc-gateway/runtime" 9 "github.com/spf13/cobra" 10 abci "github.com/tendermint/tendermint/abci/types" 11 12 "github.com/Finschia/finschia-sdk/client" 13 "github.com/Finschia/finschia-sdk/codec" 14 codectypes "github.com/Finschia/finschia-sdk/codec/types" 15 sdk "github.com/Finschia/finschia-sdk/types" 16 "github.com/Finschia/finschia-sdk/types/module" 17 "github.com/Finschia/finschia-sdk/x/collection" 18 "github.com/Finschia/finschia-sdk/x/collection/client/cli" 19 "github.com/Finschia/finschia-sdk/x/collection/keeper" 20 ) 21 22 var ( 23 _ module.AppModule = AppModule{} 24 _ module.AppModuleBasic = AppModuleBasic{} 25 ) 26 27 // AppModuleBasic defines the basic application module used by the collection module. 28 type AppModuleBasic struct{} 29 30 // Name returns the ModuleName 31 func (AppModuleBasic) Name() string { 32 return collection.ModuleName 33 } 34 35 // RegisterLegacyAminoCodec registers the collection types on the LegacyAmino codec 36 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} 37 38 // DefaultGenesis returns default genesis state as raw bytes for the collection 39 // module. 40 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 41 return cdc.MustMarshalJSON(collection.DefaultGenesisState()) 42 } 43 44 // ValidateGenesis performs genesis state validation for the collection module. 45 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { 46 var data collection.GenesisState 47 if err := cdc.UnmarshalJSON(bz, &data); err != nil { 48 return fmt.Errorf("failed to unmarshal %s genesis state: %w", collection.ModuleName, err) 49 } 50 51 return collection.ValidateGenesis(data) 52 } 53 54 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the collection module. 55 func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { 56 if err := collection.RegisterQueryHandlerClient(context.Background(), mux, collection.NewQueryClient(clientCtx)); err != nil { 57 panic(err) 58 } 59 } 60 61 // GetQueryCmd returns the cli query commands for this module 62 func (AppModuleBasic) GetQueryCmd() *cobra.Command { 63 return cli.NewQueryCmd() 64 } 65 66 // GetTxCmd returns the transaction commands for this module 67 func (AppModuleBasic) GetTxCmd() *cobra.Command { 68 return cli.NewTxCmd() 69 } 70 71 func (b AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { 72 collection.RegisterInterfaces(registry) 73 } 74 75 //____________________________________________________________________________ 76 77 // AppModule implements an application module for the collection module. 78 type AppModule struct { 79 AppModuleBasic 80 81 keeper keeper.Keeper 82 } 83 84 // NewAppModule creates a new AppModule object 85 func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule { 86 return AppModule{ 87 keeper: keeper, 88 } 89 } 90 91 // RegisterInvariants does nothing, there are no invariants to enforce 92 func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} 93 94 // Route returns the message routing key for the collection module. 95 func (am AppModule) Route() sdk.Route { return sdk.Route{} } 96 97 // QuerierRoute returns the route we respond to for abci queries 98 func (AppModule) QuerierRoute() string { return "" } 99 100 // LegacyQuerierHandler registers a query handler to respond to the module-specific queries 101 func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { 102 return nil 103 } 104 105 // RegisterServices registers a GRPC query service to respond to the 106 // module-specific GRPC queries. 107 func (am AppModule) RegisterServices(cfg module.Configurator) { 108 collection.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper)) 109 collection.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper)) 110 if err := keeper.NewMigrator(am.keeper).Register(cfg.RegisterMigration); err != nil { 111 panic(err) 112 } 113 } 114 115 // InitGenesis performs genesis initialization for the collection module. It returns 116 // no validator updates. 117 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { 118 var genesisState collection.GenesisState 119 cdc.MustUnmarshalJSON(data, &genesisState) 120 am.keeper.InitGenesis(ctx, &genesisState) 121 return []abci.ValidatorUpdate{} 122 } 123 124 // ExportGenesis returns the exported genesis state as raw bytes for the collection 125 // module. 126 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 127 gs := am.keeper.ExportGenesis(ctx) 128 return cdc.MustMarshalJSON(gs) 129 } 130 131 // ConsensusVersion implements AppModule/ConsensusVersion. 132 func (AppModule) ConsensusVersion() uint64 { return 2 }