-
Notifications
You must be signed in to change notification settings - Fork 21
/
payer.go
83 lines (73 loc) · 1.73 KB
/
payer.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
package main
import (
"strconv"
"strings"
"time"
)
type payer struct {
db *database
conf *config
owner *OwnerAPI
}
type jsonRPCResponse struct {
ID string `json:"id"`
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Result interface{} `json:"result"`
Error map[string]interface{} `json:"error"`
}
// distribute coins when balance is > 1e9 nano
func (p *payer) distribute(newBalance uint64) {
// get a distribution table
revenue4Miners := uint64(float64(newBalance) * (1 - p.conf.Payer.Fee))
p.db.calcRevenueToday(revenue4Miners)
}
func (p *payer) watch() {
go func() {
m := strings.Split(p.conf.Payer.Time, ":")
hour, err := strconv.Atoi(m[0])
if err != nil {
log.Error(err)
}
min, err := strconv.Atoi(m[1])
if err != nil {
log.Error(err)
}
var getNewBalance func() uint64
switch p.conf.Wallet.OwnerAPIVersion {
case "v1":
getNewBalance = p.owner.getNewBalanceV1
case "v2":
getNewBalance = p.owner.getNewBalanceV2
case "v3":
getNewBalance = p.owner.getNewBalanceV3
}
for {
now := time.Now()
t := time.Date(now.Year(), now.Month(), now.Day(), hour, min, 0, 0, now.Location())
if t.After(now) == false {
next := now.Add(time.Hour * 24)
t = time.Date(next.Year(), next.Month(), next.Day(), hour, min, 0, 0, next.Location())
}
timer := time.NewTimer(t.Sub(now))
select {
case <-timer.C:
newBalance := getNewBalance()
if newBalance > 1e9 {
p.distribute(newBalance - 1e9)
} else {
p.distribute(0)
}
}
}
}()
}
func initPayer(db *database, conf *config) *payer {
p := &payer{
db: db,
conf: conf,
owner: NewOwnerAPI(db, conf),
}
p.watch()
return p
}