github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/farm/module.go (about)

     1  package farm
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/gorilla/mux"
     7  	"github.com/spf13/cobra"
     8  
     9  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    10  
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    13  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    14  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    15  	"github.com/fibonacci-chain/fbc/x/farm/client/cli"
    16  	"github.com/fibonacci-chain/fbc/x/farm/client/rest"
    17  	"github.com/fibonacci-chain/fbc/x/farm/keeper"
    18  	"github.com/fibonacci-chain/fbc/x/farm/types"
    19  )
    20  
    21  // Type check to ensure the interface is properly implemented
    22  var (
    23  	_ module.AppModule      = AppModule{}
    24  	_ module.AppModuleBasic = AppModuleBasic{}
    25  )
    26  
    27  // AppModuleBasic defines the basic application module used by the farm module.
    28  type AppModuleBasic struct{}
    29  
    30  // Name returns the farm module's name.
    31  func (AppModuleBasic) Name() string {
    32  	return types.ModuleName
    33  }
    34  
    35  // RegisterCodec registers the farm module's types for the given codec.
    36  func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
    37  	types.RegisterCodec(cdc)
    38  }
    39  
    40  // DefaultGenesis returns default genesis state as raw bytes for the farm
    41  // module.
    42  func (AppModuleBasic) DefaultGenesis() json.RawMessage {
    43  	return types.ModuleCdc.MustMarshalJSON(types.DefaultGenesisState())
    44  }
    45  
    46  // ValidateGenesis performs genesis state validation for the farm module.
    47  func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
    48  	var data types.GenesisState
    49  	err := types.ModuleCdc.UnmarshalJSON(bz, &data)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	return types.ValidateGenesis(data)
    54  }
    55  
    56  // RegisterRESTRoutes registers the REST routes for the farm module.
    57  func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
    58  	rest.RegisterRoutes(ctx, rtr)
    59  }
    60  
    61  // GetTxCmd returns the root tx command for the farm module.
    62  func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
    63  	return cli.GetTxCmd(cdc)
    64  }
    65  
    66  // GetQueryCmd returns no root query command for the farm module.
    67  func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
    68  	return cli.GetQueryCmd(types.StoreKey, cdc)
    69  }
    70  
    71  //____________________________________________________________________________
    72  
    73  // AppModule implements an application module for the farm module.
    74  type AppModule struct {
    75  	AppModuleBasic
    76  
    77  	keeper keeper.Keeper
    78  }
    79  
    80  // NewAppModule creates a new AppModule object
    81  func NewAppModule(k keeper.Keeper) AppModule {
    82  	return AppModule{
    83  		AppModuleBasic: AppModuleBasic{},
    84  		keeper:         k,
    85  	}
    86  }
    87  
    88  // RegisterInvariants registers the farm module invariants.
    89  func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
    90  	RegisterInvariants(ir, am.keeper)
    91  }
    92  
    93  // Route returns the message routing key for the farm module.
    94  func (AppModule) Route() string {
    95  	return types.RouterKey
    96  }
    97  
    98  // NewHandler returns an sdk.Handler for the farm module.
    99  func (am AppModule) NewHandler() sdk.Handler {
   100  	return NewHandler(am.keeper)
   101  }
   102  
   103  // QuerierRoute returns the farm module's querier route name.
   104  func (AppModule) QuerierRoute() string {
   105  	return types.QuerierRoute
   106  }
   107  
   108  // NewQuerierHandler returns the farm module sdk.Querier.
   109  func (am AppModule) NewQuerierHandler() sdk.Querier {
   110  	return keeper.NewQuerier(am.keeper)
   111  }
   112  
   113  // InitGenesis performs genesis initialization for the farm module. It returns
   114  // no validator updates.
   115  func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
   116  	var genesisState types.GenesisState
   117  	types.ModuleCdc.MustUnmarshalJSON(data, &genesisState)
   118  	InitGenesis(ctx, am.keeper, genesisState)
   119  	return []abci.ValidatorUpdate{}
   120  }
   121  
   122  // ExportGenesis returns the exported genesis state as raw bytes for the farm
   123  // module.
   124  func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
   125  	gs := ExportGenesis(ctx, am.keeper)
   126  	return types.ModuleCdc.MustMarshalJSON(gs)
   127  }
   128  
   129  // BeginBlock returns the begin blocker for the farm module.
   130  func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
   131  	BeginBlocker(ctx, req, am.keeper)
   132  }
   133  
   134  // EndBlock returns the end blocker for the farm module. It returns no validator
   135  // updates.
   136  func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   137  	return []abci.ValidatorUpdate{}
   138  }