-
Notifications
You must be signed in to change notification settings - Fork 40
/
plan.go
81 lines (70 loc) · 1.7 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
package types
import (
"fmt"
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
hubtypes "github.com/sentinel-official/hub/v12/types"
)
func (m *Plan) GetProviderAddress() hubtypes.ProvAddress {
if m.ProviderAddress == "" {
return nil
}
addr, err := hubtypes.ProvAddressFromBech32(m.ProviderAddress)
if err != nil {
panic(err)
}
return addr
}
func (m *Plan) Price(denom string) (sdk.Coin, bool) {
for _, v := range m.Prices {
if v.Denom == denom {
return v, true
}
}
return sdk.Coin{}, false
}
func (m *Plan) Validate() error {
if m.ID == 0 {
return fmt.Errorf("id cannot be zero")
}
if m.ProviderAddress == "" {
return fmt.Errorf("provider_address cannot be empty")
}
if _, err := hubtypes.ProvAddressFromBech32(m.ProviderAddress); err != nil {
return sdkerrors.Wrapf(err, "invalid provider_address %s", m.ProviderAddress)
}
if m.Duration < 0 {
return fmt.Errorf("duration cannot be negative")
}
if m.Duration == 0 {
return fmt.Errorf("duration cannot be zero")
}
if m.Gigabytes < 0 {
return fmt.Errorf("gigabytes cannot be negative")
}
if m.Gigabytes == 0 {
return fmt.Errorf("gigabytes cannot be zero")
}
if m.Prices == nil {
return fmt.Errorf("prices cannot be nil")
}
if m.Prices.Len() == 0 {
return fmt.Errorf("prices cannot be empty")
}
if m.Prices.IsAnyNil() {
return fmt.Errorf("prices cannot contain nil")
}
if !m.Prices.IsValid() {
return fmt.Errorf("prices must be valid")
}
if !m.Status.IsOneOf(hubtypes.StatusActive, hubtypes.StatusInactive) {
return fmt.Errorf("status must be one of [active, inactive]")
}
if m.StatusAt.IsZero() {
return fmt.Errorf("status_at cannot be zero")
}
return nil
}
type (
Plans []Plan
)