From 6a2c489cd38ae1c0b042d31130c5f203f9246703 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 9 Feb 2024 10:31:50 +0530 Subject: [PATCH] Add general files for auction module --- x/auction/codec.go | 17 ++++++ x/auction/genesis.go | 24 +++++++++ x/auction/keys.go | 16 ++++++ x/auction/params.go | 124 +++++++++++++++++++++++++++++++++++++++++++ x/bond/genesis.go | 2 - x/bond/keys.go | 3 -- 6 files changed, 181 insertions(+), 5 deletions(-) create mode 100644 x/auction/codec.go create mode 100644 x/auction/genesis.go create mode 100644 x/auction/keys.go diff --git a/x/auction/codec.go b/x/auction/codec.go new file mode 100644 index 00000000..d29b3327 --- /dev/null +++ b/x/auction/codec.go @@ -0,0 +1,17 @@ +package auction + +import ( + types "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/msgservice" +) + +// RegisterInterfaces registers the interfaces types with the interface registry. +func RegisterInterfaces(registry types.InterfaceRegistry) { + registry.RegisterImplementations((*sdk.Msg)(nil), + &MsgCreateAuction{}, + &MsgCommitBid{}, + &MsgRevealBid{}, + ) + msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) +} diff --git a/x/auction/genesis.go b/x/auction/genesis.go new file mode 100644 index 00000000..81f4ea28 --- /dev/null +++ b/x/auction/genesis.go @@ -0,0 +1,24 @@ +package auction + +func DefaultGenesisState() *GenesisState { + return &GenesisState{ + Params: DefaultParams(), + Auctions: []*Auction{}, + } +} + +func NewGenesisState(params Params, auctions []*Auction) *GenesisState { + return &GenesisState{ + Params: params, + Auctions: auctions, + } +} + +// Validate performs basic genesis state validation returning an error upon any +func (gs *GenesisState) Validate() error { + if err := gs.Params.Validate(); err != nil { + return err + } + + return nil +} diff --git a/x/auction/keys.go b/x/auction/keys.go new file mode 100644 index 00000000..e835f3c4 --- /dev/null +++ b/x/auction/keys.go @@ -0,0 +1,16 @@ +package auction + +import "cosmossdk.io/collections" + +const ( + ModuleName = "auction" + + // AuctionBurnModuleAccountName is the name of the auction burn module account. + AuctionBurnModuleAccountName = "auction_burn" +) + +// Store prefixes +var ( + // ParamsKey is the prefix for params key + ParamsKeyPrefix = collections.NewPrefix(0) +) diff --git a/x/auction/params.go b/x/auction/params.go index 7e2af6fc..0dd99480 100644 --- a/x/auction/params.go +++ b/x/auction/params.go @@ -1,10 +1,44 @@ package auction import ( + "errors" fmt "fmt" "strings" + time "time" + + sdkmath "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" ) +var ( + DefaultCommitsDuration = 5 * time.Minute + DefaultRevealsDuration = 5 * time.Minute + DefaultCommitFee = sdk.Coin{Amount: sdkmath.NewInt(10), Denom: sdk.DefaultBondDenom} + DefaultRevealFee = sdk.Coin{Amount: sdkmath.NewInt(10), Denom: sdk.DefaultBondDenom} + DefaultMinimumBid = sdk.Coin{Amount: sdkmath.NewInt(1000), Denom: sdk.DefaultBondDenom} +) + +func NewParams(commitsDuration time.Duration, revealsDuration time.Duration, commitFee sdk.Coin, revealFee sdk.Coin, minimumBid sdk.Coin) Params { + return Params{ + CommitsDuration: commitsDuration, + RevealsDuration: revealsDuration, + CommitFee: commitFee, + RevealFee: revealFee, + MinimumBid: minimumBid, + } +} + +// DefaultParams returns a default set of parameters. +func DefaultParams() Params { + return Params{ + CommitsDuration: DefaultCommitsDuration, + RevealsDuration: DefaultRevealsDuration, + CommitFee: DefaultCommitFee, + RevealFee: DefaultRevealFee, + MinimumBid: DefaultMinimumBid, + } +} + // String returns a human readable string representation of the parameters. func (p Params) String() string { var sb strings.Builder @@ -16,3 +50,93 @@ func (p Params) String() string { sb.WriteString(fmt.Sprintf("MinimumBid: %s\n", p.MinimumBid.String())) return sb.String() } + +// Validate a set of params. +func (p Params) Validate() error { + if err := validateCommitsDuration(p.CommitsDuration); err != nil { + return err + } + + if err := validateRevealsDuration(p.RevealsDuration); err != nil { + return err + } + + if err := validateCommitFee(p.CommitFee); err != nil { + return err + } + + if err := validateRevealFee(p.RevealFee); err != nil { + return err + } + + if err := validateMinimumBid(p.MinimumBid); err != nil { + return err + } + + return nil +} + +func validateCommitsDuration(i interface{}) error { + v, ok := i.(time.Duration) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v < 0 { + return errors.New("commits duration cannot be negative") + } + + return nil +} + +func validateRevealsDuration(i interface{}) error { + v, ok := i.(time.Duration) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v < 0 { + return errors.New("reveals duration cannot be negative") + } + + return nil +} + +func validateCommitFee(i interface{}) error { + v, ok := i.(sdk.Coin) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v.Amount.IsNegative() { + return errors.New("commit fee must be positive") + } + + return nil +} + +func validateRevealFee(i interface{}) error { + v, ok := i.(sdk.Coin) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v.Amount.IsNegative() { + return errors.New("reveal fee must be positive") + } + + return nil +} + +func validateMinimumBid(i interface{}) error { + v, ok := i.(sdk.Coin) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + if v.Amount.IsNegative() { + return errors.New("minimum bid must be positive") + } + + return nil +} diff --git a/x/bond/genesis.go b/x/bond/genesis.go index 94c0d596..d0a19122 100644 --- a/x/bond/genesis.go +++ b/x/bond/genesis.go @@ -1,7 +1,5 @@ package bond -// DefaultGenesisState sets default evm genesis state with empty accounts and default params and -// chain config values. func DefaultGenesisState() *GenesisState { return &GenesisState{ Params: DefaultParams(), diff --git a/x/bond/keys.go b/x/bond/keys.go index ad7e1b5e..deb56ef1 100644 --- a/x/bond/keys.go +++ b/x/bond/keys.go @@ -4,9 +4,6 @@ import "cosmossdk.io/collections" const ( ModuleName = "bond" - - // StoreKey is the string store representation - StoreKey = ModuleName ) // Store prefixes