Skip to content

Commit

Permalink
Implement method to submit POL
Browse files Browse the repository at this point in the history
  • Loading branch information
evercoinx committed Aug 30, 2024
1 parent bc762bc commit 0c27c8b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
38 changes: 32 additions & 6 deletions contracts/MaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ contract MaticX is

uint256 maticxMinted = delegateToMint(
address(this),
instantPoolMatic
instantPoolMatic,
false
);
instantPoolMaticX += maticxMinted;
instantPoolMatic = 0;
Expand Down Expand Up @@ -219,7 +220,7 @@ contract MaticX is
////////////////////////////////////////////////////////////

/**
* @dev Send funds to MaticX contract and mints MaticX to msg.sender
* @dev Send MATIC token to MaticX contract and mints MaticX to msg.sender
* @notice Requires that msg.sender has approved _amount of MATIC to this contract
* @param _amount - Amount of MATIC sent from msg.sender to this contract
* @return Amount of MaticX shares generated
Expand All @@ -238,7 +239,30 @@ contract MaticX is
_amount
);

return delegateToMint(msg.sender, _amount);
return delegateToMint(msg.sender, _amount, false);
}

/**
* @dev Send POL token to MaticX contract and mints MaticX to msg.sender
* @notice Requires that msg.sender has approved _amount of POL to this contract
* @param _amount - Amount of POL sent from msg.sender to this contract
* @return Amount of MaticX shares generated
*/
function submitPOL(uint256 _amount)
external
override
whenNotPaused
returns (uint256)
{
require(_amount > 0, "Invalid amount");

IERC20Upgradeable(polToken).safeTransferFrom(
msg.sender,
address(this),
_amount
);

return delegateToMint(msg.sender, _amount, true);
}

/**
Expand Down Expand Up @@ -483,9 +507,8 @@ contract MaticX is
* @param _amount - Amount of MATIC sent from msg.sender to this contract
* @return Amount of MaticX shares generated
*/
function delegateToMint(address depositSender, uint256 _amount)
function delegateToMint(address depositSender, uint256 _amount, bool pol)
internal
whenNotPaused
returns (uint256)
{
(
Expand All @@ -501,7 +524,10 @@ contract MaticX is
.preferredDepositValidatorId();
address validatorShare = IStakeManager(stakeManager)
.getValidatorContract(preferredValidatorId);
IValidatorShare(validatorShare).buyVoucher(_amount, 0);

pol
? IValidatorShare(validatorShare).buyVoucherPOL(_amount, 0)
: IValidatorShare(validatorShare).buyVoucher(_amount, 0);

IFxStateRootTunnel(fxStateRootTunnel).sendMessageToChild(
abi.encode(totalShares + amountToMint, totalPooledMatic + _amount)
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/IMaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ interface IMaticX is IERC20Upgradeable {

function submit(uint256 _amount) external returns (uint256);

function submitPOL(uint256 _amount) external returns (uint256);

function requestWithdraw(uint256 _amount) external;

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

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

// solhint-disable-next-line func-name-mixedcase
function sellVoucher_new(uint256 claimAmount, uint256 maximumSharesToBurn)
external;
Expand Down
36 changes: 24 additions & 12 deletions contracts/mocks/ValidatorShareMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,16 @@ contract ValidatorShareMock is IValidatorShare {
override
returns (uint256)
{
uint256 totalAmount = IERC20(token).balanceOf(address(this));

uint256 shares = totalAmount != 0
? (_amount * totalShares) / totalAmount
: _amount;

totalShares += shares;
totalStaked += _amount;
require(
stakeManager.delegationDeposit(validatorId, _amount, msg.sender),
"deposit failed"
);
_buyVoucher(_amount);
return 1;
}

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

Expand Down Expand Up @@ -139,4 +136,19 @@ contract ValidatorShareMock is IValidatorShare {
);
return unbond;
}

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

uint256 shares = totalAmount != 0
? (_amount * totalShares) / totalAmount
: _amount;

totalShares += shares;
totalStaked += _amount;
require(
stakeManager.delegationDeposit(validatorId, _amount, msg.sender),
"deposit failed"
);
}
}

0 comments on commit 0c27c8b

Please sign in to comment.