-
Notifications
You must be signed in to change notification settings - Fork 202
/
plan.go
86 lines (69 loc) · 2.51 KB
/
plan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package types
import (
fmt "fmt"
"math/big"
"reflect"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Function to validate a plan object fields
func (p Plan) ValidatePlan() error {
if p.GetIndex() == "" {
return sdkerrors.Wrap(ErrInvalidPlanIndex, "plan's index can't be empty")
}
// check that the plan's price is non-zero
if p.GetPrice().Amount.IsZero() {
return sdkerrors.Wrap(ErrInvalidPlanPrice, "plan's price can't be zero")
}
// check that if overuse is allowed then the overuse rate is non-zero
if p.GetAllowOveruse() && p.GetOveruseRate() == 0 {
return sdkerrors.Wrap(ErrInvalidPlanOveruse, "plan can't allow CU overuse and have overuse rate of zero")
}
// check that if overuse is not allowed then the overuse rate is zero
if !p.GetAllowOveruse() && p.GetOveruseRate() != 0 {
return sdkerrors.Wrap(ErrInvalidPlanOveruse, "plan can't forbid CU overuse and have a non-zero overuse rate")
}
// check that the plan's description length is below the max length
if len(p.GetDescription()) > MAX_LEN_PLAN_DESCRIPTION {
return sdkerrors.Wrap(ErrInvalidPlanDescription, "plan's description is too long")
}
// check that the plan's type length is below the max length
if len(p.GetType()) > MAX_LEN_PLAN_TYPE {
return sdkerrors.Wrap(ErrInvalidPlanType, "plan's type is too long")
}
// check that the plan's annual discount is valid
if p.GetAnnualDiscountPercentage() > uint64(100) {
return sdkerrors.Wrap(ErrInvalidPlanAnnualDiscount, "plan's annual discount is invalid (not between 0-100 percent)")
}
err := p.PlanPolicy.ValidateBasicPolicy(true)
if err != nil {
return err
}
return nil
}
// PriceDecodeHookFunc helps the decoder to correctly unmarshal the price field's amount (type math.Int)
func PriceDecodeHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if t == reflect.TypeOf(sdk.NewInt(0)) {
amountStr, ok := data.(string)
if !ok {
return nil, fmt.Errorf("unexpected data type for amount field")
}
// Convert the string amount to an math.Int
amount, ok := new(big.Int).SetString(amountStr, 10)
if !ok {
return nil, fmt.Errorf("failed to convert amount to math.Int")
}
return sdk.NewIntFromBigInt(amount), nil
}
return data, nil
}
var planMandatoryFields = map[string]struct{}{"index": {}, "price": {}}
func CheckPlanMandatoryFields(unsetFields []string) bool {
planValid := true
for _, unsetF := range unsetFields {
if _, ok := planMandatoryFields[unsetF]; ok {
planValid = false
}
}
return planValid
}