From 77544d73e82512f6effa75e7cc8d741d00564828 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Thu, 19 Sep 2024 12:16:55 +0200 Subject: [PATCH] Reorder functions to appease the linter --- .../contracts-0.8/common/EpochManager.sol | 108 +++++++++--------- .../common/mocks/MockAccounts.sol | 24 ++-- .../contracts-0.8/governance/Validators.sol | 22 ++-- .../common/interfaces/IEpochManager.sol | 3 +- 4 files changed, 78 insertions(+), 79 deletions(-) diff --git a/packages/protocol/contracts-0.8/common/EpochManager.sol b/packages/protocol/contracts-0.8/common/EpochManager.sol index f5a0f906179..1a10c73d4d2 100644 --- a/packages/protocol/contracts-0.8/common/EpochManager.sol +++ b/packages/protocol/contracts-0.8/common/EpochManager.sol @@ -245,6 +245,60 @@ contract EpochManager is epochProcessing.status = EpochProcessStatus.NotStarted; } + /** + * @notice Sends the allocated epoch payment to a validator, their group, and + * delegation beneficiary. + * @param validator Account of the validator. + */ + function sendValidatorPayment(address validator) external { + IAccounts accounts = IAccounts(getAccounts()); + address signer = accounts.getValidatorSigner(validator); + + FixidityLib.Fraction memory totalPayment = FixidityLib.newFixed( + validatorPendingPayments[signer] + ); + validatorPendingPayments[signer] = 0; + + IValidators validators = getValidators(); + address group = validators.getValidatorsGroup(validator); + (, uint256 commissionUnwrapped, , , , , ) = validators.getValidatorGroup(group); + + uint256 groupPayment = totalPayment.multiply(FixidityLib.wrap(commissionUnwrapped)).fromFixed(); + FixidityLib.Fraction memory remainingPayment = FixidityLib.newFixed( + totalPayment.fromFixed() - groupPayment + ); + (address beneficiary, uint256 delegatedFraction) = getAccounts().getPaymentDelegation( + validator + ); + uint256 delegatedPayment = remainingPayment + .multiply(FixidityLib.wrap(delegatedFraction)) + .fromFixed(); + uint256 validatorPayment = remainingPayment.fromFixed() - delegatedPayment; + + IStableToken stableToken = IStableToken(getStableToken()); + + if (validatorPayment > 0) { + require(stableToken.transfer(validator, validatorPayment), "transfer failed to validator"); + } + + if (groupPayment > 0) { + require(stableToken.transfer(group, groupPayment), "transfer failed to validator group"); + } + + if (delegatedPayment > 0) { + require(stableToken.transfer(beneficiary, delegatedPayment), "transfer failed to delegatee"); + } + + emit ValidatorEpochPaymentDistributed( + validator, + validatorPayment, + group, + groupPayment, + beneficiary, + delegatedPayment + ); + } + /// returns the current epoch Info function getCurrentEpoch() external view returns (uint256, uint256, uint256, uint256) { Epoch storage _epoch = epochs[currentEpochNumber]; @@ -357,58 +411,4 @@ contract EpochManager is CELOequivalent ); } - - /** - * @notice Sends the allocated epoch payment to a validator, their group, and - * delegation beneficiary. - * @param validator Account of the validator. - */ - function sendValidatorPayment(address validator) external { - IAccounts accounts = IAccounts(getAccounts()); - address signer = accounts.getValidatorSigner(validator); - - FixidityLib.Fraction memory totalPayment = FixidityLib.newFixed( - validatorPendingPayments[signer] - ); - validatorPendingPayments[signer] = 0; - - IValidators validators = getValidators(); - address group = validators.getValidatorsGroup(validator); - (, uint256 commissionUnwrapped, , , , , ) = validators.getValidatorGroup(group); - - uint256 groupPayment = totalPayment.multiply(FixidityLib.wrap(commissionUnwrapped)).fromFixed(); - FixidityLib.Fraction memory remainingPayment = FixidityLib.newFixed( - totalPayment.fromFixed() - groupPayment - ); - (address beneficiary, uint256 delegatedFraction) = getAccounts().getPaymentDelegation( - validator - ); - uint256 delegatedPayment = remainingPayment - .multiply(FixidityLib.wrap(delegatedFraction)) - .fromFixed(); - uint256 validatorPayment = remainingPayment.fromFixed() - delegatedPayment; - - IStableToken stableToken = IStableToken(getStableToken()); - - if (validatorPayment > 0) { - require(stableToken.transfer(validator, validatorPayment), "transfer failed to validator"); - } - - if (groupPayment > 0) { - require(stableToken.transfer(group, groupPayment), "transfer failed to validator group"); - } - - if (delegatedPayment > 0) { - require(stableToken.transfer(beneficiary, delegatedPayment), "transfer failed to delegatee"); - } - - emit ValidatorEpochPaymentDistributed( - validator, - validatorPayment, - group, - groupPayment, - beneficiary, - delegatedPayment - ); - } } diff --git a/packages/protocol/contracts-0.8/common/mocks/MockAccounts.sol b/packages/protocol/contracts-0.8/common/mocks/MockAccounts.sol index e7054bf39e8..51ead07c279 100644 --- a/packages/protocol/contracts-0.8/common/mocks/MockAccounts.sol +++ b/packages/protocol/contracts-0.8/common/mocks/MockAccounts.sol @@ -16,16 +16,12 @@ contract MockAccounts { mapping(address => PaymentDelegation) delegations; mapping(address => address) accountToSigner; - function setPaymentDelegationFor( - address validator, - address beneficiary, - uint256 fraction - ) public { - delegations[validator] = PaymentDelegation(beneficiary, FixidityLib.wrap(fraction)); + function setValidatorSigner(address account, address signer) external { + accountToSigner[account] = signer; } - function deletePaymentDelegationFor(address validator) public { - delete delegations[validator]; + function getValidatorSigner(address account) external returns (address) { + return accountToSigner[account]; } function getPaymentDelegation(address account) external view returns (address, uint256) { @@ -33,11 +29,15 @@ contract MockAccounts { return (delegation.beneficiary, delegation.fraction.unwrap()); } - function setValidatorSigner(address account, address signer) external { - accountToSigner[account] = signer; + function setPaymentDelegationFor( + address validator, + address beneficiary, + uint256 fraction + ) public { + delegations[validator] = PaymentDelegation(beneficiary, FixidityLib.wrap(fraction)); } - function getValidatorSigner(address account) external returns (address) { - return accountToSigner[account]; + function deletePaymentDelegationFor(address validator) public { + delete delegations[validator]; } } diff --git a/packages/protocol/contracts-0.8/governance/Validators.sol b/packages/protocol/contracts-0.8/governance/Validators.sol index f17d103339b..ebf11dc36e6 100644 --- a/packages/protocol/contracts-0.8/governance/Validators.sol +++ b/packages/protocol/contracts-0.8/governance/Validators.sol @@ -1498,6 +1498,17 @@ contract Validators is return true; } + function _sendValidatorPaymentIfNecessary(address validator) private { + getEpochManager().sendValidatorPayment(validator); + } + + function _sendValidatorPaymentsIfNecessary(ValidatorGroup storage group) private { + address[] memory members = group.members.getKeys(); + for (uint256 i = 0; i < members.length; i++) { + _sendValidatorPaymentIfNecessary(members[i]); + } + } + /** * @notice Returns the epoch number. * @return Current epoch number. @@ -1509,15 +1520,4 @@ contract Validators is return getEpochNumber(); } } - - function _sendValidatorPaymentIfNecessary(address validator) private { - getEpochManager().sendValidatorPayment(validator); - } - - function _sendValidatorPaymentsIfNecessary(ValidatorGroup storage group) private { - address[] memory members = group.members.getKeys(); - for (uint256 i = 0; i < members.length; i++) { - _sendValidatorPaymentIfNecessary(members[i]); - } - } } diff --git a/packages/protocol/contracts/common/interfaces/IEpochManager.sol b/packages/protocol/contracts/common/interfaces/IEpochManager.sol index 3d4e5ab00d4..f1eadf15b86 100644 --- a/packages/protocol/contracts/common/interfaces/IEpochManager.sol +++ b/packages/protocol/contracts/common/interfaces/IEpochManager.sol @@ -13,6 +13,7 @@ interface IEpochManager { address[] calldata lessers, address[] calldata greaters ) external; + function sendValidatorPayment(address) external; function getCurrentEpoch() external view returns (uint256, uint256, uint256, uint256); function getCurrentEpochNumber() external view returns (uint256); function getElected() external view returns (address[] memory); @@ -22,6 +23,4 @@ interface IEpochManager { external view returns (uint256, uint256, uint256, uint256, uint256); - - function sendValidatorPayment(address) external; }