-
Notifications
You must be signed in to change notification settings - Fork 16
/
pool.go
73 lines (64 loc) · 1.77 KB
/
pool.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
package types
import "math"
// Handles the funders of a pool. Functions safely add and remove funds to funders.
// If amount drops to zero the funder is automatically removed from the list.
// AddAmountToFunder adds the given amount to an existing funder.
// If the funder does not exist, a new funder is inserted.
func (m *Pool) AddAmountToFunder(funderAddress string, amount uint64) {
for _, v := range m.Funders {
if v.Address == funderAddress {
m.TotalFunds += amount
v.Amount += amount
return
}
}
if amount > 0 {
// Funder was not found, insert new funder
m.Funders = append(m.Funders, &Funder{
Address: funderAddress,
Amount: amount,
})
m.TotalFunds += amount
}
}
// SubtractAmountFromFunder subtracts the given amount form an existing funder
// If the amount is grater or equal to the funders amount, the funder is removed.
func (m *Pool) SubtractAmountFromFunder(funderAddress string, amount uint64) {
for i := range m.Funders {
if m.Funders[i].Address == funderAddress {
if m.Funders[i].Amount > amount {
m.TotalFunds -= amount
m.Funders[i].Amount -= amount
} else {
m.TotalFunds -= m.Funders[i].Amount
// Remove funder
m.Funders[i] = m.Funders[len(m.Funders)-1]
m.Funders = m.Funders[:len(m.Funders)-1]
}
return
}
}
}
func (m *Pool) RemoveFunder(funderAddress string) {
m.SubtractAmountFromFunder(funderAddress, math.MaxUint64)
}
func (m *Pool) GetFunderAmount(address string) uint64 {
for _, v := range m.Funders {
if v.Address == address {
return v.Amount
}
}
return 0
}
func (m *Pool) GetLowestFunder() Funder {
if len(m.Funders) == 0 {
return Funder{}
}
lowestFunder := m.Funders[0]
for _, v := range m.Funders {
if v.Amount < lowestFunder.Amount {
lowestFunder = v
}
}
return *lowestFunder
}