Diff
checker
テキスト
テキスト
画像
ドキュメント
Excel
フォルダ
Legal
Enterprise
デスクトップ
料金
ログイン
Diffchecker デスクトップのダウンロード
テキスト比較
2 つのテキスト ファイルの違いを見つける
ツール
履歴
ライブエディター
未変更行を折りたたむ
折り返しなし
レイアウト
分割
統合
比較精度
スマート
単語
文字
シンタックスハイライト
構文を選択
無視
テキスト変換
最初の差分へ移動
入力を編集
Diffchecker Desktop
Diffcheckerを実行する最も安全な方法。Diffchecker Desktopアプリを入手:あなたの差分はコンピューターから出ることはありません!
Desktopを入手
staking-rewards-updates
作成日
3 年前
差分は期限切れになりません
クリア
エクスポート
共有
説明
73 削除
行
合計
削除
文字
合計
削除
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
292 行
すべてコピー
115 追加
行
合計
追加
文字
合計
追加
この機能を引き続き使用するには、アップグレードしてください
Diff
checker
Pro
価格を見る
340 行
すべてコピー
コピー
コピー済み
コピー
コピー済み
pragma solidity ^0.5.16;
// SPDX-FileCopyrightText: © 2019-2021 Synthetix
// SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org>
// Inheritance
// SPDX-License-Identifier: MIT AND AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity 0.8.19;
// https://docs.synthetix.io/contracts/source/interfaces/istakingrewards
// https://docs.synthetix.io/contracts/source/interfaces/istakingrewards
interface IStakingRewards {
interface IStakingRewards {
// Views
// Views
function balanceOf(address account) external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function earned(address account) external view returns (uint256);
function earned(address account) external view returns (uint256);
function getRewardForDuration() external view returns (uint256);
function getRewardForDuration() external view returns (uint256);
function lastTimeRewardApplicable() external view returns (uint256);
function lastTimeRewardApplicable() external view returns (uint256);
function rewardPerToken() external view returns (uint256);
function rewardPerToken() external view returns (uint256);
function rewardsDistribution() external view returns (address);
function rewardsDistribution() external view returns (address);
コピー
コピー済み
コピー
コピー済み
function rewardsToken() external view returns (
address
);
function rewardsToken() external view returns (
IERC20);
function stakingToken() external view returns (IERC20
);
function totalSupply() external view returns (uint256);
function totalSupply() external view returns (uint256);
// Mutative
// Mutative
function exit() external;
function exit() external;
function getReward() external;
function getReward() external;
function stake(uint256 amount) external;
function stake(uint256 amount) external;
コピー
コピー済み
コピー
コピー済み
function stake(uint256 amount, uint16 referral) external;
function withdraw(uint256 amount) external;
function withdraw(uint256 amount) external;
コピー
コピー済み
コピー
コピー済み
function notifyRewardAmount(uint256 reward) external;
function setRewardsDistribution(address _rewardsDistribution) external;
}
}
コピー
コピー済み
コピー
コピー済み
// SPDX-FileCopyrightText: © 2019-2021 Synthetix
// SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: MIT AND AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Inheritance
// Inheritance
コピー
コピー済み
コピー
コピー済み
// SPDX-FileCopyrightText: © 2019-2021 Synthetix
// SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: MIT AND AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// https://docs.synthetix.io/contracts/source/contracts/owned
// https://docs.synthetix.io/contracts/source/contracts/owned
contract Owned {
contract Owned {
address public owner;
address public owner;
address public nominatedOwner;
address public nominatedOwner;
コピー
コピー済み
コピー
コピー済み
constructor(address _owner)
public
{
constructor(address _owner)
{
require(_owner != address(0), "Owner address cannot be 0");
require(_owner != address(0), "Owner address cannot be 0");
owner = _owner;
owner = _owner;
emit OwnerChanged(address(0), _owner);
emit OwnerChanged(address(0), _owner);
}
}
function nominateNewOwner(address _owner) external onlyOwner {
function nominateNewOwner(address _owner) external onlyOwner {
nominatedOwner = _owner;
nominatedOwner = _owner;
emit OwnerNominated(_owner);
emit OwnerNominated(_owner);
}
}
function acceptOwnership() external {
function acceptOwnership() external {
require(msg.sender == nominatedOwner, "You must be nominated before you can accept ownership");
require(msg.sender == nominatedOwner, "You must be nominated before you can accept ownership");
emit OwnerChanged(owner, nominatedOwner);
emit OwnerChanged(owner, nominatedOwner);
owner = nominatedOwner;
owner = nominatedOwner;
nominatedOwner = address(0);
nominatedOwner = address(0);
}
}
コピー
コピー済み
コピー
コピー済み
modifier onlyOwner
{
modifier onlyOwner
()
{
_onlyOwner();
_onlyOwner();
_;
_;
}
}
function _onlyOwner() private view {
function _onlyOwner() private view {
require(msg.sender == owner, "Only the contract owner may perform this action");
require(msg.sender == owner, "Only the contract owner may perform this action");
}
}
event OwnerNominated(address newOwner);
event OwnerNominated(address newOwner);
event OwnerChanged(address oldOwner, address newOwner);
event OwnerChanged(address oldOwner, address newOwner);
}
}
コピー
コピー済み
コピー
コピー済み
// https://docs.synthetix.io/contracts/source/contracts/rewardsdistributionrecipient
contract RewardsDistributionRecipient is Owned {
address public rewardsDistribution;
function notifyRewardAmount(uint256 reward) external;
Text moved to lines 324-328
modifier onlyRewardsDistribution() {
require(msg.sender == rewardsDistribution, "Caller is not RewardsDistribution contract");
_;
}
function setRewardsDistribution(address _rewardsDistribution) external onlyOwner {
rewardsDistribution = _rewardsDistribution;
}
}
// Inheritance
// https://docs.synthetix.io/contracts/source/contracts/pausable
// https://docs.synthetix.io/contracts/source/contracts/pausable
コピー
コピー済み
コピー
コピー済み
contract Pausable is Owned {
abstract
contract Pausable is Owned {
uint public lastPauseTime;
uint public lastPauseTime;
bool public paused;
bool public paused;
コピー
コピー済み
コピー
コピー済み
constructor(
) internal {
constructor(
address
_o
wner
) Owned(_owner) {}
// This contract is abstract, and thus cannot be instantiated directly
require(owner !=
address
(0), "O
wner
must be set");
// Paused will be false, and lastPauseTime will be 0 upon initialisation
}
/**
/**
* @notice Change the paused state of the contract
* @notice Change the paused state of the contract
* @dev Only the contract owner may call this.
* @dev Only the contract owner may call this.
*/
*/
function setPaused(bool _paused) external onlyOwner {
function setPaused(bool _paused) external onlyOwner {
// Ensure we're actually changing the state before we do anything
// Ensure we're actually changing the state before we do anything
if (_paused == paused) {
if (_paused == paused) {
return;
return;
}
}
// Set our paused state.
// Set our paused state.
paused = _paused;
paused = _paused;
// If applicable, set the last pause time.
// If applicable, set the last pause time.
if (paused) {
if (paused) {
コピー
コピー済み
コピー
コピー済み
lastPauseTime =
now
;
lastPauseTime =
block.timestamp
;
}
}
// Let everyone know that our pause state has changed.
// Let everyone know that our pause state has changed.
emit PauseChanged(paused);
emit PauseChanged(paused);
}
}
event PauseChanged(bool isPaused);
event PauseChanged(bool isPaused);
コピー
コピー済み
コピー
コピー済み
modifier notPaused
{
modifier notPaused
()
{
require(!paused, "This action cannot be performed while the contract is paused");
require(!paused, "This action cannot be performed while the contract is paused");
_;
_;
}
}
}
}
// https://docs.synthetix.io/contracts/source/contracts/stakingrewards
// https://docs.synthetix.io/contracts/source/contracts/stakingrewards
コピー
コピー済み
コピー
コピー済み
contract StakingRewards is IStakingRewards,
RewardsDistributionRecipient
, ReentrancyGuard
, Pausable {
contract StakingRewards is IStakingRewards,
Pausable
, ReentrancyGuard
{
using SafeMath for uint256;
using SafeERC20 for IERC20;
using SafeERC20 for IERC20;
/* ========== STATE VARIABLES ========== */
/* ========== STATE VARIABLES ========== */
コピー
コピー済み
コピー
コピー済み
IERC20 public
rewardsToken;
IERC20 public
immutable
rewardsToken;
IERC20 public
stakingToken;
IERC20 public
immutable
stakingToken;
address public rewardsDistribution;
uint256 public periodFinish = 0;
uint256 public periodFinish = 0;
uint256 public rewardRate = 0;
uint256 public rewardRate = 0;
uint256 public rewardsDuration = 7 days;
uint256 public rewardsDuration = 7 days;
uint256 public lastUpdateTime;
uint256 public lastUpdateTime;
uint256 public rewardPerTokenStored;
uint256 public rewardPerTokenStored;
mapping(address => uint256) public userRewardPerTokenPaid;
mapping(address => uint256) public userRewardPerTokenPaid;
mapping(address => uint256) public rewards;
mapping(address => uint256) public rewards;
uint256 private _totalSupply;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
mapping(address => uint256) private _balances;
/* ========== CONSTRUCTOR ========== */
/* ========== CONSTRUCTOR ========== */
constructor(
constructor(
address _owner,
address _owner,
address _rewardsDistribution,
address _rewardsDistribution,
address _rewardsToken,
address _rewardsToken,
address _stakingToken
address _stakingToken
コピー
コピー済み
コピー
コピー済み
)
public Owned
(_owner) {
)
Pausable
(_owner) {
rewardsToken = IERC20(_rewardsToken);
rewardsToken = IERC20(_rewardsToken);
stakingToken = IERC20(_stakingToken);
stakingToken = IERC20(_stakingToken);
rewardsDistribution = _rewardsDistribution;
rewardsDistribution = _rewardsDistribution;
}
}
/* ========== VIEWS ========== */
/* ========== VIEWS ========== */
function totalSupply() external view returns (uint256) {
function totalSupply() external view returns (uint256) {
return _totalSupply;
return _totalSupply;
}
}
function balanceOf(address account) external view returns (uint256) {
function balanceOf(address account) external view returns (uint256) {
return _balances[account];
return _balances[account];
}
}
function lastTimeRewardApplicable() public view returns (uint256) {
function lastTimeRewardApplicable() public view returns (uint256) {
return block.timestamp < periodFinish ? block.timestamp : periodFinish;
return block.timestamp < periodFinish ? block.timestamp : periodFinish;
}
}
function rewardPerToken() public view returns (uint256) {
function rewardPerToken() public view returns (uint256) {
if (_totalSupply == 0) {
if (_totalSupply == 0) {
return rewardPerTokenStored;
return rewardPerTokenStored;
}
}
return
return
コピー
コピー済み
コピー
コピー済み
rewardPerTokenStored
.add(
rewardPerTokenStored
+ (((
lastTimeRewardApplicable()
-
lastUpdateTime)
*
rewardRate
*
1e18)
/
_totalSupply)
;
lastTimeRewardApplicable()
.sub(
lastUpdateTime)
.mul(
rewardRate
).mul(
1e18)
.div(
_totalSupply)
)
;
}
}
function earned(address account) public view returns (uint256) {
function earned(address account) public view returns (uint256) {
コピー
コピー済み
コピー
コピー済み
return
_balances[account]
.mul
(rewardPerToken()
.sub(
userRewardPerTokenPaid[account]))
.div(
1e18
).add(
rewards[account]
)
;
return
(
_balances[account]
*
(rewardPerToken()
-
userRewardPerTokenPaid[account]))
/
1e18
+
rewards[account]
;
}
}
function getRewardForDuration() external view returns (uint256) {
function getRewardForDuration() external view returns (uint256) {
コピー
コピー済み
コピー
コピー済み
return rewardRate
.mul(
rewardsDuration
)
;
return rewardRate
*
rewardsDuration
;
}
}
/* ========== MUTATIVE FUNCTIONS ========== */
/* ========== MUTATIVE FUNCTIONS ========== */
コピー
コピー済み
コピー
コピー済み
function stake(uint256 amount)
external
nonReentrant notPaused updateReward(msg.sender) {
function stake(uint256 amount)
public
nonReentrant notPaused updateReward(msg.sender) {
require(amount > 0, "Cannot stake 0");
require(amount > 0, "Cannot stake 0");
コピー
コピー済み
コピー
コピー済み
_totalSupply = _totalSupply
.add(
amount
)
;
_totalSupply = _totalSupply
+
amount
;
_balances[msg.sender] = _balances[msg.sender]
.add(
amount
)
;
_balances[msg.sender] = _balances[msg.sender]
+
amount
;
stakingToken.safeTransferFrom(msg.sender, address(this), amount);
stakingToken.safeTransferFrom(msg.sender, address(this), amount);
emit Staked(msg.sender, amount);
emit Staked(msg.sender, amount);
}
}
コピー
コピー済み
コピー
コピー済み
function stake(uint256 amount, uint16 referral) external {
stake(amount);
emit Referral(referral, msg.sender, amount);
}
function withdraw(uint256 amount) public nonReentrant updateReward(msg.sender) {
function withdraw(uint256 amount) public nonReentrant updateReward(msg.sender) {
require(amount > 0, "Cannot withdraw 0");
require(amount > 0, "Cannot withdraw 0");
コピー
コピー済み
コピー
コピー済み
_totalSupply = _totalSupply
.sub(
amount
)
;
_totalSupply = _totalSupply
-
amount
;
_balances[msg.sender] = _balances[msg.sender]
.sub(
amount
)
;
_balances[msg.sender] = _balances[msg.sender]
-
amount
;
stakingToken.safeTransfer(msg.sender, amount);
stakingToken.safeTransfer(msg.sender, amount);
emit Withdrawn(msg.sender, amount);
emit Withdrawn(msg.sender, amount);
}
}
function getReward() public nonReentrant updateReward(msg.sender) {
function getReward() public nonReentrant updateReward(msg.sender) {
uint256 reward = rewards[msg.sender];
uint256 reward = rewards[msg.sender];
if (reward > 0) {
if (reward > 0) {
rewards[msg.sender] = 0;
rewards[msg.sender] = 0;
rewardsToken.safeTransfer(msg.sender, reward);
rewardsToken.safeTransfer(msg.sender, reward);
emit RewardPaid(msg.sender, reward);
emit RewardPaid(msg.sender, reward);
}
}
}
}
function exit() external {
function exit() external {
withdraw(_balances[msg.sender]);
withdraw(_balances[msg.sender]);
getReward();
getReward();
}
}
/* ========== RESTRICTED FUNCTIONS ========== */
/* ========== RESTRICTED FUNCTIONS ========== */
コピー
コピー済み
コピー
コピー済み
function notifyRewardAmount(uint256 reward) external
onlyRewardsDistribution updateReward(address(0)) {
function notifyRewardAmount(uint256 reward) external
override
onlyRewardsDistribution updateReward(address(0)) {
if (block.timestamp >= periodFinish) {
if (block.timestamp >= periodFinish) {
コピー
コピー済み
コピー
コピー済み
rewardRate = reward
.div(
rewardsDuration
)
;
rewardRate = reward
/
rewardsDuration
;
} else {
} else {
コピー
コピー済み
コピー
コピー済み
uint256 remaining = periodFinish
.sub(
block.timestamp
)
;
uint256 remaining = periodFinish
-
block.timestamp
;
uint256 leftover = remaining
.mul(
rewardRate
)
;
uint256 leftover = remaining
*
rewardRate
;
rewardRate =
reward
.add(
leftover)
.div(
rewardsDuration
)
;
rewardRate =
(
reward
+
leftover)
/
rewardsDuration
;
}
}
// Ensure the provided reward amount is not more than the balance in the contract.
// 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
// This keeps the reward rate in the right range, preventing overflows due to
// very high values of rewardRate in the earned and rewardsPerToken functions;
// very high values of rewardRate in the earned and rewardsPerToken functions;
// Reward + leftover must be less than 2^256 / 10^18 to avoid overflow.
// Reward + leftover must be less than 2^256 / 10^18 to avoid overflow.
uint balance = rewardsToken.balanceOf(address(this));
uint balance = rewardsToken.balanceOf(address(this));
コピー
コピー済み
コピー
コピー済み
require(rewardRate <= balance
.div(
rewardsDuration
)
, "Provided reward too high");
require(rewardRate <= balance
/
rewardsDuration
, "Provided reward too high");
lastUpdateTime = block.timestamp;
lastUpdateTime = block.timestamp;
コピー
コピー済み
コピー
コピー済み
periodFinish = block.timestamp
.add(
rewardsDuration
)
;
periodFinish = block.timestamp
+
rewardsDuration
;
emit RewardAdded(reward);
emit RewardAdded(reward);
}
}
// Added to support recovering LP Rewards from other systems such as BAL to be distributed to holders
// 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 {
function recoverERC20(address tokenAddress, uint256 tokenAmount) external onlyOwner {
require(tokenAddress != address(stakingToken), "Cannot withdraw the staking token");
require(tokenAddress != address(stakingToken), "Cannot withdraw the staking token");
IERC20(tokenAddress).safeTransfer(owner, tokenAmount);
IERC20(tokenAddress).safeTransfer(owner, tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
emit Recovered(tokenAddress, tokenAmount);
}
}
function setRewardsDuration(uint256 _rewardsDuration) external onlyOwner {
function setRewardsDuration(uint256 _rewardsDuration) external onlyOwner {
require(
require(
block.timestamp > periodFinish,
block.timestamp > periodFinish,
"Previous rewards period must be complete before changing the duration for the new period"
"Previous rewards period must be complete before changing the duration for the new period"
);
);
rewardsDuration = _rewardsDuration;
rewardsDuration = _rewardsDuration;
emit RewardsDurationUpdated(rewardsDuration);
emit RewardsDurationUpdated(rewardsDuration);
}
}
コピー
コピー済み
コピー
コピー済み
function setRewardsDistribution(address _rewardsDistribution) external onlyOwner {
rewardsDistribution = _rewardsDistribution;
emit RewardsDistributionUpdated(rewardsDistribution);
}
/* ========== MODIFIERS ========== */
/* ========== MODIFIERS ========== */
modifier updateReward(address account) {
modifier updateReward(address account) {
rewardPerTokenStored = rewardPerToken();
rewardPerTokenStored = rewardPerToken();
lastUpdateTime = lastTimeRewardApplicable();
lastUpdateTime = lastTimeRewardApplicable();
if (account != address(0)) {
if (account != address(0)) {
rewards[account] = earned(account);
rewards[account] = earned(account);
userRewardPerTokenPaid[account] = rewardPerTokenStored;
userRewardPerTokenPaid[account] = rewardPerTokenStored;
}
}
_;
_;
}
}
コピー
コピー済み
コピー
コピー済み
Text moved from lines 80-84
modifier onlyRewardsDistribution() {
require(msg.sender == rewardsDistribution, "Caller is not RewardsDistribution contract");
_;
}
/* ========== EVENTS ========== */
/* ========== EVENTS ========== */
event RewardAdded(uint256 reward);
event RewardAdded(uint256 reward);
event Staked(address indexed user, uint256 amount);
event Staked(address indexed user, uint256 amount);
コピー
コピー済み
コピー
コピー済み
event Referral(uint16 indexed referral, address indexed user, uint256 amount);
event Withdrawn(address indexed user, uint256 amount);
event Withdrawn(address indexed user, uint256 amount);
event RewardPaid(address indexed user, uint256 reward);
event RewardPaid(address indexed user, uint256 reward);
event RewardsDurationUpdated(uint256 newDuration);
event RewardsDurationUpdated(uint256 newDuration);
コピー
コピー済み
コピー
コピー済み
event RewardsDistributionUpdated(address newRewardsDistribution);
event Recovered(address token, uint256 amount);
event Recovered(address token, uint256 amount);
}
}
コピー
コピー済み
コピー
コピー済み
保存された差分
原文
ファイルを開く
pragma solidity ^0.5.16; // Inheritance // https://docs.synthetix.io/contracts/source/interfaces/istakingrewards interface IStakingRewards { // Views function balanceOf(address account) external view returns (uint256); function earned(address account) external view returns (uint256); function getRewardForDuration() external view returns (uint256); function lastTimeRewardApplicable() external view returns (uint256); function rewardPerToken() external view returns (uint256); function rewardsDistribution() external view returns (address); function rewardsToken() external view returns (address); function totalSupply() external view returns (uint256); // Mutative function exit() external; function getReward() external; function stake(uint256 amount) external; function withdraw(uint256 amount) external; } // Inheritance // 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); } // 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; } } // Inheritance // 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"); _; } } // 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); }
変更されたテキスト
ファイルを開く
// SPDX-FileCopyrightText: © 2019-2021 Synthetix // SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org> // SPDX-License-Identifier: MIT AND AGPL-3.0-or-later // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. pragma solidity 0.8.19; // https://docs.synthetix.io/contracts/source/interfaces/istakingrewards interface IStakingRewards { // Views function balanceOf(address account) external view returns (uint256); function earned(address account) external view returns (uint256); function getRewardForDuration() external view returns (uint256); function lastTimeRewardApplicable() external view returns (uint256); function rewardPerToken() external view returns (uint256); function rewardsDistribution() external view returns (address); function rewardsToken() external view returns (IERC20); function stakingToken() external view returns (IERC20); function totalSupply() external view returns (uint256); // Mutative function exit() external; function getReward() external; function stake(uint256 amount) external; function stake(uint256 amount, uint16 referral) external; function withdraw(uint256 amount) external; function notifyRewardAmount(uint256 reward) external; function setRewardsDistribution(address _rewardsDistribution) external; } // SPDX-FileCopyrightText: © 2019-2021 Synthetix // SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org> // SPDX-License-Identifier: MIT AND AGPL-3.0-or-later // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. // Inheritance // SPDX-FileCopyrightText: © 2019-2021 Synthetix // SPDX-FileCopyrightText: © 2023 Dai Foundation <www.daifoundation.org> // SPDX-License-Identifier: MIT AND AGPL-3.0-or-later // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. // https://docs.synthetix.io/contracts/source/contracts/owned contract Owned { address public owner; address public nominatedOwner; constructor(address _owner) { 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); } // https://docs.synthetix.io/contracts/source/contracts/pausable abstract contract Pausable is Owned { uint public lastPauseTime; bool public paused; constructor(address _owner) Owned(_owner) {} /** * @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 = block.timestamp; } // 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"); _; } } // https://docs.synthetix.io/contracts/source/contracts/stakingrewards contract StakingRewards is IStakingRewards, Pausable, ReentrancyGuard { using SafeERC20 for IERC20; /* ========== STATE VARIABLES ========== */ IERC20 public immutable rewardsToken; IERC20 public immutable stakingToken; address public rewardsDistribution; 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 ) Pausable(_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 + (((lastTimeRewardApplicable() - lastUpdateTime) * rewardRate * 1e18) / _totalSupply); } function earned(address account) public view returns (uint256) { return (_balances[account] * (rewardPerToken() - userRewardPerTokenPaid[account])) / 1e18 + rewards[account]; } function getRewardForDuration() external view returns (uint256) { return rewardRate * rewardsDuration; } /* ========== MUTATIVE FUNCTIONS ========== */ function stake(uint256 amount) public nonReentrant notPaused updateReward(msg.sender) { require(amount > 0, "Cannot stake 0"); _totalSupply = _totalSupply + amount; _balances[msg.sender] = _balances[msg.sender] + amount; stakingToken.safeTransferFrom(msg.sender, address(this), amount); emit Staked(msg.sender, amount); } function stake(uint256 amount, uint16 referral) external { stake(amount); emit Referral(referral, msg.sender, amount); } function withdraw(uint256 amount) public nonReentrant updateReward(msg.sender) { require(amount > 0, "Cannot withdraw 0"); _totalSupply = _totalSupply - amount; _balances[msg.sender] = _balances[msg.sender] - 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 override onlyRewardsDistribution updateReward(address(0)) { if (block.timestamp >= periodFinish) { rewardRate = reward / rewardsDuration; } else { uint256 remaining = periodFinish - block.timestamp; uint256 leftover = remaining * rewardRate; rewardRate = (reward + leftover) / 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 / rewardsDuration, "Provided reward too high"); lastUpdateTime = block.timestamp; periodFinish = block.timestamp + 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); } function setRewardsDistribution(address _rewardsDistribution) external onlyOwner { rewardsDistribution = _rewardsDistribution; emit RewardsDistributionUpdated(rewardsDistribution); } /* ========== MODIFIERS ========== */ modifier updateReward(address account) { rewardPerTokenStored = rewardPerToken(); lastUpdateTime = lastTimeRewardApplicable(); if (account != address(0)) { rewards[account] = earned(account); userRewardPerTokenPaid[account] = rewardPerTokenStored; } _; } modifier onlyRewardsDistribution() { require(msg.sender == rewardsDistribution, "Caller is not RewardsDistribution contract"); _; } /* ========== EVENTS ========== */ event RewardAdded(uint256 reward); event Staked(address indexed user, uint256 amount); event Referral(uint16 indexed referral, address indexed user, uint256 amount); event Withdrawn(address indexed user, uint256 amount); event RewardPaid(address indexed user, uint256 reward); event RewardsDurationUpdated(uint256 newDuration); event RewardsDistributionUpdated(address newRewardsDistribution); event Recovered(address token, uint256 amount); }
違いを見つける