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