github.com/Finschia/finschia-sdk@v0.48.1/x/bank/module.go (about) 1 package bank 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "math/rand" 8 "time" 9 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 codectypes "github.com/Finschia/finschia-sdk/codec/types" 17 "github.com/Finschia/finschia-sdk/telemetry" 18 sdk "github.com/Finschia/finschia-sdk/types" 19 "github.com/Finschia/finschia-sdk/types/module" 20 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 21 "github.com/Finschia/finschia-sdk/x/bank/client/cli" 22 "github.com/Finschia/finschia-sdk/x/bank/keeper" 23 v040 "github.com/Finschia/finschia-sdk/x/bank/legacy/v040" 24 "github.com/Finschia/finschia-sdk/x/bank/simulation" 25 "github.com/Finschia/finschia-sdk/x/bank/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 bank module. 35 type AppModuleBasic struct { 36 cdc codec.Codec 37 } 38 39 func NewAppModuleBasic(cdc codec.Codec) AppModuleBasic { 40 return AppModuleBasic{cdc} 41 } 42 43 // Name returns the bank module's name. 44 func (AppModuleBasic) Name() string { return types.ModuleName } 45 46 // RegisterLegacyAminoCodec registers the bank module's types on the LegacyAmino codec. 47 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { 48 types.RegisterLegacyAminoCodec(cdc) 49 } 50 51 // DefaultGenesis returns default genesis state as raw bytes for the bank 52 // module. 53 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 54 return cdc.MustMarshalJSON(types.DefaultGenesisState()) 55 } 56 57 // ValidateGenesis performs genesis state validation for the bank module. 58 func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error { 59 var data types.GenesisState 60 if err := cdc.UnmarshalJSON(bz, &data); err != nil { 61 return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) 62 } 63 64 return data.Validate() 65 } 66 67 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the bank module. 68 func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { 69 if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil { 70 panic(err) 71 } 72 } 73 74 // GetTxCmd returns the root tx command for the bank module. 75 func (AppModuleBasic) GetTxCmd() *cobra.Command { 76 return cli.NewTxCmd() 77 } 78 79 // GetQueryCmd returns no root query command for the bank module. 80 func (AppModuleBasic) GetQueryCmd() *cobra.Command { 81 return cli.GetQueryCmd() 82 } 83 84 // RegisterInterfaces registers interfaces and implementations of the bank module. 85 func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { 86 types.RegisterInterfaces(registry) 87 88 // Register legacy interfaces for migration scripts. 89 v040.RegisterInterfaces(registry) 90 } 91 92 // AppModule implements an application module for the bank module. 93 type AppModule struct { 94 AppModuleBasic 95 96 keeper keeper.Keeper 97 accountKeeper types.AccountKeeper 98 } 99 100 // RegisterServices registers module services. 101 func (am AppModule) RegisterServices(cfg module.Configurator) { 102 types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) 103 types.RegisterQueryServer(cfg.QueryServer(), am.keeper) 104 105 m := keeper.NewMigrator(am.keeper.(keeper.BaseKeeper)) 106 if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil { 107 panic(fmt.Sprintf("failed to migrate x/bank from version 1 to 2: %v", err)) 108 } 109 } 110 111 // NewAppModule creates a new AppModule object 112 func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, accountKeeper types.AccountKeeper) AppModule { 113 return AppModule{ 114 AppModuleBasic: AppModuleBasic{cdc: cdc}, 115 keeper: keeper, 116 accountKeeper: accountKeeper, 117 } 118 } 119 120 // Name returns the bank module's name. 121 func (AppModule) Name() string { return types.ModuleName } 122 123 // RegisterInvariants registers the bank module invariants. 124 func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { 125 keeper.RegisterInvariants(ir, am.keeper) 126 } 127 128 // Route returns the message routing key for the bank module. 129 func (am AppModule) Route() sdk.Route { 130 return sdk.NewRoute(types.RouterKey, NewHandler(am.keeper)) 131 } 132 133 // QuerierRoute returns the bank module's querier route name. 134 func (AppModule) QuerierRoute() string { return types.RouterKey } 135 136 // LegacyQuerierHandler returns the bank module sdk.Querier. 137 func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier { 138 return keeper.NewQuerier(am.keeper, legacyQuerierCdc) 139 } 140 141 // InitGenesis performs genesis initialization for the bank module. It returns 142 // no validator updates. 143 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { 144 start := time.Now() 145 var genesisState types.GenesisState 146 cdc.MustUnmarshalJSON(data, &genesisState) 147 telemetry.MeasureSince(start, "InitGenesis", "crisis", "unmarshal") 148 149 am.keeper.InitGenesis(ctx, &genesisState) 150 return []abci.ValidatorUpdate{} 151 } 152 153 // ExportGenesis returns the exported genesis state as raw bytes for the bank 154 // module. 155 func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { 156 gs := am.keeper.ExportGenesis(ctx) 157 return cdc.MustMarshalJSON(gs) 158 } 159 160 // ConsensusVersion implements AppModule/ConsensusVersion. 161 func (AppModule) ConsensusVersion() uint64 { return 1 } 162 163 // AppModuleSimulation functions 164 165 // GenerateGenesisState creates a randomized GenState of the bank module. 166 func (AppModule) GenerateGenesisState(simState *module.SimulationState) { 167 simulation.RandomizedGenState(simState) 168 } 169 170 // ProposalContents doesn't return any content functions for governance proposals. 171 func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedProposalContent { 172 return nil 173 } 174 175 // RandomizedParams creates randomized bank param changes for the simulator. 176 func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange { 177 return simulation.ParamChanges(r) 178 } 179 180 // RegisterStoreDecoder registers a decoder for supply module's types 181 func (am AppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {} 182 183 // WeightedOperations returns the all the gov module operations with their respective weights. 184 func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { 185 return simulation.WeightedOperations( 186 simState.AppParams, simState.Cdc, am.accountKeeper, am.keeper, 187 ) 188 }