Skip to content

Commit 649ce64

Browse files
committed
refactor(protocol-contracts): add virtual to staking functions and change private to internal
1 parent 9141432 commit 649ce64

File tree

4 files changed

+44
-53
lines changed

4 files changed

+44
-53
lines changed

protocol-contracts/staking/contracts/OperatorRewarder.sol

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ contract OperatorRewarder {
260260
* @param receiver The receiver address.
261261
* @return The claimer address.
262262
*/
263-
function claimer(address receiver) public view returns (address) {
263+
function claimer(address receiver) public view virtual returns (address) {
264264
address authorizedClaimer = _authorizedClaimers[receiver];
265265
return authorizedClaimer == address(0) ? receiver : authorizedClaimer;
266266
}
@@ -269,47 +269,47 @@ contract OperatorRewarder {
269269
* @notice Returns the staking token address.
270270
* @return The IERC20 staking token.
271271
*/
272-
function token() public view returns (IERC20) {
272+
function token() public view virtual returns (IERC20) {
273273
return _token;
274274
}
275275

276276
/**
277277
* @notice Returns the ProtocolStaking contract address.
278278
* @return The ProtocolStaking contract.
279279
*/
280-
function protocolStaking() public view returns (ProtocolStaking) {
280+
function protocolStaking() public view virtual returns (ProtocolStaking) {
281281
return _protocolStaking;
282282
}
283283

284284
/**
285285
* @notice Returns the OperatorStaking contract address.
286286
* @return The OperatorStaking contract.
287287
*/
288-
function operatorStaking() public view returns (OperatorStaking) {
288+
function operatorStaking() public view virtual returns (OperatorStaking) {
289289
return _operatorStaking;
290290
}
291291

292292
/**
293293
* @notice Returns true if contract is shutdown.
294294
* @return True if shutdown, false otherwise.
295295
*/
296-
function isShutdown() public view returns (bool) {
296+
function isShutdown() public view virtual returns (bool) {
297297
return _shutdown;
298298
}
299299

300300
/**
301301
* @notice Returns the maximum fee in basis points that the beneficiary can set.
302302
* @return Fee in basis points.
303303
*/
304-
function maxFeeBasisPoints() public view returns (uint16) {
304+
function maxFeeBasisPoints() public view virtual returns (uint16) {
305305
return _maxFeeBasisPoints;
306306
}
307307

308308
/**
309309
* @notice Returns the fee in basis points.
310310
* @return Fee in basis points.
311311
*/
312-
function feeBasisPoints() public view returns (uint16) {
312+
function feeBasisPoints() public view virtual returns (uint16) {
313313
return _feeBasisPoints;
314314
}
315315

@@ -339,7 +339,7 @@ contract OperatorRewarder {
339339
return _unpaidFee(_totalAssetsPlusPaidRewards());
340340
}
341341

342-
function _doTransferOut(address to, uint256 amount) internal {
342+
function _doTransferOut(address to, uint256 amount) internal virtual {
343343
IERC20 token_ = token();
344344
if (amount > token_.balanceOf(address(this))) {
345345
protocolStaking().claimRewards(address(_operatorStaking));
@@ -410,20 +410,20 @@ contract OperatorRewarder {
410410
_feeBasisPoints = basisPoints;
411411
}
412412

413-
function _totalAssetsPlusPaidRewards() internal view returns (uint256) {
413+
function _totalAssetsPlusPaidRewards() internal view virtual returns (uint256) {
414414
return
415415
token().balanceOf(address(this)) +
416416
(isShutdown() ? 0 : protocolStaking().earned(address(operatorStaking()))) +
417417
_totalRewardsPaid;
418418
}
419419

420-
function _unpaidFee(uint256 totalAssetsPlusPaidRewards) internal view returns (uint256) {
420+
function _unpaidFee(uint256 totalAssetsPlusPaidRewards) internal view virtual returns (uint256) {
421421
uint256 totalAssetsPlusPaidRewardsDelta = totalAssetsPlusPaidRewards - _lastClaimTotalAssetsPlusPaidRewards;
422422
return (totalAssetsPlusPaidRewardsDelta * feeBasisPoints()) / 10_000;
423423
}
424424

425425
/// @dev Compute total allocation based on number of shares and total shares. Must take paid rewards into account after.
426-
function _allocation(uint256 share, uint256 total) private view returns (uint256) {
426+
function _allocation(uint256 share, uint256 total) internal view virtual returns (uint256) {
427427
return
428428
SafeCast.toUint256(SafeCast.toInt256(historicalReward()) + _totalVirtualRewardsPaid).mulDiv(share, total);
429429
}

protocol-contracts/staking/contracts/OperatorStaking.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ contract OperatorStaking is ERC1363Upgradeable, ReentrancyGuardTransient, UUPSUp
101101
address beneficiary_,
102102
uint16 initialMaxFeeBasisPoints_,
103103
uint16 initialFeeBasisPoints_
104-
) public initializer {
104+
) public virtual initializer {
105105
__ERC20_init(name, symbol);
106106

107107
OperatorStakingStorage storage $ = _getOperatorStakingStorage();
@@ -391,7 +391,7 @@ contract OperatorStaking is ERC1363Upgradeable, ReentrancyGuardTransient, UUPSUp
391391
return _getOperatorStakingStorage()._operator[controller][operator];
392392
}
393393

394-
function _doTransferOut(address to, uint256 amount) internal {
394+
function _doTransferOut(address to, uint256 amount) internal virtual {
395395
IERC20 asset_ = IERC20(asset());
396396
if (amount > asset_.balanceOf(address(this))) {
397397
protocolStaking().release(address(this));
@@ -422,7 +422,7 @@ contract OperatorStaking is ERC1363Upgradeable, ReentrancyGuardTransient, UUPSUp
422422
emit IERC4626.Deposit(caller, receiver, assets, shares);
423423
}
424424

425-
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
425+
function _authorizeUpgrade(address newImplementation) internal virtual override onlyOwner {}
426426

427427
function _convertToShares(uint256 assets, Math.Rounding rounding) internal view virtual returns (uint256) {
428428
// Shares in redemption have not yet received assets, so we need to account for them in the conversion.
@@ -448,7 +448,7 @@ contract OperatorStaking is ERC1363Upgradeable, ReentrancyGuardTransient, UUPSUp
448448
return 0;
449449
}
450450

451-
function _getOperatorStakingStorage() private pure returns (OperatorStakingStorage storage $) {
451+
function _getOperatorStakingStorage() internal pure returns (OperatorStakingStorage storage $) {
452452
assembly {
453453
$.slot := OPERATOR_STAKING_STORAGE_LOCATION
454454
}

protocol-contracts/staking/contracts/ProtocolStaking.sol

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
9191
address manager,
9292
uint48 initialUnstakeCooldownPeriod,
9393
uint256 initialRewardRate
94-
) public initializer {
94+
) public virtual initializer {
9595
__AccessControlDefaultAdminRules_init(0, governor);
9696
_grantRole(MANAGER_ROLE, manager);
9797
_setRoleAdmin(ELIGIBLE_ACCOUNT_ROLE, MANAGER_ROLE);
@@ -106,7 +106,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
106106
* @dev Stake `amount` tokens from `msg.sender`.
107107
* @param amount The amount of tokens to stake.
108108
*/
109-
function stake(uint256 amount) public {
109+
function stake(uint256 amount) public virtual {
110110
_mint(msg.sender, amount);
111111
IERC20(stakingToken()).safeTransferFrom(msg.sender, address(this), amount);
112112

@@ -124,7 +124,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
124124
* @param amount The amount of tokens to unstake.
125125
* @return releaseTime The timestamp when the unstaked tokens can be released.
126126
*/
127-
function unstake(uint256 amount) public returns (uint48) {
127+
function unstake(uint256 amount) public virtual returns (uint48) {
128128
_burn(msg.sender, amount);
129129

130130
ProtocolStakingStorage storage $ = _getProtocolStakingStorage();
@@ -157,7 +157,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
157157
* @dev Claim staking rewards for `account`. Can be called by anyone.
158158
* @param account The account to claim rewards for.
159159
*/
160-
function claimRewards(address account) public {
160+
function claimRewards(address account) public virtual {
161161
uint256 rewards = earned(account);
162162
if (rewards > 0) {
163163
_getProtocolStakingStorage()._paid[account] += SafeCast.toInt256(rewards);
@@ -171,7 +171,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
171171
* @dev Sets the reward rate in tokens per second. Only callable by `MANAGER_ROLE` role.
172172
* @param rewardRate_ The new reward rate in tokens per second.
173173
*/
174-
function setRewardRate(uint256 rewardRate_) public onlyRole(MANAGER_ROLE) {
174+
function setRewardRate(uint256 rewardRate_) public virtual onlyRole(MANAGER_ROLE) {
175175
_setRewardRate(rewardRate_);
176176
}
177177

@@ -181,7 +181,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
181181
* `ELIGIBLE_ACCOUNT_ROLE`. By default this is `MANAGER_ROLE`.
182182
* @param account The account to grant the `ELIGIBLE_ACCOUNT_ROLE` role to.
183183
*/
184-
function addEligibleAccount(address account) public {
184+
function addEligibleAccount(address account) public virtual {
185185
grantRole(ELIGIBLE_ACCOUNT_ROLE, account);
186186
}
187187

@@ -191,7 +191,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
191191
* `ELIGIBLE_ACCOUNT_ROLE`. By default this is `MANAGER_ROLE`.
192192
* @param account The account to revoke the `ELIGIBLE_ACCOUNT_ROLE` role from.
193193
*/
194-
function removeEligibleAccount(address account) public {
194+
function removeEligibleAccount(address account) public virtual {
195195
revokeRole(ELIGIBLE_ACCOUNT_ROLE, account);
196196
}
197197

@@ -200,7 +200,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
200200
* by `MANAGER_ROLE` role. See {unstake} for important notes regarding the cooldown period.
201201
* @param unstakeCooldownPeriod_ The new unstake cooldown period.
202202
*/
203-
function setUnstakeCooldownPeriod(uint48 unstakeCooldownPeriod_) public onlyRole(MANAGER_ROLE) {
203+
function setUnstakeCooldownPeriod(uint48 unstakeCooldownPeriod_) public virtual onlyRole(MANAGER_ROLE) {
204204
_setUnstakeCooldownPeriod(unstakeCooldownPeriod_);
205205
}
206206

@@ -210,7 +210,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
210210
* @param recipient The recipient that will receive rewards on behalf of `msg.sender` for all future {claimRewards} calls.
211211
* A value of `address(0)` indicates that rewards should be sent to `msg.sender`.
212212
*/
213-
function setRewardsRecipient(address recipient) public {
213+
function setRewardsRecipient(address recipient) public virtual {
214214
_getProtocolStakingStorage()._rewardsRecipient[msg.sender] = recipient;
215215

216216
emit RewardsRecipientSet(msg.sender, recipient);
@@ -221,7 +221,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
221221
* @param account The account to check rewards for.
222222
* @return The earned amount.
223223
*/
224-
function earned(address account) public view returns (uint256) {
224+
function earned(address account) public view virtual returns (uint256) {
225225
ProtocolStakingStorage storage $ = _getProtocolStakingStorage();
226226
uint256 stakedWeight = isEligibleAccount(account) ? weight(balanceOf(account)) : 0;
227227
// if stakedWeight == 0, there is a risk of totalStakedWeight == 0. To avoid div by 0 just return 0
@@ -231,7 +231,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
231231
}
232232

233233
/// @dev Returns the staking token which is used for staking and rewards.
234-
function stakingToken() public view returns (address) {
234+
function stakingToken() public view virtual returns (address) {
235235
return _getProtocolStakingStorage()._stakingToken;
236236
}
237237

@@ -240,17 +240,17 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
240240
* @param amount The amount being weighted.
241241
* @return The staking weight.
242242
*/
243-
function weight(uint256 amount) public pure returns (uint256) {
243+
function weight(uint256 amount) public pure virtual returns (uint256) {
244244
return Math.sqrt(amount);
245245
}
246246

247247
/// @dev Returns the current total staked weight.
248-
function totalStakedWeight() public view returns (uint256) {
248+
function totalStakedWeight() public view virtual returns (uint256) {
249249
return _getProtocolStakingStorage()._totalEligibleStakedWeight;
250250
}
251251

252252
/// @dev Returns the current unstake cooldown period in seconds.
253-
function unstakeCooldownPeriod() public view returns (uint256) {
253+
function unstakeCooldownPeriod() public view virtual returns (uint256) {
254254
return _getProtocolStakingStorage()._unstakeCooldownPeriod;
255255
}
256256

@@ -269,7 +269,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
269269
* @dev Gets the current protocol reward rate in tokens distributed per second.
270270
* @return The reward rate.
271271
*/
272-
function rewardRate() public view returns (uint256) {
272+
function rewardRate() public view virtual returns (uint256) {
273273
return _getProtocolStakingStorage()._rewardRate;
274274
}
275275

@@ -278,7 +278,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
278278
* @param account The account that earned rewards.
279279
* @return The rewards recipient.
280280
*/
281-
function rewardsRecipient(address account) public view returns (address) {
281+
function rewardsRecipient(address account) public view virtual returns (address) {
282282
address storedRewardsRecipient = _getProtocolStakingStorage()._rewardsRecipient[account];
283283
return storedRewardsRecipient == address(0) ? account : storedRewardsRecipient;
284284
}
@@ -288,11 +288,11 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
288288
* @param account The account being checked for eligibility.
289289
* @return True if eligible.
290290
*/
291-
function isEligibleAccount(address account) public view returns (bool) {
291+
function isEligibleAccount(address account) public view virtual returns (bool) {
292292
return hasRole(ELIGIBLE_ACCOUNT_ROLE, account);
293293
}
294294

295-
function _grantRole(bytes32 role, address account) internal override returns (bool) {
295+
function _grantRole(bytes32 role, address account) internal virtual override returns (bool) {
296296
bool success = super._grantRole(role, account);
297297
if (role == ELIGIBLE_ACCOUNT_ROLE && success) {
298298
require(account != address(0), InvalidEligibleAccount(account));
@@ -301,15 +301,15 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
301301
return success;
302302
}
303303

304-
function _revokeRole(bytes32 role, address account) internal override returns (bool) {
304+
function _revokeRole(bytes32 role, address account) internal virtual override returns (bool) {
305305
bool success = super._revokeRole(role, account);
306306
if (role == ELIGIBLE_ACCOUNT_ROLE && success) {
307307
_updateRewards(account, weight(balanceOf(account)), 0);
308308
}
309309
return success;
310310
}
311311

312-
function _setUnstakeCooldownPeriod(uint48 unstakeCooldownPeriod_) internal {
312+
function _setUnstakeCooldownPeriod(uint48 unstakeCooldownPeriod_) internal virtual {
313313
require(unstakeCooldownPeriod_ != 0 && unstakeCooldownPeriod_ <= 365 days, InvalidUnstakeCooldownPeriod());
314314
_getProtocolStakingStorage()._unstakeCooldownPeriod = unstakeCooldownPeriod_;
315315

@@ -320,7 +320,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
320320
* @dev Sets the reward rate in tokens per second.
321321
* @param rewardRate_ The new reward rate in tokens per second.
322322
*/
323-
function _setRewardRate(uint256 rewardRate_) internal {
323+
function _setRewardRate(uint256 rewardRate_) internal virtual {
324324
ProtocolStakingStorage storage $ = _getProtocolStakingStorage();
325325
$._lastUpdateReward = _historicalReward();
326326
$._lastUpdateTimestamp = Time.timestamp();
@@ -329,7 +329,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
329329
emit RewardRateSet(rewardRate_);
330330
}
331331

332-
function _updateRewards(address user, uint256 weightBefore, uint256 weightAfter) internal {
332+
function _updateRewards(address user, uint256 weightBefore, uint256 weightAfter) internal virtual {
333333
ProtocolStakingStorage storage $ = _getProtocolStakingStorage();
334334
uint256 oldTotalWeight = $._totalEligibleStakedWeight;
335335
$._totalEligibleStakedWeight = oldTotalWeight - weightBefore + weightAfter;
@@ -351,7 +351,7 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
351351
}
352352
}
353353

354-
function _update(address from, address to, uint256 value) internal override {
354+
function _update(address from, address to, uint256 value) internal virtual override {
355355
// Disable Transfers
356356
require(from == address(0) || to == address(0), TransferDisabled());
357357
if (isEligibleAccount(from)) {
@@ -367,21 +367,21 @@ contract ProtocolStaking is AccessControlDefaultAdminRulesUpgradeable, ERC20Vote
367367
super._update(from, to, value);
368368
}
369369

370-
function _authorizeUpgrade(address newImplementation) internal override onlyRole(DEFAULT_ADMIN_ROLE) {}
370+
function _authorizeUpgrade(address newImplementation) internal virtual override onlyRole(DEFAULT_ADMIN_ROLE) {}
371371

372-
function _historicalReward() internal view returns (uint256) {
372+
function _historicalReward() internal view virtual returns (uint256) {
373373
ProtocolStakingStorage storage $ = _getProtocolStakingStorage();
374374
return $._lastUpdateReward + (Time.timestamp() - $._lastUpdateTimestamp) * $._rewardRate;
375375
}
376376

377-
function _allocation(uint256 share, uint256 total) private view returns (uint256) {
377+
function _allocation(uint256 share, uint256 total) internal view virtual returns (uint256) {
378378
return
379379
SafeCast
380380
.toUint256(SafeCast.toInt256(_historicalReward()) + _getProtocolStakingStorage()._totalVirtualPaid)
381381
.mulDiv(share, total);
382382
}
383383

384-
function _getProtocolStakingStorage() private pure returns (ProtocolStakingStorage storage $) {
384+
function _getProtocolStakingStorage() internal pure returns (ProtocolStakingStorage storage $) {
385385
assembly {
386386
$.slot := PROTOCOL_STAKING_STORAGE_LOCATION
387387
}

protocol-contracts/staking/contracts/mocks/ProtocolStakingSlashingMock.sol

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ contract ProtocolStakingSlashingMock is ProtocolStaking {
1212
using Checkpoints for Checkpoints.Trace208;
1313
using SafeERC20 for IERC20;
1414

15-
bytes32 private constant PROTOCOL_STAKING_STORAGE_LOCATION =
16-
0xd955b2342c0487c5e5b5f50f5620ec67dcb16d94462ba5d080d7b7472b67b900;
17-
1815
mapping(address => uint256) private _slashedAmount;
1916

2017
function slash(address account, uint256 amount) public {
@@ -30,21 +27,15 @@ contract ProtocolStakingSlashingMock is ProtocolStaking {
3027
}
3128

3229
function tokensToReleaseAt(address account, uint48 timestamp) public view virtual returns (uint256) {
33-
ProtocolStakingStorage storage $ = __getProtocolStakingStorage();
30+
ProtocolStakingStorage storage $ = _getProtocolStakingStorage();
3431
return $._unstakeRequests[account].upperLookup(timestamp) - $._released[account] - _slashedAmount[account];
3532
}
3633

3734
function release(address account) public virtual override {
3835
uint256 amountToRelease = tokensToReleaseAt(account, Time.timestamp());
3936
if (amountToRelease > 0) {
40-
__getProtocolStakingStorage()._released[account] += amountToRelease;
37+
_getProtocolStakingStorage()._released[account] += amountToRelease;
4138
IERC20(stakingToken()).safeTransfer(account, amountToRelease);
4239
}
4340
}
44-
45-
function __getProtocolStakingStorage() private pure returns (ProtocolStaking.ProtocolStakingStorage storage $) {
46-
assembly {
47-
$.slot := PROTOCOL_STAKING_STORAGE_LOCATION
48-
}
49-
}
5041
}

0 commit comments

Comments
 (0)