github.com/KiraCore/sekai@v0.3.43/x/tokens/module.go (about)

     1  package tokens
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
     8  
     9  	tokenscli "github.com/KiraCore/sekai/x/tokens/client/cli"
    10  	tokenskeeper "github.com/KiraCore/sekai/x/tokens/keeper"
    11  	"github.com/KiraCore/sekai/x/tokens/types"
    12  	tokenstypes "github.com/KiraCore/sekai/x/tokens/types"
    13  
    14  	abci "github.com/cometbft/cometbft/abci/types"
    15  	"github.com/cosmos/cosmos-sdk/client"
    16  	"github.com/cosmos/cosmos-sdk/codec"
    17  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    18  	sdk "github.com/cosmos/cosmos-sdk/types"
    19  	"github.com/cosmos/cosmos-sdk/types/module"
    20  	"github.com/gorilla/mux"
    21  	"github.com/spf13/cobra"
    22  )
    23  
    24  var (
    25  	_ module.AppModule      = AppModule{}
    26  	_ module.AppModuleBasic = AppModuleBasic{}
    27  )
    28  
    29  type AppModuleBasic struct{}
    30  
    31  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the staking module.
    32  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
    33  }
    34  
    35  func (b AppModuleBasic) Name() string {
    36  	return tokenstypes.ModuleName
    37  }
    38  
    39  func (b AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
    40  	tokenstypes.RegisterInterfaces(registry)
    41  }
    42  
    43  func (b AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    44  	return cdc.MustMarshalJSON(tokenstypes.DefaultGenesis())
    45  }
    46  
    47  func (b AppModuleBasic) ValidateGenesis(marshaler codec.JSONCodec, config client.TxEncodingConfig, message json.RawMessage) error {
    48  	return nil
    49  }
    50  
    51  func (b AppModuleBasic) RegisterRESTRoutes(clientCtx client.Context, router *mux.Router) {
    52  }
    53  
    54  func (b AppModuleBasic) RegisterGRPCRoutes(clientCtx client.Context, serveMux *runtime.ServeMux) {
    55  	tokenstypes.RegisterQueryHandlerClient(context.Background(), serveMux, types.NewQueryClient(clientCtx))
    56  }
    57  
    58  func (b AppModuleBasic) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) {
    59  	tokenstypes.RegisterCodec(amino)
    60  }
    61  
    62  func (b AppModuleBasic) GetTxCmd() *cobra.Command {
    63  	return tokenscli.NewTxCmd()
    64  }
    65  
    66  // GetQueryCmd implement query commands for this module
    67  func (b AppModuleBasic) GetQueryCmd() *cobra.Command {
    68  	return tokenscli.NewQueryCmd()
    69  }
    70  
    71  // AppModule for tokens management
    72  type AppModule struct {
    73  	AppModuleBasic
    74  	tokensKeeper    tokenskeeper.Keeper
    75  	customGovKeeper tokenstypes.CustomGovKeeper
    76  }
    77  
    78  // RegisterQueryService registers a GRPC query service to respond to the
    79  // module-specific GRPC queries.
    80  func (am AppModule) RegisterServices(cfg module.Configurator) {
    81  	tokenstypes.RegisterMsgServer(cfg.MsgServer(), tokenskeeper.NewMsgServerImpl(am.tokensKeeper, am.customGovKeeper))
    82  	querier := tokenskeeper.NewQuerier(am.tokensKeeper)
    83  	tokenstypes.RegisterQueryServer(cfg.QueryServer(), querier)
    84  }
    85  
    86  func (am AppModule) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
    87  	tokenstypes.RegisterInterfaces(registry)
    88  }
    89  
    90  func (am AppModule) InitGenesis(
    91  	ctx sdk.Context,
    92  	cdc codec.JSONCodec,
    93  	data json.RawMessage,
    94  ) []abci.ValidatorUpdate {
    95  	var genesisState tokenstypes.GenesisState
    96  	cdc.MustUnmarshalJSON(data, &genesisState)
    97  
    98  	for _, alias := range genesisState.Aliases {
    99  		am.tokensKeeper.UpsertTokenAlias(ctx, *alias)
   100  	}
   101  
   102  	for _, rate := range genesisState.Rates {
   103  		am.tokensKeeper.UpsertTokenRate(ctx, *rate)
   104  	}
   105  
   106  	am.tokensKeeper.SetTokenBlackWhites(ctx, genesisState.TokenBlackWhites)
   107  
   108  	return nil
   109  }
   110  
   111  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   112  	var genesisState tokenstypes.GenesisState
   113  	genesisState.Aliases = am.tokensKeeper.ListTokenAlias(ctx)
   114  	genesisState.Rates = am.tokensKeeper.GetAllTokenRates(ctx)
   115  	genesisState.TokenBlackWhites = am.tokensKeeper.GetTokenBlackWhites(ctx)
   116  	return cdc.MustMarshalJSON(&genesisState)
   117  }
   118  
   119  // ConsensusVersion implements AppModule/ConsensusVersion.
   120  func (AppModule) ConsensusVersion() uint64 { return 1 }
   121  
   122  func (am AppModule) RegisterInvariants(registry sdk.InvariantRegistry) {}
   123  
   124  func (am AppModule) QuerierRoute() string {
   125  	return tokenstypes.QuerierRoute
   126  }
   127  
   128  func (am AppModule) BeginBlock(clientCtx sdk.Context, block abci.RequestBeginBlock) {}
   129  
   130  func (am AppModule) EndBlock(ctx sdk.Context, block abci.RequestEndBlock) []abci.ValidatorUpdate {
   131  	return nil
   132  }
   133  
   134  func (am AppModule) Name() string {
   135  	return tokenstypes.ModuleName
   136  }
   137  
   138  // NewAppModule returns a new Custom Staking module.
   139  func NewAppModule(
   140  	keeper tokenskeeper.Keeper,
   141  	customGovKeeper tokenstypes.CustomGovKeeper,
   142  ) AppModule {
   143  	return AppModule{
   144  		tokensKeeper:    keeper,
   145  		customGovKeeper: customGovKeeper,
   146  	}
   147  }