github.com/Finschia/finschia-sdk@v0.49.1/x/fswap/module.go (about)

     1  package fswap
     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  	cdctypes "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/fswap/client/cli"
    18  	"github.com/Finschia/finschia-sdk/x/fswap/keeper"
    19  	"github.com/Finschia/finschia-sdk/x/fswap/types"
    20  )
    21  
    22  var (
    23  	_ module.AppModule         = AppModule{}
    24  	_ module.AppModuleBasic    = AppModuleBasic{}
    25  	_ module.EndBlockAppModule = AppModule{}
    26  )
    27  
    28  // ----------------------------------------------------------------------------
    29  // AppModuleBasic
    30  // ----------------------------------------------------------------------------
    31  
    32  // AppModuleBasic implements the AppModuleBasic interface for the fswap module.
    33  type AppModuleBasic struct {
    34  	cdc codec.BinaryCodec
    35  }
    36  
    37  func NewAppModuleBasic(cdc codec.BinaryCodec) AppModuleBasic {
    38  	return AppModuleBasic{cdc: cdc}
    39  }
    40  
    41  // Name returns the fswap module's name.
    42  func (AppModuleBasic) Name() string {
    43  	return types.ModuleName
    44  }
    45  
    46  func (AppModuleBasic) RegisterLegacyAminoCodec(_ *codec.LegacyAmino) {}
    47  
    48  // RegisterInterfaces registers the module's interface types
    49  func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {
    50  	types.RegisterInterfaces(reg)
    51  }
    52  
    53  // DefaultGenesis returns the fswap module's default genesis state.
    54  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    55  	return cdc.MustMarshalJSON(types.DefaultGenesis())
    56  }
    57  
    58  // ValidateGenesis performs genesis state validation for the fswap module.
    59  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, _ client.TxEncodingConfig, bz json.RawMessage) error {
    60  	var genState types.GenesisState
    61  	if err := cdc.UnmarshalJSON(bz, &genState); err != nil {
    62  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    63  	}
    64  	return genState.Validate()
    65  }
    66  
    67  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the 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 fswap module's root tx command.
    75  func (a AppModuleBasic) GetTxCmd() *cobra.Command {
    76  	return cli.GetTxCmd()
    77  }
    78  
    79  // GetQueryCmd returns the fswap module's root query command.
    80  func (AppModuleBasic) GetQueryCmd() *cobra.Command {
    81  	return cli.GetQueryCmd(types.StoreKey)
    82  }
    83  
    84  // ----------------------------------------------------------------------------
    85  // AppModule
    86  // ----------------------------------------------------------------------------
    87  
    88  // AppModule implements the AppModule interface for the fswap module.
    89  type AppModule struct {
    90  	AppModuleBasic
    91  
    92  	keeper     keeper.Keeper
    93  	bankKeeper keeper.BankKeeper
    94  }
    95  
    96  func NewAppModule(
    97  	cdc codec.Codec,
    98  	keeper keeper.Keeper,
    99  	bankKeeper keeper.BankKeeper,
   100  ) AppModule {
   101  	return AppModule{
   102  		AppModuleBasic: NewAppModuleBasic(cdc),
   103  		keeper:         keeper,
   104  		bankKeeper:     bankKeeper,
   105  	}
   106  }
   107  
   108  // Name returns the fswap module's name.
   109  func (am AppModule) Name() string {
   110  	return am.AppModuleBasic.Name()
   111  }
   112  
   113  // Route returns the fswap module's message routing key.
   114  func (AppModule) Route() sdk.Route { return sdk.Route{} }
   115  
   116  // QuerierRoute returns the fswap module's query routing key.
   117  func (AppModule) QuerierRoute() string { return types.QuerierRoute }
   118  
   119  // LegacyQuerierHandler returns the fswap module's Querier.
   120  func (am AppModule) LegacyQuerierHandler(_ *codec.LegacyAmino) sdk.Querier {
   121  	return nil
   122  }
   123  
   124  // RegisterServices registers a GRPC query service to respond to the
   125  // module-specific GRPC queries.
   126  func (am AppModule) RegisterServices(cfg module.Configurator) {
   127  	types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServer(am.keeper))
   128  	types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServer(am.keeper))
   129  }
   130  
   131  // RegisterInvariants registers the fswap module's invariants.
   132  func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
   133  
   134  // InitGenesis performs the fswap module's genesis initialization It returns
   135  // no validator updates.
   136  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) []abci.ValidatorUpdate {
   137  	genState := types.GenesisState{}
   138  	// Initialize global index to index in genesis state
   139  	cdc.MustUnmarshalJSON(gs, &genState)
   140  	if err := am.keeper.InitGenesis(ctx, &genState); err != nil {
   141  		panic(err)
   142  	}
   143  	return []abci.ValidatorUpdate{}
   144  }
   145  
   146  // ExportGenesis returns the fswap module's exported genesis state as raw JSON bytes.
   147  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   148  	genState := am.keeper.ExportGenesis(ctx)
   149  	return cdc.MustMarshalJSON(genState)
   150  }
   151  
   152  // ConsensusVersion implements ConsensusVersion.
   153  func (AppModule) ConsensusVersion() uint64 { return 1 }
   154  
   155  // BeginBlock executes all ABCI BeginBlock logic respective to the fswap module.
   156  func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {}
   157  
   158  // EndBlock executes all ABCI EndBlock logic respective to the fswap module. It
   159  // returns no validator updates.
   160  func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   161  	return []abci.ValidatorUpdate{}
   162  }