get-ema-price.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { readApi, solidity, ethersJS } from "./common";
  2. import { ParameterType } from "../../components/EvmApi";
  3. export const getEmaPrice = readApi<"id">({
  4. name: "getEmaPrice",
  5. summary:
  6. "Get the **latest** exponentially weighted moving average (EMA) price object for the requested price feed ID.",
  7. description: `
  8. This method returns the latest price object containing **exponentially-weighted moving average** price for the requested price feed ID.
  9. The \`price\` object contains the following fields:
  10. 1. \`price\`: The latest **EMA** price of the price feed.
  11. 2. \`conf\`: The confidence level of the price feed.
  12. 3. \`expo\`: The exponent of the price feed.
  13. 4. \`publishtime\`: The time when the price feed was last updated.
  14. Sample \`price\` object:
  15. \`\`\`json
  16. {
  17. price: 123456789,
  18. conf: 180726074,
  19. expo: -8,
  20. publishTime: 1721765108
  21. }
  22. \`\`\`
  23. The price above is in the format of \`price * 10^expo\`. So, the price in above
  24. mentioned sample represents the number \`123456789 * 10(-8) = 1.23456789\` in
  25. this case.
  26. ### Error Response
  27. The above method can return the following error response:
  28. - \`StalePrice\`: The on-chain price has not been updated within the last
  29. [\`getValidTimePeriod()\`](getValidTimePeriod) seconds. Try calling
  30. [\`updatePriceFeeds()\`](updatePriceFeeds) to update the price feed with the
  31. latest price.
  32. - \`PriceFeedNotFound\`: The requested price feed has never received a price
  33. update or does not exist. Try calling
  34. [\`updatePriceFeeds()\`](updatePriceFeeds) to update the price feed.
  35. `,
  36. parameters: [
  37. {
  38. name: "id",
  39. type: ParameterType.PriceFeedId,
  40. description: "The ID of the price feed you want to read",
  41. },
  42. ],
  43. code: [
  44. solidity(
  45. ({ id }) => `
  46. bytes32 priceId = ${id ?? "/* <id> */"};
  47. PythStructs.Price memory currentBasePrice = pyth.getEmaPrice(priceId);
  48. `,
  49. ),
  50. ethersJS(
  51. ({ id }) => `
  52. const priceId = ${id ? `'${id}'` : "/* <id> */"};
  53. const [price, conf, expo, timestamp] = await contract.getEmaPrice(priceId);
  54. `,
  55. ),
  56. ],
  57. });