diff --git a/contracts/stake/StakedTokenV3.sol b/contracts/stake/StakedTokenV3.sol index b060195..f66015e 100644 --- a/contracts/stake/StakedTokenV3.sol +++ b/contracts/stake/StakedTokenV3.sol @@ -58,6 +58,11 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { _; } + modifier onlyStakedTokenIsRewardToken { + require((REWARD_TOKEN == STAKED_TOKEN), 'REWARD_TOKEN_IS_NOT_STAKED_TOKEN'); + _; + } + event Staked(address indexed from, address indexed to, uint256 amount, uint256 sharesMinted); event Redeem( address indexed from, @@ -156,8 +161,6 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { _initAdmins(adminsRoles, adminsAddresses); _maxSlashablePercentage = maxSlashablePercentage; - - IERC20(STAKED_TOKEN).approve(address(this), type(uint256).max); } /** @@ -166,7 +169,7 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { * @param amount The amount to be staked **/ function stake(address to, uint256 amount) external override(IStakedToken, StakedTokenV2) { - _stake(msg.sender, to, amount); + _stake(msg.sender, to, amount, false); } /** @@ -188,7 +191,7 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { bytes32 s ) external override { IERC20WithPermit(address(STAKED_TOKEN)).permit(from, address(this), amount, deadline, v, r, s); - _stake(from, to, amount); + _stake(from, to, amount, false); } /** @@ -220,7 +223,7 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { * @param amount Amount to stake **/ function claimRewards(address to, uint256 amount) external override(StakedTokenV2, IStakedToken) { - _claimRewards(msg.sender, to, amount); + _claimRewards(msg.sender, to, amount, false); } /** @@ -234,7 +237,7 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { address to, uint256 amount ) external override onlyClaimHelper { - _claimRewards(from, to, amount); + _claimRewards(from, to, amount, false); } /** @@ -242,9 +245,13 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { * @param to Address to stake to * @param amount Amount to claim **/ - function claimRewardsAndStake(address to, uint256 amount) external override { - uint256 rewardsClaimed = _claimRewards(msg.sender, address(this), amount); - _stake(address(this), to, rewardsClaimed); + function claimRewardsAndStake(address to, uint256 amount) + external + override + onlyStakedTokenIsRewardToken + { + uint256 rewardsClaimed = _claimRewards(msg.sender, address(this), amount, true); + _stake(address(this), to, rewardsClaimed, true); } /** @@ -257,9 +264,9 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { address from, address to, uint256 amount - ) external override onlyClaimHelper { - uint256 rewardsClaimed = _claimRewards(from, address(this), amount); - _stake(address(this), to, rewardsClaimed); + ) external override onlyStakedTokenIsRewardToken onlyClaimHelper { + uint256 rewardsClaimed = _claimRewards(from, address(this), amount, true); + _stake(address(this), to, rewardsClaimed, true); } /** @@ -273,7 +280,7 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { uint256 claimAmount, uint256 redeemAmount ) external override { - _claimRewards(msg.sender, to, claimAmount); + _claimRewards(msg.sender, to, claimAmount, false); _redeem(msg.sender, to, redeemAmount); } @@ -290,7 +297,7 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { uint256 claimAmount, uint256 redeemAmount ) external override onlyClaimHelper { - _claimRewards(from, to, claimAmount); + _claimRewards(from, to, claimAmount, false); _redeem(from, to, redeemAmount); } @@ -372,14 +379,17 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { function _claimRewards( address from, address to, - uint256 amount + uint256 amount, + bool claimToReserve ) internal returns (uint256) { uint256 newTotalRewards = _updateCurrentUnclaimedRewards(from, balanceOf(from), false); uint256 amountToClaim = (amount == type(uint256).max) ? newTotalRewards : amount; stakerRewardsToClaim[from] = newTotalRewards.sub(amountToClaim, 'INVALID_AMOUNT'); - REWARD_TOKEN.safeTransferFrom(REWARDS_VAULT, to, amountToClaim); + if (!claimToReserve) { + REWARD_TOKEN.safeTransferFrom(REWARDS_VAULT, to, amountToClaim); + } emit RewardsClaimed(from, to, amountToClaim); return (amountToClaim); @@ -388,7 +398,8 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { function _stake( address from, address to, - uint256 amount + uint256 amount, + bool stakeFromReserve ) internal { require(amount != 0, 'INVALID_ZERO_AMOUNT'); @@ -407,7 +418,13 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager { uint256 sharesToMint = amount.mul(1e18).div(exchangeRate()); _mint(to, sharesToMint); - STAKED_TOKEN.safeTransferFrom(from, address(this), amount); + if (stakeFromReserve) { + // redondant in current implementation + require((REWARD_TOKEN == STAKED_TOKEN), 'SHOULD_BE_SETTLED'); + REWARD_TOKEN.safeTransferFrom(REWARDS_VAULT, address(this), amount); + } else { + STAKED_TOKEN.safeTransferFrom(from, address(this), amount); + } emit Staked(from, to, amount, sharesToMint); }