Skip to content

Commit

Permalink
improve rewarders
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsam4 committed Sep 24, 2021
1 parent 710c2a7 commit 7e96b59
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 18 deletions.
29 changes: 25 additions & 4 deletions contracts/mocks/CloneRewarderTime.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "@boringcrypto/boring-solidity/contracts/libraries/BoringMath.sol";
import "@boringcrypto/boring-solidity/contracts/BoringOwnable.sol";

interface IMasterChefV2 {
function lpToken(uint256 pid) external view returns (IERC20 _lpToken);
function lpToken(uint256 pid) external view returns (IERC20 _lpToken);
}

/// @author @0xKeno
Expand Down Expand Up @@ -75,13 +75,20 @@ contract CloneRewarderTime is IRewarder, BoringOwnable{
(user.amount.mul(pool.accToken1PerShare) / ACC_TOKEN_PRECISION).sub(
user.rewardDebt
);
rewardToken.safeTransfer(to, pending);
uint256 balance = rewardToken.balanceOf(address(this));
if (pending > balance) {
rewardToken.safeTransfer(to, balance);
pending -= balance;
} else {
rewardToken.safeTransfer(to, pending);
pending = 0;
}
}
user.amount = lpTokenAmount;
user.rewardDebt = lpTokenAmount.mul(pool.accToken1PerShare) / ACC_TOKEN_PRECISION;
user.rewardDebt = (lpTokenAmount.mul(pool.accToken1PerShare) / ACC_TOKEN_PRECISION) - pending;
emit LogOnReward(_user, pid, pending, to);
}

