Skip to content

Commit

Permalink
Add support for POL token on MaticX contract
Browse files Browse the repository at this point in the history
  • Loading branch information
evercoinx committed Aug 30, 2024
1 parent 7715d72 commit 89b4343
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
60 changes: 35 additions & 25 deletions contracts/MaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,39 @@ contract MaticX is
{
using SafeERC20Upgradeable for IERC20Upgradeable;

bytes32 public constant INSTANT_POOL_OWNER = keccak256("IPO");
bytes32 public constant PREDICATE_ROLE = keccak256("PREDICATE_ROLE");
bytes32 public constant BOT = keccak256("BOT");

address private validatorRegistry;
address private stakeManager;
address private polygonERC20;
address private maticToken;

address public override treasury;
string public override version;
uint8 public override feePercent;

bytes32 public constant INSTANT_POOL_OWNER = keccak256("IPO");

address public override instantPoolOwner;
uint256 public override instantPoolMatic;
uint256 public override instantPoolMaticX;

/// @notice Mapping of all user ids with withdraw requests.
mapping(address => WithdrawalRequest[]) private userWithdrawalRequests;

bytes32 public constant PREDICATE_ROLE = keccak256("PREDICATE_ROLE");

address public override fxStateRootTunnel;

bytes32 public constant BOT = keccak256("BOT");
address private polToken;

/// @notice Initialize the MaticX contract.
/// @param _validatorRegistry - Address of the validator registry
/// @param _stakeManager - Address of the stake manager
/// @param _polygonERC20 - Address of matic token on Ethereum
/// @param _maticToken - Address of matic token on Ethereum
/// @param _manager - Address of the manager
/// @param _instantPoolOwner - Address of the instant pool owner
/// @param _treasury - Address of the treasury
function initialize(
address _validatorRegistry,
address _stakeManager,
address _polygonERC20,
address _maticToken,
address _manager,
address _instantPoolOwner,
address _treasury
Expand All @@ -72,11 +71,11 @@ contract MaticX is
validatorRegistry = _validatorRegistry;
stakeManager = _stakeManager;
treasury = _treasury;
polygonERC20 = _polygonERC20;
maticToken = _maticToken;

feePercent = 5;

IERC20Upgradeable(polygonERC20).safeApprove(
IERC20Upgradeable(maticToken).safeApprove(
stakeManager,
type(uint256).max
);
Expand All @@ -92,6 +91,14 @@ contract MaticX is
_setRoleAdmin(BOT, INSTANT_POOL_OWNER);
}

/// @dev Set the address of the POL token
/// @param _polToken - Address of the POL token
function setPOLToken(
address _polToken
) external onlyRole(DEFAULT_ADMIN_ROLE) {
polToken = _polToken;
}

////////////////////////////////////////////////////////////
///// ///
///// ***Instant Pool Interactions*** ///
Expand All @@ -107,7 +114,7 @@ contract MaticX is
onlyRole(INSTANT_POOL_OWNER)
{
require(_amount > 0, "Invalid amount");
IERC20Upgradeable(polygonERC20).safeTransferFrom(
IERC20Upgradeable(maticToken).safeTransferFrom(
msg.sender,
address(this),
_amount
Expand Down Expand Up @@ -168,7 +175,7 @@ contract MaticX is
);

instantPoolMatic -= _amount;
IERC20Upgradeable(polygonERC20).safeTransfer(instantPoolOwner, _amount);
IERC20Upgradeable(maticToken).safeTransfer(instantPoolOwner, _amount);
}

/// @dev mints MaticX to instantPoolMatic. It uses instantPoolMatic funds
Expand Down Expand Up @@ -196,7 +203,7 @@ contract MaticX is
whenNotPaused
{
require(_amount > 0, "Invalid amount");
IERC20Upgradeable(polygonERC20).safeTransferFrom(
IERC20Upgradeable(maticToken).safeTransferFrom(
msg.sender,
address(this),
_amount
Expand Down Expand Up @@ -232,7 +239,8 @@ contract MaticX is
returns (uint256)
{
require(_amount > 0, "Invalid amount");
IERC20Upgradeable(polygonERC20).safeTransferFrom(

IERC20Upgradeable(maticToken).safeTransferFrom(
msg.sender,
address(this),
_amount
Expand Down Expand Up @@ -334,10 +342,10 @@ contract MaticX is
address validatorShare = IStakeManager(stakeManager)
.getValidatorContract(_validatorId);

uint256 balanceBeforeRewards = IERC20Upgradeable(polygonERC20)
uint256 balanceBeforeRewards = IERC20Upgradeable(maticToken)
.balanceOf(address(this));
IValidatorShare(validatorShare).withdrawRewards();
uint256 rewards = IERC20Upgradeable(polygonERC20).balanceOf(
uint256 rewards = IERC20Upgradeable(maticToken).balanceOf(
address(this)
) - balanceBeforeRewards;

Expand Down Expand Up @@ -391,7 +399,7 @@ contract MaticX is
address validatorShare = IStakeManager(stakeManager)
.getValidatorContract(_validatorId);

uint256 rewards = IERC20Upgradeable(polygonERC20).balanceOf(
uint256 rewards = IERC20Upgradeable(maticToken).balanceOf(
address(this)
) - instantPoolMatic;

Expand All @@ -400,7 +408,7 @@ contract MaticX is
uint256 treasuryFees = (rewards * feePercent) / 100;

if (treasuryFees > 0) {
IERC20Upgradeable(polygonERC20).safeTransfer(
IERC20Upgradeable(maticToken).safeTransfer(
treasury,
treasuryFees
);
Expand Down Expand Up @@ -522,7 +530,7 @@ contract MaticX is
returns (uint256)
{
uint256 amountToClaim = 0;
uint256 balanceBeforeClaim = IERC20Upgradeable(polygonERC20).balanceOf(
uint256 balanceBeforeClaim = IERC20Upgradeable(maticToken).balanceOf(
address(this)
);
WithdrawalRequest[] storage userRequests = userWithdrawalRequests[_to];
Expand All @@ -541,10 +549,10 @@ contract MaticX is
userRequests.pop();

amountToClaim =
IERC20Upgradeable(polygonERC20).balanceOf(address(this)) -
IERC20Upgradeable(maticToken).balanceOf(address(this)) -
balanceBeforeClaim;

IERC20Upgradeable(polygonERC20).safeTransfer(_to, amountToClaim);
IERC20Upgradeable(maticToken).safeTransfer(_to, amountToClaim);

emit ClaimWithdrawal(_to, _idx, amountToClaim);
return amountToClaim;
Expand Down Expand Up @@ -784,12 +792,14 @@ contract MaticX is
override
returns (
address _stakeManager,
address _polygonERC20,
address _validatorRegistry
address _maticToken,
address _validatorRegistry,
address _polToken
)
{
_stakeManager = stakeManager;
_polygonERC20 = polygonERC20;
_maticToken = maticToken;
_validatorRegistry = validatorRegistry;
_polToken = polToken;
}
}
5 changes: 3 additions & 2 deletions contracts/interfaces/IMaticX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ interface IMaticX is IERC20Upgradeable {
view
returns (
address _stakeManager,
address _polygonERC20,
address _validatorRegistry
address _maticToken,
address _validatorRegistry,
address _polToken
);

event Submit(address indexed _from, uint256 _amount);
Expand Down

0 comments on commit 89b4343

Please sign in to comment.