Verified contractExactMIT
FeedSwitchV2
Address0x3326a10a83b77faae29aedbb8aaeb18e5872624d
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.20
- Same code
Only this contract
Details
- Compiler
- solc 0.8.20+commit.a1b79de6
- Language
- solidity
- Optimization
- Yes, 10,000 runs
- EVM version
- shanghai
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 24,284,476
- Creation transaction
- 0x938b30fd37…3d6ab4fe4a
2 files
- src/util/FeedSwitchV2.solmain5.76 KB
- src/interfaces/IChainlinkFeed.sol861 B
src/util/FeedSwitchV2.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import "src/interfaces/IChainlinkFeed.sol";
/// @title FeedSwitch
/// @notice A contract to switch between feeds after a timelock period
/// @dev The switch can only be initiated by the guardian and will be effective after the timelock period has passed, which could be zero.
/// If the switch is initiated but not yet effective, it can be cancelled by the guardian without timelock period (if is not zero).
/// The guardian can initiate the switch again to the previous feed multiple times.
contract FeedSwitchV2 {
error NotGuardian();
error NotGov();
error NotPendingGov();
error FeedDecimalsMismatch();
address public pendingGov;
address public gov;
uint256 public timelockPeriod;
IChainlinkFeed public feed;
IChainlinkFeed public previousFeed;
uint256 public switchCompletedAt;
mapping(address => bool) public isGuardian;
IChainlinkFeed public immutable initialFeed;
IChainlinkFeed public immutable fallbackFeed;
event FeedSwitchInitiated(address indexed newFeed, uint256 effectiveAt);
event NewPendingGov(address indexed pendingGov);
event GovChanged(address indexed newGov);
event GuardianSet(address indexed guardian, bool isGuardian);
event TimelockPeriodChanged(uint256 newTimelockPeriod);
constructor(
address _initialFeed,
address _fallbackFeed,
uint256 _timelockPeriod,
address _gov,
address _guardian
) {
feed = IChainlinkFeed(_initialFeed);
initialFeed = IChainlinkFeed(_initialFeed);
fallbackFeed = IChainlinkFeed(_fallbackFeed);
if (
fallbackFeed.decimals() != 18 ||
feed.decimals() != 18
) revert FeedDecimalsMismatch();
timelockPeriod = _timelockPeriod;
gov = _gov;
isGuardian[_guardian] = true;
}
modifier onlyGov() {
if (msg.sender != gov) revert NotGov();
_;
}
/// @notice Toggle the feed switch, entering or exiting the timelock period
/// @dev Can only be called by the guardian
function toggleFeedSwitch() external {
if (!isGuardian[msg.sender]) revert NotGuardian();
if (switchCompletedAt < block.timestamp) {
switchCompletedAt = block.timestamp + timelockPeriod;
} else switchCompletedAt = 0;
if (address(feed) == address(initialFeed)) {
feed = fallbackFeed;
previousFeed = initialFeed;
} else {
feed = initialFeed;
previousFeed = fallbackFeed;
}
emit FeedSwitchInitiated(address(feed), switchCompletedAt > 0 ? switchCompletedAt : block.timestamp);
}
/// @notice Get the current feed data
/// @return roundId The round ID
/// @return price The price of the asset
/// @return startedAt The timestamp of the start of the round
/// @return updatedAt The timestamp of the last update
/// @return answeredInRound The round ID in which the price was answered
function latestRoundData()
public
view
returns (
uint80 roundId,
int256 price,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
if (block.timestamp >= switchCompletedAt) {
return feed.latestRoundData();
} else {
return previousFeed.latestRoundData();
}
}
/// @notice Get the latest price of the asset
/// @return price The price of the asset
function latestAnswer() external view returns (int256) {
(, int256 latestPrice, , , ) = latestRoundData();
return latestPrice;
}
/// @notice Get the number of decimals of the feed
/// @return decimals The number of decimals
function decimals() external pure returns (uint8) {
return 18;
}
/// @notice Check if the feed switch is queued
/// @dev If not queued, will return 0 as time left
/// @return timeLeft The time left for the switch to be effective
function isFeedSwitchQueued() external view returns (uint256) {
bool isQueued = block.timestamp < switchCompletedAt;
if (!isQueued) return 0;
else return switchCompletedAt - block.timestamp;
}
/// @notice Set a new pending governance
/// @dev Can only be called by the current governance, the new pending governance must call acceptGov to become the new governance
/// @param _pendingGov The address of the new pending governance
function setPendingGov(address _pendingGov) external onlyGov {
pendingGov = _pendingGov;
emit NewPendingGov(_pendingGov);
}
/// @notice Accept the governance role
/// @dev Can only be called by the pending governance
function acceptGov() external {
if (msg.sender != pendingGov) revert NotPendingGov();
gov = pendingGov;
pendingGov = address(0);
emit GovChanged(gov);
}
/// @notice Set the guardian role for an address
/// @dev Can only be called by the governance
/// @param guardianAddr The address to set the guardian role for
/// @param _isGuardian Whether the address should be a guardian
function setGuardian(address guardianAddr, bool _isGuardian) external onlyGov {
isGuardian[guardianAddr] = _isGuardian;
emit GuardianSet(guardianAddr, _isGuardian);
}
/// @notice Set a new timelock period
/// @dev Can only be called by the governance
/// @param _timelockPeriod The new timelock period in seconds
function setTimelockPeriod(uint256 _timelockPeriod) external onlyGov {
timelockPeriod = _timelockPeriod;
emit TimelockPeriodChanged(_timelockPeriod);
}
}
src/interfaces/IChainlinkFeed.sol
pragma solidity ^0.8.13;
interface IChainlinkFeed {
function aggregator() external view returns (address aggregator);
function decimals() external view returns (uint8 decimals);
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 crvUsdPrice,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
);
function latestAnswer() external view returns (int256 price);
function description() external view returns (string memory description);
}
interface IChainlinkBasePriceFeed is IChainlinkFeed {
function assetToUsd() external view returns (IChainlinkFeed);
function assetToUsdFallback() external view returns (IChainlinkFeed);
function assetToUsdHeartbeat() external view returns (uint256 heartbeat);
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 10000,
"enabled": true
},
"evmVersion": "shanghai",
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"ds-test/=lib/forge-std/lib/ds-test/src/",
"erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
"forge-std/=lib/forge-std/src/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/",
"solmate/=lib/solmate/src/",
"bytes-utils/=lib/bytes-utils/contracts/",
"halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.