From 4188ddafa710aa2833e64a8628f180dafdb8df78 Mon Sep 17 00:00:00 2001 From: Assaf Morami Date: Wed, 18 Dec 2024 20:33:46 +0200 Subject: [PATCH] extract auction and price query validation logic into separate functions to share code between msg validation and genesis validation --- x/auction/types/genesis.go | 14 ++++------- x/auction/types/msgs.go | 31 +++++++----------------- x/auction/types/validate.go | 45 +++++++++++++++++++++++++++++++++++ x/icqoracle/types/genesis.go | 12 ++++------ x/icqoracle/types/msgs.go | 24 ++++++------------- x/icqoracle/types/validate.go | 26 ++++++++++++++++++++ 6 files changed, 96 insertions(+), 56 deletions(-) create mode 100644 x/auction/types/validate.go create mode 100644 x/icqoracle/types/validate.go diff --git a/x/auction/types/genesis.go b/x/auction/types/genesis.go index 53b631d90..e588b403c 100644 --- a/x/auction/types/genesis.go +++ b/x/auction/types/genesis.go @@ -10,23 +10,19 @@ func DefaultGenesis() *GenesisState { } // Performs basic genesis state validation by iterating through all auctions and validating -// using ValidateBasic() since it already implements thorough validation of all auction fields +// using ValidateCreateAuctionParams() func (gs GenesisState) Validate() error { for i, auction := range gs.Auctions { - - msg := NewMsgCreateAuction( - "stride16eenchewedupsplt0ut600ed0ffstageeeervs", // dummy address, not stored in auction + err := ValidateCreateAuctionParams( auction.Name, auction.Type, auction.SellingDenom, auction.PaymentDenom, - auction.Enabled, - auction.PriceMultiplier.String(), - auction.MinBidAmount.Uint64(), + auction.PriceMultiplier, + auction.MinBidAmount, auction.Beneficiary, ) - - if err := msg.ValidateBasic(); err != nil { + if err != nil { return fmt.Errorf("invalid genesis auction at index %d: %w", i, err) } } diff --git a/x/auction/types/msgs.go b/x/auction/types/msgs.go index d7f0736b2..8530a362c 100644 --- a/x/auction/types/msgs.go +++ b/x/auction/types/msgs.go @@ -144,29 +144,16 @@ func (msg *MsgCreateAuction) ValidateBasic() error { if err := utils.ValidateAdminAddress(msg.Admin); err != nil { return err } - if msg.AuctionName == "" { - return errors.New("auction-name must be specified") - } - if _, ok := AuctionType_name[int32(msg.AuctionType)]; !ok { - return fmt.Errorf("auction-type %d is invalid", msg.AuctionType) - } - if msg.SellingDenom == "" { - return errors.New("selling-denom must be specified") - } - if msg.PaymentDenom == "" { - return errors.New("payment-denom must be specified") - } - if !(msg.PriceMultiplier.GT(math.LegacyZeroDec()) && msg.PriceMultiplier.LTE(math.LegacyOneDec())) { - return errors.New("price-multiplier must be > 0 and <= 1 (0 > priceMultiplier >= 1)") - } - if msg.MinBidAmount.LT(math.ZeroInt()) { - return errors.New("min-bid-amount must be >= 0") - } - if _, err := sdk.AccAddressFromBech32(msg.Beneficiary); err != nil { - return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid beneficiary address (%s)", err) - } - return nil + return ValidateCreateAuctionParams( + msg.AuctionName, + msg.AuctionType, + msg.SellingDenom, + msg.PaymentDenom, + msg.PriceMultiplier, + msg.MinBidAmount, + msg.Beneficiary, + ) } // ---------------------------------------------- diff --git a/x/auction/types/validate.go b/x/auction/types/validate.go new file mode 100644 index 000000000..f899de4de --- /dev/null +++ b/x/auction/types/validate.go @@ -0,0 +1,45 @@ +package types + +import ( + "errors" + fmt "fmt" + + errorsmod "cosmossdk.io/errors" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +func ValidateCreateAuctionParams( + auctionName string, + auctionType AuctionType, + sellingDenom string, + paymentDenom string, + priceMultiplier math.LegacyDec, + minBidAmount math.Int, + beneficiary string, +) error { + if auctionName == "" { + return errors.New("auction-name must be specified") + } + if _, ok := AuctionType_name[int32(auctionType)]; !ok { + return fmt.Errorf("auction-type %d is invalid", auctionType) + } + if sellingDenom == "" { + return errors.New("selling-denom must be specified") + } + if paymentDenom == "" { + return errors.New("payment-denom must be specified") + } + if !(priceMultiplier.GT(math.LegacyZeroDec()) && priceMultiplier.LTE(math.LegacyOneDec())) { + return errors.New("price-multiplier must be > 0 and <= 1 (0 > priceMultiplier >= 1)") + } + if minBidAmount.LT(math.ZeroInt()) { + return errors.New("min-bid-amount must be >= 0") + } + if _, err := sdk.AccAddressFromBech32(beneficiary); err != nil { + return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid beneficiary address (%s)", err) + } + + return nil +} diff --git a/x/icqoracle/types/genesis.go b/x/icqoracle/types/genesis.go index b40cf00bd..bddce6128 100644 --- a/x/icqoracle/types/genesis.go +++ b/x/icqoracle/types/genesis.go @@ -10,23 +10,19 @@ func DefaultGenesis() *GenesisState { } // Performs basic genesis state validation by iterating through all token prices and validating -// using ValidateBasic() since it already implements thorough validation of the important token -// price fields. +// using ValidateTokenPriceQueryParams(). // We ignore the SpotPrice, UpdatedAt & QueryInProgress fields since they are reset in InitGenesis(). func (gs GenesisState) Validate() error { for i, tokenPrice := range gs.TokenPrices { - - msg := NewMsgRegisterTokenPriceQuery( - "stride1palmssweatykneesweakarmsareheavy8ahm9u", // dummy address, not stored in token price + err := ValidateTokenPriceQueryParams( tokenPrice.BaseDenom, tokenPrice.QuoteDenom, tokenPrice.OsmosisPoolId, tokenPrice.OsmosisBaseDenom, tokenPrice.OsmosisQuoteDenom, ) - - if err := msg.ValidateBasic(); err != nil { - return fmt.Errorf("invalid genesis token price at index %d: %w", i, err) + if err != nil { + return fmt.Errorf("invalid genesis token price query at index %d: %w", i, err) } } return nil diff --git a/x/icqoracle/types/msgs.go b/x/icqoracle/types/msgs.go index 17468c2f5..e3b21b655 100644 --- a/x/icqoracle/types/msgs.go +++ b/x/icqoracle/types/msgs.go @@ -64,23 +64,13 @@ func (msg *MsgRegisterTokenPriceQuery) ValidateBasic() error { if err := utils.ValidateAdminAddress(msg.Admin); err != nil { return err } - if msg.BaseDenom == "" { - return errors.New("base-denom must be specified") - } - if msg.QuoteDenom == "" { - return errors.New("quote-denom must be specified") - } - if _, err := strconv.ParseUint(msg.OsmosisPoolId, 10, 64); err != nil { - return errors.New("osmosis-pool-id must be uint64") - } - if msg.OsmosisBaseDenom == "" { - return errors.New("osmosis-base-denom must be specified") - } - if msg.OsmosisQuoteDenom == "" { - return errors.New("osmosis-quote-denom must be specified") - } - - return nil + return ValidateTokenPriceQueryParams( + msg.BaseDenom, + msg.QuoteDenom, + msg.OsmosisPoolId, + msg.OsmosisBaseDenom, + msg.OsmosisQuoteDenom, + ) } // ---------------------------------------------- diff --git a/x/icqoracle/types/validate.go b/x/icqoracle/types/validate.go new file mode 100644 index 000000000..a84a72984 --- /dev/null +++ b/x/icqoracle/types/validate.go @@ -0,0 +1,26 @@ +package types + +import ( + "errors" + "strconv" +) + +func ValidateTokenPriceQueryParams(baseDenom, quoteDenom, osmosisPoolId, osmosisBaseDenom, osmosisQuoteDenom string) error { + if baseDenom == "" { + return errors.New("base-denom must be specified") + } + if quoteDenom == "" { + return errors.New("quote-denom must be specified") + } + if _, err := strconv.ParseUint(osmosisPoolId, 10, 64); err != nil { + return errors.New("osmosis-pool-id must be uint64") + } + if osmosisBaseDenom == "" { + return errors.New("osmosis-base-denom must be specified") + } + if osmosisQuoteDenom == "" { + return errors.New("osmosis-quote-denom must be specified") + } + + return nil +}