|
4 | 4 | "errors" |
5 | 5 | "fmt" |
6 | 6 | "strings" |
7 | | - |
8 | | - sdkmath "cosmossdk.io/math" |
9 | 7 | ) |
10 | 8 |
|
11 | 9 | // String converts a RenewalPricePolicy to its string representation. |
@@ -75,80 +73,45 @@ func RenewalPricePolicyFromString(s string) RenewalPricePolicy { |
75 | 73 | } |
76 | 74 | } |
77 | 75 |
|
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 | + |
81 | 84 | switch r { |
82 | 85 | case RenewalPricePolicyUnspecified: |
83 | | - return errors.New("renewal policy unspecified") |
| 86 | + return errors.New("renewal price policy is unspecified") |
84 | 87 | case RenewalPricePolicyIfLesser: |
85 | | - if !curr.LT(prev) { |
| 88 | + if !curr.IsLT(prev) { |
86 | 89 | return fmt.Errorf("current price %s is not less than previous price %s", curr, prev) |
87 | 90 | } |
88 | 91 | case RenewalPricePolicyIfLesserOrEqual: |
89 | | - if !curr.LTE(prev) { |
| 92 | + if !curr.IsLTE(prev) { |
90 | 93 | return fmt.Errorf("current price %s is not less than or equal to previous price %s", curr, prev) |
91 | 94 | } |
92 | 95 | case RenewalPricePolicyIfEqual: |
93 | | - if !curr.Equal(prev) { |
| 96 | + if !curr.IsEqual(prev) { |
94 | 97 | return fmt.Errorf("current price %s is not equal to previous price %s", curr, prev) |
95 | 98 | } |
96 | 99 | case RenewalPricePolicyIfNotEqual: |
97 | | - if curr.Equal(prev) { |
| 100 | + if curr.IsEqual(prev) { |
98 | 101 | return fmt.Errorf("current price %s is equal to previous price %s", curr, prev) |
99 | 102 | } |
100 | 103 | case RenewalPricePolicyIfGreater: |
101 | | - if !curr.GT(prev) { |
| 104 | + if !curr.IsGT(prev) { |
102 | 105 | return fmt.Errorf("current price %s is not greater than previous price %s", curr, prev) |
103 | 106 | } |
104 | 107 | case RenewalPricePolicyIfGreaterOrEqual: |
105 | | - if !curr.GTE(prev) { |
| 108 | + if !curr.IsGTE(prev) { |
106 | 109 | return fmt.Errorf("current price %s is not greater than or equal to previous price %s", curr, prev) |
107 | 110 | } |
108 | 111 | case RenewalPricePolicyAlways: |
109 | 112 | return nil |
110 | 113 | 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) |
152 | 115 | } |
153 | 116 |
|
154 | 117 | return nil |
|
0 commit comments