Skip to content

Commit

Permalink
removed useless transfers in claimAndStake
Browse files Browse the repository at this point in the history
  • Loading branch information
dhadrien authored and eboadom committed Feb 19, 2021
1 parent 5bd671a commit 586d02f
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions contracts/stake/StakedTokenV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -156,8 +161,6 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager {
_initAdmins(adminsRoles, adminsAddresses);

_maxSlashablePercentage = maxSlashablePercentage;

IERC20(STAKED_TOKEN).approve(address(this), type(uint256).max);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -234,17 +237,21 @@ contract StakedTokenV3 is StakedTokenV2, IStakedTokenV3, RoleManager {
address to,
uint256 amount
) external override onlyClaimHelper {
_claimRewards(from, to, amount);
_claimRewards(from, to, amount, false);
}

/**
* @dev Claims an `amount` of `REWARD_TOKEN` amd restakes
* @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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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');

Expand All @@ -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);
}
Expand Down

0 comments on commit 586d02f

Please sign in to comment.