Skip to content

Commit 21ad688

Browse files
committed
chore: use comparison methods of price in renewal price policy Validate method
1 parent 5715506 commit 21ad688

File tree

1 file changed

+16
-53
lines changed

1 file changed

+16
-53
lines changed

types/v1/renewal.go

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"strings"
7-
8-
sdkmath "cosmossdk.io/math"
97
)
108

119
// String converts a RenewalPricePolicy to its string representation.
@@ -75,80 +73,45 @@ func RenewalPricePolicyFromString(s string) RenewalPricePolicy {
7573
}
7674
}
7775

78-
// Validate validates whether a subscription can be renewed based on the policy and given DecCoin conditions.
79-
// Returns an error if the renewal is not allowed or invalid.
80-
func (r RenewalPricePolicy) validate(curr, prev sdkmath.LegacyDec) error {
76+
// Validate checks whether the current price satisfies the policy conditions
77+
// when compared to the previous price. Returns an error if the policy is not met
78+
// or if the policy is invalid or unspecified.
79+
func (r RenewalPricePolicy) Validate(curr, prev Price) error {
80+
if curr.Denom != prev.Denom {
81+
return fmt.Errorf("price denom mismatch; current=%s, previous=%s", curr.Denom, prev.Denom)
82+
}
83+
8184
switch r {
8285
case RenewalPricePolicyUnspecified:
83-
return errors.New("renewal policy unspecified")
86+
return errors.New("renewal price policy is unspecified")
8487
case RenewalPricePolicyIfLesser:
85-
if !curr.LT(prev) {
88+
if !curr.IsLT(prev) {
8689
return fmt.Errorf("current price %s is not less than previous price %s", curr, prev)
8790
}
8891
case RenewalPricePolicyIfLesserOrEqual:
89-
if !curr.LTE(prev) {
92+
if !curr.IsLTE(prev) {
9093
return fmt.Errorf("current price %s is not less than or equal to previous price %s", curr, prev)
9194
}
9295
case RenewalPricePolicyIfEqual:
93-
if !curr.Equal(prev) {
96+
if !curr.IsEqual(prev) {
9497
return fmt.Errorf("current price %s is not equal to previous price %s", curr, prev)
9598
}
9699
case RenewalPricePolicyIfNotEqual:
97-
if curr.Equal(prev) {
100+
if curr.IsEqual(prev) {
98101
return fmt.Errorf("current price %s is equal to previous price %s", curr, prev)
99102
}
100103
case RenewalPricePolicyIfGreater:
101-
if !curr.GT(prev) {
104+
if !curr.IsGT(prev) {
102105
return fmt.Errorf("current price %s is not greater than previous price %s", curr, prev)
103106
}
104107
case RenewalPricePolicyIfGreaterOrEqual:
105-
if !curr.GTE(prev) {
108+
if !curr.IsGTE(prev) {
106109
return fmt.Errorf("current price %s is not greater than or equal to previous price %s", curr, prev)
107110
}
108111
case RenewalPricePolicyAlways:
109112
return nil
110113
default:
111-
return errors.New("invalid renewal policy")
112-
}
113-
114-
return nil
115-
}
116-
117-
func (r RenewalPricePolicy) Validate(curr, prev Price) error {
118-
if r.Equal(RenewalPricePolicyAlways) {
119-
return nil
120-
}
121-
122-
if curr.Denom != prev.Denom {
123-
return fmt.Errorf("current price denom %s does not match previous price denom %s", curr.Denom, prev.Denom)
124-
}
125-
126-
if prev.BaseValue.IsZero() && prev.QuoteValue.IsZero() && curr.BaseValue.IsZero() && curr.QuoteValue.IsZero() {
127-
return r.validate(prev.BaseValue, curr.BaseValue)
128-
}
129-
130-
if prev.BaseValue.IsZero() && prev.QuoteValue.IsZero() && curr.BaseValue.IsZero() && !curr.QuoteValue.IsZero() {
131-
return r.validate(prev.QuoteValue.ToLegacyDec(), curr.QuoteValue.ToLegacyDec())
132-
}
133-
134-
if prev.BaseValue.IsZero() && prev.QuoteValue.IsZero() && !curr.BaseValue.IsZero() {
135-
return r.validate(prev.BaseValue, curr.BaseValue)
136-
}
137-
138-
if prev.BaseValue.IsZero() && !prev.QuoteValue.IsZero() {
139-
return r.validate(prev.QuoteValue.ToLegacyDec(), curr.QuoteValue.ToLegacyDec())
140-
}
141-
142-
if !prev.BaseValue.IsZero() && curr.BaseValue.IsZero() && curr.QuoteValue.IsZero() {
143-
return r.validate(prev.BaseValue, curr.BaseValue)
144-
}
145-
146-
if !prev.BaseValue.IsZero() && curr.BaseValue.IsZero() && !curr.QuoteValue.IsZero() {
147-
return r.validate(prev.QuoteValue.ToLegacyDec(), curr.QuoteValue.ToLegacyDec())
148-
}
149-
150-
if !prev.BaseValue.IsZero() && !curr.BaseValue.IsZero() {
151-
return r.validate(prev.BaseValue, curr.BaseValue)
114+
return fmt.Errorf("unsupported renewal price policy %v", r)
152115
}
153116

154117
return nil

0 commit comments

Comments
 (0)