-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidatorData.sol
371 lines (288 loc) · 12.6 KB
/
ValidatorData.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.19;
interface InterfaceValidator {
enum Status {
// validator not exist, default status
NotExist,
// validator created
Created,
// anyone has staked for the validator
Staked,
// validator's staked coins < MinimalStakingCoin
Unstaked,
// validator is jailed by system(validator have to repropose)
Jailed
}
function getTopValidators() external view returns(address[] memory);
function getValidatorInfo(address val)external view returns(address payable, Status, uint256, uint256, uint256, address[] memory);
function getValidatorDescription(address val) external view returns ( string memory,string memory,string memory,string memory,string memory);
function totalStake() external view returns(uint256);
function getStakingInfo(address staker, address validator) external view returns(uint256, uint256, uint256);
function viewStakeReward(address _staker, address _validator) external view returns(uint256);
function MinimalStakingCoin() external view returns(uint256);
function StakingLockPeriod() external view returns(uint64);
function MaxValidators() external view returns(uint256);
function stakeTime(address staker, address validator) external view returns(uint);
//write functions
function createOrEditValidator(
address payable feeAddr,
string calldata moniker,
string calldata identity,
string calldata website,
string calldata email,
string calldata details
) external payable returns (bool);
function unstake(address validator)
external
returns (bool);
function withdrawProfits(address validator) external returns (bool);
function unstakeWaitingTime() external view returns(uint256);
}
interface InterfaceStaking {
function withdrawStakingReward(address staker) external returns(bool);
function mutateValStakeTimeMapping(address validator, uint256 value) external ;
function unstake() external returns (bool);
}
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(
address indexed previousOwner,
address indexed newOwner
);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(
newOwner != address(0),
"Ownable: new owner is the zero address"
);
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
contract ValidatorHelper is Ownable {
InterfaceValidator public valContract = InterfaceValidator(0x000000000000000000000000000000000000f000);
InterfaceStaking public stakingContract;
uint256 public minimumValidatorStaking = 50000 * 1e18;
//events
event Stake(address validator, uint256 amount, uint256 timestamp);
event Unstake(address validator, uint256 timestamp);
event WithdrawProfit(address validator, uint256 amount, uint256 timestamp);
receive() external payable {
}
function createOrEditValidator(
address payable feeAddr,
string calldata moniker,
string calldata identity,
string calldata website,
string calldata email,
string calldata details
) external payable returns (bool) {
require(msg.value >= minimumValidatorStaking, "Please stake minimum validator staking" );
valContract.createOrEditValidator{value: msg.value}(feeAddr, moniker, identity, website, email, details);
stakingContract.mutateValStakeTimeMapping(msg.sender,0);
emit Stake(msg.sender, msg.value, block.timestamp);
return true;
}
function unstake(address validator)
external
returns (bool)
{
valContract.unstake(validator);
stakingContract.mutateValStakeTimeMapping(validator,1);
emit Unstake(msg.sender, block.timestamp);
return true;
}
function withdrawStakingReward(address validator) external {
require(validator == tx.origin, "caller should be real validator");
valContract.withdrawProfits(validator);
}
function viewValidatorRewards(address validator) public view returns(uint256){
(, InterfaceValidator.Status validatorStatus, , uint256 rewardAmount , , ) = valContract.getValidatorInfo(validator);
// if validator is jailed, non-exist, or created, then he will not get any rewards
if(validatorStatus == InterfaceValidator.Status.Jailed || validatorStatus == InterfaceValidator.Status.NotExist || validatorStatus == InterfaceValidator.Status.Created ){
return 0;
}
rewardAmount += valContract.viewStakeReward(validator,validator);
return rewardAmount;
}
/**
admin functions
*/
function rescueCoins() external onlyOwner{
payable(msg.sender).transfer(address(this).balance);
}
function changeMinimumValidatorStaking(uint256 amount) external onlyOwner{
minimumValidatorStaking = amount;
}
function setStakingContract(address _staking) external onlyOwner
{
require(_staking != address(0), "invalid contract");
stakingContract = InterfaceStaking(_staking);
}
/**
View functions
*/
function getMaxValidator() external view returns(uint256)
{
return valContract.MaxValidators();
}
function getminimumValidatorStaking() external view returns (uint256)
{
return minimumValidatorStaking;
}
function getStakingTime(address staker, address val) external view returns (uint256)
{
return valContract.stakeTime(staker, val);
}
function getAllValidatorInfo() external view returns (uint256 totalValidatorCount,uint256 totalStakedCoins,address[] memory,InterfaceValidator.Status[] memory,uint256[] memory,string[] memory,string[] memory)
{
address[] memory highestValidatorsSet = valContract.getTopValidators();
uint256 totalValidators = highestValidatorsSet.length;
uint256 totalunstaked ;
InterfaceValidator.Status[] memory statusArray = new InterfaceValidator.Status[](totalValidators);
uint256[] memory coinsArray = new uint256[](totalValidators);
string[] memory identityArray = new string[](totalValidators);
string[] memory websiteArray = new string[](totalValidators);
for(uint8 i=0; i < totalValidators; i++){
(, InterfaceValidator.Status status, uint256 coins, , , ) = valContract.getValidatorInfo(highestValidatorsSet[i]);
if(coins>0){
(, string memory identity, string memory website, ,) = valContract.getValidatorDescription(highestValidatorsSet[i]);
statusArray[i] = status;
coinsArray[i] = coins;
identityArray[i] = identity;
websiteArray[i] = website;
}
else
{
totalunstaked += 1;
}
}
return(totalValidators - totalunstaked , valContract.totalStake(), highestValidatorsSet, statusArray, coinsArray, identityArray, websiteArray);
}
function validatorSpecificInfo1(address validatorAddress, address user) external view returns(string memory identityName, string memory website, string memory otherDetails, uint256 withdrawableRewards, uint256 stakedCoins, uint256 waitingBlocksForUnstake ){
(, string memory identity, string memory websiteLocal, ,string memory details) = valContract.getValidatorDescription(validatorAddress);
uint256 unstakeBlock;
(stakedCoins, unstakeBlock, ) = valContract.getStakingInfo(validatorAddress,validatorAddress);
if(unstakeBlock!=0){
waitingBlocksForUnstake = stakedCoins;
stakedCoins = 0;
}
return(identity, websiteLocal, details, viewValidatorRewards(validatorAddress), stakedCoins, waitingBlocksForUnstake) ;
}
function validatorSpecificInfo2(address validatorAddress, address user) external view returns(uint256 totalStakedCoins, InterfaceValidator.Status status, uint256 selfStakedCoins, uint256 masterVoters, uint256 stakers, address){
address[] memory stakersArray;
(, status, totalStakedCoins, , , stakersArray) = valContract.getValidatorInfo(validatorAddress);
(selfStakedCoins, , ) = valContract.getStakingInfo(validatorAddress,validatorAddress);
return (totalStakedCoins, status, selfStakedCoins, 0, stakersArray.length, user);
}
function waitingWithdrawProfit(address user, address validatorAddress) external view returns(uint256){
// no waiting to withdraw profit.
// this is kept for backward UI compatibility
return 0;
}
function waitingUnstaking(address user, address validator) external view returns(uint256){
uint256 valstaketime = valContract.stakeTime(validator, validator) + valContract.unstakeWaitingTime();
if(block.timestamp < valstaketime)
{
return valstaketime - block.timestamp;
}
return 0;
}
function waitingWithdrawStaking(address user, address validatorAddress) public view returns(uint256){
//validator and delegators will have waiting
(, uint256 unstakeBlock, ) = valContract.getStakingInfo(user,validatorAddress);
if(unstakeBlock==0){
return 0;
}
if(unstakeBlock + valContract.StakingLockPeriod() > block.number){
return 2 * ((unstakeBlock + valContract.StakingLockPeriod()) - block.number);
}
return 0;
}
function minimumStakingAmount() external view returns(uint256){
return valContract.MinimalStakingCoin();
}
function stakingValidations(address user, address validatorAddress) external view returns(uint256 minimumStakingAmt, uint256 stakingWaiting){
return (valContract.MinimalStakingCoin(), waitingWithdrawStaking(user, validatorAddress));
}
function checkValidator(address user) external view returns(bool){
//this function is for UI compatibility
return true;
}
}