get-price.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { readApi, solidity, ethersJS } from "./common";
  2. import { ParameterType } from "../../components/EvmApi";
  3. export const getPrice = readApi<"id">({
  4. name: "getPrice",
  5. summary: "Get the **latest** price object for the requested price feed ID.",
  6. description: `
  7. This method returns the latest price object for the requested price feed ID.
  8. The \`Price\` object contains the following fields:
  9. 1. \`price\`: The price of the asset.
  10. 2. \`conf\`: The confidence level of the price.
  11. 3. \`expo\`: The exponent of the price.
  12. 4. \`publishTime\`: The timestamp of the price update.
  13. Sample \`Price\` object:
  14. \`\`\`json
  15. {
  16. "price": "1234",
  17. "conf": "1000",
  18. "expo": "-2",
  19. "publishTime": 1626892800
  20. }
  21. \`\`\`
  22. Get the latest price and confidence interval for the requested price feed id.
  23. The price feed id is a 32-byte id written as a hexadecimal string; see the
  24. [price feed ids](https://pyth.network/developers/price-feed-ids) page to look up
  25. the id for a given symbol. The returned price and confidence are decimal numbers
  26. written in the form \`a * 10^e\`, where \`e\` is an exponent included in the
  27. result. For example, a price of 1234 with an exponent of -2 represents the
  28. number 12.34. The result also includes a \`publishTime\` which is the unix
  29. timestamp for the price update.
  30. This function reverts with a \`StalePrice\` error if the on-chain price has not
  31. been updated within the last [getValidTimePeriod()](getValidTimePeriod)
  32. seconds. The default valid time period is set to a reasonable default on each
  33. chain and is typically around 1 minute. Call
  34. [updatePriceFeeds](updatePriceFeeds) to pull a fresh price on-chain and solve
  35. this problem. If you would like to configure the valid time period, see
  36. [getPriceNoOlderThan](getPriceNoOlderThan). If you want the latest price
  37. regardless of when it was updated, see [getPriceUnsafe](getPriceUnsafe).
  38. This function reverts with a \`PriceFeedNotFound\` error if the requested feed
  39. id has never received a price update. This error could either mean that the
  40. provided price feed id is incorrect, or (more typically) that this is the first
  41. attempted use of that feed on-chain. In the second case, calling
  42. [updatePriceFeeds](updatePriceFeeds) will solve this problem.
  43. `,
  44. parameters: [
  45. {
  46. name: "id",
  47. type: ParameterType.PriceFeedId,
  48. description: "The ID of the price feed you want to read",
  49. },
  50. ],
  51. code: [
  52. solidity(
  53. ({ id }) => `
  54. bytes32 priceId = ${id ?? "/* <id> */"};
  55. PythStructs.Price memory currentBasePrice = pyth.getPrice(priceId);
  56. `,
  57. ),
  58. ethersJS(
  59. ({ id }) => `
  60. const priceId = ${id ? `'${id}'` : "/* <id> */"};
  61. const [price, conf, expo, timestamp] = await contract.getPrice(priceId);
  62. `,
  63. ),
  64. ],
  65. });