-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_reward.go
121 lines (98 loc) · 3.92 KB
/
check_reward.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
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"fmt"
// "io/ioutil"
"net/http"
"strconv"
"encoding/json"
"time"
)
type Earnings struct {
FilAmount float64
Timestamp string
}
type Data struct {
Earnings []Earnings
}
var filAddressLocationMap = map[string]string{
"f1wf7lu7quwz5hgsl5qybnjf................": "US", //这里填写自己的地址和对应地址地理位置,可以添加多个地址
"f1pyfpcscyfto7phqkv2ybx7................": "US",
"f1qbqd757pul7b5dpttmcz2c................": "US",
}
func main() {
filAddresses := []string{
"f1wf7lu7quwz5hgsl........................", //这里对应的是上文中的地址
"f1pyfpcscyfto7phq........................",
"f1qbqd757pul7b5dp........................",
}
for _, filAddress := range filAddresses {
if err := fetchAndProcessData(filAddress); err != nil {
fmt.Println(err)
}
}
sumTotalFilAmount()
}
func fetchAndProcessData(filAddress string) error {
currentTime := time.Now()
currentTimestamp := currentTime.Unix()
currentTimestampInt := int(currentTimestamp)
currentTimestampStr := strconv.Itoa(currentTimestampInt)
fmt.Printf("\nFetching data for Fil address: %s\n", filAddress)
earnings, err := fetchEarningsForTimeRange(filAddress, currentTimestamp-86400, currentTimestamp, currentTimestampStr)
if err != nil {
return err
}
var output string
var output1 string
var totalFilAmount float64
for _, v := range earnings {
output += fmt.Sprintf("filAmount: \x1b[32m%f\x1b[0m, timestamp: %s\n", v.FilAmount, v.Timestamp)
totalFilAmount += v.FilAmount
}
avgFilAmount := totalFilAmount / float64(len(earnings))
output1 = fmt.Sprintf("avgFilAmount: \x1b[32m%f\x1b[0m, totalFilAmount: \x1b[32m%f\x1b[0m", avgFilAmount, totalFilAmount)
fmt.Println(output)
fmt.Println(output1)
location, exists := filAddressLocationMap[filAddress]
if exists {
fmt.Printf("Location: %s\n", location)
} else {
fmt.Println("Location not found for Fil address: ", filAddress)
}
return nil
}
func fetchEarningsForTimeRange(filAddress string, startTimestamp, endTimestamp int64, currentTimestampStr string) ([]Earnings, error) {
url := fmt.Sprintf("https://uc2x7t32m6qmbscsljxoauwoae0yeipw.lambda-url.us-west-2.on.aws/?filAddress=%s&startDate=%d000&endDate=%d000&step=hour¤tTimestamp=%s000", filAddress, startTimestamp, endTimestamp, currentTimestampStr)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var data Data
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data.Earnings, nil
}
func sumTotalFilAmount() {
filAddresses := []string{
"f1wf7lu7quwz5hgsl5qyb................", //这里对应的是上文中的地址
"f1pyfpcscyfto7phqkv2y................",
"f1qbqd757pul7b5dpttmc................",
}
totalFilAmount := 0.0
for _, filAddress := range filAddresses {
earnings, err := fetchEarningsForTimeRange(filAddress, time.Now().Unix()-86400, time.Now().Unix(), strconv.Itoa(int(time.Now().Unix())))
if err != nil {
fmt.Println(err)
continue
}
var new Earnings
for _, v := range earnings {
new.FilAmount += v.FilAmount
new.Timestamp = v.Timestamp
}
totalFilAmount += new.FilAmount
}
fmt.Printf("\x1b[31mTotal Fil Amount: %f\x1b[0m\n", totalFilAmount)
}