Skip to content

Commit

Permalink
Update public interfaces of external contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
evercoinx committed Sep 29, 2024
1 parent a4ee1c4 commit 2b11895
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 882 deletions.
4 changes: 2 additions & 2 deletions contracts/ValidatorRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ contract ValidatorRegistry is
return validators[_idx];
}

/// @notice Returns a list of registered validators.
/// @return List of registered validators
/// @notice Returns an array of registered validator ids.
/// @return Array of registered validator ids
function getValidators() external view override returns (uint256[] memory) {
return validators;
}
Expand Down
73 changes: 15 additions & 58 deletions contracts/interfaces/IStakeManager.sol
Original file line number Diff line number Diff line change
@@ -1,63 +1,7 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;

/// @title polygon stake manager interface.
/// @notice User to interact with the polygon stake manager.
interface IStakeManager {
/// @notice Request unstake a validator.
/// @param validatorId validator id.
function unstake(uint256 validatorId) external;

/// @notice Get the validator id using the user address.
/// @param user user that own the validator in our case the validator contract.
/// @return return the validator id
function getValidatorId(address user) external view returns (uint256);

/// @notice get the validator contract used for delegation.
/// @param validatorId validator id.
/// @return return the address of the validator contract.
function getValidatorContract(
uint256 validatorId
) external view returns (address);

/// @notice Withdraw accumulated rewards
/// @param validatorId validator id.
function withdrawRewards(uint256 validatorId) external;

/// @notice Get validator total staked.
/// @param validatorId validator id.
function validatorStake(
uint256 validatorId
) external view returns (uint256);

/// @notice Allows to unstake the staked tokens on the stakeManager.
/// @param validatorId validator id.
function unstakeClaim(uint256 validatorId) external;

/// @notice Allows to migrate the staked tokens to another validator.
/// @param fromValidatorId From validator id.
/// @param toValidatorId To validator id.
/// @param amount amount in Matic.
function migrateDelegation(
uint256 fromValidatorId,
uint256 toValidatorId,
uint256 amount
) external;

/// @notice Returns a withdrawal delay.
function withdrawalDelay() external view returns (uint256);

/// @notice Transfers amount from delegator
function delegationDeposit(
uint256 validatorId,
uint256 amount,
address delegator
) external returns (bool);

function setCurrentEpoch(uint256 _currentEpoch) external;

function epoch() external view returns (uint256);

enum Status {
Inactive,
Active,
Expand All @@ -81,10 +25,23 @@ interface IStakeManager {
uint256 initialRewardPerStake;
}

function migrateDelegation(
uint256 _fromValidatorId,
uint256 _toValidatorId,
uint256 _amount
) external;

function setCurrentEpoch(uint256 _currentEpoch) external;

function getValidatorContract(
uint256 _validatorId
) external view returns (address);

function validators(
uint256 _index
) external view returns (Validator memory);

// TODO: Remove it and use stakeFor instead
function createValidator(uint256 _validatorId) external;
function epoch() external view returns (uint256);

function withdrawalDelay() external view returns (uint256);
}
4 changes: 2 additions & 2 deletions contracts/interfaces/IValidatorRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ interface IValidatorRegistry {
/// @return Validator id
function getValidatorId(uint256 _idx) external view returns (uint256);

/// @notice Returns all the validator addresses joined the MaticX protocol.
/// @return List of validator addresses
/// @notice Returns an array of registered validator ids.
/// @return Array of registered validator ids
function getValidators() external view returns (uint256[] memory);
}
35 changes: 10 additions & 25 deletions contracts/interfaces/IValidatorShare.sol
Original file line number Diff line number Diff line change
@@ -1,58 +1,43 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;

interface IValidatorShare {
import { IERC20Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";

interface IValidatorShare is IERC20Upgradeable {
struct DelegatorUnbond {
uint256 shares;
uint256 withdrawEpoch;
}

function stakingLogger() external view returns (address);

function minAmount() external view returns (uint256);

function unbondNonces(address _address) external view returns (uint256);

function validatorId() external view returns (uint256);

function delegation() external view returns (bool);

function buyVoucher(
uint256 _amount,
uint256 _minSharesToMint
) external returns (uint256);
) external returns (uint256 amountToDeposit);

function buyVoucherPOL(
uint256 _amount,
uint256 _minSharesToMint
) external returns (uint256);

function sellVoucher_new(
uint256 _claimAmount,
uint256 _maximumSharesToBurn
) external;
) external returns (uint256 amountToDeposit);

function sellVoucher_newPOL(
uint256 _claimAmount,
uint256 _maximumSharesToBurn
) external;

function unstakeClaimTokens_new(uint256 _unbondNonce) external;

function unstakeClaimTokens_newPOL(uint256 _unbondNonce) external;

function restake() external returns (uint256, uint256);

function withdrawRewards() external;

function withdrawRewardsPOL() external;

function getTotalStake(
address _user
) external view returns (uint256, uint256);

function unbondNonces(address _user) external view returns (uint256);

function unbonds_new(
address _address,
address _user,
uint256 _unbondNonce
) external view returns (DelegatorUnbond memory);

function stakingLogger() external view returns (address);
}
68 changes: 44 additions & 24 deletions contracts/mocks/StakeManagerMock.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.7;

import "../interfaces/IStakeManager.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../mocks/ValidatorShareMock.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { ValidatorShareMock } from "./ValidatorShareMock.sol";

contract StakeManagerMock {
enum Status {
Inactive,
Active,
Locked,
Unstaked
}

struct Validator {
uint256 amount;
uint256 reward;
uint256 activationEpoch;
uint256 deactivationEpoch;
uint256 jailTime;
address signer;
address contractAddress;
Status status;
uint256 commissionRate;
uint256 lastCommissionUpdate;
uint256 delegatorsReward;
uint256 delegatedAmount;
uint256 initialRewardPerStake;
}

contract StakeManagerMock is IStakeManager {
event UpdateSigner(uint256 validatorId, bytes signerPubkey);
event UpdateCommissionRate(uint256 validatorId, uint256 newCommissionRate);

mapping(uint256 => IStakeManager.Validator) smValidators;
mapping(uint256 => Validator) smValidators;
struct State {
address token;
address stakeNFT;
Expand All @@ -30,12 +52,12 @@ contract StakeManagerMock is IStakeManager {
state.stakeNFT = _stakeNFT;
}

function unstake(uint256 _validatorId) external override {
function unstake(uint256 _validatorId) external {
smValidators[_validatorId].deactivationEpoch = block.timestamp;
}

function createValidator(uint256 _validatorId) external override {
smValidators[_validatorId] = IStakeManager.Validator({
function createValidator(uint256 _validatorId) external {
smValidators[_validatorId] = Validator({
amount: 0,
reward: 0,
activationEpoch: block.timestamp,
Expand All @@ -45,7 +67,7 @@ contract StakeManagerMock is IStakeManager {
contractAddress: address(
new ValidatorShareMock(state.token, address(this), _validatorId)
),
status: IStakeManager.Status.Active,
status: Status.Active,
commissionRate: 0,
lastCommissionUpdate: 0,
delegatorsReward: 0,
Expand All @@ -57,68 +79,66 @@ contract StakeManagerMock is IStakeManager {
);
}

function getValidatorId(
address _user
) external view override returns (uint256) {
function getValidatorId(address _user) external view returns (uint256) {
return state.validators[_user];
}

function getValidatorContract(
uint256 _validatorId
) external view override returns (address) {
) external view returns (address) {
return state.validatorShares[_validatorId];
}

function withdrawRewards(uint256) external override {
function withdrawRewards(uint256) external {
IERC20(state.token).transfer(msg.sender, 1000);
}

function unstakeClaim(uint256 _validatorId) external override {
function unstakeClaim(uint256 _validatorId) external {
IERC20(state.token).transfer(
msg.sender,
IERC20(state.token).balanceOf(address(this))
);
state.delegator2Amount[msg.sender] = 0;
smValidators[_validatorId].status = IStakeManager.Status.Unstaked;
smValidators[_validatorId].status = Status.Unstaked;
}

function validatorStake(
uint256 _validatorId
) external view override returns (uint256) {
) external view returns (uint256) {
return state.stakedAmount[_validatorId];
}

function withdrawalDelay() external pure override returns (uint256) {
function withdrawalDelay() external pure returns (uint256) {
return (2 ** 13);
}

function delegationDeposit(
uint256 _validatorId,
uint256 _amount,
address _delegator
) external override returns (bool) {
) external returns (bool) {
state.delegator2Amount[msg.sender] += _amount;
state.stakedAmount[_validatorId] += _amount;
IERC20(state.token).transferFrom(_delegator, address(this), _amount);
return IERC20(state.token).transfer(msg.sender, _amount);
}

function setCurrentEpoch(uint256 _currentEpoch) external override {
function setCurrentEpoch(uint256 _currentEpoch) external {
state.epoch = _currentEpoch;
}

function epoch() external view override returns (uint256) {
function epoch() external view returns (uint256) {
return state.epoch;
}

function slash(uint256 _validatorId) external {
smValidators[_validatorId].status = IStakeManager.Status.Locked;
smValidators[_validatorId].status = Status.Locked;
state.stakedAmount[_validatorId] -= 100;
}

function validators(
uint256 _validatorId
) external view override returns (Validator memory) {
) external view returns (Validator memory) {
return smValidators[_validatorId];
}

Expand All @@ -130,7 +150,7 @@ contract StakeManagerMock is IStakeManager {
uint256 _fromValidatorId,
uint256 _toValidatorId,
uint256 _amount
) public override {
) public {
state.stakedAmount[_fromValidatorId] -= _amount;
state.stakedAmount[_toValidatorId] += _amount;
}
Expand Down
Loading

0 comments on commit 2b11895

Please sign in to comment.