From 81b10e481b6ed1e19adeeb59c7b933fe89ef112b Mon Sep 17 00:00:00 2001 From: Apotheosis <97164662+0xApotheosis@users.noreply.github.com> Date: Mon, 16 Sep 2024 12:50:46 +1000 Subject: [PATCH] chore: remove duplicate contracts --- contracts/Owned.sol | 43 ----- contracts/Pausable.sol | 48 ----- contracts/RewardsDistributionRecipient.sol | 25 --- contracts/StakingRewards.sol | 199 --------------------- test/utils.ts | 2 +- 5 files changed, 1 insertion(+), 316 deletions(-) delete mode 100644 contracts/Owned.sol delete mode 100644 contracts/Pausable.sol delete mode 100644 contracts/RewardsDistributionRecipient.sol delete mode 100644 contracts/StakingRewards.sol diff --git a/contracts/Owned.sol b/contracts/Owned.sol deleted file mode 100644 index 8b1ac6a..0000000 --- a/contracts/Owned.sol +++ /dev/null @@ -1,43 +0,0 @@ -pragma solidity ^0.5.16; - -// https://docs.synthetix.io/contracts/source/contracts/owned -contract Owned { - address public owner; - address public nominatedOwner; - - constructor(address _owner) public { - require(_owner != address(0), "Owner address cannot be 0"); - owner = _owner; - emit OwnerChanged(address(0), _owner); - } - - function nominateNewOwner(address _owner) external onlyOwner { - nominatedOwner = _owner; - emit OwnerNominated(_owner); - } - - function acceptOwnership() external { - require( - msg.sender == nominatedOwner, - "You must be nominated before you can accept ownership" - ); - emit OwnerChanged(owner, nominatedOwner); - owner = nominatedOwner; - nominatedOwner = address(0); - } - - modifier onlyOwner() { - _onlyOwner(); - _; - } - - function _onlyOwner() private view { - require( - msg.sender == owner, - "Only the contract owner may perform this action" - ); - } - - event OwnerNominated(address newOwner); - event OwnerChanged(address oldOwner, address newOwner); -} diff --git a/contracts/Pausable.sol b/contracts/Pausable.sol deleted file mode 100644 index 73ab79f..0000000 --- a/contracts/Pausable.sol +++ /dev/null @@ -1,48 +0,0 @@ -pragma solidity ^0.5.16; - -// Inheritance -import "./Owned.sol"; - -// https://docs.synthetix.io/contracts/source/contracts/pausable -contract Pausable is Owned { - uint public lastPauseTime; - bool public paused; - - constructor() internal { - // This contract is abstract, and thus cannot be instantiated directly - require(owner != address(0), "Owner must be set"); - // Paused will be false, and lastPauseTime will be 0 upon initialisation - } - - /** - * @notice Change the paused state of the contract - * @dev Only the contract owner may call this. - */ - function setPaused(bool _paused) external onlyOwner { - // Ensure we're actually changing the state before we do anything - if (_paused == paused) { - return; - } - - // Set our paused state. - paused = _paused; - - // If applicable, set the last pause time. - if (paused) { - lastPauseTime = now; - } - - // Let everyone know that our pause state has changed. - emit PauseChanged(paused); - } - - event PauseChanged(bool isPaused); - - modifier notPaused() { - require( - !paused, - "This action cannot be performed while the contract is paused" - ); - _; - } -} diff --git a/contracts/RewardsDistributionRecipient.sol b/contracts/RewardsDistributionRecipient.sol deleted file mode 100644 index fde54cd..0000000 --- a/contracts/RewardsDistributionRecipient.sol +++ /dev/null @@ -1,25 +0,0 @@ -pragma solidity ^0.5.16; - -// Inheritance -import "./Owned.sol"; - -// https://docs.synthetix.io/contracts/source/contracts/rewardsdistributionrecipient -contract RewardsDistributionRecipient is Owned { - address public rewardsDistribution; - - function notifyRewardAmount(uint256 reward) external; - - modifier onlyRewardsDistribution() { - require( - msg.sender == rewardsDistribution, - "Caller is not RewardsDistribution contract" - ); - _; - } - - function setRewardsDistribution( - address _rewardsDistribution - ) external onlyOwner { - rewardsDistribution = _rewardsDistribution; - } -} diff --git a/contracts/StakingRewards.sol b/contracts/StakingRewards.sol deleted file mode 100644 index ded7134..0000000 --- a/contracts/StakingRewards.sol +++ /dev/null @@ -1,199 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0 -pragma solidity ^0.5.16; - -import "openzeppelin-solidity-2.3.0/contracts/math/SafeMath.sol"; -import "openzeppelin-solidity-2.3.0/contracts/token/ERC20/ERC20Detailed.sol"; -import "openzeppelin-solidity-2.3.0/contracts/token/ERC20/SafeERC20.sol"; -import "openzeppelin-solidity-2.3.0/contracts/utils/ReentrancyGuard.sol"; - -// Inheritance -import "./interfaces/IStakingRewards.sol"; -import "./RewardsDistributionRecipient.sol"; -import "./Pausable.sol"; - -// https://docs.synthetix.io/contracts/source/contracts/stakingrewards -contract StakingRewards is - IStakingRewards, - RewardsDistributionRecipient, - ReentrancyGuard, - Pausable -{ - using SafeMath for uint256; - using SafeERC20 for IERC20; - - /* ========== STATE VARIABLES ========== */ - - IERC20 public rewardsToken; - IERC20 public stakingToken; - uint256 public periodFinish = 0; - uint256 public rewardRate = 0; - uint256 public rewardsDuration = 7 days; - uint256 public lastUpdateTime; - uint256 public rewardPerTokenStored; - - mapping(address => uint256) public userRewardPerTokenPaid; - mapping(address => uint256) public rewards; - - uint256 private _totalSupply; - mapping(address => uint256) private _balances; - - /* ========== CONSTRUCTOR ========== */ - - constructor( - address _owner, - address _rewardsDistribution, - address _rewardsToken, - address _stakingToken - ) public Owned(_owner) { - rewardsToken = IERC20(_rewardsToken); - stakingToken = IERC20(_stakingToken); - rewardsDistribution = _rewardsDistribution; - } - - /* ========== VIEWS ========== */ - - function totalSupply() external view returns (uint256) { - return _totalSupply; - } - - function balanceOf(address account) external view returns (uint256) { - return _balances[account]; - } - - function lastTimeRewardApplicable() public view returns (uint256) { - return block.timestamp < periodFinish ? block.timestamp : periodFinish; - } - - function rewardPerToken() public view returns (uint256) { - if (_totalSupply == 0) { - return rewardPerTokenStored; - } - return - rewardPerTokenStored.add( - lastTimeRewardApplicable() - .sub(lastUpdateTime) - .mul(rewardRate) - .mul(1e18) - .div(_totalSupply) - ); - } - - function earned(address account) public view returns (uint256) { - return - _balances[account] - .mul(rewardPerToken().sub(userRewardPerTokenPaid[account])) - .div(1e18) - .add(rewards[account]); - } - - function getRewardForDuration() external view returns (uint256) { - return rewardRate.mul(rewardsDuration); - } - - /* ========== MUTATIVE FUNCTIONS ========== */ - - function stake( - uint256 amount - ) external nonReentrant notPaused updateReward(msg.sender) { - require(amount > 0, "Cannot stake 0"); - _totalSupply = _totalSupply.add(amount); - _balances[msg.sender] = _balances[msg.sender].add(amount); - stakingToken.safeTransferFrom(msg.sender, address(this), amount); - emit Staked(msg.sender, amount); - } - - function withdraw( - uint256 amount - ) public nonReentrant updateReward(msg.sender) { - require(amount > 0, "Cannot withdraw 0"); - _totalSupply = _totalSupply.sub(amount); - _balances[msg.sender] = _balances[msg.sender].sub(amount); - stakingToken.safeTransfer(msg.sender, amount); - emit Withdrawn(msg.sender, amount); - } - - function getReward() public nonReentrant updateReward(msg.sender) { - uint256 reward = rewards[msg.sender]; - if (reward > 0) { - rewards[msg.sender] = 0; - rewardsToken.safeTransfer(msg.sender, reward); - emit RewardPaid(msg.sender, reward); - } - } - - function exit() external { - withdraw(_balances[msg.sender]); - getReward(); - } - - /* ========== RESTRICTED FUNCTIONS ========== */ - - function notifyRewardAmount( - uint256 reward - ) external onlyRewardsDistribution updateReward(address(0)) { - if (block.timestamp >= periodFinish) { - rewardRate = reward.div(rewardsDuration); - } else { - uint256 remaining = periodFinish.sub(block.timestamp); - uint256 leftover = remaining.mul(rewardRate); - rewardRate = reward.add(leftover).div(rewardsDuration); - } - - // Ensure the provided reward amount is not more than the balance in the contract. - // This keeps the reward rate in the right range, preventing overflows due to - // very high values of rewardRate in the earned and rewardsPerToken functions; - // Reward + leftover must be less than 2^256 / 10^18 to avoid overflow. - uint balance = rewardsToken.balanceOf(address(this)); - require( - rewardRate <= balance.div(rewardsDuration), - "Provided reward too high" - ); - - lastUpdateTime = block.timestamp; - periodFinish = block.timestamp.add(rewardsDuration); - emit RewardAdded(reward); - } - - // Added to support recovering LP Rewards from other systems such as BAL to be distributed to holders - function recoverERC20( - address tokenAddress, - uint256 tokenAmount - ) external onlyOwner { - require( - tokenAddress != address(stakingToken), - "Cannot withdraw the staking token" - ); - IERC20(tokenAddress).safeTransfer(owner, tokenAmount); - emit Recovered(tokenAddress, tokenAmount); - } - - function setRewardsDuration(uint256 _rewardsDuration) external onlyOwner { - require( - block.timestamp > periodFinish, - "Previous rewards period must be complete before changing the duration for the new period" - ); - rewardsDuration = _rewardsDuration; - emit RewardsDurationUpdated(rewardsDuration); - } - - /* ========== MODIFIERS ========== */ - - modifier updateReward(address account) { - rewardPerTokenStored = rewardPerToken(); - lastUpdateTime = lastTimeRewardApplicable(); - if (account != address(0)) { - rewards[account] = earned(account); - userRewardPerTokenPaid[account] = rewardPerTokenStored; - } - _; - } - - /* ========== EVENTS ========== */ - - event RewardAdded(uint256 reward); - event Staked(address indexed user, uint256 amount); - event Withdrawn(address indexed user, uint256 amount); - event RewardPaid(address indexed user, uint256 reward); - event RewardsDurationUpdated(uint256 newDuration); - event Recovered(address token, uint256 amount); -} diff --git a/test/utils.ts b/test/utils.ts index b123de6..a340998 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -65,7 +65,7 @@ export async function deployStakingRewardsFixture() { symbol: 'STK', }); - const stakingRewards = await hre.viem.deployContract('contracts/StakingRewards.sol:StakingRewards', [ + const stakingRewards = await hre.viem.deployContract('StakingRewards', [ owner.account.address, rewardsDistribution.account.address, rewardsToken.address,