github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/gov/module.go (about) 1 package gov 2 3 import ( 4 "encoding/json" 5 "math/rand" 6 7 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 8 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 9 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 10 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module" 11 sim "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation" 12 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 13 "github.com/fibonacci-chain/fbc/x/gov/client" 14 "github.com/fibonacci-chain/fbc/x/gov/client/cli" 15 "github.com/fibonacci-chain/fbc/x/gov/client/rest" 16 "github.com/fibonacci-chain/fbc/x/gov/keeper" 17 "github.com/fibonacci-chain/fbc/x/gov/types" 18 "github.com/fibonacci-chain/fbc/x/wasm/watcher" 19 "github.com/gorilla/mux" 20 "github.com/spf13/cobra" 21 ) 22 23 var ( 24 _ module.AppModule = AppModule{} 25 _ module.AppModuleBasic = AppModuleBasic{} 26 ) 27 28 // AppModuleBasic defines app module basics object 29 type AppModuleBasic struct { 30 proposalHandlers []client.ProposalHandler // proposal handlers which live in governance cli and rest 31 } 32 33 // NewAppModuleBasic creates a new AppModuleBasic object 34 func NewAppModuleBasic(proposalHandlers ...client.ProposalHandler) AppModuleBasic { 35 return AppModuleBasic{ 36 proposalHandlers: proposalHandlers, 37 } 38 } 39 40 var _ module.AppModuleBasic = AppModuleBasic{} 41 42 // Name gets basic module name 43 func (AppModuleBasic) Name() string { 44 return types.ModuleName 45 } 46 47 // RegisterCodec registers module codec 48 func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) { 49 RegisterCodec(cdc) 50 } 51 52 // DefaultGenesis sets default genesis state 53 func (AppModuleBasic) DefaultGenesis() json.RawMessage { 54 return types.ModuleCdc.MustMarshalJSON(DefaultGenesisState()) 55 } 56 57 // ValidateGenesis validates module genesis 58 func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error { 59 var data GenesisState 60 err := types.ModuleCdc.UnmarshalJSON(bz, &data) 61 if err != nil { 62 return err 63 } 64 return ValidateGenesis(data) 65 } 66 67 // RegisterRESTRoutes registers rest routes 68 func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) { 69 proposalRESTHandlers := make([]rest.ProposalRESTHandler, len(a.proposalHandlers)) 70 for i, proposalHandler := range a.proposalHandlers { 71 proposalRESTHandlers[i] = proposalHandler.RESTHandler(ctx) 72 } 73 74 rest.RegisterRoutes(ctx, rtr, proposalRESTHandlers) 75 } 76 77 // GetTxCmd gets the root tx command of this module 78 func (a AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command { 79 return nil 80 } 81 82 // GetQueryCmd gets the root query command of this module 83 func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command { 84 return cli.GetQueryCmd(types.StoreKey, cdc) 85 } 86 87 // AppModule defines app module 88 type AppModule struct { 89 AppModuleBasic 90 keeper Keeper 91 supplyKeeper keeper.SupplyKeeper 92 } 93 94 // NewAppModule creates a new AppModule object 95 func NewAppModule(keeper Keeper, supplyKeeper keeper.SupplyKeeper) AppModule { 96 return AppModule{ 97 AppModuleBasic: AppModuleBasic{}, 98 keeper: keeper, 99 supplyKeeper: supplyKeeper, 100 } 101 } 102 103 // Name gets module name 104 func (AppModule) Name() string { 105 return types.ModuleName 106 } 107 108 // RegisterInvariants registers invariants 109 func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { 110 RegisterInvariants(ir, am.keeper) 111 } 112 113 // Route gets module message route name 114 func (AppModule) Route() string { 115 return types.RouterKey 116 } 117 118 // NewHandler gets module handler 119 func (am AppModule) NewHandler() sdk.Handler { 120 return NewHandler(am.keeper) 121 } 122 123 // QuerierRoute module querier route name 124 func (AppModule) QuerierRoute() string { 125 return types.QuerierRoute 126 } 127 128 // NewQuerierHandler gets module querier 129 func (am AppModule) NewQuerierHandler() sdk.Querier { 130 return NewQuerier(am.keeper) 131 } 132 133 // InitGenesis inits module genesis 134 func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { 135 var genesisState GenesisState 136 types.ModuleCdc.MustUnmarshalJSON(data, &genesisState) 137 InitGenesis(ctx, am.keeper, am.supplyKeeper, genesisState) 138 return []abci.ValidatorUpdate{} 139 } 140 141 // ExportGenesis exports module genesis 142 func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { 143 gs := ExportGenesis(ctx, am.keeper) 144 return types.ModuleCdc.MustMarshalJSON(gs) 145 } 146 147 // BeginBlock implements module begin-block 148 func (AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} 149 150 // EndBlock implements module end-block 151 func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { 152 EndBlocker(ctx, am.keeper) 153 if watcher.Enable() { 154 watcher.Save(nil) 155 } 156 157 return []abci.ValidatorUpdate{} 158 } 159 160 // AppModuleSimulation functions 161 // TODO: implement the AppModuleSimulation interface 162 163 // GenerateGenesisState creates a randomized GenState of the staking module. 164 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 165 } 166 167 // ProposalContents doesn't return any content functions for governance proposals. 168 func (AppModule) ProposalContents(_ module.SimulationState) []sim.WeightedProposalContent { 169 return nil 170 } 171 172 // RandomizedParams creates randomized staking param changes for the simulator. 173 func (AppModule) RandomizedParams(r *rand.Rand) []sim.ParamChange { 174 return nil 175 } 176 177 // RegisterStoreDecoder registers a decoder for staking module's types 178 func (AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) { 179 180 } 181 182 // WeightedOperations returns the all the staking module operations with their respective weights. 183 func (am AppModule) WeightedOperations(simState module.SimulationState) []sim.WeightedOperation { 184 return nil 185 }