function pendingTokens(uint256 pid, address user, uint256) override external view returns (IERC20[] memory rewardTokens, uint256[] memory rewardAmounts) {
IERC20[] memory _rewardTokens = new IERC20[](1);
_rewardTokens[0] = (rewardToken);
Expand All @@ -103,6 +110,20 @@ contract CloneRewarderTime is IRewarder, BoringOwnable{
emit LogRewardPerSecond(_rewardPerSecond);
}

/// @notice Allows owner to reclaim/withdraw any tokens (including reward tokens) held by this contract
/// @param token Token to reclaim, use 0x00 for Ethereum
/// @param amount Amount of tokens to reclaim
/// @param to Receiver of the tokens, first of his name, rightful heir to the lost tokens,
/// reightful owner of the extra tokens, and ether, protector of mistaken transfers, mother of token reclaimers,
/// the Khaleesi of the Great Token Sea, the Unburnt, the Breaker of blockchains.
function reclaimTokens(address token, uint256 amount, address payable to) public onlyOwner {
if (token == address(0)) {
to.transfer(amount);
} else {
IERC20(token).safeTransfer(to, amount);
}
}

modifier onlyMCV2 {
require(
msg.sender == MASTERCHEF_V2,
Expand Down
47 changes: 39 additions & 8 deletions contracts/mocks/CloneRewarderTimeDual.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "@boringcrypto/boring-solidity/contracts/libraries/BoringMath.sol";
import "@boringcrypto/boring-solidity/contracts/BoringOwnable.sol";

interface IMasterChefV2 {
function lpToken(uint256 pid) external view returns (IERC20 _lpToken);
function lpToken(uint256 pid) external view returns (IERC20 _lpToken);
}

/// @author @0xKeno
Expand Down Expand Up @@ -85,22 +85,39 @@ contract CloneRewarderTimeDual is IRewarder, BoringOwnable{
(_userInfo.amount.mul(pool.accToken2PerShare) / ACC_TOKEN_PRECISION).sub(
_userInfo.rewardDebt2
);
rewardToken1.safeTransfer(to, pending1);
rewardToken2.safeTransfer(to, pending2);

uint256 balance1 = rewardToken1.balanceOf(address(this));
uint256 balance2 = rewardToken2.balanceOf(address(this));

if (pending1 > balance1) {
rewardToken1.safeTransfer(to, balance1);
pending1 -= balance1;
} else {
rewardToken1.safeTransfer(to, pending1);
pending1 = 0;
}

if (pending2 > balance2) {
rewardToken2.safeTransfer(to, balance2);
pending2 -= balance2;
} else {
rewardToken2.safeTransfer(to, pending2);
pending2 = 0;
}
}
_userInfo.amount = lpTokenAmount;
_userInfo.rewardDebt1 = lpTokenAmount.mul(pool.accToken1PerShare) / ACC_TOKEN_PRECISION;
_userInfo.rewardDebt2 = lpTokenAmount.mul(pool.accToken2PerShare) / ACC_TOKEN_PRECISION;
_userInfo.rewardDebt1 = (lpTokenAmount.mul(pool.accToken1PerShare) / ACC_TOKEN_PRECISION) - pending1;
_userInfo.rewardDebt2 = (lpTokenAmount.mul(pool.accToken2PerShare) / ACC_TOKEN_PRECISION) - pending2;

userInfo[pid][_user] = _userInfo;

emit LogOnReward(_user, pid, pending1, pending2, to);
}

function pendingTokens(uint256 pid, address user, uint256) override external view returns (IERC20[] memory rewardTokens, uint256[] memory rewardAmounts) {
IERC20[] memory _rewardTokens = new IERC20[](2);
_rewardTokens[0] = rewardToken1;
_rewardTokens[1] = rewardToken2;
_rewardTokens[1] = rewardToken2;
uint256[] memory _rewardAmounts = new uint256[](2);
(uint256 reward1, uint256 reward2) = pendingToken(pid, user);
_rewardAmounts[0] = reward1;
Expand All @@ -111,7 +128,7 @@ contract CloneRewarderTimeDual is IRewarder, BoringOwnable{
function rewardRates() external view returns (uint256[] memory) {
uint256[] memory _rewardRates = new uint256[](2);
_rewardRates[0] = rewardPerSecond1;
_rewardRates[1] = rewardPerSecond2;
_rewardRates[1] = rewardPerSecond2;
return (_rewardRates);
}

Expand All @@ -124,6 +141,20 @@ contract CloneRewarderTimeDual is IRewarder, BoringOwnable{
emit LogRewardPerSecond(_rewardPerSecond1, _rewardPerSecond2);
}

/// @notice Allows owner to reclaim/withdraw any tokens (including reward tokens) held by this contract
/// @param token Token to reclaim, use 0x00 for Ethereum
/// @param amount Amount of tokens to reclaim
/// @param to Receiver of the tokens, first of his name, rightful heir to the lost tokens,
/// reightful owner of the extra tokens, and ether, protector of mistaken transfers, mother of token reclaimers,
/// the Khaleesi of the Great Token Sea, the Unburnt, the Breaker of blockchains.
function reclaimTokens(address token, uint256 amount, address payable to) public onlyOwner {
if (token == address(0)) {
to.transfer(amount);
} else {
IERC20(token).safeTransfer(to, amount);
}
}

modifier onlyMCV2 {
require(
msg.sender == MASTERCHEF_V2,
Expand Down
27 changes: 24 additions & 3 deletions contracts/mocks/ComplexRewarder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,20 @@ contract ComplexRewarder is IRewarder, BoringOwnable{
(user.amount.mul(pool.accSushiPerShare) / ACC_TOKEN_PRECISION).sub(
user.rewardDebt
);
rewardToken.safeTransfer(to, pending);
uint256 balance = rewardToken.balanceOf(address(this));
if (pending > balance) {
rewardToken.safeTransfer(to, balance);
pending -= balance;
} else {
rewardToken.safeTransfer(to, pending);
pending = 0;
}
}
user.amount = lpToken;
user.rewardDebt = lpToken.mul(pool.accSushiPerShare) / ACC_TOKEN_PRECISION;
user.rewardDebt = (lpToken.mul(pool.accSushiPerShare) / ACC_TOKEN_PRECISION) - pending;
emit LogOnReward(_user, pid, pending, to);
}

function pendingTokens(uint256 pid, address user, uint256) override external view returns (IERC20[] memory rewardTokens, uint256[] memory rewardAmounts) {
IERC20[] memory _rewardTokens = new IERC20[](1);
_rewardTokens[0] = (rewardToken);
Expand Down Expand Up @@ -125,6 +132,20 @@ contract ComplexRewarder is IRewarder, BoringOwnable{
emit LogSetPool(_pid, _allocPoint);
}

/// @notice Allows owner to reclaim/withdraw any tokens (including reward tokens) held by this contract
/// @param token Token to reclaim, use 0x00 for Ethereum
/// @param amount Amount of tokens to reclaim
/// @param to Receiver of the tokens, first of his name, rightful heir to the lost tokens,
/// reightful owner of the extra tokens, and ether, protector of mistaken transfers, mother of token reclaimers,
/// the Khaleesi of the Great Token Sea, the Unburnt, the Breaker of blockchains.
function reclaimTokens(address token, uint256 amount, address payable to) public onlyOwner {
if (token == address(0)) {
to.transfer(amount);
} else {
IERC20(token).safeTransfer(to, amount);
}
}

/// @notice View function to see pending Token
/// @param _pid The index of the pool. See `poolInfo`.
/// @param _user Address of user.
Expand Down
27 changes: 24 additions & 3 deletions contracts/mocks/ComplexRewarderTime.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,20 @@ contract ComplexRewarderTime is IRewarder, BoringOwnable{
(user.amount.mul(pool.accSushiPerShare) / ACC_TOKEN_PRECISION).sub(
user.rewardDebt
);
rewardToken.safeTransfer(to, pending);
uint256 balance = rewardToken.balanceOf(address(this));
if (pending > balance) {
rewardToken.safeTransfer(to, balance);
pending -= balance;
} else {
rewardToken.safeTransfer(to, pending);
pending = 0;
}
}
user.amount = lpToken;
user.rewardDebt = lpToken.mul(pool.accSushiPerShare) / ACC_TOKEN_PRECISION;
user.rewardDebt = (lpToken.mul(pool.accSushiPerShare) / ACC_TOKEN_PRECISION) - pending;
emit LogOnReward(_user, pid, pending, to);
}

function pendingTokens(uint256 pid, address user, uint256) override external view returns (IERC20[] memory rewardTokens, uint256[] memory rewardAmounts) {
IERC20[] memory _rewardTokens = new IERC20[](1);
_rewardTokens[0] = (rewardToken);
Expand Down Expand Up @@ -133,6 +140,20 @@ contract ComplexRewarderTime is IRewarder, BoringOwnable{
emit LogSetPool(_pid, _allocPoint);
}

/// @notice Allows owner to reclaim/withdraw any tokens (including reward tokens) held by this contract
/// @param token Token to reclaim, use 0x00 for Ethereum
/// @param amount Amount of tokens to reclaim
/// @param to Receiver of the tokens, first of his name, rightful heir to the lost tokens,
/// reightful owner of the extra tokens, and ether, protector of mistaken transfers, mother of token reclaimers,
/// the Khaleesi of the Great Token Sea, the Unburnt, the Breaker of blockchains.
function reclaimTokens(address token, uint256 amount, address payable to) public onlyOwner {
if (token == address(0)) {
to.transfer(amount);
} else {
IERC20(token).safeTransfer(to, amount);
}
}

/// @notice View function to see pending Token
/// @param _pid The index of the pool. See `poolInfo`.
/// @param _user Address of user.
Expand Down

0 comments on commit 7e96b59

Please sign in to comment.