From 7be3d59fb5f0aa3c5204fc97803f08d9016ca6d1 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Mon, 25 Mar 2024 20:45:10 +0100 Subject: [PATCH] feat: auction module (#2470) * feat: reards auction proto * lint * update genesis * fix * add usd rewards * update params * add events * update genesis * update * feat: auction module * module wiring * syntax * comment * fix app keys * fix codec --- app/app.go | 43 ++++++--- x/auction/codec.go | 42 +++++++++ x/auction/expected_keepers.go | 4 + x/auction/genesis.go | 12 +++ x/auction/keeper/genesis.go | 14 +++ x/auction/keeper/keeper.go | 26 ++++++ x/auction/keys.go | 9 ++ x/auction/module/genesis.go | 19 ++++ x/auction/module/module.go | 159 ++++++++++++++++++++++++++++++++++ 9 files changed, 318 insertions(+), 10 deletions(-) create mode 100644 x/auction/codec.go create mode 100644 x/auction/expected_keepers.go create mode 100644 x/auction/genesis.go create mode 100644 x/auction/keeper/genesis.go create mode 100644 x/auction/keeper/keeper.go create mode 100644 x/auction/keys.go create mode 100644 x/auction/module/genesis.go create mode 100644 x/auction/module/module.go diff --git a/app/app.go b/app/app.go index fa243d5d36..b8dbb26315 100644 --- a/app/app.go +++ b/app/app.go @@ -130,6 +130,9 @@ import ( uwasm "github.com/umee-network/umee/v6/app/wasm" "github.com/umee-network/umee/v6/swagger" "github.com/umee-network/umee/v6/util/genmap" + "github.com/umee-network/umee/v6/x/auction" + auctionkeeper "github.com/umee-network/umee/v6/x/auction/keeper" + auctionmodule "github.com/umee-network/umee/v6/x/auction/module" "github.com/umee-network/umee/v6/x/incentive" incentivekeeper "github.com/umee-network/umee/v6/x/incentive/keeper" incentivemodule "github.com/umee-network/umee/v6/x/incentive/module" @@ -200,11 +203,13 @@ func init() { ica.AppModuleBasic{}, // intertx.AppModuleBasic{}, // ibcfee.AppModuleBasic{}, + leverage.AppModuleBasic{}, oracle.AppModuleBasic{}, uibcmodule.AppModuleBasic{}, ugovmodule.AppModuleBasic{}, wasm.AppModuleBasic{}, + auctionmodule.AppModuleBasic{}, incentivemodule.AppModuleBasic{}, metokenmodule.AppModuleBasic{}, packetforward.AppModuleBasic{}, @@ -228,11 +233,12 @@ func init() { leveragetypes.ModuleName: {authtypes.Minter, authtypes.Burner}, wasmtypes.ModuleName: {authtypes.Burner}, + auction.ModuleName: nil, incentive.ModuleName: nil, + metoken.ModuleName: {authtypes.Minter, authtypes.Burner}, oracletypes.ModuleName: nil, - uibc.ModuleName: nil, ugov.ModuleName: nil, - metoken.ModuleName: {authtypes.Minter, authtypes.Burner}, + uibc.ModuleName: nil, } // if Experimental {} } @@ -279,11 +285,13 @@ type UmeeApp struct { PacketForwardKeeper *packetforwardkeeper.Keeper ICAHostKeeper icahostkeeper.Keeper LeverageKeeper leveragekeeper.Keeper - IncentiveKeeper incentivekeeper.Keeper - OracleKeeper oraclekeeper.Keeper - UIbcQuotaKeeperB uibcquota.Builder - UGovKeeperB ugovkeeper.Builder - MetokenKeeperB metokenkeeper.Builder + + AuctionKeeperB auctionkeeper.Builder + IncentiveKeeper incentivekeeper.Keeper + MetokenKeeperB metokenkeeper.Builder + OracleKeeper oraclekeeper.Keeper + UGovKeeperB ugovkeeper.Builder + UIbcQuotaKeeperB uibcquota.Builder // make scoped keepers public for testing purposes ScopedIBCKeeper capabilitykeeper.ScopedKeeper @@ -335,12 +343,17 @@ func New( evidencetypes.StoreKey, capabilitytypes.StoreKey, authzkeeper.StoreKey, nftkeeper.StoreKey, group.StoreKey, ibcexported.StoreKey, ibctransfertypes.StoreKey, icahosttypes.StoreKey, - leveragetypes.StoreKey, oracletypes.StoreKey, packetforwardtypes.StoreKey, - uibc.StoreKey, ugov.StoreKey, + packetforwardtypes.StoreKey, wasmtypes.StoreKey, + consensusparamstypes.StoreKey, crisistypes.StoreKey, + + auction.StoreKey, incentive.StoreKey, + leveragetypes.StoreKey, metoken.StoreKey, - consensusparamstypes.StoreKey, crisistypes.StoreKey, + oracletypes.StoreKey, + ugov.StoreKey, + uibc.StoreKey, } // if Experimental {} @@ -503,6 +516,11 @@ func New( app.UGovKeeperB.EmergencyGroup, ) + app.AuctionKeeperB = auctionkeeper.NewBuilder( + appCodec, + keys[auction.StoreKey], + ) + // register the staking hooks // NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks app.StakingKeeper.SetHooks( @@ -734,6 +752,7 @@ func New( wasm.NewAppModule(app.appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.MsgServiceRouter(), app.GetSubspace(wasmtypes.ModuleName)), //nolint: lll incentivemodule.NewAppModule(appCodec, app.IncentiveKeeper, app.BankKeeper, app.LeverageKeeper), metokenmodule.NewAppModule(appCodec, app.MetokenKeeperB), + auctionmodule.NewAppModule(appCodec, app.AuctionKeeperB, app.BankKeeper), } // if Experimental {} @@ -774,6 +793,7 @@ func New( ugov.ModuleName, wasmtypes.ModuleName, incentive.ModuleName, + auction.ModuleName, } endBlockers := []string{ crisistypes.ModuleName, @@ -793,6 +813,7 @@ func New( ugov.ModuleName, wasmtypes.ModuleName, incentive.ModuleName, + auction.ModuleName, } // NOTE: The genutils module must occur after staking so that pools are @@ -818,6 +839,7 @@ func New( wasmtypes.ModuleName, incentive.ModuleName, metoken.ModuleName, + auction.ModuleName, } orderMigrations := []string{ capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, @@ -835,6 +857,7 @@ func New( wasmtypes.ModuleName, incentive.ModuleName, metoken.ModuleName, + auction.ModuleName, } // if Experimental {} diff --git a/x/auction/codec.go b/x/auction/codec.go new file mode 100644 index 0000000000..38032406f2 --- /dev/null +++ b/x/auction/codec.go @@ -0,0 +1,42 @@ +package auction + +import ( + "github.com/cosmos/cosmos-sdk/codec" + cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + // "github.com/cosmos/cosmos-sdk/types/msgservice" +) + +// Amino codecs +// Note, the ModuleCdc should ONLY be used in certain instances of tests and for JSON +// encoding as Amino is still used for that purpose. +var ( + amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewAminoCodec(amino) +) + +func init() { + RegisterLegacyAminoCodec(amino) + cryptocodec.RegisterCrypto(amino) + amino.Seal() +} + +// RegisterLegacyAminoCodec registers the necessary x/uibc interfaces and +// concrete types on the provided LegacyAmino codec. These types are used for +// Amino JSON serialization. +func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + cdc.RegisterConcrete(&MsgGovSetRewardsParams{}, "umee/auction/MsgGovSetRewardsParams", nil) + cdc.RegisterConcrete(&MsgRewardsBid{}, "umee/auction/MsgRewardsBid", nil) +} + +func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { + registry.RegisterImplementations( + (*sdk.Msg)(nil), + // &MsgGovSetRewardsParams{}, + // &MsgRewardsBid{}, + ) + + // TODO + // msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) +} diff --git a/x/auction/expected_keepers.go b/x/auction/expected_keepers.go new file mode 100644 index 0000000000..22d7036807 --- /dev/null +++ b/x/auction/expected_keepers.go @@ -0,0 +1,4 @@ +package auction + +// TODO: create a shared interface for bank keeper in /sdkutils +type BankKeeper interface{} diff --git a/x/auction/genesis.go b/x/auction/genesis.go new file mode 100644 index 0000000000..a9f5de0334 --- /dev/null +++ b/x/auction/genesis.go @@ -0,0 +1,12 @@ +package auction + +// DefaultGenesis creates a default genesis state +func DefaultGenesis() *GenesisState { + // TODO + return &GenesisState{} +} + +func (gs *GenesisState) Validate() error { + // TODO + return nil +} diff --git a/x/auction/keeper/genesis.go b/x/auction/keeper/genesis.go new file mode 100644 index 0000000000..731e52f17e --- /dev/null +++ b/x/auction/keeper/genesis.go @@ -0,0 +1,14 @@ +package keeper + +import ( + "github.com/umee-network/umee/v6/x/auction" +) + +func (k Keeper) ExportGenesis() *auction.GenesisState { + // TODO + return &auction.GenesisState{} +} + +func (k Keeper) InitGenesis(*auction.GenesisState) { + // TODO +} diff --git a/x/auction/keeper/keeper.go b/x/auction/keeper/keeper.go new file mode 100644 index 0000000000..c086b5b474 --- /dev/null +++ b/x/auction/keeper/keeper.go @@ -0,0 +1,26 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/codec" + storetypes "github.com/cosmos/cosmos-sdk/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +type Builder struct { + cdc codec.Codec + storeKey storetypes.StoreKey +} + +func NewBuilder(cdc codec.Codec, key storetypes.StoreKey) Builder { + return Builder{cdc: cdc, storeKey: key} +} + +func (kb Builder) Keeper(ctx *sdk.Context) Keeper { + return Keeper{ + ctx: ctx, + } +} + +type Keeper struct { + ctx *sdk.Context +} diff --git a/x/auction/keys.go b/x/auction/keys.go new file mode 100644 index 0000000000..ea72d54f41 --- /dev/null +++ b/x/auction/keys.go @@ -0,0 +1,9 @@ +package auction + +const ( + // ModuleName defines the module name + ModuleName = "auction" + + // StoreKey defines the primary module store key + StoreKey = ModuleName +) diff --git a/x/auction/module/genesis.go b/x/auction/module/genesis.go new file mode 100644 index 0000000000..4abed50fa0 --- /dev/null +++ b/x/auction/module/genesis.go @@ -0,0 +1,19 @@ +package module + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/umee-network/umee/v6/x/incentive" + "github.com/umee-network/umee/v6/x/incentive/keeper" +) + +// InitGenesis initializes the x/incentive module's state from a provided genesis +// state. +func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState incentive.GenesisState) { + k.InitGenesis(ctx, genState) +} + +// ExportGenesis returns the x/incentive module's exported genesis state. +func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *incentive.GenesisState { + return k.ExportGenesis(ctx) +} diff --git a/x/auction/module/module.go b/x/auction/module/module.go new file mode 100644 index 0000000000..218ad7c23e --- /dev/null +++ b/x/auction/module/module.go @@ -0,0 +1,159 @@ +package module + +import ( + "context" + "encoding/json" + "fmt" + + abci "github.com/cometbft/cometbft/abci/types" + "github.com/gorilla/mux" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/spf13/cobra" + + "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" + + "github.com/umee-network/umee/v6/x/auction" + // "github.com/umee-network/umee/v6/x/auction/client/cli" + "github.com/umee-network/umee/v6/x/auction/keeper" +) + +var ( + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} +) + +// AppModuleBasic implements the AppModuleBasic interface for the x/leverage +// module. +type AppModuleBasic struct { + cdc codec.Codec +} + +func NewAppModuleBasic(cdc codec.Codec) AppModuleBasic { + return AppModuleBasic{cdc: cdc} +} + +// Name returns the x/auction module's name. +func (AppModuleBasic) Name() string { + return auction.ModuleName +} + +// RegisterLegacyAminoCodec registers the x/auction module's types with a legacy +// Amino codec. +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + auction.RegisterLegacyAminoCodec(cdc) +} + +// RegisterInterfaces registers the module's interface types. +func (a AppModuleBasic) RegisterInterfaces(reg cdctypes.InterfaceRegistry) { + auction.RegisterInterfaces(reg) +} + +// DefaultGenesis returns the x/auction module's default genesis state. +func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { + return cdc.MustMarshalJSON(auction.DefaultGenesis()) +} + +// ValidateGenesis performs genesis state validation for the x/auction module. +func (AppModuleBasic) ValidateGenesis( + cdc codec.JSONCodec, + _ client.TxEncodingConfig, + bz json.RawMessage, +) error { + var genState auction.GenesisState + if err := cdc.UnmarshalJSON(bz, &genState); err != nil { + return fmt.Errorf("failed to unmarshal %s genesis state: %w", auction.ModuleName, err) + } + + return genState.Validate() +} + +// Deprecated: RegisterRESTRoutes performs a no-op. Querying is delegated to the +// gRPC service. +func (AppModuleBasic) RegisterRESTRoutes(_ client.Context, _ *mux.Router) {} + +// RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the x/leverage +// module. +func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) { + err := auction.RegisterQueryHandlerClient(context.Background(), mux, auction.NewQueryClient(clientCtx)) + if err != nil { + panic(err) + } +} + +// GetTxCmd returns the x/auction module's root tx command. +func (a AppModuleBasic) GetTxCmd() *cobra.Command { + // TODO return cli.GetTxCmd() + return nil +} + +// GetQueryCmd returns the x/auction module's root query command. +func (AppModuleBasic) GetQueryCmd() *cobra.Command { + // TODO return cli.GetQueryCmd() + return nil +} + +// AppModule implements the AppModule interface for the x/auction module. +type AppModule struct { + AppModuleBasic + + kb keeper.Builder + bankKeeper auction.BankKeeper +} + +func NewAppModule( + cdc codec.Codec, keeper keeper.Builder, bk auction.BankKeeper, +) AppModule { + return AppModule{ + AppModuleBasic: NewAppModuleBasic(cdc), + kb: keeper, + bankKeeper: bk, + } +} + +// Name returns the x/auction module's name. +func (am AppModule) Name() string { + return am.AppModuleBasic.Name() +} + +func (AppModule) ConsensusVersion() uint64 { + return 1 +} + +// RegisterServices registers gRPC services. +func (am AppModule) RegisterServices(cfg module.Configurator) { + // auction.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) + // auction.RegisterQueryServer(cfg.QueryServer(), keeper.NewQuerier(am.keeper)) +} + +// RegisterInvariants registers the x/auction module's invariants. +func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {} + +// InitGenesis performs the x/auction module's genesis initialization. It returns +// no validator updates. +func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { + var genState auction.GenesisState + cdc.MustUnmarshalJSON(data, &genState) + am.kb.Keeper(&ctx).InitGenesis(&genState) + + return []abci.ValidatorUpdate{} +} + +// ExportGenesis returns the x/auction module's exported genesis state as raw +// JSON bytes. +func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage { + genState := am.kb.Keeper(&ctx).ExportGenesis() + return cdc.MustMarshalJSON(genState) +} + +// BeginBlock executes all ABCI BeginBlock logic respective to the x/auction module. +func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} + +// EndBlock executes all ABCI EndBlock logic respective to the x/auction module. +// It returns no validator updates. +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + return []abci.ValidatorUpdate{} +}