github.com/Finschia/finschia-sdk@v0.48.1/x/mint/module.go (about) 1 package mint 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "math/rand" 8 9 ocabci "github.com/Finschia/ostracon/abci/types" 10 "github.com/grpc-ecosystem/grpc-gateway/runtime" 11 "github.com/spf13/cobra" 12 abci "github.com/tendermint/tendermint/abci/types" 13 14 "github.com/Finschia/finschia-sdk/client" 15 "github.com/Finschia/finschia-sdk/codec" 16 cdctypes "github.com/Finschia/finschia-sdk/codec/types" 17 sdk "github.com/Finschia/finschia-sdk/types" 18 "github.com/Finschia/finschia-sdk/types/module" 19 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 20 "github.com/Finschia/finschia-sdk/x/mint/client/cli" 21 "github.com/Finschia/finschia-sdk/x/mint/keeper" 22 "github.com/Finschia/finschia-sdk/x/mint/simulation" 23 "github.com/Finschia/finschia-sdk/x/mint/types" 24 ) 25 26 var ( 27 _ module.AppModule = AppModule{} 28 _ module.AppModuleBasic = AppModuleBasic{} 29 _ module.AppModuleSimulation = AppModule{} 30 _ module.BeginBlockAppModule = AppModule{} 31 ) 32 33 // AppModuleBasic defines the basic application module used by the mint module. 34 type AppModuleBasic struct { 35 cdc codec.Codec 36 } 37 38 // Name returns the mint module's name. 39 func (AppModuleBasic) Name() string { 40 return types.ModuleName 41 } 42 43 // RegisterLegacyAminoCodec registers the mint module's types on the given LegacyAmino codec. 44 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} 45 46 // RegisterInterfaces registers the module's interface types 47 func (b AppModuleBasic) RegisterInterfaces(_ cdctypes.InterfaceRegistry) {} 48 49 // DefaultGenesis returns default genesis state as raw bytes for the mint 50 // module. 51 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 52 return cdc.MustMarshalJSON(types.DefaultGenesisState()) 53 } 54 55 // ValidateGenesis performs genesis state validation for the mint module. 56 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error { 57 var data types.GenesisState 58 if err := cdc.UnmarshalJSON(bz, &data); err != nil { 59 return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) 60 } 61 62 return types.ValidateGenesis(data) 63 } 64 65 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the mint module. 66 func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { 67 if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { 68 panic(err) 69 } 70 } 71 72 // GetTxCmd returns no root tx command for the mint module. 73 func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } 74 75 // GetQueryCmd returns the root query command for the mint module. 76 func (AppModuleBasic) GetQueryCmd() *cobra.Command { 77 return cli.GetQueryCmd() 78 } 79 80 // AppModule implements an application module for the mint module. 81 type AppModule struct { 82 AppModuleBasic 83 84 keeper keeper.Keeper 85 authKeeper types.AccountKeeper 86 } 87 88 // NewAppModule creates a new AppModule object 89 func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak types.AccountKeeper) AppModule { 90 return AppModule{ 91 AppModuleBasic: AppModuleBasic{cdc: cdc}, 92 keeper: keeper, 93 authKeeper: ak, 94 } 95 } 96 97 // Name returns the mint module's name. 98 func (AppModule) Name() string { 99 return types.ModuleName 100 } 101 102 // RegisterInvariants registers the mint module invariants. 103 func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} 104 105 // Route returns the message routing key for the mint module. 106 func (AppModule) Route() sdk.Route { return sdk.Route{} } 107 108 // QuerierRoute returns the mint module's querier route name. 109 func (AppModule) QuerierRoute() string { 110 return types.QuerierRoute 111 } 112 113 // LegacyQuerierHandler returns the mint module sdk.Querier. 114 func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { 115 return keeper.NewQuerier(am.keeper, legacyQuerierCdc) 116 } 117 118 // RegisterServices registers a gRPC query service to respond to the 119 // module-specific gRPC queries. 120 func (am AppModule) RegisterServices(cfg module.Configurator) { 121 types.RegisterQueryServer(cfg.QueryServer(), am.keeper) 122 123 /* m := keeper.NewMigrator(am.keeper) 124 if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { 125 panic(fmt.Sprintf("failed to migrate x/mint from version 1 to 2: %v", err)) 126 } */ 127 } 128 129 // InitGenesis performs genesis initialization for the mint module. It returns 130 // no validator updates. 131 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { 132 var genesisState types.GenesisState 133 cdc.MustUnmarshalJSON(data, &genesisState) 134 135 InitGenesis(ctx, am.keeper, am.authKeeper, &genesisState) 136 return []abci.ValidatorUpdate{} 137 } 138 139 // ExportGenesis returns the exported genesis state as raw bytes for the mint 140 // module. 141 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 142 gs := ExportGenesis(ctx, am.keeper) 143 return cdc.MustMarshalJSON(gs) 144 } 145 146 // ConsensusVersion implements AppModule/ConsensusVersion. 147 func (AppModule) ConsensusVersion() uint64 { return 1 } 148 149 // BeginBlock returns the begin blocker for the mint module. 150 func (am AppModule) BeginBlock(ctx sdk.Context, _ ocabci.RequestBeginBlock) { 151 BeginBlocker(ctx, am.keeper) 152 } 153 154 // AppModuleSimulation functions 155 156 // GenerateGenesisState creates a randomized GenState of the mint module. 157 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 158 simulation.RandomizedGenState(simState) 159 } 160 161 // ProposalContents doesn't return any content functions for governance proposals. 162 func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent { 163 return nil 164 } 165 166 // RandomizedParams creates randomized mint param changes for the simulator. 167 func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { 168 return simulation.ParamChanges(r) 169 } 170 171 // RegisterStoreDecoder registers a decoder for mint module's types. 172 func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { 173 sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc) 174 } 175 176 // WeightedOperations doesn't return any mint module operation. 177 func (AppModule) WeightedOperations(_ module.SimulationState) []simtypes.WeightedOperation { 178 return nil 179 }