/
subscription.go
116 lines (101 loc) · 2.39 KB
/
subscription.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package types
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
hubtypes "github.com/sentinel-official/hub/types"
)
func (m *Subscription) GetNode() hubtypes.NodeAddress {
if m.Node == "" {
return nil
}
address, err := hubtypes.NodeAddressFromBech32(m.Node)
if err != nil {
panic(err)
}
return address
}
func (m *Subscription) GetOwner() sdk.AccAddress {
if m.Owner == "" {
return nil
}
address, err := sdk.AccAddressFromBech32(m.Owner)
if err != nil {
panic(err)
}
return address
}
func (m *Subscription) Amount(consumed sdk.Int) sdk.Coin {
var (
amount sdk.Int
x = hubtypes.Gigabyte.Quo(m.Price.Amount)
)
if x.IsPositive() {
amount = hubtypes.NewBandwidth(consumed, sdk.ZeroInt()).
CeilTo(x).
Sum().Quo(x)
} else {
y := sdk.NewDecFromInt(m.Price.Amount).
QuoInt(hubtypes.Gigabyte).
Ceil().TruncateInt()
amount = consumed.Mul(y)
}
return sdk.NewCoin(m.Price.Denom, amount)
}
func (m *Subscription) Validate() error {
if m.Id == 0 {
return fmt.Errorf("id cannot be zero")
}
if m.Owner == "" {
return fmt.Errorf("owner cannot be empty")
}
if _, err := sdk.AccAddressFromBech32(m.Owner); err != nil {
return errors.Wrapf(err, "invalid owner %s", m.Owner)
}
if m.Node == "" && m.Plan == 0 {
return fmt.Errorf("both node and plan cannot be empty")
}
if m.Node != "" && m.Plan != 0 {
return fmt.Errorf("either node or plan must be empty")
}
if m.Node != "" {
if _, err := hubtypes.NodeAddressFromBech32(m.Node); err != nil {
return errors.Wrapf(err, "invalid node %s", m.Node)
}
if m.Price.IsZero() {
return fmt.Errorf("price cannot be zero")
}
if !m.Price.IsValid() {
return fmt.Errorf("price must be valid")
}
if m.Deposit.IsZero() {
return fmt.Errorf("deposit cannot be zero")
}
if !m.Deposit.IsValid() {
return fmt.Errorf("deposit must be valid")
}
}
if m.Plan != 0 {
if m.Denom != "" {
if err := sdk.ValidateDenom(m.Denom); err != nil {
return errors.Wrapf(err, "invalid denom %s", m.Denom)
}
}
if m.Expiry.IsZero() {
return fmt.Errorf("expiry cannot be zero")
}
}
if m.Free.IsNegative() {
return fmt.Errorf("free cannot not be negative")
}
if !m.Status.IsValid() {
return fmt.Errorf("status must be valid")
}
if m.StatusAt.IsZero() {
return fmt.Errorf("status_at cannot be zero")
}
return nil
}
type (
Subscriptions []Subscription
)