| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import { readApi, solidity, ethersJS } from "./common";
- import { ParameterType } from "../../components/EvmApi";
- export const getPrice = readApi<"id">({
- name: "getPrice",
- summary: "Get the **latest** price object for the requested price feed ID.",
- description: `
- This method returns the latest price object for the requested price feed ID.
- The \`Price\` object contains the following fields:
- 1. \`price\`: The price of the asset.
- 2. \`conf\`: The confidence level of the price.
- 3. \`expo\`: The exponent of the price.
- 4. \`publishTime\`: The timestamp of the price update.
- Sample \`Price\` object:
- \`\`\`json
- {
- "price": "1234",
- "conf": "1000",
- "expo": "-2",
- "publishTime": 1626892800
- }
- \`\`\`
- Get the latest price and confidence interval for the requested price feed id.
- The price feed id is a 32-byte id written as a hexadecimal string; see the
- [price feed ids](https://pyth.network/developers/price-feed-ids) page to look up
- the id for a given symbol. The returned price and confidence are decimal numbers
- written in the form \`a * 10^e\`, where \`e\` is an exponent included in the
- result. For example, a price of 1234 with an exponent of -2 represents the
- number 12.34. The result also includes a \`publishTime\` which is the unix
- timestamp for the price update.
- This function reverts with a \`StalePrice\` error if the on-chain price has not
- been updated within the last [getValidTimePeriod()](getValidTimePeriod)
- seconds. The default valid time period is set to a reasonable default on each
- chain and is typically around 1 minute. Call
- [updatePriceFeeds](updatePriceFeeds) to pull a fresh price on-chain and solve
- this problem. If you would like to configure the valid time period, see
- [getPriceNoOlderThan](getPriceNoOlderThan). If you want the latest price
- regardless of when it was updated, see [getPriceUnsafe](getPriceUnsafe).
- This function reverts with a \`PriceFeedNotFound\` error if the requested feed
- id has never received a price update. This error could either mean that the
- provided price feed id is incorrect, or (more typically) that this is the first
- attempted use of that feed on-chain. In the second case, calling
- [updatePriceFeeds](updatePriceFeeds) will solve this problem.
- `,
- parameters: [
- {
- name: "id",
- type: ParameterType.PriceFeedId,
- description: "The ID of the price feed you want to read",
- },
- ],
- code: [
- solidity(
- ({ id }) => `
- bytes32 priceId = ${id ?? "/* <id> */"};
- PythStructs.Price memory currentBasePrice = pyth.getPrice(priceId);
- `,
- ),
- ethersJS(
- ({ id }) => `
- const priceId = ${id ? `'${id}'` : "/* <id> */"};
- const [price, conf, expo, timestamp] = await contract.getPrice(priceId);
- `,
- ),
- ],
- });
|