-
Notifications
You must be signed in to change notification settings - Fork 41
/
cellarfees.go
68 lines (59 loc) · 1.57 KB
/
cellarfees.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
package types
import (
"sort"
"strings"
)
func DefaultFeeAccrualCounters() FeeAccrualCounters {
return FeeAccrualCounters{
Counters: []FeeAccrualCounter{},
}
}
// Implementing sort.Interface (see https://pkg.go.dev/sort#Interface)
func (f FeeAccrualCounters) Len() int {
return len(f.Counters)
}
func (f FeeAccrualCounters) Less(i, j int) bool {
return strings.Compare(f.Counters[i].Denom, f.Counters[j].Denom) == -1
}
func (f FeeAccrualCounters) Swap(i, j int) {
f.Counters[i], f.Counters[j] = f.Counters[j], f.Counters[i]
}
// WARNING: If editing these methods, be aware that insert() doesn't check if the denom is already
// present. Duplicate denom entries can result in fees not being auctioned.
func (f *FeeAccrualCounters) insertCounter(denom string, count uint64) {
f.Counters = append(f.Counters, FeeAccrualCounter{Denom: denom, Count: count})
sort.Sort(f)
}
// Increment fee accrual counter for denom. If it isn't present, append it to the counters
// slice and increment to 1.
func (f *FeeAccrualCounters) IncrementCounter(denom string) uint64 {
found := false
var count uint64
for i, k := range f.Counters {
if k.Denom == denom {
found = true
f.Counters[i].Count++
count = f.Counters[i].Count
break
}
}
if !found {
f.insertCounter(denom, 1)
count = 1
}
return count
}
// Sets the denom's fee accrual counter to zero
func (f *FeeAccrualCounters) ResetCounter(denom string) {
found := false
for i, k := range f.Counters {
if k.Denom == denom {
found = true
f.Counters[i].Count = 0
break
}
}
if !found {
f.insertCounter(denom, 0)
}
}