github.com/Finschia/finschia-sdk@v0.48.1/x/token/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/token"
    18  	"github.com/Finschia/finschia-sdk/x/token/client/cli"
    19  	"github.com/Finschia/finschia-sdk/x/token/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 token module.
    28  type AppModuleBasic struct{}
    29  
    30  // Name returns the ModuleName
    31  func (AppModuleBasic) Name() string {
    32  	return token.ModuleName
    33  }
    34  
    35  // RegisterLegacyAminoCodec registers the token types on the LegacyAmino codec
    36  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}
    37  
    38  // DefaultGenesis returns default genesis state as raw bytes for the token
    39  // module.
    40  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    41  	return cdc.MustMarshalJSON(token.DefaultGenesisState())
    42  }
    43  
    44  // ValidateGenesis performs genesis state validation for the token module.
    45  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
    46  	var data token.GenesisState
    47  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    48  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", token.ModuleName, err)
    49  	}
    50  
    51  	return token.ValidateGenesis(data)
    52  }
    53  
    54  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the token module.
    55  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
    56  	if err := token.RegisterQueryHandlerClient(context.Background(), mux, token.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  	token.RegisterInterfaces(registry)
    73  }
    74  
    75  //____________________________________________________________________________
    76  
    77  // AppModule implements an application module for the token 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 token 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  	token.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper))
   109  	token.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper))
   110  }
   111  
   112  // InitGenesis performs genesis initialization for the token module. It returns
   113  // no validator updates.
   114  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
   115  	var genesisState token.GenesisState
   116  	cdc.MustUnmarshalJSON(data, &genesisState)
   117  	am.keeper.InitGenesis(ctx, &genesisState)
   118  	return []abci.ValidatorUpdate{}
   119  }
   120  
   121  // ExportGenesis returns the exported genesis state as raw bytes for the token
   122  // module.
   123  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   124  	gs := am.keeper.ExportGenesis(ctx)
   125  	return cdc.MustMarshalJSON(gs)
   126  }
   127  
   128  // ConsensusVersion implements AppModule/ConsensusVersion.
   129  func (AppModule) ConsensusVersion() uint64 { return 1 }
   130  
   131  //____________________________________________________________________________
   132  
   133  // AppModuleSimulation functions
   134  
   135  // // GenerateGenesisState creates a randomized GenState of the token module.
   136  // func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   137  // 	simulation.RandomizedGenState(simState)
   138  // }
   139  
   140  // // ProposalContents returns all the token content functions used to
   141  // // simulate token proposals.
   142  // func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
   143  // 	return simulation.ProposalContents()
   144  // }
   145  
   146  // // RandomizedParams creates randomized token param changes for the simulator.
   147  // func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
   148  // 	return simulation.ParamChanges(r)
   149  // }
   150  
   151  // // RegisterStoreDecoder registers a decoder for token module's types
   152  // func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
   153  // 	sdr[types.StoreKey] = simulation.NewDecodeStore(am.cdc)
   154  // }
   155  
   156  // // WeightedOperations returns the all the token module operations with their respective weights.
   157  // func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
   158  // 	return simulation.WeightedOperations(
   159  // 		simState.AppParams, simState.Cdc,
   160  // 		am.stakingKeeper, am.slashingKeeper, am.keeper, simState.Contents,
   161  // 	)
   162  // }