forked from axelarnetwork/axelar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreshold.go
65 lines (50 loc) · 2.04 KB
/
threshold.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
package utils
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// ZeroThreshold is a threshold that equals to 0
var ZeroThreshold Threshold = NewThreshold(0, 1)
// OneThreshold is a threshold that equals to 1
var OneThreshold Threshold = NewThreshold(1, 1)
// NewThreshold is the constructor for Threshold
func NewThreshold(numerator, denominator int64) Threshold {
return Threshold{Numerator: numerator, Denominator: denominator}
}
// String returns a string representation of the threshold
func (m Threshold) String() string {
return fmt.Sprintf("%d/%d", m.Numerator, m.Denominator)
}
// IsMet returns true if share >= threshold * total
func (m Threshold) IsMet(share sdk.Int, total sdk.Int) bool {
return share.MulRaw(m.Denominator).GTE(total.MulRaw(m.Numerator))
}
// GT returns true if and only if threshold is greater than the given one
func (m Threshold) GT(t Threshold) bool {
return sdk.NewInt(m.Numerator).MulRaw(t.Denominator).GT(sdk.NewInt(m.Denominator).MulRaw(t.Numerator))
}
// GTE returns true if and only if threshold is greater than or equal to the given one
func (m Threshold) GTE(t Threshold) bool {
return sdk.NewInt(m.Numerator).MulRaw(t.Denominator).GTE(sdk.NewInt(m.Denominator).MulRaw(t.Numerator))
}
// LT returns true if and only if threshold is less than the given one
func (m Threshold) LT(t Threshold) bool {
return sdk.NewInt(m.Numerator).MulRaw(t.Denominator).LT(sdk.NewInt(m.Denominator).MulRaw(t.Numerator))
}
// LTE returns true if and only if threshold is less than or equal to the given one
func (m Threshold) LTE(t Threshold) bool {
return sdk.NewInt(m.Numerator).MulRaw(t.Denominator).LTE(sdk.NewInt(m.Denominator).MulRaw(t.Numerator))
}
// Validate returns an error if threshold is invalid
func (m Threshold) Validate() error {
if m.Numerator <= 0 {
return fmt.Errorf("threshold numerator must be a positive integer")
}
if m.Denominator <= 0 {
return fmt.Errorf("threshold denominator must be a positive integer")
}
if m.Numerator > m.Denominator {
return fmt.Errorf("threshold must be <= 1")
}
return nil
}