-
Notifications
You must be signed in to change notification settings - Fork 31
feat:-add-fetch-update-data-guide #286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { Callout } from "nextra/components"; | ||
|
||
# How To Fetch Price Updates | ||
|
||
The following guide explains how to fetch price updates. | ||
Price updates can be submitted to the Pyth Price Feeds contract to update the on-chain price. | ||
Please see [What is a Pull Oracle?](/price-feeds/pull-updates) to learn more. | ||
|
||
Price updates are served from [Hermes](/price-feeds/how-pyth-works/hermes), which | ||
provides three different ways to fetch price updates: | ||
|
||
1. [REST API](#rest-api) | ||
1. [Streaming](#streaming) | ||
1. [SDK](#sdk) | ||
|
||
<Callout emoji="ℹ️"> | ||
Fetching a price from Hermes requires a price feed ID. This ID serves as a | ||
unique identifier for each price feed (e.g., BTC/USD). The complete list of | ||
Pyth price feed IDs is available at | ||
https://pyth.network/developers/price-feed-ids | ||
</Callout> | ||
|
||
## REST API | ||
|
||
Hermes exposes several endpoints to fetch the price updates. | ||
Use the `/v2/updates/price/latest` endpoint to fetch the latest price update for one or more feeds. | ||
This endpoint allows you to fetch the latest price updates for multiple feeds in a single request. | ||
For example, the following command retrieves the latest price updates for BTC/USD and ETH/USD: | ||
|
||
```bash copy | ||
curl -X 'GET' \ | ||
'https://hermes.pyth.network/v2/updates/price/latest?ids%5B%5D=0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43&ids%5B%5D=0xc96458d393fe9deb7a7d63a0ac41e2898a67a7750dbd166673279e06c868df0a' | ||
``` | ||
|
||
The output will be similar to the following containing the requested price update: | ||
|
||
```bash | ||
{ | ||
"binary": { | ||
"encoding": "hex", | ||
"data": [ | ||
"504e41550100000003b801000000040d00561f4ceb8ce5eb58adda318009817714a017b0db9a7f1ef57253c81d1984d8140cdee5c06925a1cbd7a2612211fddcd91008dd854444b513519a06fdc1a7b00101021612a8c846810b86a42eb3c9fc25ad9b1c5bbccf6bcd2df39fa83bfd580a58646d508fa28c4cecd8878eefaf964eca8de36031cad28b3c8a870a409a8b0a062d0003e8c8dd8bc33307235e3073e7a66af5087824628e8e6b4fa02df9e8fd1bf4757f28388255e1866b52edb0d8f604e97c6afcb05a33dce52b48dbdeeea85028e9ac0004460bf2bce4fd0f84961c20728aa48d35c35ca7347ad6229800312013e3645371016c837a779fe8c31e6e9b6d5cdeb41e6e215627d6a51e2bf8faaa7ddf25e0ec00065283785418ac10b5b7ee3eb5b753f7e319a6c9890180821c8c6b2b57912ec96d315c91ab544c330839ee1c23f3fefdd063cd36fa77dc06a84a566bf30d9f4c2d01075a1471c93ce6275e0319438f013058aeaf3c4029bab24f3bf8b89786992ff03513440d8a61c4a31b76ea14fd96ec010a52ce2aa6212783163532b6bee047d90b000a4ab3faa7466ed5a62402024f62e0f6ea10b5e44341bf1869dc0317091cbc38444c27c4e88f6d9a8f0c85355c341b108537f01e79363e5c27331e031cccec82bb000b56537c736f12f44027c86e16da23af8515535b7839ff9095a93db178450f954f10f7c7ea7b8132f1b75909aa996ea6fb661cc3bbf12624f88c646fe2f964279d000d1edd3f3b34464a2a103ae7b6a2705f385207d4a91d721a175157f9238d0a54dd1127286db3a13ff1df67f02f195b665c09e4301f9df8fcfd251fefba30d12190010e8814c0416af5a6c702fbdee573ea5819a8ec452db5d182fafc37b47c25937e1d6359b17ddedd696056732ae37a80463ce515b85744a5342999599a9bdcc75ea2010f5821c0d3a9887f0f5f1c59e5a7e62d98fa7f4cd737c44ce4d26f3b466ffe5b310fed58f2282d0449de119d8ad12788624e87aee0d5f49d8881eaa14b5f2d653f00117d01a5a41e69dc58ce406c51ba2254128faa522b69d7f8c8a2eeb862858469eb119c119439513a1e068ad8df5214b251c6b4ef74600366935bdb1b72244dc97200128e34e3a83b7382a8f498235a52efea79d0c9a4e1de30340d70b5e9d8013927be137185a5423ae05a743fd4c5d2374de5aba72d86f70b7bfb2eb88ef8df052e79016634f2f500000000001ae101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa7100000000033d2ada01415557560000000000084728a20000271034997d7ffc8a4495fd78fed48c2bf8d5e188d74e02005500e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b4300000595cfbc774800000000c3f8e497fffffff8000000006634f2f5000000006634f2f50000058adefc436000000000e212dff80ab660ac1113aacd22a0d1e837a9cbbb4316ccdd64272484102a99c1337c282e56981a68916d29df1d1716b33f63a3b78b8d7b88ab46652879685487e2520aed797adec6550872f99049088ffbde70766eadb20520615efadb40d92afc542d42a113e1ef3c2d2bb920002cfb5b0bb3452b59e5e59585e25964682084851e189cf74861cd010893df90a7718062c4b6e89f5bfa8269d152c6f58be368cc588af6575595bc376b65a0e7f916fc5eef915402a661ccf024bf94f181b8193367251f0d7411aa23094626f7005500c96458d393fe9deb7a7d63a0ac41e2898a67a7750dbd166673279e06c868df0a00000000004bad0f0000000000001559fffffff8000000006634f2f5000000006634f2f500000000004c074200000000000015a00a3f32b0cd92140161520c68b4df848dce5b315f5a67f61563ec3b2fb6b17ab64fe530f438c7f035cb7c204ba80e3c6b8e14fc54f10b8e79af497f7dbf3c225f183f509b2db99ab2fdee7f1a36b699cc6fc1a868ad8b7fbf9a28461133fb45cb7940271678ca5835657a5089335850924b016ef17cc280e4f7582c879140889bbe4a4d39d5160d5b71f7983a53c4b6e89f5bfa8269d152c6f58be368cc588af6575595bc376b65a0e7f916fc5eef915402a661ccf024bf94f181b8193367251f0d7411aa23094626f7" | ||
] | ||
}, | ||
"parsed": [ | ||
{ | ||
"id": "e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", | ||
"price": { | ||
"price": "6140993501000", | ||
"conf": "3287868567", | ||
"expo": -8, | ||
"publish_time": 1714746101 | ||
}, | ||
"ema_price": { | ||
"price": "6094004700000", | ||
"conf": "3792887800", | ||
"expo": -8, | ||
"publish_time": 1714746101 | ||
}, | ||
"metadata": { | ||
"slot": 138881186, | ||
"proof_available_time": 1714746103, | ||
"prev_publish_time": 1714746101 | ||
} | ||
}, | ||
{ | ||
"id": "c96458d393fe9deb7a7d63a0ac41e2898a67a7750dbd166673279e06c868df0a", | ||
"price": { | ||
"price": "4959503", | ||
"conf": "5465", | ||
"expo": -8, | ||
"publish_time": 1714746101 | ||
}, | ||
"ema_price": { | ||
"price": "4982594", | ||
"conf": "5536", | ||
"expo": -8, | ||
"publish_time": 1714746101 | ||
}, | ||
"metadata": { | ||
"slot": 138881186, | ||
"proof_available_time": 1714746103, | ||
"prev_publish_time": 1714746101 | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
Hermes offers several other endpoints for retrieving price updates. For more information, see the [Hermes API Reference](https://hermes.pyth.network/docs/#/). | ||
|
||
## Streaming | ||
|
||
Hermes also provides a Server-Sent Events (SSE) endpoint to stream price updates. | ||
The `/v2/updates/price/stream` endpoint continuously streams price updates for the requested feeds to the caller. | ||
|
||
For example, to stream price updates for BTC/USD, run: | ||
|
||
```bash copy | ||
curl -N 'https://hermes.pyth.network/v2/updates/price/stream?ids[]=0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43' | ||
``` | ||
|
||
The output is a stream of events containing the requested price updates, similar to the following: | ||
|
||
```bash | ||
data:{"binary":{"encoding":"hex","data":["504e41550100000003b801000000040d00eabd2d495ed4f262eb6fafb24d8086ee608eb19da4e447c64a38e6b3eefcf2d27bfb21bdbf1bf803115a1fcda040931e851c48b52215cbcc291663ba53b6b24b01018f071b30658331897090011cbbe96ff202d5d066714006b3670440122089d1ad1e6ce7236b89d3ccacfeee49512065db54233aca946a8e8e28e83968e3bc517e0002f678f31f75bb9d0a1957119205859fd348f07b069b97e575b73bbc82d402b16f24b62b5366f3f05aab7d528532dfc1911646ed705b795f4492fe84d542b507f60103b03dc0569c018119aa3c7c98cb156c7423ed8913d95e08b44674e82523b8f1ab437cc4b3d284e1f4f80cda67ef123b892f15021951e0fbcd80216a2beda9ac5c0104337f1053c8a1c7550609436b9426530df1e22f0afaf26c9210887d8bbe563a8418cbb2bcea8a095fb9ad3b06f5e575ae168eb5f9ab93a0d2a6be2ffd631cc2ad0006c42a1ada427f7f8756d14d5a6764ac738474f17ed2a1866cf10d94544a92a9040c9ad6caef6aa1c4ef9d67f808c0d75f52a18e60fd3589d76bc21d5ed3512e8d01076ba49d01aefbfb54bbe8d37a003cfd91625dcbab52691121d99d490567a409e73fb5489180dc582c3110c6dc81b95cd1c380e7c76d53c1238aa035ea17251d950008b6f57f8221306f54f6405f34cf4dce795e8b9c7dd309c6069212e0dfa7a427234adcd296892087e7e109d85d27aaea267c83f98a9bf49b92a27f16b5974ed101000b0756503ae6178fcd2ce6286dc2210415ee229cdc7cb57892b3d13a45661e2c33077ec4afa45d03e8755bbcb129f64d21f302f6e759321fe05a1edd8eb782a04d010df60048fe6b5d3217c8a8fe03351a9bab77f0938c2d5913d24bf9a52da54bd0ce3227dbbb83cf67f9b10ab6adeea005f54011202beafed7814e5172c9ca0a06ad000f4e231dd10e55ae5149f982833093937237bf261e05fd903b953f3771b02ef49640ef0cd9c11ba80a7afb55fece73f548c4808b713b99438502df818c5150ecb00011fe3be80af9b0cdbee241689da36793a7d101fb4b9357f6a97f97a581b253d9dd4ee56dffe5d8c5b217e4b1070607a7f5114d9a63769f49ad7045cfc86f40a9890012bbfdd274135f9fecd68d7f88d05756e6666a3a28665852f8af380c48d6bc91a850770fa193fe3d80b0cec9a5451200bcac20a16a397267e9a0bb2f680f5d1dfa016634fb8d00000000001ae101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa7100000000033d3e2b0141555756000000000008473bf600002710d2760ed144ee74be4d41297509cdbff411fffa5801005500e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b430000059afeec470000000000c2d215effffffff8000000006634fb8c000000006634fb8c00000593465c858000000000d5cfbf900a6526b4409251e9d274f511a52a6cbabb1b9a6c348d4bc394abfdba2e30ac9f4b127502861670451c1ae10f8231549fdd446eea366199686b78cebe4a918671b73aa1e534b4c66c38d01100a593681cf47897a3cb14045dff7c2795033a295d9a01d08cef7033f64f7700464f4cfaec825fa3117f04b4be713cadf32dcbe8ce1c763d311ee040e1a1de13c56e4bfaba769449d8c7ebf8685a756b1ebe6efd41df5f59a2bd7042b74982afe362bf7df2951f2f47e758f98107849cdb94fa063cb3b5bc0560e67d4929"]},"parsed":[{"id":"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43","price":{"price":"6163260000000","conf":"3268548079","expo":-8,"publish_time":1714748300},"ema_price":{"price":"6130098800000","conf":"3587162000","expo":-8,"publish_time":1714748300},"metadata":{"slot":138886134,"proof_available_time":1714748302,"prev_publish_time":1714748300}}]} | ||
|
||
data:{"binary":{"encoding":"hex","data":["504e41550100000003b801000000040d00c225b810b04721ccc39a0206054caf741eb99e349b22ab29706f2678d17fe4895d4e4aeca6933f82102b6e3aeebd295c5bce268e86700c15922f007fa940e9070102501280da67908dc8a20f7c2ff4503247eb6665818cdb59e589432cc7ce02b8480da408192ab382fe99ebec381bac3a8b044b38272c95177bcc52294a92bee1450103ba792f1f72f9649b6de0b5e57c3c9b5241f5bfc6ef7eddef3dc43fef853712244ae0fef85a9a716713f057fc3419c734d87d0559687e3afc80c0ed98a639027e010685b897965b108523c8e7f5cd71ae6ea949709c5c53317e2f10c7675b50b33bc5219423aa360fd45503bf38095b995ea4dcd322d60fe6a419523e586c18913be400076b7d062164bb2683488cb58c937e98a654335dbca2bf0022c227f163523bf0b108cc21912193bb3ce8f0f629b64790d3078aa8e2f971da5f08fb6131b452a4b600092b98506c6918be41718667ba9eb77442af665be25daafba429c1b8c9efe956221e42f5a0dbfa663a2bd1ed2ec8b2de05ac208a87c7ab7c7b09d5c08850b79bb0010aedf6b21ec277ba911cf991ccd61a552ed05ea43757fce4d8d80cd1ba644464b94f1915c68dfc2824266e19ab33efb556ce5efda2a8f191c86410aae7b169639f010bf8762ef1e3923662b5c70ce60eb9973253a10935d2e1be5064c5f4bd0c071aac3d803a97f5a7640b6a3eace962ad19b9590ffb41a2461a44ae2248ab5a5d1030010c711b2cbce91aa23bc0059c0f1bbfb4330eed0ffdd91edc6a7be43c8e2a7d79e755a7722bc789729301d85ea6602c70a5f84a978b90551b248869ddf5c542d498010d9b91a1658a6e9d18af4a40438ee9c9d5d3906d1f73c1a0793231a17bfc7b5f823fba94bc05f237d661ee344f434afa8c4c42bbcecd94c7653fa795ffcb1c1cf8010efbcc92e2e80ff957061146f713f5ad4b55c789c93cdf3f963ef2bb1a7c08b32f67a809f50892aeb7147150afdee20d6015f740bc9efdb42782e443720d3d32b8010facda4336d8313e28b1d65617d1c2a04a5a8b03f0513304ac67ccf550c0729e4054dfb5b89a0a8cc880fa4772416a843b5f73947c2576477ba564f41258bc1035001226486d88c8234e7d61945cafd925f6edb78b18b88313b0774021d6569884a49517c0d527f71c17bdfaf04e80fc3da394d8ce85119c76c5ac70953820510ad2c0016634fb8d00000000001ae101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa7100000000033d3e2c0141555756000000000008473bf70000271070d00559e2514c7d37112103ee50821d9f3fea1301005500e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b430000059af4abc40000000000c52e0e33fffffff8000000006634fb8d000000006634fb8c00000593469c9bc000000000d5cf34800a0c6b422d2499aadf7404f3883f57950914cb5244a7eac10d23c63c6cb83687c792233d96d4f990a9ea788e9d37c1d58f50a2c17c4346cea646effc273e57f2e79f3674fb4c9ca7b0d229076b29f8632847de2b7e97ef5b554e42c9b1c3c9d62d7ea8859f6270b45d85256e206b25637f7b0a625186d5d58d86c7f1249b33f093de368e293daf679ad3fad5f19dea39b804c189b231b5cded362b5023c6a36b273191575d8e10f0c7aa2dc0479898b110e26a5238a2d90781e7db2318aa0751e00222d2be5ca0b707"]},"parsed":[{"id":"e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43","price":{"price":"6163088000000","conf":"3308129843","expo":-8,"publish_time":1714748301},"ema_price":{"price":"6130103000000","conf":"3587126400","expo":-8,"publish_time":1714748301},"metadata":{"slot":138886135,"proof_available_time":1714748302,"prev_publish_time":1714748300}}]} | ||
``` | ||
|
||
## SDK | ||
|
||
Pyth provides a typescript SDK to fetch price updates. | ||
The `PriceServiceConnection` class in this SDK connects to Hermes to fetch and stream price updates. | ||
|
||
```typescript copy | ||
const connection = new PriceServiceConnection("https://hermes.pyth.network", { | ||
priceFeedRequestConfig: { | ||
// Provide this option to retrieve binary price updates for on-chain contracts. | ||
// Ignore this option for off-chain use. | ||
binary: true, | ||
}, | ||
}); | ||
|
||
const priceIds = [ | ||
// You can find the IDs of prices at https://pyth.network/developers/price-feed-ids | ||
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id | ||
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id | ||
]; | ||
|
||
// Get the latest price updates for the given price IDs. | ||
// If you set `binary: true` above, then this method also returns signed price updates for the on-chain Pyth contract. | ||
const priceUpdates = await connection.getLatestVaas(priceIds); | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there's also a way to subscribe to updates here, maybe worth including that as well? |
||
|
||
`PriceServiceConnection` also allows subscribing to price updates via a WebSocket connection: | ||
|
||
```typescript copy | ||
// priceIds here is the one declared in the above code snippet. | ||
const priceFeeds = await connection.getLatestPriceFeeds(priceIds); | ||
|
||
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => { | ||
// It will include signed price updates if the binary option was provided to the connection constructor. | ||
console.log( | ||
`Received an update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan( | ||
60 | ||
)}` | ||
); | ||
}); | ||
|
||
// When using the subscription, make sure to close the websocket upon termination to finish the process gracefully. | ||
setTimeout(() => { | ||
connection.closeWebSocket(); | ||
}, 60000); | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.