Contract Name:
PriceFeedTimelock
Contract Source Code:
//SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IAdmin {
function setAdmin(address _admin) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IVaultPriceFeed {
function adjustmentBasisPoints(address _token) external view returns (uint256);
function isAdjustmentAdditive(address _token) external view returns (bool);
function setAdjustment(address _token, bool _isAdditive, uint256 _adjustmentBps) external;
function setUseV2Pricing(bool _useV2Pricing) external;
function setIsAmmEnabled(bool _isEnabled) external;
function setIsSecondaryPriceEnabled(bool _isEnabled) external;
function setSpreadBasisPoints(address _token, uint256 _spreadBasisPoints) external;
function setSpreadThresholdBasisPoints(uint256 _spreadThresholdBasisPoints) external;
function setFavorPrimaryPrice(bool _favorPrimaryPrice) external;
function setPriceSampleSpace(uint256 _priceSampleSpace) external;
function setMaxStrictPriceDeviation(uint256 _maxStrictPriceDeviation) external;
function getPrice(address _token, bool _maximise, bool _includeAmmPrice, bool _useSwapPricing) external view returns (uint256);
function getAmmPrice(address _token) external view returns (uint256);
function getLatestPrimaryPrice(address _token) external view returns (uint256);
function getPrimaryPrice(address _token, bool _maximise) external view returns (uint256);
function setTokenConfig(
address _token,
address _priceFeed,
uint256 _priceDecimals,
bool _isStrictStable
) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IFastPriceFeed {
function lastUpdatedAt() external view returns (uint256);
function lastUpdatedBlock() external view returns (uint256);
function setSigner(address _account, bool _isActive) external;
function setUpdater(address _account, bool _isActive) external;
function setPriceDuration(uint256 _priceDuration) external;
function setMaxPriceUpdateDelay(uint256 _maxPriceUpdateDelay) external;
function setSpreadBasisPointsIfInactive(uint256 _spreadBasisPointsIfInactive) external;
function setSpreadBasisPointsIfChainError(uint256 _spreadBasisPointsIfChainError) external;
function setMinBlockInterval(uint256 _minBlockInterval) external;
function setIsSpreadEnabled(bool _isSpreadEnabled) external;
function setMaxDeviationBasisPoints(uint256 _maxDeviationBasisPoints) external;
function setMaxCumulativeDeltaDiffs(address[] memory _tokens, uint256[] memory _maxCumulativeDeltaDiffs) external;
function setPriceDataInterval(uint256 _priceDataInterval) external;
function setVaultPriceFeed(address _vaultPriceFeed) external;
function setPricesWithBits(uint256 _priceBits, uint256 _timestamp) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IHandlerTarget {
function isHandler(address _account) external returns (bool);
function setHandler(address _handler, bool _isActive) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface ITimelockTarget {
function setGov(address _gov) external;
function withdrawToken(address _token, address _account, uint256 _amount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "./interfaces/ITimelockTarget.sol";
import "./interfaces/IHandlerTarget.sol";
import "../access/interfaces/IAdmin.sol";
import "../core/interfaces/IVaultPriceFeed.sol";
import "../oracle/interfaces/IFastPriceFeed.sol";
import "../referrals/interfaces/IReferralStorage.sol";
import "../tokens/interfaces/IYieldToken.sol";
import "../tokens/interfaces/IBaseToken.sol";
import "../tokens/interfaces/IMintable.sol";
import "../tokens/interfaces/IUSDG.sol";
import "../staking/interfaces/IVester.sol";
import "../libraries/math/SafeMath.sol";
import "../libraries/token/IERC20.sol";
contract PriceFeedTimelock {
using SafeMath for uint256;
uint256 public constant MAX_BUFFER = 5 days;
uint256 public buffer;
address public admin;
address public tokenManager;
mapping (bytes32 => uint256) public pendingActions;
mapping (address => bool) public isHandler;
mapping (address => bool) public isKeeper;
event SignalPendingAction(bytes32 action);
event SignalApprove(address token, address spender, uint256 amount, bytes32 action);
event SignalWithdrawToken(address target, address token, address receiver, uint256 amount, bytes32 action);
event SignalSetGov(address target, address gov, bytes32 action);
event SignalSetPriceFeedWatcher(address fastPriceFeed, address account, bool isActive);
event SignalPriceFeedSetTokenConfig(
address vaultPriceFeed,
address token,
address priceFeed,
uint256 priceDecimals,
bool isStrictStable
);
event ClearAction(bytes32 action);
modifier onlyAdmin() {
require(msg.sender == admin, "Timelock: forbidden");
_;
}
modifier onlyHandlerAndAbove() {
require(msg.sender == admin || isHandler[msg.sender], "Timelock: forbidden");
_;
}
modifier onlyKeeperAndAbove() {
require(msg.sender == admin || isHandler[msg.sender] || isKeeper[msg.sender], "Timelock: forbidden");
_;
}
modifier onlyTokenManager() {
require(msg.sender == tokenManager, "Timelock: forbidden");
_;
}
constructor(
address _admin,
uint256 _buffer,
address _tokenManager
) public {
require(_buffer <= MAX_BUFFER, "Timelock: invalid _buffer");
admin = _admin;
buffer = _buffer;
tokenManager = _tokenManager;
}
function setAdmin(address _admin) external onlyTokenManager {
admin = _admin;
}
function setExternalAdmin(address _target, address _admin) external onlyAdmin {
require(_target != address(this), "Timelock: invalid _target");
IAdmin(_target).setAdmin(_admin);
}
function setContractHandler(address _handler, bool _isActive) external onlyAdmin {
isHandler[_handler] = _isActive;
}
function setKeeper(address _keeper, bool _isActive) external onlyAdmin {
isKeeper[_keeper] = _isActive;
}
function setBuffer(uint256 _buffer) external onlyAdmin {
require(_buffer <= MAX_BUFFER, "Timelock: invalid _buffer");
// require(_buffer > buffer, "Timelock: buffer cannot be decreased");
buffer = _buffer;
}
function setIsAmmEnabled(address _priceFeed, bool _isEnabled) external onlyAdmin {
IVaultPriceFeed(_priceFeed).setIsAmmEnabled(_isEnabled);
}
function setIsSecondaryPriceEnabled(address _priceFeed, bool _isEnabled) external onlyAdmin {
IVaultPriceFeed(_priceFeed).setIsSecondaryPriceEnabled(_isEnabled);
}
function setMaxStrictPriceDeviation(address _priceFeed, uint256 _maxStrictPriceDeviation) external onlyAdmin {
IVaultPriceFeed(_priceFeed).setMaxStrictPriceDeviation(_maxStrictPriceDeviation);
}
function setUseV2Pricing(address _priceFeed, bool _useV2Pricing) external onlyAdmin {
IVaultPriceFeed(_priceFeed).setUseV2Pricing(_useV2Pricing);
}
function setAdjustment(address _priceFeed, address _token, bool _isAdditive, uint256 _adjustmentBps) external onlyKeeperAndAbove {
IVaultPriceFeed(_priceFeed).setAdjustment(_token, _isAdditive, _adjustmentBps);
}
function setSpreadBasisPoints(address _priceFeed, address _token, uint256 _spreadBasisPoints) external onlyKeeperAndAbove {
IVaultPriceFeed(_priceFeed).setSpreadBasisPoints(_token, _spreadBasisPoints);
}
function setPriceSampleSpace(address _priceFeed,uint256 _priceSampleSpace) external onlyHandlerAndAbove {
require(_priceSampleSpace <= 5, "Invalid _priceSampleSpace");
IVaultPriceFeed(_priceFeed).setPriceSampleSpace(_priceSampleSpace);
}
function setVaultPriceFeed(address _fastPriceFeed, address _vaultPriceFeed) external onlyAdmin {
IFastPriceFeed(_fastPriceFeed).setVaultPriceFeed(_vaultPriceFeed);
}
function setPriceDuration(address _fastPriceFeed, uint256 _priceDuration) external onlyHandlerAndAbove {
IFastPriceFeed(_fastPriceFeed).setPriceDuration(_priceDuration);
}
function setMaxPriceUpdateDelay(address _fastPriceFeed, uint256 _maxPriceUpdateDelay) external onlyHandlerAndAbove {
IFastPriceFeed(_fastPriceFeed).setMaxPriceUpdateDelay(_maxPriceUpdateDelay);
}
function setSpreadBasisPointsIfInactive(address _fastPriceFeed, uint256 _spreadBasisPointsIfInactive) external onlyAdmin {
IFastPriceFeed(_fastPriceFeed).setSpreadBasisPointsIfInactive(_spreadBasisPointsIfInactive);
}
function setSpreadBasisPointsIfChainError(address _fastPriceFeed, uint256 _spreadBasisPointsIfChainError) external onlyAdmin {
IFastPriceFeed(_fastPriceFeed).setSpreadBasisPointsIfChainError(_spreadBasisPointsIfChainError);
}
function setMinBlockInterval(address _fastPriceFeed, uint256 _minBlockInterval) external onlyAdmin {
IFastPriceFeed(_fastPriceFeed).setMinBlockInterval(_minBlockInterval);
}
function setIsSpreadEnabled(address _fastPriceFeed, bool _isSpreadEnabled) external onlyAdmin {
IFastPriceFeed(_fastPriceFeed).setIsSpreadEnabled(_isSpreadEnabled);
}
function transferIn(address _sender, address _token, uint256 _amount) external onlyAdmin {
IERC20(_token).transferFrom(_sender, address(this), _amount);
}
function signalApprove(address _token, address _spender, uint256 _amount) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("approve", _token, _spender, _amount));
_setPendingAction(action);
emit SignalApprove(_token, _spender, _amount, action);
}
function approve(address _token, address _spender, uint256 _amount) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("approve", _token, _spender, _amount));
_validateAction(action);
_clearAction(action);
IERC20(_token).approve(_spender, _amount);
}
function signalWithdrawToken(address _target, address _token, address _receiver, uint256 _amount) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("withdrawToken", _target, _token, _receiver, _amount));
_setPendingAction(action);
emit SignalWithdrawToken(_target, _token, _receiver, _amount, action);
}
function withdrawToken(address _target, address _token, address _receiver, uint256 _amount) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("withdrawToken", _target, _token, _receiver, _amount));
_validateAction(action);
_clearAction(action);
IBaseToken(_target).withdrawToken(_token, _receiver, _amount);
}
function signalSetGov(address _target, address _gov) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("setGov", _target, _gov));
_setPendingAction(action);
emit SignalSetGov(_target, _gov, action);
}
function setGov(address _target, address _gov) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("setGov", _target, _gov));
_validateAction(action);
_clearAction(action);
ITimelockTarget(_target).setGov(_gov);
}
function signalSetPriceFeedWatcher(address _fastPriceFeed, address _account, bool _isActive) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("setPriceFeedWatcher", _fastPriceFeed, _account, _isActive));
_setPendingAction(action);
emit SignalSetPriceFeedWatcher(_fastPriceFeed, _account, _isActive);
}
function setPriceFeedWatcher(address _fastPriceFeed, address _account, bool _isActive) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("setPriceFeedWatcher", _fastPriceFeed, _account, _isActive));
_validateAction(action);
_clearAction(action);
IFastPriceFeed(_fastPriceFeed).setSigner(_account, _isActive);
}
function signalSetPriceFeedUpdater(address _fastPriceFeed, address _account, bool _isActive) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("setPriceFeedUpdater", _fastPriceFeed, _account, _isActive));
_setPendingAction(action);
emit SignalSetPriceFeedWatcher(_fastPriceFeed, _account, _isActive);
}
function setPriceFeedUpdater(address _fastPriceFeed, address _account, bool _isActive) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked("setPriceFeedUpdater", _fastPriceFeed, _account, _isActive));
_validateAction(action);
_clearAction(action);
IFastPriceFeed(_fastPriceFeed).setUpdater(_account, _isActive);
}
function signalPriceFeedSetTokenConfig(
address _vaultPriceFeed,
address _token,
address _priceFeed,
uint256 _priceDecimals,
bool _isStrictStable
) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked(
"priceFeedSetTokenConfig",
_vaultPriceFeed,
_token,
_priceFeed,
_priceDecimals,
_isStrictStable
));
_setPendingAction(action);
emit SignalPriceFeedSetTokenConfig(
_vaultPriceFeed,
_token,
_priceFeed,
_priceDecimals,
_isStrictStable
);
}
function priceFeedSetTokenConfig(
address _vaultPriceFeed,
address _token,
address _priceFeed,
uint256 _priceDecimals,
bool _isStrictStable
) external onlyAdmin {
bytes32 action = keccak256(abi.encodePacked(
"priceFeedSetTokenConfig",
_vaultPriceFeed,
_token,
_priceFeed,
_priceDecimals,
_isStrictStable
));
_validateAction(action);
_clearAction(action);
IVaultPriceFeed(_vaultPriceFeed).setTokenConfig(
_token,
_priceFeed,
_priceDecimals,
_isStrictStable
);
}
function cancelAction(bytes32 _action) external onlyAdmin {
_clearAction(_action);
}
function _setPendingAction(bytes32 _action) private {
pendingActions[_action] = block.timestamp.add(buffer);
emit SignalPendingAction(_action);
}
function _validateAction(bytes32 _action) private view {
require(pendingActions[_action] != 0, "Timelock: action not signalled");
require(pendingActions[_action] < block.timestamp, "Timelock: action time not yet passed");
}
function _clearAction(bytes32 _action) private {
require(pendingActions[_action] != 0, "Timelock: invalid _action");
delete pendingActions[_action];
emit ClearAction(_action);
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IReferralStorage {
function codeOwners(bytes32 _code) external view returns (address);
function traderReferralCodes(address _account) external view returns (bytes32);
function referrerDiscountShares(address _account) external view returns (uint256);
function referrerTiers(address _account) external view returns (uint256);
function getTraderReferralInfo(address _account) external view returns (bytes32, address);
function setTraderReferralCode(address _account, bytes32 _code) external;
function setTier(uint256 _tierId, uint256 _totalRebate, uint256 _discountShare) external;
function setReferrerTier(address _referrer, uint256 _tierId) external;
function govSetCodeOwner(bytes32 _code, address _newAccount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IVester {
function rewardTracker() external view returns (address);
function claimForAccount(address _account, address _receiver) external returns (uint256);
function claimable(address _account) external view returns (uint256);
function cumulativeClaimAmounts(address _account) external view returns (uint256);
function claimedAmounts(address _account) external view returns (uint256);
function pairAmounts(address _account) external view returns (uint256);
function getVestedAmount(address _account) external view returns (uint256);
function transferredAverageStakedAmounts(address _account) external view returns (uint256);
function transferredCumulativeRewards(address _account) external view returns (uint256);
function cumulativeRewardDeductions(address _account) external view returns (uint256);
function bonusRewards(address _account) external view returns (uint256);
function transferStakeValues(address _sender, address _receiver) external;
function setTransferredAverageStakedAmounts(address _account, uint256 _amount) external;
function setTransferredCumulativeRewards(address _account, uint256 _amount) external;
function setCumulativeRewardDeductions(address _account, uint256 _amount) external;
function setBonusRewards(address _account, uint256 _amount) external;
function getMaxVestableAmount(address _account) external view returns (uint256);
function getCombinedAverageStakedAmount(address _account) external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IBaseToken {
function totalStaked() external view returns (uint256);
function stakedBalance(address _account) external view returns (uint256);
function removeAdmin(address _account) external;
function setInPrivateTransferMode(bool _inPrivateTransferMode) external;
function withdrawToken(address _token, address _account, uint256 _amount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IMintable {
function isMinter(address _account) external returns (bool);
function setMinter(address _minter, bool _isActive) external;
function mint(address _account, uint256 _amount) external;
function burn(address _account, uint256 _amount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IUSDG {
function addVault(address _vault) external;
function removeVault(address _vault) external;
function mint(address _account, uint256 _amount) external;
function burn(address _account, uint256 _amount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
interface IYieldToken {
function totalStaked() external view returns (uint256);
function stakedBalance(address _account) external view returns (uint256);
function removeAdmin(address _account) external;
}