Skip to content

Commit

Permalink
Implement method to request withdraw POL
Browse files Browse the repository at this point in the history
  • Loading branch information
evercoinx committed Sep 2, 2024
1 parent 026e74c commit 9cbfd56
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"func-named-parameters": ["error", 11],
"func-param-name-mixedcase": "error",
"func-visibility": ["error", { "ignoreConstructors": true }],
"function-max-lines": ["error", 70],
"function-max-lines": ["error", 75],
"gas-custom-errors": "off",
"immutable-vars-naming": ["error", { "immutablesAsConstants": false }],
"imports-on-top": "error",
Expand Down
29 changes: 23 additions & 6 deletions contracts/MaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,22 @@ contract MaticX is
}

/**
* @dev Stores user's request to withdraw into WithdrawalRequest struct
* @param _amount - Amount of maticX that is requested to withdraw
* @dev Stores user's request to withdraw MATIC into WithdrawalRequest struct
* @param _amount - Amount of Matic that is requested to withdraw
*/
function requestWithdraw(uint256 _amount) external override whenNotPaused {
_requestWithdraw(_amount, false);
}

/**
* @dev Stores user's request to withdraw POL into WithdrawalRequest struct
* @param _amount - Amount of POL that is requested to withdraw
*/
function requestWithdrawPOL(uint256 _amount) external override whenNotPaused {
_requestWithdraw(_amount, true);
}

function _requestWithdraw(uint256 _amount, bool pol) private {
require(_amount > 0, "Invalid amount");

(
Expand Down Expand Up @@ -311,10 +323,15 @@ contract MaticX is
? validatorBalance
: leftAmount2WithdrawInMatic;

IValidatorShare(validatorShare).sellVoucher_new(
amount2WithdrawFromValidator,
type(uint256).max
);
pol
? IValidatorShare(validatorShare).sellVoucher_newPOL(
amount2WithdrawFromValidator,
type(uint256).max
)
: IValidatorShare(validatorShare).sellVoucher_new(
amount2WithdrawFromValidator,
type(uint256).max
);

userWithdrawalRequests[msg.sender].push(
WithdrawalRequest(
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/IMaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ interface IMaticX is IERC20Upgradeable {

function requestWithdraw(uint256 _amount) external;

function requestWithdrawPOL(uint256 _amount) external;

function claimWithdrawal(uint256 _idx) external;

function withdrawRewards(uint256 _validatorId) external returns (uint256);
Expand Down
4 changes: 4 additions & 0 deletions contracts/interfaces/IValidatorShare.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ interface IValidatorShare {
function sellVoucher_new(uint256 claimAmount, uint256 maximumSharesToBurn)
external;

// solhint-disable-next-line func-name-mixedcase
function sellVoucher_newPOL(uint256 claimAmount, uint256 maximumSharesToBurn)
external;

// solhint-disable-next-line func-name-mixedcase
function unstakeClaimTokens_new(uint256 unbondNonce) external;

Expand Down
32 changes: 20 additions & 12 deletions contracts/mocks/ValidatorShareMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,23 @@ contract ValidatorShareMock is IValidatorShare {
override
returns (uint256)
{
_buyVoucher(_amount);
return 1;
return _buyVoucher(_amount);
}

function buyVoucherPOL(uint256 _amount, uint256)
external
override
returns (uint256)
{
_buyVoucher(_amount);
return 1;
return _buyVoucher(_amount);
}

function sellVoucher_new(uint256 _claimAmount, uint256) external override {
uint256 unbondNonce = unbondNonces[msg.sender] + 1;

withdrawPool += _claimAmount;
totalWithdrawPoolShares += _claimAmount;
totalStaked -= _claimAmount;
_sellVoucher_new(_claimAmount);
}

unbondNonces[msg.sender] = unbondNonce;
user2WithdrawPoolShare[msg.sender][unbondNonce] = _claimAmount;
function sellVoucher_newPOL(uint256 _claimAmount, uint256) external override {
_sellVoucher_new(_claimAmount);
}

function unstakeClaimTokens_new(uint256 _unbondNonce) external override {
Expand Down Expand Up @@ -137,7 +132,7 @@ contract ValidatorShareMock is IValidatorShare {
return unbond;
}

function _buyVoucher(uint256 _amount) private {
function _buyVoucher(uint256 _amount) private returns (uint256) {
uint256 totalAmount = IERC20(token).balanceOf(address(this));

uint256 shares = totalAmount != 0
Expand All @@ -150,5 +145,18 @@ contract ValidatorShareMock is IValidatorShare {
stakeManager.delegationDeposit(validatorId, _amount, msg.sender),
"deposit failed"
);

return 1;
}

function _sellVoucher_new(uint256 _claimAmount) private {
uint256 unbondNonce = unbondNonces[msg.sender] + 1;

withdrawPool += _claimAmount;
totalWithdrawPoolShares += _claimAmount;
totalStaked -= _claimAmount;

unbondNonces[msg.sender] = unbondNonce;
user2WithdrawPoolShare[msg.sender][unbondNonce] = _claimAmount;
}
}

0 comments on commit 9cbfd56

Please sign in to comment.