11package keeper
22
33import (
4+ "fmt"
45 "time"
56
67 sdk "github.com/cosmos/cosmos-sdk/types"
@@ -50,46 +51,18 @@ func (k *Keeper) MinHourlyPrices(ctx sdk.Context) v1base.Prices {
5051 return k .GetParams (ctx ).MinHourlyPrices
5152}
5253
53- // IsValidGigabytePrices checks if the provided gigabyte prices are valid based on the minimum prices defined in the module's parameters.
54- func (k * Keeper ) IsValidGigabytePrices (ctx sdk.Context , prices v1base.Prices ) bool {
55- if prices .Len () == 0 {
56- return true
57- }
58-
54+ // ValidateGigabytePrices validates prices against the minimum gigabyte prices.
55+ func (k * Keeper ) ValidateGigabytePrices (ctx sdk.Context , prices v1base.Prices ) error {
5956 minPrices := k .MinGigabytePrices (ctx )
60- for _ , price := range minPrices {
61- baseValue , quoteValue := prices .AmountOf (price .Denom )
62- if ! baseValue .IsZero () && baseValue .LT (price .BaseValue ) {
63- return false
64- }
65-
66- if ! quoteValue .IsZero () && quoteValue .LT (price .QuoteValue ) {
67- return false
68- }
69- }
7057
71- return true
58+ return validatePrices ( prices , minPrices )
7259}
7360
74- // IsValidHourlyPrices checks if the provided hourly prices are valid based on the minimum prices defined in the module's parameters.
75- func (k * Keeper ) IsValidHourlyPrices (ctx sdk.Context , prices v1base.Prices ) bool {
76- if prices .Len () == 0 {
77- return true
78- }
79-
61+ // ValidateHourlyPrices validates prices against the minimum hourly prices.
62+ func (k * Keeper ) ValidateHourlyPrices (ctx sdk.Context , prices v1base.Prices ) error {
8063 minPrices := k .MinHourlyPrices (ctx )
81- for _ , price := range minPrices {
82- baseValue , quoteValue := prices .AmountOf (price .Denom )
83- if ! baseValue .IsZero () && baseValue .LT (price .BaseValue ) {
84- return false
85- }
86-
87- if ! quoteValue .IsZero () && quoteValue .LT (price .QuoteValue ) {
88- return false
89- }
90- }
9164
92- return true
65+ return validatePrices ( prices , minPrices )
9366}
9467
9568// GetInactiveAt returns the inactive time by adding ActiveDuration to the current block time.
@@ -98,3 +71,35 @@ func (k *Keeper) GetInactiveAt(ctx sdk.Context) time.Time {
9871
9972 return ctx .BlockTime ().Add (d )
10073}
74+
75+ // validatePrices checks that all provided prices use allowed denoms and
76+ // meet or exceed the given minimums (zero values and empty lists are valid).
77+ func validatePrices (prices v1base.Prices , minPrices v1base.Prices ) error {
78+ // Empty set of prices is valid: treated as zero prices for all denoms.
79+ if prices .Len () == 0 {
80+ return nil
81+ }
82+
83+ // Build a lookup map for minPrices
84+ m := minPrices .Map ()
85+
86+ for _ , price := range prices {
87+ minPrice , ok := m [price .Denom ]
88+ if ! ok {
89+ return fmt .Errorf ("denom %s is not allowed" , price .Denom )
90+ }
91+
92+ // Only check non-zero values
93+ if ! price .BaseValue .IsZero () && price .BaseValue .LT (minPrice .BaseValue ) {
94+ return fmt .Errorf ("base value %s for denom %s is lesser than %s" ,
95+ price .BaseValue , price .Denom , minPrice .BaseValue )
96+ }
97+
98+ if ! price .QuoteValue .IsZero () && price .QuoteValue .LT (minPrice .QuoteValue ) {
99+ return fmt .Errorf ("quote value %s for denom %s is lesser than %s" ,
100+ price .QuoteValue , price .Denom , minPrice .QuoteValue )
101+ }
102+ }
103+
104+ return nil
105+ }
0 commit comments