getEmaPriceUnsafe
Get the last updated exponentially weighted moving average (EMA) price object for the requested price feed ID. Caution: This function may return a price arbitrarily in the past
Description
This method returns the price object containing last updated exponentially-weighted moving average(EMA) price for the requested price feed ID.
This function may return a price from arbitrarily far in the past. It is the
caller's responsibility to check the returned publishTime
to ensure that the
update is recent enough for their use case. If you need the latest price, update the price using updatePriceFeeds()
and then call getEmaPrice()
.
The price object contains the following fields:
price
: The latest price of the price feed.conf
: The confidence level of the price feed.expo
: The exponent of the price feed.publishtime
: The time when the price feed was last updated.
Sample price
object:
{ price: 123456789n, conf: 180726074n, expo: -8, publishTime: 1721765108n}
The price
above is in the format of price * 10^expo
. So, the price
in above
mentioned sample represents the number 123456789 * 10(-8) = 1.23456789
in
this case.
Error Response
The above method can return the following error response:
StalePrice
: The on-chain price has not been updated within the lastgetValidTimePeriod()
seconds. Try callingupdatePriceFeeds()
to update the price feed with the latest price.PriceFeedNotFound
: The requested price feed has never received a price update or does not exist. Try callingupdatePriceFeeds()
to update the price feed.
Arguments
The ID of the price feed you want to read
import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";// address contractAddress = IPyth pyth = IPyth(contractAddress);bytes32 priceId = /* <id> */;PythStructs.Price memory currentBasePrice = pyth.getEmaPriceUnsafe(priceId);