Skip to content

Commit

Permalink
extract auction and price query validation logic into separate functions
Browse files Browse the repository at this point in the history
to share code between msg validation and genesis validation
  • Loading branch information
assafmo committed Dec 18, 2024
1 parent be789ef commit 4188dda
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 56 deletions.
14 changes: 5 additions & 9 deletions x/auction/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
31 changes: 9 additions & 22 deletions x/auction/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

// ----------------------------------------------
Expand Down
45 changes: 45 additions & 0 deletions x/auction/types/validate.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 4 additions & 8 deletions x/icqoracle/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 7 additions & 17 deletions x/icqoracle/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

// ----------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions x/icqoracle/types/validate.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4188dda

Please sign in to comment.