Skip to content

Commit

Permalink
minimum module compat
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Aug 15, 2024
1 parent ae1e3b8 commit ff0ebeb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package <%= moduleName %>

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"context"

"<%= modulePath %>/x/<%= moduleName %>/keeper"
"<%= modulePath %>/x/<%= moduleName %>/types"
)

// InitGenesis initializes the module's state from a provided genesis state.
func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) error {
func InitGenesis(ctx context.Context, k keeper.Keeper, genState types.GenesisState) error {
// this line is used by starport scaffolding # genesis/module/init
return k.Params.Set(ctx, genState.Params)
}

// ExportGenesis returns the module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) (*types.GenesisState, error) {
func ExportGenesis(ctx context.Context, k keeper.Keeper) (*types.GenesisState, error) {
var err error

genesis := types.DefaultGenesis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "cosmossdk.io/x/auth/types"
govtypes "cosmossdk.io/x/gov/types"
Expand All @@ -32,12 +31,10 @@ import (
)

var (
_ module.AppModuleBasic = (*AppModule)(nil)
_ module.AppModuleSimulation = (*AppModule)(nil)
_ module.HasGenesis = (*AppModule)(nil)
_ module.HasInvariants = (*AppModule)(nil)
_ module.HasConsensusVersion = (*AppModule)(nil)

_ appmodule.HasGenesis = (*AppModule)(nil)
_ appmodule.HasConsensusVersion = (*AppModule)(nil)
_ appmodule.AppModule = (*AppModule)(nil)
_ appmodule.HasBeginBlocker = (*AppModule)(nil)
_ appmodule.HasEndBlocker = (*AppModule)(nil)
Expand All @@ -51,10 +48,10 @@ var (
// AppModuleBasic implements the AppModuleBasic interface that defines the
// independent methods a Cosmos SDK module needs to implement.
type AppModuleBasic struct {
cdc codec.BinaryCodec
cdc codec.Codec
}

func NewAppModuleBasic(cdc codec.BinaryCodec) AppModuleBasic {
func NewAppModuleBasic(cdc codec.Codec) AppModuleBasic {
return AppModuleBasic{cdc: cdc}
}

Expand All @@ -74,14 +71,14 @@ func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) {

// DefaultGenesis returns a default GenesisState for the module, marshalled to json.RawMessage.
// The default GenesisState need to be defined by the module developer and is primarily used for testing.
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(types.DefaultGenesis())
func (am AppModuleBasic) DefaultGenesis() json.RawMessage {
return am.cdc.MustMarshalJSON(types.DefaultGenesis())
}

// ValidateGenesis used to validate the GenesisState, given in its json.RawMessage form.
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
func (am AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
var genState types.GenesisState
if err := cdc.UnmarshalJSON(bz, &genState); err != nil {
if err := am.cdc.UnmarshalJSON(bz, &genState); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}
return genState.Validate()
Expand Down Expand Up @@ -135,27 +132,25 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
}

// RegisterInvariants registers the invariants of the module. If an invariant deviates from its predicted value, the InvariantRegistry triggers appropriate logic (most often the chain will be halted)
func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}

// InitGenesis performs the module's genesis initialization. It returns no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, gs json.RawMessage) {
func (am AppModule) InitGenesis(ctx context.Context, gs json.RawMessage) error {
var genState types.GenesisState
// Initialize global index to index in genesis state
cdc.MustUnmarshalJSON(gs, &genState)

if err := InitGenesis(ctx, am.keeper, genState); err != nil {
panic(err)
if err := am.cdc.UnmarshalJSON(gs, &genState); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}

return InitGenesis(ctx, am.keeper, genState)
}

// ExportGenesis returns the module's exported genesis state as raw JSON bytes.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) {
genState, err := ExportGenesis(ctx, am.keeper)
if err != nil {
panic(err)
return nil, err
}
return cdc.MustMarshalJSON(genState)

return am.cdc.MarshalJSON(genState)
}

// ConsensusVersion is a sequence number for state-breaking change of the module.
Expand Down

0 comments on commit ff0ebeb

Please sign in to comment.