-
Notifications
You must be signed in to change notification settings - Fork 2
/
supply.go
158 lines (137 loc) · 4.89 KB
/
supply.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package routes
import (
"encoding/json"
"fmt"
"github.com/deso-smart/deso-core/v2/lib"
"github.com/golang/glog"
"net/http"
"sort"
"time"
)
const richListLength = 1000
// Only keep balances in rich list if balance is greater than 100 DESO
const richListMin = 100 * lib.NanosPerUnit
type RichListEntry struct {
KeyBytes []byte
BalanceNanos uint64
}
type RichListEntryResponse struct {
PublicKeyBase58Check string
BalanceNanos uint64
BalanceDESO float64
Percentage float64
Value float64
}
// StartSupplyMonitoring begins monitoring the top 1000 public keys with the most DESO and the total supply
func (fes *APIServer) StartSupplyMonitoring() {
go func() {
out:
for {
select {
case <-time.After(10 * time.Minute):
fes.UpdateSupplyStats()
case <-fes.quit:
break out
}
}
}()
}
func (fes *APIServer) UpdateSupplyStats() {
totalSupply := uint64(0)
totalKeysWithDESO := uint64(0)
// Get all the balances from the DB
startPrefix := lib.DbGetPrefixForPublicKeyToDesoBalanceNanos()
validForPrefix := lib.DbGetPrefixForPublicKeyToDesoBalanceNanos()
keysFound, valsFound, err := lib.DBGetPaginatedKeysAndValuesForPrefix(
fes.TXIndex.TXIndexChain.DB(), startPrefix, validForPrefix,
0, -1, false, true)
if err != nil {
glog.Errorf("StartSupplyMonitoring: Error getting all balances")
}
var richList []RichListEntry
// For each key-value pair from the list of all DESO balances, if the balance is high enough to be in
// the rich list at the current state, add it to our temporary rich list.
// NOTE: this would be more performance if we kept some sort of priority queue
for keyIndex, key := range keysFound {
balanceNanos := lib.DecodeUint64(valsFound[keyIndex])
totalSupply += balanceNanos
if balanceNanos > 0 {
totalKeysWithDESO++
}
// We don't need to keep all of the balances, just the top ones so let's skip adding items
// if we know they won't make the cut.
if balanceNanos >= richListMin {
richList = append(richList, RichListEntry{
KeyBytes: key,
BalanceNanos: balanceNanos,
})
}
}
fes.CountKeysWithDESO = totalKeysWithDESO
// Get all the keys for the Prefix that is ordered by DESO locked in creator coins
uint64BytesLen := 8
ccStartPrefix := lib.DbPrefixForCreatorDeSoLockedNanosCreatorPKID()
ccValidForPrefix := lib.DbPrefixForCreatorDeSoLockedNanosCreatorPKID()
ccKeysFound, _, err := lib.DBGetPaginatedKeysAndValuesForPrefix(
fes.TXIndex.TXIndexChain.DB(), ccStartPrefix, ccValidForPrefix,
0, -1, false, false)
if err != nil {
glog.Errorf("StartSupplyMonitoring: Error getting all DESO locked in CCs")
}
// For each key, extract the DESO locked and add it to the total supply
for _, ccKey := range ccKeysFound {
totalSupply += lib.DecodeUint64(ccKey[1 : 1+uint64BytesLen])
}
fes.TotalSupplyNanos = totalSupply
fes.TotalSupplyDESO = float64(totalSupply) / float64(lib.NanosPerUnit)
sort.Slice(richList, func(ii, jj int) bool {
return richList[ii].BalanceNanos > richList[jj].BalanceNanos
})
endIdx := richListLength
if len(richList) < richListLength {
endIdx = len(richList)
}
richList = richList[:endIdx]
// Convert RichListEntries to RichListEntryResponses
var richListResponses []RichListEntryResponse
for _, item := range richList {
richListResponses = append(richListResponses, RichListEntryResponse{
PublicKeyBase58Check: lib.PkToString(item.KeyBytes[1:], fes.Params),
BalanceNanos: item.BalanceNanos,
BalanceDESO: float64(item.BalanceNanos) / float64(lib.NanosPerUnit),
Value: fes.GetUSDFromNanos(item.BalanceNanos),
Percentage: float64(item.BalanceNanos) / float64(totalSupply),
})
}
fes.RichList = richListResponses
}
func (fes *APIServer) GetTotalSupply(ww http.ResponseWriter, req *http.Request) {
if !fes.Config.RunSupplyMonitoringRoutine {
_AddBadRequestError(ww, fmt.Sprintf("Supply Monitoring is not enabled on this node"))
return
}
if err := json.NewEncoder(ww).Encode(fes.TotalSupplyDESO); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetTotalSupply: Error encoding response: %v", err))
return
}
}
func (fes *APIServer) GetRichList(ww http.ResponseWriter, req *http.Request) {
if !fes.Config.RunSupplyMonitoringRoutine {
_AddBadRequestError(ww, fmt.Sprintf("Supply Monitoring is not enabled on this node"))
return
}
if err := json.NewEncoder(ww).Encode(fes.RichList); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetRichList: Error encoding response: %v", err))
return
}
}
func (fes *APIServer) GetCountKeysWithDESO(ww http.ResponseWriter, req *http.Request) {
if !fes.Config.RunSupplyMonitoringRoutine {
_AddBadRequestError(ww, fmt.Sprintf("Supply Monitoring is not enabled on this node"))
return
}
if err := json.NewEncoder(ww).Encode(fes.CountKeysWithDESO); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetCountKeysWithDESO: Error encoding response: %v", err))
return
}
}