Skip to content

Commit

Permalink
Refactor MaticX contract
Browse files Browse the repository at this point in the history
  • Loading branch information
evercoinx committed Sep 17, 2024
1 parent cd7cd70 commit 5ab605d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
26 changes: 16 additions & 10 deletions contracts/MaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract MaticX is
address private polToken;
uint256 private reentrancyGuardStatus;

/// -------------------------------modifiers-----------------------------------
/// ------------------------------ Modifiers -------------------------------

/// @notice Enables guard from reentrant calls.
modifier nonReentrant() {
Expand All @@ -60,7 +60,13 @@ contract MaticX is
reentrancyGuardStatus = NOT_ENTERED;
}

/// -------------------------- Initialize ----------------------------------
/// -------------------------- Initializers --------------------------------

/// @dev The constructor is disabled for a proxy upgrade.
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

/// @notice Initializes the current contract.
/// @param _validatorRegistry - Address of the validator registry
Expand Down Expand Up @@ -263,19 +269,19 @@ contract MaticX is
IValidatorShare(validatorShare)
);

uint256 amount2WithdrawFromValidator = (validatorBalance <=
uint256 amountToWithdrawFromValidator = (validatorBalance <=
leftAmountToWithdraw)
? validatorBalance
: leftAmountToWithdraw;

if (amount2WithdrawFromValidator > 0) {
if (amountToWithdrawFromValidator > 0) {
_pol
? IValidatorShare(validatorShare).sellVoucher_newPOL(
amount2WithdrawFromValidator,
amountToWithdrawFromValidator,
type(uint256).max
)
: IValidatorShare(validatorShare).sellVoucher_new(
amount2WithdrawFromValidator,
amountToWithdrawFromValidator,
type(uint256).max
);

Expand All @@ -290,7 +296,7 @@ contract MaticX is
)
);

leftAmountToWithdraw -= amount2WithdrawFromValidator;
leftAmountToWithdraw -= amountToWithdrawFromValidator;
}

--totalValidatorRequests;
Expand Down Expand Up @@ -534,7 +540,7 @@ contract MaticX is
paused() ? _unpause() : _pause();
}

/// -------------------------------Helpers----------------------------------
/// ------------------------------ Helpers ---------------------------------

/// @notice Converts an arbitrary amount of MaticX shares to stake tokens.
/// @param _balance - Balance in MaticX
Expand Down Expand Up @@ -634,7 +640,7 @@ contract MaticX is
emit MintFromPolygon(_user, _amount);
}

/// -------------------------------Setters-----------------------------------
/// ------------------------------ Setters ---------------------------------

/// @notice Sets a fee percent.
/// @param _feePercent - Fee percent (10 = 10%)
Expand Down Expand Up @@ -702,7 +708,7 @@ contract MaticX is
emit SetPOLToken(_address);
}

/// -------------------------------Getters-----------------------------------
/// ------------------------------ Getters ---------------------------------

/// @notice Returns total pooled stake tokens from all registered validators.
/// @return Total pooled stake tokens
Expand Down
44 changes: 19 additions & 25 deletions contracts/ValidatorRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { IValidatorShare } from "./interfaces/IValidatorShare.sol";
import { IValidatorRegistry } from "./interfaces/IValidatorRegistry.sol";

/// @title ValidatorRegistry contract
/// @notice ValidatorRegistry is the main contract that manages validators
/// @notice ValidatorRegistry is the main contract that manages validators.
contract ValidatorRegistry is
IValidatorRegistry,
PausableUpgradeable,
Expand All @@ -30,15 +30,10 @@ contract ValidatorRegistry is
uint256[] private validators;
address private polToken;

/// -------------------------------modifiers-----------------------------------
/// ------------------------------ Modifiers -------------------------------

/**
* @dev Modifier to make a function callable only when the validator id exists in our registry.
* @param _validatorId the validator id.
* Requirements:
*
* - The validator id must exist in our registry.
*/
/// @notice Checks if the given validator id exists in our registry.
/// @param _validatorId - Validator id
modifier whenValidatorIdExists(uint256 _validatorId) {
require(
validatorIdExists[_validatorId],
Expand All @@ -47,14 +42,8 @@ contract ValidatorRegistry is
_;
}

/**
* @dev Modifier to make a function callable only when the validator id doesn't exist in our registry.
* @param _validatorId the validator id.
*
* Requirements:
*
* - The validator id must not exist in our registry.
*/
/// @notice Checks if the given validator id doesn't exist in our registry.
/// @param _validatorId - Validator id
modifier whenValidatorIdDoesNotExist(uint256 _validatorId) {
require(
!validatorIdExists[_validatorId],
Expand All @@ -63,13 +52,18 @@ contract ValidatorRegistry is
_;
}

/// -------------------------- Initialize ----------------------------------
/// -------------------------- Initializers --------------------------------

/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}

/// @notice Initialize the ValidatorRegistry contract.
/// @param _stakeManager address of the polygon stake manager.
/// @param _maticToken address of the polygon ERC20 contract.
/// @param _maticX address of the MaticX contract.
/// @param _manager address of the manager.
/// @param _stakeManager address of the polygon stake manager
/// @param _maticToken address of the polygon ERC20 contract
/// @param _maticX address of the MaticX contract
/// @param _manager address of the manager
function initialize(
address _stakeManager,
address _maticToken,
Expand Down Expand Up @@ -139,7 +133,7 @@ contract ValidatorRegistry is
}

/// @notice Removes a validator from the registry.
/// @param _validatorId - Validator id.
/// @param _validatorId - Validator id
// slither-disable-next-line pess-multiple-storage-read
function removeValidator(
uint256 _validatorId
Expand Down Expand Up @@ -182,7 +176,7 @@ contract ValidatorRegistry is
emit RemoveValidator(_validatorId);
}

/// -------------------------------Setters-----------------------------------
/// ------------------------------ Setters ---------------------------------

/// @notice Sets the prefered validator id for deposits.
/// @param _validatorId - Validator id for deposits
Expand Down Expand Up @@ -242,7 +236,7 @@ contract ValidatorRegistry is
paused() ? _unpause() : _pause();
}

/// -------------------------------Getters-----------------------------------
/// ------------------------------ Getters ---------------------------------

/// @notice Returns the contract addresses used on the current contract.
/// @return _stakeManager - Address of the stake manager
Expand Down
15 changes: 15 additions & 0 deletions contracts/interfaces/IValidatorRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,35 @@ interface IValidatorRegistry {

event SetVersion(string _version);

/// @notice Allows a validator that was already staked on the stake manager
/// to join the MaticX protocol.
/// @param _validatorId - Validator id
function addValidator(uint256 _validatorId) external;

/// @notice Removes a validator from the registry.
/// @param _validatorId - Validator id
function removeValidator(uint256 _validatorId) external;

/// @notice Sets the prefered validator id for deposits.
/// @param _validatorId - Validator id for deposits
function setPreferredDepositValidatorId(uint256 _validatorId) external;

/// @notice Set the prefered validator id for withdrawals.
/// @param _validatorId - Validator id for withdrawals
function setPreferredWithdrawalValidatorId(uint256 _validatorId) external;

/// @notice Sets the address of MaticX.
/// @param _address - Address of MaticX
function setMaticX(address _address) external;

/// @notice Sets a new version of this contract
/// @param _version - New version of this contract
function setVersion(string memory _version) external;

/// @notice Toggles the paused status of this contract.
function togglePause() external;

/// @notice Returns the v
function version() external view returns (string memory);

function preferredDepositValidatorId() external view returns (uint256);
Expand Down

0 comments on commit 5ab605d

Please sign in to comment.