-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (99 loc) · 3.13 KB
/
main.go
File metadata and controls
138 lines (99 loc) · 3.13 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"encoding/hex"
"strconv"
"time"
"github.com/AccumulateNetwork/metrics-api/accumulate"
"github.com/AccumulateNetwork/metrics-api/api"
"github.com/AccumulateNetwork/metrics-api/schema"
"github.com/AccumulateNetwork/metrics-api/store"
"github.com/jinzhu/copier"
"github.com/labstack/gommon/log"
)
const ACCUMULATE_API = "https://mainnet.accumulatenetwork.io/v2"
const ACCUMULATE_CLIENT_TIMEOUT = 5
const API_PORT = 8082
const ACME_TOKEN_ISSUER = "acc://acme"
const STAKING_DATA_ACCOUNT = "acc://staking.acme/registered"
const STAKING_PAGESIZE = 10000
func main() {
store.StakingRecords = &schema.StakingRecords{}
client := accumulate.NewAccumulateClient(ACCUMULATE_API, ACCUMULATE_CLIENT_TIMEOUT)
die := make(chan bool)
go getStats(client, die)
log.Fatal(api.StartAPI(API_PORT))
}
func getStats(client *accumulate.AccumulateClient, die chan bool) {
for {
select {
default:
acme := &schema.ACME{}
acmeData, err := client.QueryToken(&accumulate.Params{URL: ACME_TOKEN_ISSUER})
if err != nil {
log.Error(err)
}
copier.Copy(&acme, acmeData.Data)
acme.Total, err = strconv.ParseInt(acmeData.Data.Issued, 10, 64)
if err != nil {
log.Error(err)
}
acme.Max, err = strconv.ParseInt(acmeData.Data.SupplyLimit, 10, 64)
if err != nil {
log.Error(err)
}
store.ACME = acme
stakingData, err := client.QueryDataSet(&accumulate.Params{URL: STAKING_DATA_ACCOUNT, Count: STAKING_PAGESIZE, Start: 0, Expand: true})
if err != nil {
log.Error(err)
}
log.Info("received ", len(stakingData.Items), " data entries from ", STAKING_DATA_ACCOUNT)
// parse staking data entries
for _, entry := range stakingData.Items {
entryData, err := hex.DecodeString(entry.Entry.Data[0])
if err != nil {
log.Error(err)
continue
}
stRecord, err := schema.ParseStakingRecord(entryData)
if err != nil {
log.Error(err)
continue
}
// fill entry hash
stRecord.EntryHash = entry.EntryHash
// check if record with this identity already exists
exists := store.SearchStakingRecordByIdentity(stRecord.Identity)
// if not found, append new record
if exists == nil {
log.Debug("added staking record for: ", stRecord.Identity)
store.StakingRecords.Items = append(store.StakingRecords.Items, stRecord)
continue
}
log.Debug("updated staking record for: ", stRecord.Identity)
*exists = *stRecord
}
log.Info("total staking records: ", len(store.StakingRecords.Items))
snapshot := &schema.StakingRecords{}
copier.Copy(&snapshot.Items, store.StakingRecords.Items)
// get ACME balances of stakers
for _, record := range snapshot.Items {
balance, err := client.QueryTokenAccount(&accumulate.Params{URL: record.Stake})
if err != nil {
log.Error(err)
continue
}
record.Balance, err = strconv.ParseInt(balance.Data.Balance, 10, 64)
if err != nil {
log.Error(err)
continue
}
}
copier.Copy(&store.StakingRecords.Items, snapshot.Items)
now := time.Now()
store.UpdatedAt = &now
time.Sleep(time.Duration(10) * time.Minute)
case <-die:
return
}
}
}