cosmos-sdk/x/feegrant/module/module.go
Facundo Medica 445dc8afe0
refactor!: use store service in x/feegrant (#15606)
## Description

Closes: #XXXX



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

* [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] added `!` to the type prefix if API or client breaking change
* [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
* [ ] provided a link to the relevant issue or specification
* [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/docs/building-modules)
* [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
* [ ] added a changelog entry to `CHANGELOG.md`
* [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
* [ ] updated the relevant documentation or specification
* [ ] reviewed "Files changed" and left comments if necessary
* [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

* [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
* [ ] confirmed `!` in the type prefix if API or client breaking change
* [ ] confirmed all author checklist items have been addressed 
* [ ] reviewed state machine logic
* [ ] reviewed API design and naming
* [ ] reviewed documentation is accurate
* [ ] reviewed tests and test coverage
* [ ] manually tested (if applicable)
2023-04-04 16:02:16 +00:00

224 lines
7.0 KiB
Go

package module
import (
"context"
"encoding/json"
"fmt"
abci "github.com/cometbft/cometbft/abci/types"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/errors"
modulev1 "cosmossdk.io/api/cosmos/feegrant/module/v1"
"cosmossdk.io/depinject"
"cosmossdk.io/core/store"
"cosmossdk.io/x/feegrant"
"cosmossdk.io/x/feegrant/client/cli"
"cosmossdk.io/x/feegrant/keeper"
"cosmossdk.io/x/feegrant/simulation"
sdkclient "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"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)
var (
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModuleSimulation = AppModule{}
)
// ----------------------------------------------------------------------------
// AppModuleBasic
// ----------------------------------------------------------------------------
// AppModuleBasic defines the basic application module used by the feegrant module.
type AppModuleBasic struct {
cdc codec.Codec
ac address.Codec
}
// Name returns the feegrant module's name.
func (ab AppModuleBasic) Name() string {
return feegrant.ModuleName
}
// RegisterServices registers a gRPC query service to respond to the
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
feegrant.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
feegrant.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := keeper.NewMigrator(am.keeper)
err := cfg.RegisterMigration(feegrant.ModuleName, 1, m.Migrate1to2)
if err != nil {
panic(fmt.Sprintf("failed to migrate x/feegrant from version 1 to 2: %v", err))
}
}
// RegisterLegacyAminoCodec registers the feegrant module's types for the given codec.
func (ab AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
feegrant.RegisterLegacyAminoCodec(cdc)
}
// RegisterInterfaces registers the feegrant module's interface types
func (ab AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
feegrant.RegisterInterfaces(registry)
}
// DefaultGenesis returns default genesis state as raw bytes for the feegrant
// module.
func (ab AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
return cdc.MustMarshalJSON(feegrant.DefaultGenesisState())
}
// ValidateGenesis performs genesis state validation for the feegrant module.
func (ab AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config sdkclient.TxEncodingConfig, bz json.RawMessage) error {
var data feegrant.GenesisState
if err := cdc.UnmarshalJSON(bz, &data); err != nil {
return errors.Wrapf(err, "failed to unmarshal %s genesis state", feegrant.ModuleName)
}
return feegrant.ValidateGenesis(data)
}
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the feegrant module.
func (ab AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *gwruntime.ServeMux) {
if err := feegrant.RegisterQueryHandlerClient(context.Background(), mux, feegrant.NewQueryClient(clientCtx)); err != nil {
panic(err)
}
}
// GetTxCmd returns the root tx command for the feegrant module.
func (ab AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.GetTxCmd(ab.ac)
}
// GetQueryCmd returns no root query command for the feegrant module.
func (ab AppModuleBasic) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd(ab.ac)
}
// ----------------------------------------------------------------------------
// AppModule
// ----------------------------------------------------------------------------
// AppModule implements an application module for the feegrant module.
type AppModule struct {
AppModuleBasic
keeper keeper.Keeper
accountKeeper feegrant.AccountKeeper
bankKeeper feegrant.BankKeeper
registry cdctypes.InterfaceRegistry
}
// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, ak feegrant.AccountKeeper, bk feegrant.BankKeeper, keeper keeper.Keeper, registry cdctypes.InterfaceRegistry) AppModule {
return AppModule{
AppModuleBasic: AppModuleBasic{cdc: cdc, ac: ak},
keeper: keeper,
accountKeeper: ak,
bankKeeper: bk,
registry: registry,
}
}
var (
_ appmodule.AppModule = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
)
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
func (am AppModule) IsOnePerModuleType() {}
// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}
// Name returns the feegrant module's name.
func (AppModule) Name() string {
return feegrant.ModuleName
}
// InitGenesis performs genesis initialization for the feegrant module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, bz json.RawMessage) []abci.ValidatorUpdate {
var gs feegrant.GenesisState
cdc.MustUnmarshalJSON(bz, &gs)
err := am.keeper.InitGenesis(ctx, &gs)
if err != nil {
panic(err)
}
return []abci.ValidatorUpdate{}
}
// ExportGenesis returns the exported genesis state as raw bytes for the feegrant
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
gs, err := am.keeper.ExportGenesis(ctx)
if err != nil {
panic(err)
}
return cdc.MustMarshalJSON(gs)
}
// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 2 }
// EndBlock returns the end blocker for the feegrant module. It returns no validator
// updates.
func (am AppModule) EndBlock(ctx context.Context) error {
c := sdk.UnwrapSDKContext(ctx)
EndBlocker(c, am.keeper)
return nil
}
func init() {
appmodule.Register(&modulev1.Module{},
appmodule.Provide(ProvideModule),
)
}
type FeegrantInputs struct {
depinject.In
StoreService store.KVStoreService
Cdc codec.Codec
AccountKeeper feegrant.AccountKeeper
BankKeeper feegrant.BankKeeper
Registry cdctypes.InterfaceRegistry
}
func ProvideModule(in FeegrantInputs) (keeper.Keeper, appmodule.AppModule) {
k := keeper.NewKeeper(in.Cdc, in.StoreService, in.AccountKeeper)
m := NewAppModule(in.Cdc, in.AccountKeeper, in.BankKeeper, k, in.Registry)
return k, m
}
// AppModuleSimulation functions
// GenerateGenesisState creates a randomized GenState of the feegrant module.
func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
simulation.RandomizedGenState(simState)
}
// RegisterStoreDecoder registers a decoder for feegrant module's types
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
sdr[feegrant.StoreKey] = simulation.NewDecodeStore(am.cdc)
}
// WeightedOperations returns all the feegrant module operations with their respective weights.
func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
return simulation.WeightedOperations(
am.registry, simState.AppParams, simState.Cdc, am.accountKeeper, am.bankKeeper, am.keeper, am.ac,
)
}