-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHubReader.sol
212 lines (193 loc) · 6.93 KB
/
HubReader.sol
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import {IStakeHub} from "./interface/IStakeHub.sol";
import {IStakeCredit} from "./interface/IStakeCredit.sol";
struct Validator {
address operatorAddress;
bool jailed;
string moniker;
uint64 commission;
uint64 apy;
}
struct Delegation {
address delegatorAddress;
address validatorAddress;
uint256 amount;
uint256 shares;
}
struct Undelegation {
address delegatorAddress;
address validatorAddress;
uint256 amount;
uint256 shares;
uint256 unlockTime;
}
contract HubReader {
IStakeHub public stakeHub;
constructor() {
stakeHub = IStakeHub(0x0000000000000000000000000000000000002002);
}
/*
* @dev Get validators by offset and limit (pagination), this is an intense function that execution might revert if limit is too large
* for some node, the best way to call it is to use a batch JSON RPC calls with proper pagination (e.g. 15 or 20 validators per call)
* @param offset The offset to query validators
* @param limit The limit to query validators
*
* @return The validators
*/
function getValidators(
uint16 offset,
uint16 limit
) external view returns (Validator[] memory) {
(address[] memory operatorAddrs, , uint256 totalLength) = stakeHub
.getValidators(offset, limit);
uint256 validatorCount = totalLength < limit ? totalLength : limit;
Validator[] memory validators = new Validator[](validatorCount);
for (uint256 i = 0; i < validatorCount; i++) {
(, bool jailed, ) = stakeHub.getValidatorBasicInfo(
operatorAddrs[i]
);
string memory moniker = stakeHub
.getValidatorDescription(operatorAddrs[i])
.moniker;
uint64 rate = stakeHub
.getValidatorCommission(operatorAddrs[i])
.rate;
validators[i] = Validator({
operatorAddress: operatorAddrs[i],
moniker: moniker,
commission: rate,
jailed: jailed,
apy: 0
});
}
uint64[] memory apys = this.getAPYs(operatorAddrs, block.timestamp);
for (uint256 i = 0; i < validatorCount; i++) {
validators[i].apy = apys[i];
}
return validators;
}
/*
* @dev Get current delegations of a delegator
* @param delegator The address of the delegator
* @param offset The offset to query validators
* @param limit The limit to query validators
*
* @return The delegations of the delegator
*/
function getDelegations(
address delegator,
uint16 offset,
uint16 limit
) external view returns (Delegation[] memory) {
(
address[] memory operatorAddrs,
address[] memory creditAddrs,
uint256 totalLength
) = stakeHub.getValidators(offset, limit);
uint256 validatorCount = totalLength < limit ? totalLength : limit;
uint256 delegationCount = 0;
Delegation[] memory delegations = new Delegation[](validatorCount);
for (uint256 i = 0; i < validatorCount; i++) {
IStakeCredit creditContract = IStakeCredit(creditAddrs[i]);
uint256 shares = creditContract.balanceOf(delegator);
uint256 amount = creditContract.getPooledBNBByShares(shares);
if (amount > 0) {
delegations[delegationCount] = Delegation({
delegatorAddress: delegator,
validatorAddress: operatorAddrs[i],
shares: shares,
amount: amount
});
delegationCount++;
}
}
// Resize the array to fit actual number of delegations
assembly {
mstore(delegations, delegationCount)
}
return delegations;
}
/**
* @dev Get undelegations of a delegator
* @param delegator The address of the delegator
* @param offset The offset to query validators
* @param limit The limit to query validators
*
* @return The undelegations of the delegator
*/
function getUndelegations(
address delegator,
uint16 offset,
uint16 limit
) external view returns (Undelegation[] memory) {
(
address[] memory operatorAddrs,
address[] memory creditAddrs,
uint256 totalLength
) = stakeHub.getValidators(offset, limit);
uint256 validatorCount = totalLength < limit ? totalLength : limit;
// first loop to get the number of unbond requests
uint256 undelegationCount = 0;
for (uint256 i = 0; i < validatorCount; i++) {
undelegationCount += IStakeCredit(creditAddrs[i])
.pendingUnbondRequest(delegator);
}
Undelegation[] memory undelegations = new Undelegation[](
undelegationCount
);
// resuse same local variable
undelegationCount = 0;
for (uint256 i = 0; i < validatorCount; i++) {
uint256 unbondCount = IStakeCredit(creditAddrs[i])
.pendingUnbondRequest(delegator);
for (uint256 j = 0; j < unbondCount; j++) {
IStakeCredit.UnbondRequest memory req = IStakeCredit(
creditAddrs[i]
).unbondRequest(delegator, j);
undelegations[undelegationCount] = Undelegation({
delegatorAddress: delegator,
validatorAddress: operatorAddrs[i],
shares: req.shares,
amount: req.bnbAmount,
unlockTime: req.unlockTime
});
undelegationCount++;
}
}
return undelegations;
}
/*
* @dev Get APYs of an array of validators at a given timestamp
* @param operatorAddr The address of the validator
* @param timestamp The timestamp of the block
*
* @return The APYs of the validator in basis points, e.g. 195 is 1.95%
*/
function getAPYs(
address[] memory operatorAddrs,
uint256 timestamp
) external view returns (uint64[] memory) {
uint256 dayIndex = timestamp / stakeHub.BREATHE_BLOCK_INTERVAL();
uint256 length = operatorAddrs.length;
uint64[] memory apys = new uint64[](length);
for (uint256 i = 0; i < length; i++) {
uint256 total = stakeHub.getValidatorTotalPooledBNBRecord(
operatorAddrs[i],
dayIndex
);
if (total == 0) {
continue;
}
uint256 reward = stakeHub.getValidatorRewardRecord(
operatorAddrs[i],
dayIndex
);
if (reward == 0) {
continue;
}
apys[i] = uint64((reward * 365 * 10000) / total);
}
return apys;
}
}