Skip to content

Commit

Permalink
Add staked deso to total supply (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazynina authored Jul 13, 2024
1 parent 1ca20b6 commit 6f63f77
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions routes/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,8 @@ type APIServer struct {
// Cache of Total Supply and Rich List
TotalSupplyNanos uint64
TotalSupplyDESO float64
TotalStakedNanos uint64
TotalStakedDESO float64
RichList []RichListEntryResponse
CountKeysWithDESO uint64

Expand Down
53 changes: 53 additions & 0 deletions routes/supply.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package routes

import (
"bytes"
"encoding/json"
"fmt"
"github.com/deso-protocol/core/lib"
Expand Down Expand Up @@ -100,6 +101,58 @@ func (fes *APIServer) UpdateSupplyStats() {
totalSupply += lib.DecodeUint64(ccKey[1 : 1+uint64BytesLen])
}

validatorsStartPrefix := append([]byte{}, lib.Prefixes.PrefixValidatorByStatusAndStakeAmount...)
validatorsValidForPrefix := append([]byte{}, lib.Prefixes.PrefixValidatorByStatusAndStakeAmount...)
validatorsKeysFound, _, err := lib.DBGetPaginatedKeysAndValuesForPrefix(
fes.TXIndex.TXIndexChain.DB(), validatorsStartPrefix, validatorsValidForPrefix,
0, -1, false, false)
if err != nil {
glog.Errorf("StartSupplyMonitoring: Error getting all validators")
}
totalStakeSupply := uint64(0)
for _, validatorKey := range validatorsKeysFound {
validatorStakeAmount, err := lib.FixedWidthDecodeUint256(bytes.NewReader(validatorKey[2:]))
if err != nil {
glog.Errorf("StartSupplyMonitoring: Error decoding validator stake amount: %v", err)
continue
}
if validatorStakeAmount == nil || !validatorStakeAmount.IsUint64() {
glog.Errorf("StartSupplyMonitoring: Validator stake amount is not a uint64")
continue
}
totalStakeSupply += validatorStakeAmount.Uint64()
}
totalSupply += totalStakeSupply

lockedStakeStartPrefix := append([]byte{}, lib.Prefixes.PrefixLockedStakeByValidatorAndStakerAndLockedAt...)
lockedStakeValidForPrefix := append([]byte{}, lib.Prefixes.PrefixLockedStakeByValidatorAndStakerAndLockedAt...)
_, lockedStakeEntries, err := lib.DBGetPaginatedKeysAndValuesForPrefix(
fes.TXIndex.TXIndexChain.DB(), lockedStakeStartPrefix, lockedStakeValidForPrefix,
0, -1, false, true)
if err != nil {
glog.Errorf("StartSupplyMonitoring: Error getting all locked stake entries")
}
committedTip, _ := fes.TXIndex.TXIndexChain.GetCommittedTip()

for _, lockedStakeEntry := range lockedStakeEntries {
lse := &lib.LockedStakeEntry{}
if err = lse.RawDecodeWithoutMetadata(
uint64(committedTip.Height),
bytes.NewReader(lockedStakeEntry),
); err != nil {
glog.Errorf("StartSupplyMonitoring: Error decoding locked stake entry: %v", err)
continue
}

if !lse.LockedAmountNanos.IsUint64() {
glog.Errorf("StartSupplyMonitoring: Locked amount is not a uint64")
continue
}
totalSupply += lse.LockedAmountNanos.Uint64()
}

fes.TotalStakedNanos = totalStakeSupply
fes.TotalStakedDESO = float64(totalStakeSupply) / float64(lib.NanosPerUnit)
fes.TotalSupplyNanos = totalSupply
fes.TotalSupplyDESO = float64(totalSupply) / float64(lib.NanosPerUnit)

Expand Down

0 comments on commit 6f63f77

Please sign in to comment.