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

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