137 lines
4.3 KiB
Go
137 lines
4.3 KiB
Go
package module
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
|
|
"google.golang.org/grpc"
|
|
|
|
"cosmossdk.io/core/appmodule"
|
|
"cosmossdk.io/core/registry"
|
|
"cosmossdk.io/errors"
|
|
"cosmossdk.io/x/nft"
|
|
"cosmossdk.io/x/nft/keeper"
|
|
"cosmossdk.io/x/nft/simulation"
|
|
|
|
sdkclient "github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/simsx"
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
|
|
)
|
|
|
|
var (
|
|
_ module.HasGRPCGateway = AppModule{}
|
|
_ module.AppModuleSimulation = AppModule{}
|
|
|
|
_ appmodule.AppModule = AppModule{}
|
|
_ appmodule.HasGenesis = AppModule{}
|
|
_ appmodule.HasRegisterInterfaces = AppModule{}
|
|
)
|
|
|
|
const ConsensusVersion = 1
|
|
|
|
// AppModule implements the sdk.AppModule interface
|
|
type AppModule struct {
|
|
cdc codec.Codec
|
|
registry cdctypes.InterfaceRegistry
|
|
|
|
keeper keeper.Keeper
|
|
accountKeeper nft.AccountKeeper
|
|
bankKeeper nft.BankKeeper
|
|
}
|
|
|
|
// NewAppModule creates a new AppModule object
|
|
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak nft.AccountKeeper, bk nft.BankKeeper, registry cdctypes.InterfaceRegistry) AppModule {
|
|
return AppModule{
|
|
cdc: cdc,
|
|
keeper: keeper,
|
|
accountKeeper: ak,
|
|
bankKeeper: bk,
|
|
registry: registry,
|
|
}
|
|
}
|
|
|
|
// IsAppModule implements the appmodule.AppModule interface.
|
|
func (AppModule) IsAppModule() {}
|
|
|
|
// Name returns the nft module's name.
|
|
// Deprecated: kept for legacy reasons.
|
|
func (AppModule) Name() string {
|
|
return nft.ModuleName
|
|
}
|
|
|
|
// RegisterServices registers a gRPC query service to respond to the
|
|
// module-specific gRPC queries.
|
|
func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
|
|
nft.RegisterMsgServer(registrar, am.keeper)
|
|
nft.RegisterQueryServer(registrar, am.keeper)
|
|
return nil
|
|
}
|
|
|
|
// RegisterInterfaces registers the nft module's interface types
|
|
func (AppModule) RegisterInterfaces(registrar registry.InterfaceRegistrar) {
|
|
nft.RegisterInterfaces(registrar)
|
|
}
|
|
|
|
// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the nft module.
|
|
func (AppModule) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *gwruntime.ServeMux) {
|
|
if err := nft.RegisterQueryHandlerClient(context.Background(), mux, nft.NewQueryClient(clientCtx)); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// DefaultGenesis returns default genesis state as raw bytes for the nft module.
|
|
func (am AppModule) DefaultGenesis() json.RawMessage {
|
|
return am.cdc.MustMarshalJSON(nft.DefaultGenesisState())
|
|
}
|
|
|
|
// ValidateGenesis performs genesis state validation for the nft module.
|
|
func (am AppModule) ValidateGenesis(bz json.RawMessage) error {
|
|
var data nft.GenesisState
|
|
if err := am.cdc.UnmarshalJSON(bz, &data); err != nil {
|
|
return errors.Wrapf(err, "failed to unmarshal %s genesis state", nft.ModuleName)
|
|
}
|
|
|
|
return nft.ValidateGenesis(data, am.accountKeeper.AddressCodec())
|
|
}
|
|
|
|
// InitGenesis performs genesis initialization for the nft module.
|
|
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error {
|
|
var genesisState nft.GenesisState
|
|
if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil {
|
|
return err
|
|
}
|
|
return am.keeper.InitGenesis(ctx, &genesisState)
|
|
}
|
|
|
|
// ExportGenesis returns the exported genesis state as raw bytes for the nft module.
|
|
func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) {
|
|
gs, err := am.keeper.ExportGenesis(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return am.cdc.MarshalJSON(gs)
|
|
}
|
|
|
|
// ConsensusVersion implements HasConsensusVersion
|
|
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
|
|
|
|
// AppModuleSimulation functions
|
|
|
|
// GenerateGenesisState creates a randomized GenState of the nft module.
|
|
func (am AppModule) GenerateGenesisState(simState *module.SimulationState) {
|
|
simulation.RandomizedGenState(simState, am.accountKeeper.AddressCodec())
|
|
}
|
|
|
|
// RegisterStoreDecoder registers a decoder for nft module's types
|
|
func (am AppModule) RegisterStoreDecoder(sdr simtypes.StoreDecoderRegistry) {
|
|
sdr[keeper.StoreKey] = simulation.NewDecodeStore(am.cdc)
|
|
}
|
|
|
|
func (am AppModule) WeightedOperationsX(weights simsx.WeightSource, reg simsx.Registry) {
|
|
reg.Add(weights.Get("msg_send", 100), simulation.MsgSendFactory(am.keeper))
|
|
}
|