-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathSortitionModuleNeo.sol
119 lines (103 loc) · 4.2 KB
/
SortitionModuleNeo.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
// SPDX-License-Identifier: MIT
/**
* @custom:authors: [@jaybuidl, @unknownunknown1]
* @custom:reviewers: []
* @custom:auditors: []
* @custom:bounties: []
* @custom:deployments: []
*/
pragma solidity 0.8.24;
import {SortitionModuleBase, KlerosCore, RNG, StakingResult} from "./SortitionModuleBase.sol";
/// @title SortitionModuleNeo
/// @dev A factory of trees that keeps track of staked values for sortition.
contract SortitionModuleNeo is SortitionModuleBase {
string public constant override version = "0.8.0";
// ************************************* //
// * Storage * //
// ************************************* //
uint256 public maxStakePerJuror;
uint256 public maxTotalStaked;
uint256 public totalStaked;
// ************************************* //
// * Constructor * //
// ************************************* //
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/// @dev Initializer (constructor equivalent for upgradable contracts).
/// @param _governor The governor.
/// @param _core The KlerosCore.
/// @param _minStakingTime Minimal time to stake
/// @param _maxDrawingTime Time after which the drawing phase can be switched
/// @param _rng The random number generator.
/// @param _rngLookahead Lookahead value for rng.
/// @param _maxStakePerJuror The maximum amount of PNK a juror can stake in a court.
/// @param _maxTotalStaked The maximum amount of PNK that can be staked in all courts.
function initialize(
address _governor,
KlerosCore _core,
uint256 _minStakingTime,
uint256 _maxDrawingTime,
RNG _rng,
uint256 _rngLookahead,
uint256 _maxStakePerJuror,
uint256 _maxTotalStaked
) external reinitializer(2) {
__SortitionModuleBase_initialize(_governor, _core, _minStakingTime, _maxDrawingTime, _rng, _rngLookahead);
maxStakePerJuror = _maxStakePerJuror;
maxTotalStaked = _maxTotalStaked;
}
function initialize3() external reinitializer(3) {
// NOP
}
// ************************************* //
// * Governance * //
// ************************************* //
/// @dev Access Control to perform implementation upgrades (UUPS Proxiable)
/// Only the governor can perform upgrades (`onlyByGovernor`)
function _authorizeUpgrade(address) internal view override onlyByGovernor {
// NOP
}
function changeMaxStakePerJuror(uint256 _maxStakePerJuror) external onlyByGovernor {
maxStakePerJuror = _maxStakePerJuror;
}
function changeMaxTotalStaked(uint256 _maxTotalStaked) external onlyByGovernor {
maxTotalStaked = _maxTotalStaked;
}
// ************************************* //
// * State Modifiers * //
// ************************************* //
function _setStake(
address _account,
uint96 _courtID,
uint256 _newStake,
bool _alreadyTransferred
) internal override onlyByCore returns (uint256 pnkDeposit, uint256 pnkWithdrawal, StakingResult stakingResult) {
uint256 currentStake = stakeOf(_account, _courtID);
bool stakeIncrease = _newStake > currentStake;
uint256 stakeChange = stakeIncrease ? _newStake - currentStake : currentStake - _newStake;
Juror storage juror = jurors[_account];
if (stakeIncrease && !_alreadyTransferred) {
if (juror.stakedPnk + stakeChange > maxStakePerJuror) {
return (0, 0, StakingResult.CannotStakeMoreThanMaxStakePerJuror);
}
if (totalStaked + stakeChange > maxTotalStaked) {
return (0, 0, StakingResult.CannotStakeMoreThanMaxTotalStaked);
}
}
if (phase == Phase.staking) {
if (stakeIncrease) {
totalStaked += stakeChange;
} else {
totalStaked -= stakeChange;
}
}
(pnkDeposit, pnkWithdrawal, stakingResult) = super._setStake(
_account,
_courtID,
_newStake,
_alreadyTransferred
);
}
}