Setup module files with depinject and autocli

This commit is contained in:
Prathamesh Musale 2024-02-09 12:35:19 +05:30
parent 19e3cec976
commit f76a6b1a6c
4 changed files with 211 additions and 0 deletions

15
x/auction/module/abci.go Normal file
View File

@ -0,0 +1,15 @@
package module
import (
"context"
"git.vdb.to/cerc-io/laconic2d/x/auction/keeper"
)
// EndBlocker is called every block
func EndBlocker(ctx context.Context, k keeper.Keeper) error {
// TODO: Implement
// k.EndBlockerProcessAuctions(ctx)
return nil
}

View File

@ -0,0 +1,16 @@
package module
import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
"cosmossdk.io/client/v2/autocli"
)
var _ autocli.HasAutoCLIConfig = AppModule{}
// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: nil,
Tx: nil,
}
}

View File

@ -0,0 +1,53 @@
package module
import (
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/codec"
auth "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
modulev1 "git.vdb.to/cerc-io/laconic2d/api/cerc/auction/module/v1"
"git.vdb.to/cerc-io/laconic2d/x/auction/keeper"
)
var _ appmodule.AppModule = AppModule{}
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}
func init() {
appmodule.Register(
&modulev1.Module{},
appmodule.Provide(ProvideModule),
)
}
type ModuleInputs struct {
depinject.In
Cdc codec.Codec
StoreService store.KVStoreService
AccountKeeper auth.AccountKeeper
BankKeeper bank.Keeper
}
type ModuleOutputs struct {
depinject.Out
Keeper keeper.Keeper
Module appmodule.AppModule
}
func ProvideModule(in ModuleInputs) ModuleOutputs {
k := keeper.NewKeeper(in.Cdc, in.StoreService, in.AccountKeeper, in.BankKeeper)
m := NewAppModule(in.Cdc, k)
return ModuleOutputs{Module: m, Keeper: k}
}

127
x/auction/module/module.go Normal file
View File

@ -0,0 +1,127 @@
package module
import (
"context"
"encoding/json"
"fmt"
"cosmossdk.io/core/appmodule"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"git.vdb.to/cerc-io/laconic2d/x/auction"
"git.vdb.to/cerc-io/laconic2d/x/auction/keeper"
)
// TODO: Port remaining AppModule methods
var (
_ module.AppModuleBasic = AppModule{}
_ appmodule.AppModule = AppModule{}
_ module.HasGenesis = AppModule{}
_ module.HasServices = AppModule{}
_ module.HasConsensusVersion = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
)
// ConsensusVersion defines the current module consensus version
const ConsensusVersion = 1
type AppModule struct {
cdc codec.Codec
keeper keeper.Keeper
}
// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
return AppModule{
cdc: cdc,
keeper: keeper,
}
}
func NewAppModuleBasic(m AppModule) module.AppModuleBasic {
return module.CoreAppModuleBasicAdaptor(m.Name(), m)
}
// module.AppModuleBasic
// Name returns the auction module's name.
func (AppModule) Name() string { return auction.ModuleName }
// RegisterLegacyAminoCodec registers the auction module's types on the LegacyAmino codec.
// New modules do not need to support Amino.
func (AppModule) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the auction module.
func (AppModule) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) {
if err := auction.RegisterQueryHandlerClient(context.Background(), mux, auction.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}
// RegisterInterfaces registers interfaces and implementations of the auction module.
func (AppModule) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
auction.RegisterInterfaces(registry)
}
// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
// module.HasGenesis
// DefaultGenesis returns default genesis state as raw bytes for the module.
func (AppModule) DefaultGenesis(jsonCodec codec.JSONCodec) json.RawMessage {
return jsonCodec.MustMarshalJSON(auction.DefaultGenesisState())
}
// ValidateGenesis performs genesis state validation for the module.
func (AppModule) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, message json.RawMessage) error {
var data auction.GenesisState
if err := cdc.UnmarshalJSON(message, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", auction.ModuleName, err)
}
return data.Validate()
}
// InitGenesis performs genesis initialization for the checkers module.
// It returns no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) {
var genesisState auction.GenesisState
cdc.MustUnmarshalJSON(data, &genesisState)
if err := am.keeper.InitGenesis(ctx, &genesisState); err != nil {
panic(fmt.Sprintf("failed to initialize %s genesis state: %v", auction.ModuleName, err))
}
}
// ExportGenesis returns the exported genesis state as raw bytes for the circuit
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs, err := am.keeper.ExportGenesis(ctx)
if err != nil {
panic(fmt.Sprintf("failed to export %s genesis state: %v", auction.ModuleName, err))
}
return cdc.MustMarshalJSON(gs)
}
// module.HasServices
func (am AppModule) RegisterServices(cfg module.Configurator) {
// Register servers
// auction.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
// auction.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
}
// appmodule.HasEndBlocker
func (am AppModule) EndBlock(ctx context.Context) error {
return EndBlocker(ctx, am.keeper)
}