Contract Name:
PythAggregatorV3EmergencyFeed
Contract Source Code:
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;
import "./PythStructs.sol";
import "./IPythEvents.sol";
/// @title Consume prices from the Pyth Network (https://pyth.network/).
/// @dev Please refer to the guidance at https://docs.pyth.network/documentation/pythnet-price-feeds/best-practices for how to consume prices safely.
/// @author Pyth Data Association
interface IPyth is IPythEvents {
/// @notice Returns the price of a price feed without any sanity checks.
/// @dev This function returns the most recent price update in this contract without any recency checks.
/// This function is unsafe as the returned price update may be arbitrarily far in the past.
///
/// Users of this function should check the `publishTime` in the price to ensure that the returned price is
/// sufficiently recent for their application. If you are considering using this function, it may be
/// safer / easier to use `getPriceNoOlderThan`.
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
function getPriceUnsafe(
bytes32 id
) external view returns (PythStructs.Price memory price);
/// @notice Returns the price that is no older than `age` seconds of the current time.
/// @dev This function is a sanity-checked version of `getPriceUnsafe` which is useful in
/// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
/// recently.
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
function getPriceNoOlderThan(
bytes32 id,
uint age
) external view returns (PythStructs.Price memory price);
/// @notice Returns the exponentially-weighted moving average price of a price feed without any sanity checks.
/// @dev This function returns the same price as `getEmaPrice` in the case where the price is available.
/// However, if the price is not recent this function returns the latest available price.
///
/// The returned price can be from arbitrarily far in the past; this function makes no guarantees that
/// the returned price is recent or useful for any particular application.
///
/// Users of this function should check the `publishTime` in the price to ensure that the returned price is
/// sufficiently recent for their application. If you are considering using this function, it may be
/// safer / easier to use either `getEmaPrice` or `getEmaPriceNoOlderThan`.
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
function getEmaPriceUnsafe(
bytes32 id
) external view returns (PythStructs.Price memory price);
/// @notice Returns the exponentially-weighted moving average price that is no older than `age` seconds
/// of the current time.
/// @dev This function is a sanity-checked version of `getEmaPriceUnsafe` which is useful in
/// applications that require a sufficiently-recent price. Reverts if the price wasn't updated sufficiently
/// recently.
/// @return price - please read the documentation of PythStructs.Price to understand how to use this safely.
function getEmaPriceNoOlderThan(
bytes32 id,
uint age
) external view returns (PythStructs.Price memory price);
/// @notice Update price feeds with given update messages.
/// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
/// `getUpdateFee` with the length of the `updateData` array.
/// Prices will be updated if they are more recent than the current stored prices.
/// The call will succeed even if the update is not the most recent.
/// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid.
/// @param updateData Array of price update data.
function updatePriceFeeds(bytes[] calldata updateData) external payable;
/// @notice Wrapper around updatePriceFeeds that rejects fast if a price update is not necessary. A price update is
/// necessary if the current on-chain publishTime is older than the given publishTime. It relies solely on the
/// given `publishTimes` for the price feeds and does not read the actual price update publish time within `updateData`.
///
/// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
/// `getUpdateFee` with the length of the `updateData` array.
///
/// `priceIds` and `publishTimes` are two arrays with the same size that correspond to senders known publishTime
/// of each priceId when calling this method. If all of price feeds within `priceIds` have updated and have
/// a newer or equal publish time than the given publish time, it will reject the transaction to save gas.
/// Otherwise, it calls updatePriceFeeds method to update the prices.
///
/// @dev Reverts if update is not needed or the transferred fee is not sufficient or the updateData is invalid.
/// @param updateData Array of price update data.
/// @param priceIds Array of price ids.
/// @param publishTimes Array of publishTimes. `publishTimes[i]` corresponds to known `publishTime` of `priceIds[i]`
function updatePriceFeedsIfNecessary(
bytes[] calldata updateData,
bytes32[] calldata priceIds,
uint64[] calldata publishTimes
) external payable;
/// @notice Returns the required fee to update an array of price updates.
/// @param updateData Array of price update data.
/// @return feeAmount The required fee in Wei.
function getUpdateFee(
bytes[] calldata updateData
) external view returns (uint feeAmount);
/// @notice Parse `updateData` and return price feeds of the given `priceIds` if they are all published
/// within `minPublishTime` and `maxPublishTime`.
///
/// You can use this method if you want to use a Pyth price at a fixed time and not the most recent price;
/// otherwise, please consider using `updatePriceFeeds`. This method may store the price updates on-chain, if they
/// are more recent than the current stored prices.
///
/// This method requires the caller to pay a fee in wei; the required fee can be computed by calling
/// `getUpdateFee` with the length of the `updateData` array.
///
///
/// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
/// no update for any of the given `priceIds` within the given time range.
/// @param updateData Array of price update data.
/// @param priceIds Array of price ids.
/// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
/// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
/// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
function parsePriceFeedUpdates(
bytes[] calldata updateData,
bytes32[] calldata priceIds,
uint64 minPublishTime,
uint64 maxPublishTime
) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
/// @notice Similar to `parsePriceFeedUpdates` but ensures the updates returned are
/// the first updates published in minPublishTime. That is, if there are multiple updates for a given timestamp,
/// this method will return the first update. This method may store the price updates on-chain, if they
/// are more recent than the current stored prices.
///
///
/// @dev Reverts if the transferred fee is not sufficient or the updateData is invalid or there is
/// no update for any of the given `priceIds` within the given time range and uniqueness condition.
/// @param updateData Array of price update data.
/// @param priceIds Array of price ids.
/// @param minPublishTime minimum acceptable publishTime for the given `priceIds`.
/// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
/// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
function parsePriceFeedUpdatesUnique(
bytes[] calldata updateData,
bytes32[] calldata priceIds,
uint64 minPublishTime,
uint64 maxPublishTime
) external payable returns (PythStructs.PriceFeed[] memory priceFeeds);
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;
/// @title IPythEvents contains the events that Pyth contract emits.
/// @dev This interface can be used for listening to the updates for off-chain and testing purposes.
interface IPythEvents {
/// @dev Emitted when the price feed with `id` has received a fresh update.
/// @param id The Pyth Price Feed ID.
/// @param publishTime Publish time of the given price update.
/// @param price Price of the given price update.
/// @param conf Confidence interval of the given price update.
event PriceFeedUpdate(
bytes32 indexed id,
uint64 publishTime,
int64 price,
uint64 conf
);
}
// SPDX-License-Identifier: Apache 2
pragma solidity 0.8.20;
import {PythStructs} from "./PythStructs.sol";
import {IPyth} from "./IPyth.sol";
// This interface is forked from the Zerolend Adapter found here:
// https://github.com/zerolend/pyth-oracles/blob/master/contracts/PythAggregatorV3.sol
// Original license found under licenses/zerolend-pyth-oracles.md
/**
* @title A port of the ChainlinkAggregatorV3 interface that supports Pyth price feeds
* @notice This does not store any roundId information on-chain. Please review the code before using this implementation.
* Users should deploy an instance of this contract to wrap every price feed id that they need to use.
* @notice This version was modified by fMoney (Nikar0) to use in the event of a stable wrapper losing peg on-chain vs it's price feed.
* This gives the protocol a chance to protect against bad debt and maintain stability in the platform.
* Ideal use: Deploy secondary Lender Oracle pointing to the same feeds as primary but replace possibly at risk stable asset's PythAggregator to Emergency version.
* In the event of a depeg, switch Oracle to secondary & manually serve on-chain price for affected asset(s) until event is resolved, then switch Oracle back to main.
* For security reasons the Emergency version should NOT be the default feed for an asset as admin or guardian could maliciously tank price & cause forced liquidations.
* It's a temporary last resort mechanism for a chance to protect the protocol from bad debt and survive in black swan events.
*/
contract PythAggregatorV3EmergencyFeed {
// New events
event depegSet(int depegPrice, bool isActive);
event AdminSet(address indexed previousAdmin, address indexed newAdmin);
event DepegGuardianSet(address indexed previousGuardian, address indexed newGuardian);
// New variables
address public admin; // Multisig
address public depegGuardian;
int256 public depegPrice;
bool public isActive;
bytes32 public priceId;
IPyth public pyth;
constructor(address _pyth, bytes32 _priceId, address _admin, address _depegGuardian) {
priceId = _priceId;
pyth = IPyth(_pyth);
admin = _admin;
depegGuardian = _depegGuardian;
}
// Wrapper function to update the underlying Pyth price feeds. Not part of the AggregatorV3 interface but useful.
function updateFeeds(bytes[] calldata priceUpdateData) public payable {
// Update the prices to the latest available values and pay the required fee for it. The `priceUpdateData` data
// should be retrieved from our off-chain Price Service API using the `pyth-evm-js` package.
// See section "How Pyth Works on EVM Chains" below for more information.
uint fee = pyth.getUpdateFee(priceUpdateData);
pyth.updatePriceFeeds{value: fee}(priceUpdateData);
// refund remaining eth
payable(msg.sender).call{value: address(this).balance}("");
}
function decimals() public view virtual returns (uint8) {
PythStructs.Price memory price = pyth.getPriceUnsafe(priceId);
return uint8(-1 * int8(price.expo));
}
function description() public pure returns (string memory) {
return "Modified emergency feed for Stable (USDC) feed. Enables manual price serving in the event of an on-chain depeg vs oracle feed.";
}
function version() public pure returns (uint256) {
return 1;
}
function latestAnswer() public view virtual returns (int256) {
PythStructs.Price memory price = pyth.getPriceUnsafe(priceId);
return int256(price.price);
}
function latestTimestamp() public view returns (uint256) {
PythStructs.Price memory price = pyth.getPriceUnsafe(priceId);
return price.publishTime;
}
function latestRound() public view returns (uint256) {
// use timestamp as the round id
return latestTimestamp();
}
function getAnswer(uint256) public view returns (int256) {
return latestAnswer();
}
function getTimestamp(uint256) external view returns (uint256) {
return latestTimestamp();
}
function getRoundData(
uint80 _roundId
)
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
PythStructs.Price memory price = pyth.getPriceUnsafe(priceId);
return (
_roundId,
int256(price.price),
price.publishTime,
price.publishTime,
_roundId
);
}
function latestRoundData()
external
view
returns (
uint80 roundId,
int256 answer,
uint256 startedAt,
uint256 updatedAt,
uint80 answeredInRound
)
{
PythStructs.Price memory price = pyth.getPriceUnsafe(priceId);
roundId = uint80(price.publishTime);
int256 finalPrice;
if(!isActive){
finalPrice = int256(price.price);
} else {
finalPrice = depegPrice;
}
return (
roundId,
finalPrice,
price.publishTime,
price.publishTime,
roundId
);
}
// Manually sets price in the event of an on-chain depeg vs feed.
function depegSetter(int _price, bool _isActive) external {
require(msg.sender == admin || msg.sender == depegGuardian, "!Auth");
if(_price == 0){
isActive = false;
depegPrice = previewDepegPrice(_price);
emit depegSet(depegPrice, isActive);
} else {
depegPrice = previewDepegPrice(_price);
if(_isActive != isActive){
isActive = _isActive;
}
emit depegSet(depegPrice, isActive);
}
}
// Helper function to preview price output on depegSetter
function previewDepegPrice(int _price) public pure returns(int){
if(_price > 1e4 || _price == 0){return 1e8;}
return _price * 1e4;
}
// Assigns admin address with access to all gated functions
function setAdmin(address _newAdmin) external {
require(msg.sender == admin, "!Admin");
require(_newAdmin != address(0), "0x00");
if(_newAdmin != admin){
emit AdminSet(admin, _newAdmin);
admin = _newAdmin;
}
}
// Assigns depegGuardian address, can only access depegSetter.
function setDepegGuardian(address _newGuardian) external{
require(msg.sender == admin, "!Admin");
require(_newGuardian != address(0), "0x00");
if(_newGuardian != depegGuardian){
emit DepegGuardianSet(depegGuardian, _newGuardian);
depegGuardian = _newGuardian;
}
}
}
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.20;
contract PythStructs {
// A price with a degree of uncertainty, represented as a price +- a confidence interval.
//
// The confidence interval roughly corresponds to the standard error of a normal distribution.
// Both the price and confidence are stored in a fixed-point numeric representation,
// `x * (10^expo)`, where `expo` is the exponent.
//
// Please refer to the documentation at https://docs.pyth.network/documentation/pythnet-price-feeds/best-practices for how
// to how this price safely.
struct Price {
// Price
int64 price;
// Confidence interval around the price
uint64 conf;
// Price exponent
int32 expo;
// Unix timestamp describing when the price was published
uint publishTime;
}
// PriceFeed represents a current aggregate price from pyth publisher feeds.
struct PriceFeed {
// The price ID.
bytes32 id;
// Latest available price
Price price;
// Latest available exponentially-weighted moving average price
Price emaPrice;
}
}