This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 231
/
stake.go
156 lines (136 loc) · 4.15 KB
/
stake.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
package solana
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
"github.com/btcsuite/btcutil/base58"
log "github.com/sirupsen/logrus"
"github.com/trustwallet/blockatlas/pkg/blockatlas"
"github.com/trustwallet/blockatlas/services/assets"
"github.com/trustwallet/golibs/types"
)
func arrayOfPubkey(pubkey string) [32]byte {
var array [32]byte
copy(array[:], base58.Decode(pubkey))
return array
}
func (p *Platform) GetActiveValidators() (blockatlas.StakeValidators, error) {
validators, err := assets.GetValidatorsMap(p)
if err != nil {
return nil, err
}
result := make(blockatlas.StakeValidators, 0, len(validators))
for _, v := range validators {
result = append(result, v)
}
return result, nil
}
func (p *Platform) GetValidators() (blockatlas.ValidatorPage, error) {
results := make(blockatlas.ValidatorPage, 0)
validators, err := p.client.GetCurrentVoteAccounts()
if err != nil {
return results, err
}
minimumBalance, err := p.client.GetMinimumBalanceForRentExemption()
if err != nil {
minimumBalance = 0
}
for _, v := range validators {
results = append(results, normalizeValidator(v, minimumBalance))
}
return results, nil
}
func (p *Platform) GetDetails() blockatlas.StakingDetails {
return getDetails()
}
func getDetails() blockatlas.StakingDetails {
return blockatlas.StakingDetails{
Reward: blockatlas.StakingReward{Annual: 0},
MinimumAmount: "0",
LockTime: 0,
Type: blockatlas.DelegationTypeDelegate,
}
}
func normalizeValidator(v VoteAccount, minimumBalance uint64) (validator blockatlas.Validator) {
minimumAmount := strconv.FormatUint(minimumBalance+1, 10) // Must stake at least 1 lamport
return blockatlas.Validator{
Status: true,
ID: v.VotePubkey,
Details: blockatlas.StakingDetails{
Reward: blockatlas.StakingReward{Annual: 0},
MinimumAmount: types.Amount(minimumAmount),
LockTime: 0,
Type: blockatlas.DelegationTypeDelegate,
},
}
}
func (p *Platform) GetDelegations(address string) (blockatlas.DelegationsPage, error) {
accounts, err := p.client.GetStakeAccounts()
if err != nil {
return nil, err
}
stakeAccounts := make([]StakeData, 0)
for _, keyedAccount := range accounts {
account, err := parseStakeData(keyedAccount.Account)
if err != nil {
return nil, err
}
if account.State == StakeStateDelegated && isAuthorized(account, address) {
stakeAccounts = append(stakeAccounts, account)
}
}
if len(stakeAccounts) == 0 {
return make(blockatlas.DelegationsPage, 0), nil
}
validators, err := assets.GetValidatorsMap(p)
if err != nil {
return nil, err
}
epochInfo, err := p.client.GetEpochInfo()
if err != nil {
return nil, err
}
return NormalizeDelegations(stakeAccounts, validators, epochInfo)
}
func parseStakeData(account Account) (stakeAccount StakeData, err error) {
buffer := base58.Decode(account.Data)
r := bytes.NewReader(buffer)
err = binary.Read(r, binary.LittleEndian, &stakeAccount)
return
}
func isAuthorized(stakeAccount StakeData, address string) bool {
return stakeAccount.AuthorizedStaker == arrayOfPubkey(address)
}
func (p *Platform) UndelegatedBalance(address string) (string, error) {
account, err := p.client.GetAccount(address)
if err != nil {
return "0", err
}
return strconv.FormatUint(account.Lamports, 10), nil
}
func NormalizeDelegations(stakeAccounts []StakeData, validators blockatlas.ValidatorMap, epochInfo EpochInfo) (blockatlas.DelegationsPage, error) {
results := make([]blockatlas.Delegation, 0)
for _, stakeState := range stakeAccounts {
votePubkey := base58.Encode(stakeState.VoterPubkey[:])
validator, ok := validators[votePubkey]
if !ok {
log.Debug(fmt.Sprintf("Unpublished solana validator: %s", votePubkey))
continue
}
status := blockatlas.DelegationStatusPending
if stakeState.ActivationEpoch > 0 && stakeState.ActivationEpoch <= epochInfo.Epoch {
status = blockatlas.DelegationStatusActive
}
if epochInfo.Epoch > stakeState.DeactivationEpoch {
continue
}
delegation := blockatlas.Delegation{
Delegator: validator,
Value: strconv.FormatUint(stakeState.Stake, 10),
Status: status,
}
results = append(results, delegation)
}
return results, nil
}