forked from stader-labs/ethx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidatorWithdrawalVault.sol
147 lines (127 loc) · 6.41 KB
/
ValidatorWithdrawalVault.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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.16;
import "./library/UtilLib.sol";
import "./library/ValidatorStatus.sol";
import "./VaultProxy.sol";
import "./interfaces/IPenalty.sol";
import "./interfaces/IPoolUtils.sol";
import "./interfaces/INodeRegistry.sol";
import "./interfaces/IStaderStakePoolManager.sol";
import "./interfaces/IValidatorWithdrawalVault.sol";
import "./interfaces/SDCollateral/ISDCollateral.sol";
import "./interfaces/IOperatorRewardsCollector.sol";
import "@openzeppelin/contracts/utils/math/Math.sol";
contract ValidatorWithdrawalVault is IValidatorWithdrawalVault {
bool internal vaultSettleStatus;
using Math for uint256;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {}
// Allows the contract to receive ETH
receive() external payable {
emit ETHReceived(msg.sender, msg.value);
}
function distributeRewards() external override {
uint8 poolId = VaultProxy(payable(address(this))).poolId();
uint256 validatorId = VaultProxy(payable(address(this))).id();
IStaderConfig staderConfig = VaultProxy(payable(address(this))).staderConfig();
uint256 totalRewards = address(this).balance;
if (!staderConfig.onlyManagerRole(msg.sender) && totalRewards > staderConfig.getRewardsThreshold()) {
emit DistributeRewardFailed(totalRewards, staderConfig.getRewardsThreshold());
revert InvalidRewardAmount();
}
if (totalRewards == 0) {
revert NotEnoughRewardToDistribute();
}
(uint256 userShare, uint256 operatorShare, uint256 protocolShare) = IPoolUtils(staderConfig.getPoolUtils())
.calculateRewardShare(poolId, totalRewards);
// Distribute rewards
IStaderStakePoolManager(staderConfig.getStakePoolManager()).receiveWithdrawVaultUserShare{ value: userShare }();
UtilLib.sendValue(payable(staderConfig.getStaderTreasury()), protocolShare);
IOperatorRewardsCollector(staderConfig.getOperatorRewardsCollector()).depositFor{ value: operatorShare }(
getOperatorAddress(poolId, validatorId, staderConfig)
);
emit DistributedRewards(userShare, operatorShare, protocolShare);
}
function settleFunds() external override {
uint8 poolId = VaultProxy(payable(address(this))).poolId();
uint256 validatorId = VaultProxy(payable(address(this))).id();
IStaderConfig staderConfig = VaultProxy(payable(address(this))).staderConfig();
address nodeRegistry = IPoolUtils(staderConfig.getPoolUtils()).getNodeRegistry(poolId);
if (msg.sender != nodeRegistry) {
revert CallerNotNodeRegistryContract();
}
(uint256 userSharePrelim, uint256 operatorShare, uint256 protocolShare) = calculateValidatorWithdrawalShare();
uint256 penaltyAmount = getUpdatedPenaltyAmount(poolId, validatorId, staderConfig);
if (operatorShare < penaltyAmount) {
ISDCollateral(staderConfig.getSDCollateral()).slashValidatorSD(validatorId, poolId);
penaltyAmount = operatorShare;
}
uint256 userShare = userSharePrelim + penaltyAmount;
operatorShare = operatorShare - penaltyAmount;
// Final settlement
vaultSettleStatus = true;
IPenalty(staderConfig.getPenaltyContract()).markValidatorSettled(poolId, validatorId);
IStaderStakePoolManager(staderConfig.getStakePoolManager()).receiveWithdrawVaultUserShare{ value: userShare }();
UtilLib.sendValue(payable(staderConfig.getStaderTreasury()), protocolShare);
IOperatorRewardsCollector(staderConfig.getOperatorRewardsCollector()).depositFor{ value: operatorShare }(
getOperatorAddress(poolId, validatorId, staderConfig)
);
emit SettledFunds(userShare, operatorShare, protocolShare);
}
function calculateValidatorWithdrawalShare()
public
view
returns (uint256 _userShare, uint256 _operatorShare, uint256 _protocolShare)
{
uint8 poolId = VaultProxy(payable(address(this))).poolId();
IStaderConfig staderConfig = VaultProxy(payable(address(this))).staderConfig();
uint256 TOTAL_STAKED_ETH = staderConfig.getStakedEthPerNode();
uint256 collateralETH = getCollateralETH(poolId, staderConfig); // 0, incase of permissioned NOs
uint256 usersETH = TOTAL_STAKED_ETH - collateralETH;
uint256 contractBalance = address(this).balance;
uint256 totalRewards;
if (contractBalance <= usersETH) {
_userShare = contractBalance;
return (_userShare, _operatorShare, _protocolShare);
} else if (contractBalance <= TOTAL_STAKED_ETH) {
_userShare = usersETH;
_operatorShare = contractBalance - _userShare;
return (_userShare, _operatorShare, _protocolShare);
} else {
totalRewards = contractBalance - TOTAL_STAKED_ETH;
_operatorShare = collateralETH;
_userShare = usersETH;
}
if (totalRewards > 0) {
(uint256 userReward, uint256 operatorReward, uint256 protocolReward) = IPoolUtils(
staderConfig.getPoolUtils()
).calculateRewardShare(poolId, totalRewards);
_userShare += userReward;
_operatorShare += operatorReward;
_protocolShare += protocolReward;
}
}
// HELPER METHODS
function getCollateralETH(uint8 _poolId, IStaderConfig _staderConfig) internal view returns (uint256) {
return IPoolUtils(_staderConfig.getPoolUtils()).getCollateralETH(_poolId);
}
function getOperatorAddress(
uint8 _poolId,
uint256 _validatorId,
IStaderConfig _staderConfig
) internal view returns (address) {
return UtilLib.getOperatorAddressByValidatorId(_poolId, _validatorId, _staderConfig);
}
function getUpdatedPenaltyAmount(
uint8 _poolId,
uint256 _validatorId,
IStaderConfig _staderConfig
) internal returns (uint256) {
address nodeRegistry = IPoolUtils(_staderConfig.getPoolUtils()).getNodeRegistry(_poolId);
(, bytes memory pubkey, , , , , , ) = INodeRegistry(nodeRegistry).validatorRegistry(_validatorId);
bytes[] memory pubkeyArray = new bytes[](1);
pubkeyArray[0] = pubkey;
IPenalty(_staderConfig.getPenaltyContract()).updateTotalPenaltyAmount(pubkeyArray);
return IPenalty(_staderConfig.getPenaltyContract()).totalPenaltyAmount(pubkey);
}
}