Setup hooks wiring between auction and registry modules with depinject
This commit is contained in:
parent
ba7a127d7f
commit
43afe3fc2a
@ -76,7 +76,7 @@ type LaconicApp struct {
|
||||
ConsensusParamsKeeper consensuskeeper.Keeper
|
||||
|
||||
// laconic keepers
|
||||
AuctionKeeper auctionkeeper.Keeper
|
||||
AuctionKeeper *auctionkeeper.Keeper // (Use * as per ProvideModule implementation)
|
||||
BondKeeper bondkeeper.Keeper
|
||||
RegistryKeeper registrykeeper.Keeper
|
||||
// RegistryRecordKeeper registrykeeper.RecordKeeper
|
||||
|
23
x/auction/expected_keepers.go
Normal file
23
x/auction/expected_keepers.go
Normal file
@ -0,0 +1,23 @@
|
||||
package auction
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
// AuctionUsageKeeper keep track of auction usage in other modules.
|
||||
// Used to, for example, prevent deletion of a auction that's in use.
|
||||
type AuctionUsageKeeper interface {
|
||||
ModuleName() string
|
||||
UsesAuction(ctx sdk.Context, auctionID string) bool
|
||||
|
||||
OnAuction(ctx sdk.Context, auctionID string)
|
||||
OnAuctionBid(ctx sdk.Context, auctionID string, bidderAddress string)
|
||||
OnAuctionWinnerSelected(ctx sdk.Context, auctionID string)
|
||||
}
|
||||
|
||||
// AuctionHooksWrapper is a wrapper for modules to inject AuctionUsageKeeper using depinject.
|
||||
// Reference: https://github.com/cosmos/cosmos-sdk/tree/v0.50.3/core/appmodule#resolving-circular-dependencies
|
||||
type AuctionHooksWrapper struct{ AuctionUsageKeeper }
|
||||
|
||||
// IsOnePerModuleType implements the depinject.OnePerModuleType interface.
|
||||
func (AuctionHooksWrapper) IsOnePerModuleType() {}
|
@ -73,7 +73,7 @@ type Keeper struct {
|
||||
bankKeeper bank.Keeper
|
||||
|
||||
// Track auction usage in other cosmos-sdk modules (more like a usage tracker).
|
||||
// usageKeepers []types.AuctionUsageKeeper
|
||||
usageKeepers []auctiontypes.AuctionUsageKeeper
|
||||
|
||||
// state management
|
||||
Schema collections.Schema
|
||||
@ -88,7 +88,7 @@ func NewKeeper(
|
||||
storeService storetypes.KVStoreService,
|
||||
accountKeeper auth.AccountKeeper,
|
||||
bankKeeper bank.Keeper,
|
||||
) Keeper {
|
||||
) *Keeper {
|
||||
sb := collections.NewSchemaBuilder(storeService)
|
||||
k := Keeper{
|
||||
cdc: cdc,
|
||||
@ -97,7 +97,7 @@ func NewKeeper(
|
||||
Params: collections.NewItem(sb, auctiontypes.ParamsPrefix, "params", codec.CollValue[auctiontypes.Params](cdc)),
|
||||
Auctions: collections.NewIndexedMap(sb, auctiontypes.AuctionsPrefix, "auctions", collections.StringKey, codec.CollValue[auctiontypes.Auction](cdc), newAuctionIndexes(sb)),
|
||||
Bids: collections.NewIndexedMap(sb, auctiontypes.BidsPrefix, "bids", collections.PairKeyCodec(collections.StringKey, collections.StringKey), codec.CollValue[auctiontypes.Bid](cdc), newBidsIndexes(sb)),
|
||||
// usageKeepers: usageKeepers,
|
||||
usageKeepers: nil,
|
||||
}
|
||||
|
||||
schema, err := sb.Build()
|
||||
@ -107,22 +107,29 @@ func NewKeeper(
|
||||
|
||||
k.Schema = schema
|
||||
|
||||
return k
|
||||
return &k
|
||||
}
|
||||
|
||||
// func (k *Keeper) SetUsageKeepers(usageKeepers []types.AuctionUsageKeeper) {
|
||||
// k.usageKeepers = usageKeepers
|
||||
// }
|
||||
func (k *Keeper) SetUsageKeepers(usageKeepers []auctiontypes.AuctionUsageKeeper) {
|
||||
if k.usageKeepers != nil {
|
||||
panic("cannot set auction hooks twice")
|
||||
}
|
||||
|
||||
k.usageKeepers = usageKeepers
|
||||
}
|
||||
|
||||
// SaveAuction - saves a auction to the store.
|
||||
func (k Keeper) SaveAuction(ctx sdk.Context, auction *auctiontypes.Auction) error {
|
||||
return k.Auctions.Set(ctx, auction.Id, *auction)
|
||||
if err := k.Auctions.Set(ctx, auction.Id, *auction); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// // Notify interested parties.
|
||||
// for _, keeper := range k.usageKeepers {
|
||||
// keeper.OnAuction(ctx, auction.Id)
|
||||
// }
|
||||
// return nil
|
||||
// Notify interested parties.
|
||||
for _, keeper := range k.usageKeepers {
|
||||
keeper.OnAuction(ctx, auction.Id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteAuction - deletes the auction.
|
||||
@ -153,13 +160,16 @@ func (k Keeper) HasAuction(ctx sdk.Context, id string) (bool, error) {
|
||||
|
||||
func (k Keeper) SaveBid(ctx sdk.Context, bid *auctiontypes.Bid) error {
|
||||
key := collections.Join(bid.AuctionId, bid.BidderAddress)
|
||||
return k.Bids.Set(ctx, key, *bid)
|
||||
if err := k.Bids.Set(ctx, key, *bid); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// // Notify interested parties.
|
||||
// for _, keeper := range k.usageKeepers {
|
||||
// keeper.OnAuctionBid(ctx, bid.AuctionId, bid.BidderAddress)
|
||||
// }
|
||||
// return nil
|
||||
// Notify interested parties.
|
||||
for _, keeper := range k.usageKeepers {
|
||||
keeper.OnAuctionBid(ctx, bid.AuctionId, bid.BidderAddress)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k Keeper) DeleteBid(ctx sdk.Context, bid auctiontypes.Bid) error {
|
||||
@ -725,13 +735,12 @@ func (k Keeper) pickAuctionWinner(ctx sdk.Context, auction *auctiontypes.Auction
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// Notify other modules (hook).
|
||||
// ctx.Logger().Info(fmt.Sprintf("Auction %s notifying %d modules.", auction.Id, len(k.usageKeepers)))
|
||||
// for _, keeper := range k.usageKeepers {
|
||||
// ctx.Logger().Info(fmt.Sprintf("Auction %s notifying module %s.", auction.Id, keeper.ModuleName()))
|
||||
// keeper.OnAuctionWinnerSelected(ctx, auction.Id)
|
||||
// }
|
||||
ctx.Logger().Info(fmt.Sprintf("Auction %s notifying %d modules.", auction.Id, len(k.usageKeepers)))
|
||||
for _, keeper := range k.usageKeepers {
|
||||
ctx.Logger().Info(fmt.Sprintf("Auction %s notifying module %s.", auction.Id, keeper.ModuleName()))
|
||||
keeper.OnAuctionWinnerSelected(ctx, auction.Id)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -10,11 +10,11 @@ import (
|
||||
var _ auctiontypes.MsgServer = msgServer{}
|
||||
|
||||
type msgServer struct {
|
||||
k Keeper
|
||||
k *Keeper
|
||||
}
|
||||
|
||||
// NewMsgServerImpl returns an implementation of the module MsgServer interface.
|
||||
func NewMsgServerImpl(keeper Keeper) auctiontypes.MsgServer {
|
||||
func NewMsgServerImpl(keeper *Keeper) auctiontypes.MsgServer {
|
||||
return &msgServer{k: keeper}
|
||||
}
|
||||
|
||||
|
@ -13,11 +13,11 @@ import (
|
||||
var _ auctiontypes.QueryServer = queryServer{}
|
||||
|
||||
type queryServer struct {
|
||||
k Keeper
|
||||
k *Keeper
|
||||
}
|
||||
|
||||
// NewQueryServerImpl returns an implementation of the module QueryServer.
|
||||
func NewQueryServerImpl(k Keeper) auctiontypes.QueryServer {
|
||||
func NewQueryServerImpl(k *Keeper) auctiontypes.QueryServer {
|
||||
return queryServer{k}
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
// EndBlocker is called every block
|
||||
func EndBlocker(ctx context.Context, k keeper.Keeper) error {
|
||||
func EndBlocker(ctx context.Context, k *keeper.Keeper) error {
|
||||
sdkCtx := sdk.UnwrapSDKContext(ctx)
|
||||
|
||||
return k.EndBlockerProcessAuctions(sdkCtx)
|
||||
|
@ -4,12 +4,14 @@ import (
|
||||
"cosmossdk.io/core/appmodule"
|
||||
"cosmossdk.io/core/store"
|
||||
"cosmossdk.io/depinject"
|
||||
"golang.org/x/exp/maps"
|
||||
|
||||
"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"
|
||||
"git.vdb.to/cerc-io/laconic2d/x/auction/keeper"
|
||||
)
|
||||
|
||||
@ -25,6 +27,7 @@ func init() {
|
||||
appmodule.Register(
|
||||
&modulev1.Module{},
|
||||
appmodule.Provide(ProvideModule),
|
||||
appmodule.Invoke(InvokeSetAuctionHooks),
|
||||
)
|
||||
}
|
||||
|
||||
@ -41,7 +44,11 @@ type ModuleInputs struct {
|
||||
type ModuleOutputs struct {
|
||||
depinject.Out
|
||||
|
||||
Keeper keeper.Keeper
|
||||
// Use * as required by InvokeSetAuctionHooks
|
||||
// https://github.com/cosmos/cosmos-sdk/tree/v0.50.3/core/appmodule#invoker-invocation-details
|
||||
// https://github.com/cosmos/cosmos-sdk/tree/v0.50.3/core/appmodule#regular-golang-types
|
||||
Keeper *keeper.Keeper
|
||||
|
||||
Module appmodule.AppModule
|
||||
}
|
||||
|
||||
@ -51,3 +58,25 @@ func ProvideModule(in ModuleInputs) ModuleOutputs {
|
||||
|
||||
return ModuleOutputs{Module: m, Keeper: k}
|
||||
}
|
||||
|
||||
func InvokeSetAuctionHooks(
|
||||
config *modulev1.Module,
|
||||
keeper *keeper.Keeper,
|
||||
auctionHooks map[string]auction.AuctionHooksWrapper,
|
||||
) error {
|
||||
// all arguments to invokers are optional
|
||||
if keeper == nil || config == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var usageKeepers []auction.AuctionUsageKeeper
|
||||
|
||||
for _, modName := range maps.Keys(auctionHooks) {
|
||||
hook := auctionHooks[modName]
|
||||
usageKeepers = append(usageKeepers, hook)
|
||||
}
|
||||
|
||||
keeper.SetUsageKeepers(usageKeepers)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -36,11 +36,11 @@ const ConsensusVersion = 1
|
||||
|
||||
type AppModule struct {
|
||||
cdc codec.Codec
|
||||
keeper keeper.Keeper
|
||||
keeper *keeper.Keeper
|
||||
}
|
||||
|
||||
// NewAppModule creates a new AppModule object
|
||||
func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
|
||||
func NewAppModule(cdc codec.Codec, keeper *keeper.Keeper) AppModule {
|
||||
return AppModule{
|
||||
cdc: cdc,
|
||||
keeper: keeper,
|
||||
|
@ -89,7 +89,7 @@ type Keeper struct {
|
||||
bankKeeper bank.Keeper
|
||||
recordKeeper RecordKeeper
|
||||
bondKeeper bondkeeper.Keeper
|
||||
auctionKeeper auctionkeeper.Keeper
|
||||
auctionKeeper *auctionkeeper.Keeper
|
||||
|
||||
// state management
|
||||
Schema collections.Schema
|
||||
@ -109,7 +109,7 @@ func NewKeeper(
|
||||
bankKeeper bank.Keeper,
|
||||
recordKeeper RecordKeeper,
|
||||
bondKeeper bondkeeper.Keeper,
|
||||
auctionKeeper auctionkeeper.Keeper,
|
||||
auctionKeeper *auctionkeeper.Keeper,
|
||||
) Keeper {
|
||||
sb := collections.NewSchemaBuilder(storeService)
|
||||
k := Keeper{
|
||||
|
@ -8,19 +8,55 @@ import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
|
||||
auctiontypes "git.vdb.to/cerc-io/laconic2d/x/auction"
|
||||
auctionkeeper "git.vdb.to/cerc-io/laconic2d/x/auction/keeper"
|
||||
registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry"
|
||||
)
|
||||
|
||||
// TODO: Add methods
|
||||
|
||||
// Record keeper implements the bond usage keeper interface.
|
||||
var (
|
||||
_ auctiontypes.AuctionUsageKeeper = RecordKeeper{}
|
||||
// _ bondtypes.BondUsageKeeper = (*RecordKeeper)(nil)
|
||||
)
|
||||
|
||||
// RecordKeeper exposes the bare minimal read-only API for other modules.
|
||||
type RecordKeeper struct {
|
||||
cdc codec.BinaryCodec // The wire codec for binary encoding/decoding.
|
||||
auctionKeeper auctionkeeper.Keeper
|
||||
auctionKeeper *auctionkeeper.Keeper
|
||||
// storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context
|
||||
}
|
||||
|
||||
// NewRecordKeeper creates new instances of the registry RecordKeeper
|
||||
func NewRecordKeeper(cdc codec.BinaryCodec, auctionKeeper *auctionkeeper.Keeper) RecordKeeper {
|
||||
return RecordKeeper{
|
||||
auctionKeeper: auctionKeeper,
|
||||
cdc: cdc,
|
||||
}
|
||||
}
|
||||
|
||||
// ModuleName returns the module name.
|
||||
func (k RecordKeeper) ModuleName() string {
|
||||
return registrytypes.ModuleName
|
||||
}
|
||||
|
||||
func (k RecordKeeper) UsesAuction(ctx sdk.Context, auctionID string) bool {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
func (k RecordKeeper) OnAuction(ctx sdk.Context, auctionID string) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
func (k RecordKeeper) OnAuctionBid(ctx sdk.Context, auctionID string, bidderAddress string) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
func (k RecordKeeper) OnAuctionWinnerSelected(ctx sdk.Context, auctionID string) {
|
||||
panic("unimplemented")
|
||||
}
|
||||
|
||||
// RenewRecord renews a record.
|
||||
func (k Keeper) RenewRecord(ctx sdk.Context, msg registrytypes.MsgRenewRecord) error {
|
||||
if has, err := k.HasRecord(ctx, msg.RecordId); !has {
|
||||
|
@ -10,6 +10,7 @@ import (
|
||||
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
|
||||
modulev1 "git.vdb.to/cerc-io/laconic2d/api/cerc/registry/module/v1"
|
||||
auction "git.vdb.to/cerc-io/laconic2d/x/auction"
|
||||
auctionkeeper "git.vdb.to/cerc-io/laconic2d/x/auction/keeper"
|
||||
bondkeeper "git.vdb.to/cerc-io/laconic2d/x/bond/keeper"
|
||||
"git.vdb.to/cerc-io/laconic2d/x/registry/keeper"
|
||||
@ -40,7 +41,7 @@ type ModuleInputs struct {
|
||||
BankKeeper bank.Keeper
|
||||
|
||||
BondKeeper bondkeeper.Keeper
|
||||
AuctionKeeper auctionkeeper.Keeper
|
||||
AuctionKeeper *auctionkeeper.Keeper
|
||||
}
|
||||
|
||||
type ModuleOutputs struct {
|
||||
@ -48,19 +49,23 @@ type ModuleOutputs struct {
|
||||
|
||||
Keeper keeper.Keeper
|
||||
Module appmodule.AppModule
|
||||
|
||||
AuctionHooks auction.AuctionHooksWrapper
|
||||
}
|
||||
|
||||
func ProvideModule(in ModuleInputs) ModuleOutputs {
|
||||
recordKeeper := keeper.NewRecordKeeper(in.Cdc, in.AuctionKeeper)
|
||||
|
||||
k := keeper.NewKeeper(
|
||||
in.Cdc,
|
||||
in.StoreService,
|
||||
in.AccountKeeper,
|
||||
in.BankKeeper,
|
||||
keeper.RecordKeeper{},
|
||||
recordKeeper,
|
||||
in.BondKeeper,
|
||||
in.AuctionKeeper,
|
||||
)
|
||||
m := NewAppModule(in.Cdc, k)
|
||||
|
||||
return ModuleOutputs{Module: m, Keeper: k}
|
||||
return ModuleOutputs{Module: m, Keeper: k, AuctionHooks: auction.AuctionHooksWrapper{AuctionUsageKeeper: recordKeeper}}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user