-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
109 lines (98 loc) · 2.86 KB
/
main.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
// Copyright (c) 2020 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package main
import (
"context"
"log"
"time"
"github.com/iotexproject/iotex-antenna-go/v2/account"
"github.com/iotexproject/iotex-antenna-go/v2/iotex"
"github.com/iotexproject/iotex-proto/golang/iotexapi"
"github.com/iotexproject/iotex-hermes/cmd/claim"
"github.com/iotexproject/iotex-hermes/cmd/dao"
"github.com/iotexproject/iotex-hermes/cmd/distribute"
"github.com/iotexproject/iotex-hermes/util"
)
// main runs the hermes command
func main() {
endpoint := util.MustFetchNonEmptyParam("IO_ENDPOINT")
conn, err := iotex.NewDefaultGRPCConn(endpoint)
if err != nil {
log.Fatalf("construct grpc connection error: %v\n", err)
}
defer conn.Close()
emptyAccount, err := account.NewAccount()
if err != nil {
log.Fatalf("new empty account error: %v\n", err)
}
c := iotex.NewAuthedClient(iotexapi.NewAPIServiceClient(conn), emptyAccount)
err = dao.ConnectDatabase()
if err != nil {
log.Fatalf("create database error: %v\n", err)
}
retry := 0
for {
if retry == 3 {
log.Fatalf("retry 2 times failure, exit.")
}
lastEndEpoch, err := distribute.GetLastEndEpoch(c)
if err != nil {
log.Printf("get last end epoch error: %v\n", err)
retry++
continue
}
startEpoch := lastEndEpoch + 1
resp, err := c.API().GetChainMeta(context.Background(), &iotexapi.GetChainMetaRequest{})
if err != nil {
log.Printf("get chain meta error: %v\n", err)
retry++
continue
}
curEpoch := resp.ChainMeta.Epoch.Num
endEpoch := startEpoch + 23
if endEpoch+2 > curEpoch {
sender, err := distribute.NewSender()
if err != nil {
log.Printf("new sender error: %v\n", err)
retry++
continue
}
sender.Send()
resp, err := c.API().GetChainMeta(context.Background(), &iotexapi.GetChainMetaRequest{})
if err != nil {
log.Printf("get chain meta error: %v\n", err)
retry++
continue
}
curEpoch = resp.ChainMeta.Epoch.Num
if endEpoch+2-curEpoch > 0 {
duration := time.Duration(endEpoch + 2 - curEpoch)
log.Printf("waiting %d hours for next distribute", duration)
time.Sleep(duration * time.Hour)
continue
}
}
err = claim.Reward()
if err != nil {
log.Printf("claim reward error: %v\n", err)
retry++
continue
}
err = distribute.Reward()
if err != nil {
log.Printf("distribute reward error: %v\n", err)
retry++
continue
}
sender, err := distribute.NewSender()
if err != nil {
log.Printf("new sender error: %v\n", err)
retry++
continue
}
sender.Send()
}
}