-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add lease renewal price policy
- Loading branch information
1 parent
6f13d58
commit 1dc8d09
Showing
18 changed files
with
787 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
syntax = "proto3"; | ||
package sentinel.types.v1; | ||
|
||
import "gogoproto/gogo.proto"; | ||
|
||
option go_package = "github.com/sentinel-official/hub/v12/types/v1"; | ||
option (gogoproto.goproto_enum_prefix_all) = false; | ||
option (gogoproto.goproto_enum_stringer_all) = false; | ||
option (gogoproto.goproto_getters_all) = false; | ||
|
||
// Enum for renewal price policies | ||
enum RenewalPricePolicy { | ||
RENEWAL_PRICE_POLICY_UNSPECIFIED = 0 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyUnspecified"]; // Default value, do not renew | ||
RENEWAL_PRICE_POLICY_IF_LESSER = 1 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyIfLesser"]; // Renew if the current price is lesser than the previous price | ||
RENEWAL_PRICE_POLICY_IF_LESSER_OR_EQUAL = 2 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyIfLesserOrEqual"]; // Renew if the current price is lesser than or equal to the previous price | ||
RENEWAL_PRICE_POLICY_IF_EQUAL = 3 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyIfEqual"]; // Renew if the current price is equal to the previous price | ||
RENEWAL_PRICE_POLICY_IF_NOT_EQUAL = 4 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyIfNotEqual"]; // Renew if the current price is not equal to the previous price | ||
RENEWAL_PRICE_POLICY_IF_GREATER = 5 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyIfGreater"]; // Renew if the current price is greater than the previous price | ||
RENEWAL_PRICE_POLICY_IF_GREATER_OR_EQUAL = 6 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyIfGreaterOrEqual"]; // Renew if the current price is greater than or equal to the previous price | ||
RENEWAL_PRICE_POLICY_ALWAYS = 7 [(gogoproto.enumvalue_customname) = "RenewalPricePolicyAlways"]; // Always renew | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package v1 | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// String converts a RenewalPricePolicy to its string representation. | ||
func (r RenewalPricePolicy) String() string { | ||
switch r { | ||
case RenewalPricePolicyIfLesser: | ||
return "if_lesser" | ||
case RenewalPricePolicyIfLesserOrEqual: | ||
return "if_lesser_or_equal" | ||
case RenewalPricePolicyIfEqual: | ||
return "if_equal" | ||
case RenewalPricePolicyIfNotEqual: | ||
return "if_not_equal" | ||
case RenewalPricePolicyIfGreater: | ||
return "if_greater" | ||
case RenewalPricePolicyIfGreaterOrEqual: | ||
return "if_greater_or_equal" | ||
case RenewalPricePolicyAlways: | ||
return "always" | ||
default: | ||
return "unspecified" | ||
} | ||
} | ||
|
||
// Equal checks if two RenewalPricePolicy values are equal. | ||
func (r RenewalPricePolicy) Equal(v RenewalPricePolicy) bool { | ||
return r == v | ||
} | ||
|
||
// IsValid checks whether the RenewalPricePolicy is a valid value. | ||
func (r RenewalPricePolicy) IsValid() bool { | ||
switch r { | ||
case RenewalPricePolicyUnspecified, | ||
RenewalPricePolicyIfLesser, | ||
RenewalPricePolicyIfLesserOrEqual, | ||
RenewalPricePolicyIfEqual, | ||
RenewalPricePolicyIfNotEqual, | ||
RenewalPricePolicyIfGreater, | ||
RenewalPricePolicyIfGreaterOrEqual, | ||
RenewalPricePolicyAlways: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
// RenewalPricePolicyFromString converts a string to a RenewalPricePolicy. | ||
func RenewalPricePolicyFromString(s string) RenewalPricePolicy { | ||
s = strings.ToLower(s) | ||
switch s { | ||
case "if_lesser": | ||
return RenewalPricePolicyIfLesser | ||
case "if_lesser_or_equal": | ||
return RenewalPricePolicyIfLesserOrEqual | ||
case "if_equal": | ||
return RenewalPricePolicyIfEqual | ||
case "if_not_equal": | ||
return RenewalPricePolicyIfNotEqual | ||
case "if_greater": | ||
return RenewalPricePolicyIfGreater | ||
case "if_greater_or_equal": | ||
return RenewalPricePolicyIfGreaterOrEqual | ||
case "always": | ||
return RenewalPricePolicyAlways | ||
default: | ||
return RenewalPricePolicyUnspecified | ||
} | ||
} | ||
|
||
// Validate validates whether a subscription can be renewed based on the policy and given DecCoin conditions. | ||
// Returns an error if the renewal is not allowed or invalid. | ||
func (r RenewalPricePolicy) Validate(currentPrice, previousPrice sdk.DecCoin) error { | ||
// Skip denomination check for RenewalPricePolicyAlways | ||
if r != RenewalPricePolicyAlways && currentPrice.Denom != previousPrice.Denom { | ||
return fmt.Errorf("current price denom %s does not match previous price denom %s", currentPrice.Denom, previousPrice.Denom) | ||
} | ||
|
||
// Compare prices based on the policy | ||
switch r { | ||
case RenewalPricePolicyUnspecified: | ||
return fmt.Errorf("renewal policy unspecified") | ||
case RenewalPricePolicyIfLesser: | ||
if !currentPrice.Amount.LT(previousPrice.Amount) { | ||
return fmt.Errorf("current price %s is not less than previous price %s", currentPrice, previousPrice) | ||
} | ||
case RenewalPricePolicyIfLesserOrEqual: | ||
if !currentPrice.Amount.LTE(previousPrice.Amount) { | ||
return fmt.Errorf("current price %s is not less than or equal to previous price %s", currentPrice, previousPrice) | ||
} | ||
case RenewalPricePolicyIfEqual: | ||
if !currentPrice.Amount.Equal(previousPrice.Amount) { | ||
return fmt.Errorf("current price %s is not equal to previous price %s", currentPrice, previousPrice) | ||
} | ||
case RenewalPricePolicyIfNotEqual: | ||
if currentPrice.Amount.Equal(previousPrice.Amount) { | ||
return fmt.Errorf("current price %s is equal to previous price %s", currentPrice, previousPrice) | ||
} | ||
case RenewalPricePolicyIfGreater: | ||
if !currentPrice.Amount.GT(previousPrice.Amount) { | ||
return fmt.Errorf("current price %s is not greater than previous price %s", currentPrice, previousPrice) | ||
} | ||
case RenewalPricePolicyIfGreaterOrEqual: | ||
if !currentPrice.Amount.GTE(previousPrice.Amount) { | ||
return fmt.Errorf("current price %s is not greater than or equal to previous price %s", currentPrice, previousPrice) | ||
} | ||
case RenewalPricePolicyAlways: | ||
return nil | ||
default: | ||
return errors.New("invalid renewal policy") | ||
} | ||
|
||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,24 @@ | ||
package cli | ||
|
||
import ( | ||
"github.com/spf13/pflag" | ||
|
||
v1base "github.com/sentinel-official/hub/v12/types/v1" | ||
) | ||
|
||
const ( | ||
flagDenom = "denom" | ||
flagRenewable = "renewable" | ||
flagDenom = "denom" | ||
flagRenewalPricePolicy = "renewal-price-policy" | ||
) | ||
|
||
func GetRenewalPricePolicy(flags *pflag.FlagSet) (v1base.RenewalPricePolicy, error) { | ||
s, err := flags.GetString(flagRenewalPricePolicy) | ||
if err != nil { | ||
return v1base.RenewalPricePolicyUnspecified, err | ||
} | ||
if s == "" { | ||
return v1base.RenewalPricePolicyUnspecified, nil | ||
} | ||
|
||
return v1base.RenewalPricePolicyFromString(s), nil | ||
} |
Oops, something went wrong.