From 3613fde71c3575b879fcb8a47c38ea376e595b44 Mon Sep 17 00:00:00 2001 From: Aditya Arora Date: Thu, 2 May 2024 12:01:25 -0400 Subject: [PATCH 1/6] first update --- pages/price-feeds/_meta.json | 1 + pages/price-feeds/fetch-price-updates.mdx | 81 +++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 pages/price-feeds/fetch-price-updates.mdx diff --git a/pages/price-feeds/_meta.json b/pages/price-feeds/_meta.json index 3cdf8d12..4d596ea5 100644 --- a/pages/price-feeds/_meta.json +++ b/pages/price-feeds/_meta.json @@ -24,6 +24,7 @@ }, "use-real-time-data": "Use Real-Time Price Data", + "fetch-price-updates": "Fetch Price Updates", "schedule-price-updates": "Schedule Price Updates", "migrate-an-app-to-pyth": "Migrate an App to Pyth", "publish-data": "Publish Data", diff --git a/pages/price-feeds/fetch-price-updates.mdx b/pages/price-feeds/fetch-price-updates.mdx new file mode 100644 index 00000000..0683b1bc --- /dev/null +++ b/pages/price-feeds/fetch-price-updates.mdx @@ -0,0 +1,81 @@ +import { Callout } from 'nextra/components' + +# How To Fetch Price Updates + +The following guides explain how to fetch price updates to update Pyth price feeds on-chain. +As a pull oracle, Pyth's users are typically responsible for updating the state of on-chain feeds. +Please see [What is a Pull Oracle?](/price-feeds/pull-updates) to learn more about pull updates. + + +The price updates are requested from Pythnet and the Wormhole network. +Pyth Data Association provides a web service named [Hermes](/price-feeds/how-pyth-works/hermes) which listens to the abovementioned networks and provides price updates to the users. +Hermes provides Pyth's latest price update data format that is more cost-effective to verify and use on-chain. + +There are three different ways to fetch price updates: + +1. [REST API](#rest-api) +1. [SDK](#sdk) +1. [Websocket](#websocket) + + + You need to pass the Pyth price feed's ID to request the price updates. These IDs are unique identifiers for price feeds (e.g., BTC/USD). \ + The complete list of Pyth price feed IDs is available at https://pyth.network/developers/price-feed-ids + + + +## REST API + +Hermes exposes APIs to fetch the latest price updates. +Consult the [Hermes API documentation](https://hermes.pyth.network/docs/#/) for more information. + +Here is an example of retrieving the latest price update of the BTC/USD price feed: + +```bash +curl -X 'GET' \ + 'https://hermes.pyth.network/v2/updates/price/latest?ids%5B%5D=0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43' \ + -H 'accept: application/json' +``` + +The output will be similar to the following containing the requested price update: + +``` bash +{ + "binary": { + "encoding": "hex", + "data": [ + "504e41550100000003b801000000040d002188e98ad3de530647c3aee42f8bb2cdb3b3b6fa9a9382aab2acf2528a4493c73360498d7ece59862d637e93d263f1a088c9a2ba5d32df7357f809878a02f3210002b8f66b286c196eacf577551fde3f05132931e4f5dd62a9d628dbe0eb850cd1e62cddf370f1a7367bc271702ca7b4a57c312cceae4d8324995820b5f37980c22e000359154db70a32fd77a29820bdb9aa41be7b913eca2ab0c9078e318647466426b766492e6dfa313ce02849a5808ffdf15b20923d98c36766e2c4e765e60d614d780006a00999c38d23c84e42837abc1215d29610939d3ccf8a4c9970ec1d5d41175d693482908685825898d5691504d24bfc5174ae329bd75eeda336559c057419107300071870315e009141af927ef98bea6a35f35f9da25b87569ce5354d4550de67417a3055c323ab98ea7e1ba114a4840de6e5810444d6c7033cfa3a0c25128f56d5c20108e5fffa3969de52f08444b662bd10a1a49e63af0a4a3eea5213a1ce252de090576877540a2b1276a0586dde42c3cecc600eb4197eb0efa04b483f8957de29024e010a66c8fad6fdb0cc85fa399c9640fdf5f60a0f0ca80bd788ddc1c20e19ace0c2171bc49876915d5ae54795e90aff7832a3a2db5b69ebd0849b7b2e3a03c7f3e13e010b7d0749e1afe455bf0f0997df8fa378cd451cfd02e3fdbadced1014a797a220e54eb429a4aa67b04888d9478a1e5e3e70c6c5bc2c5d7972af57e19c37fde9fb3b000c0e0061af49d17002356cc9e5c067d0b0871345dbcaa379e19c05ff1c229a27345f19a3683165443d814b0c652927a8f81ebfb22c4b35e309ea73472a50e4cf10000dde4b497454cb1b37edfc05e485d2544a0ac657f2b8b1c71fe0e07aad631430725328d176053f642a696fc738afe42b5914fa9f90421e8bfb54eedcf4d5ca0912000e2a117d0cf720a6277d38f1ce886993c3318b92b319b999a90ac7a2d58a9f11e40b91c1f5e74cc2a6bb3a0a69e8d445fe0647b2b8bcae7ef087850d3d1b243bda000fdba3db2e120d29b9a986bf0c2eebcb5bc481ab3fb4574f6c97685a3b096e7fb073c09fcb40fbce2e62a0c7059dbbdd6083d063b88c41f36efc610c258034a70c0012144cc9947356370db75b365ac7dd279fd1d94f9939099abbd75c273fe69527186982c1ff4eb96bf60f5866b7d63a9652e257442d5374bdcaa3242d22df89724e006633a8dd00000000001ae101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa7100000000033a30b10141555756000000000008442d8b00002710a3ead17e8fc983eb6a9089bc4772a2a5395949fb01005500e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b430000055dfa01811f000000011651312bfffffff8000000006633a8dc000000006633a8dc00000555bb770ae000000000eb8c19000a56d089dceea392760aadb468ea4545ac175b2c51f0285865a623489a35bbc8d74c627f8719e3a1ce2cc32be9e3fb046da02e358d581a780b9cbc37432b7dd3f6c83f64ef143c6147c5d3a28e7eff2347842ea15c23e7eabd9cab0cd34c8c45d88393c42fe94a49149db8ab4237bbd92637f4506ad11853586555d58ad6480887029e4acc65eb8f4a375c5c7b0ad11813b5a9b1202912c6adcfe92fffb703f04e99abb6de8a588446bd86c6d62e27356e38c479ca143259195a4d38355dee36b9f45a0825af1c2b2b" + ] + } +} +``` + + +## SDK + +Pyth provides a typescript SDK to fetch price updates. This SDK allows you to read the real-time price updates to consume it on- and off-chain applications. + +``` typescript +const connection = new PriceServiceConnection("https://hermes.pyth.network", { + priceFeedRequestConfig: { + // Provide this option to retrieve signed 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); +``` + + +## Websocket + +Hermes also provides a WebSocket API to fetch the latest price updates. +Consult the [Schedule Price Updates](/price-feeds/schedule-price-updates) guide to learn how to subscribe to Hermes websocket for streaming price updates. \ No newline at end of file From 98623acc09e3a693b51d82f126eab4099f4a9777 Mon Sep 17 00:00:00 2001 From: Aditya Arora Date: Thu, 2 May 2024 12:08:55 -0400 Subject: [PATCH 2/6] pre-commit --- pages/price-feeds/fetch-price-updates.mdx | 26 +++++++++++------------ 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/pages/price-feeds/fetch-price-updates.mdx b/pages/price-feeds/fetch-price-updates.mdx index 0683b1bc..a6a0c0eb 100644 --- a/pages/price-feeds/fetch-price-updates.mdx +++ b/pages/price-feeds/fetch-price-updates.mdx @@ -1,4 +1,4 @@ -import { Callout } from 'nextra/components' +import { Callout } from "nextra/components"; # How To Fetch Price Updates @@ -6,8 +6,7 @@ The following guides explain how to fetch price updates to update Pyth price fee As a pull oracle, Pyth's users are typically responsible for updating the state of on-chain feeds. Please see [What is a Pull Oracle?](/price-feeds/pull-updates) to learn more about pull updates. - -The price updates are requested from Pythnet and the Wormhole network. +The price updates are requested from Pythnet and the Wormhole network. Pyth Data Association provides a web service named [Hermes](/price-feeds/how-pyth-works/hermes) which listens to the abovementioned networks and provides price updates to the users. Hermes provides Pyth's latest price update data format that is more cost-effective to verify and use on-chain. @@ -18,11 +17,12 @@ There are three different ways to fetch price updates: 1. [Websocket](#websocket) - You need to pass the Pyth price feed's ID to request the price updates. These IDs are unique identifiers for price feeds (e.g., BTC/USD). \ - The complete list of Pyth price feed IDs is available at https://pyth.network/developers/price-feed-ids + You need to pass the Pyth price feed's ID to request the price updates. These + IDs are unique identifiers for price feeds (e.g., BTC/USD). \ The complete + list of Pyth price feed IDs is available at + https://pyth.network/developers/price-feed-ids - ## REST API Hermes exposes APIs to fetch the latest price updates. @@ -38,7 +38,7 @@ curl -X 'GET' \ The output will be similar to the following containing the requested price update: -``` bash +```bash { "binary": { "encoding": "hex", @@ -49,12 +49,11 @@ The output will be similar to the following containing the requested price updat } ``` +## SDK -## SDK +Pyth provides a typescript SDK to fetch price updates. This SDK allows you to read the real-time price updates to consume it on- and off-chain applications. -Pyth provides a typescript SDK to fetch price updates. This SDK allows you to read the real-time price updates to consume it on- and off-chain applications. - -``` typescript +```typescript const connection = new PriceServiceConnection("https://hermes.pyth.network", { priceFeedRequestConfig: { // Provide this option to retrieve signed price updates for on-chain contracts. @@ -74,8 +73,7 @@ const priceIds = [ const priceUpdates = await connection.getLatestVaas(priceIds); ``` - ## Websocket -Hermes also provides a WebSocket API to fetch the latest price updates. -Consult the [Schedule Price Updates](/price-feeds/schedule-price-updates) guide to learn how to subscribe to Hermes websocket for streaming price updates. \ No newline at end of file +Hermes also provides a WebSocket API to fetch the latest price updates. +Consult the [Schedule Price Updates](/price-feeds/schedule-price-updates) guide to learn how to subscribe to Hermes websocket for streaming price updates. From f0685a36d941b007aef1df354ea3f9dfe5ca49e0 Mon Sep 17 00:00:00 2001 From: Aditya Arora Date: Thu, 2 May 2024 13:50:46 -0400 Subject: [PATCH 3/6] update --- pages/price-feeds/fetch-price-updates.mdx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pages/price-feeds/fetch-price-updates.mdx b/pages/price-feeds/fetch-price-updates.mdx index a6a0c0eb..4f188e17 100644 --- a/pages/price-feeds/fetch-price-updates.mdx +++ b/pages/price-feeds/fetch-price-updates.mdx @@ -3,12 +3,9 @@ import { Callout } from "nextra/components"; # How To Fetch Price Updates The following guides explain how to fetch price updates to update Pyth price feeds on-chain. -As a pull oracle, Pyth's users are typically responsible for updating the state of on-chain feeds. -Please see [What is a Pull Oracle?](/price-feeds/pull-updates) to learn more about pull updates. - -The price updates are requested from Pythnet and the Wormhole network. -Pyth Data Association provides a web service named [Hermes](/price-feeds/how-pyth-works/hermes) which listens to the abovementioned networks and provides price updates to the users. -Hermes provides Pyth's latest price update data format that is more cost-effective to verify and use on-chain. +Price updates can be submitted to the Pyth Price Feeds contract to update the on-chain price. +These price updates are fetched from [Hermes](/price-feeds/how-pyth-works/hermes) which are verified on-chain. +Please see [What is a Pull Oracle?](/price-feeds/pull-updates) to learn more. There are three different ways to fetch price updates: From 27ed5b700c6521541cac1939ce84bda03665ca1d Mon Sep 17 00:00:00 2001 From: Aditya Arora Date: Thu, 2 May 2024 14:13:02 -0400 Subject: [PATCH 4/6] update --- pages/price-feeds/fetch-price-updates.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/price-feeds/fetch-price-updates.mdx b/pages/price-feeds/fetch-price-updates.mdx index 4f188e17..1bb09ece 100644 --- a/pages/price-feeds/fetch-price-updates.mdx +++ b/pages/price-feeds/fetch-price-updates.mdx @@ -60,12 +60,12 @@ const connection = new PriceServiceConnection("https://hermes.pyth.network", { }); const priceIds = [ - // You can find the Ids of prices at https://pyth.network/developers/price-feed-ids + // 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. +// 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); ``` From 4afe8a453d5c66eb0bdc1cef982e8850f526ea18 Mon Sep 17 00:00:00 2001 From: Aditya Arora Date: Fri, 3 May 2024 11:27:17 -0400 Subject: [PATCH 5/6] comment fix --- pages/price-feeds/fetch-price-updates.mdx | 117 ++++++++++++++++++---- 1 file changed, 98 insertions(+), 19 deletions(-) diff --git a/pages/price-feeds/fetch-price-updates.mdx b/pages/price-feeds/fetch-price-updates.mdx index 1bb09ece..7dba1836 100644 --- a/pages/price-feeds/fetch-price-updates.mdx +++ b/pages/price-feeds/fetch-price-updates.mdx @@ -2,35 +2,34 @@ import { Callout } from "nextra/components"; # How To Fetch Price Updates -The following guides explain how to fetch price updates to update Pyth price feeds on-chain. +The following guides explain how to fetch price updates. Price updates can be submitted to the Pyth Price Feeds contract to update the on-chain price. -These price updates are fetched from [Hermes](/price-feeds/how-pyth-works/hermes) which are verified on-chain. + +Price updates are fetched from [Hermes](/price-feeds/how-pyth-works/hermes) which are verified on-chain. Please see [What is a Pull Oracle?](/price-feeds/pull-updates) to learn more. There are three different ways to fetch price updates: 1. [REST API](#rest-api) +1. [Streaming](#streaming) 1. [SDK](#sdk) -1. [Websocket](#websocket) - You need to pass the Pyth price feed's ID to request the price updates. These - IDs are unique identifiers for price feeds (e.g., BTC/USD). \ The complete - list of Pyth price feed IDs is available at - https://pyth.network/developers/price-feed-ids + Fetching a price from Hermes requires a price feed ID. These IDs are unique + identifiers for price feeds (e.g., BTC/USD). The complete list of Pyth price + feed IDs is available at https://pyth.network/developers/price-feed-ids ## REST API Hermes exposes APIs to fetch the latest price updates. -Consult the [Hermes API documentation](https://hermes.pyth.network/docs/#/) for more information. - -Here is an example of retrieving the latest price update of the BTC/USD price feed: +Use the `/v2/updates/price/latest` endpoint of the Hermes REST API to fetch the latest price update for a feed. +This API also allows you to fetch the latest price updates for multiple feeds in a single request. +For example, to retrieve the latest BTC/USD & ETH/USD price updates, run -```bash +```bash copy curl -X 'GET' \ - 'https://hermes.pyth.network/v2/updates/price/latest?ids%5B%5D=0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43' \ - -H 'accept: application/json' + '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: @@ -40,17 +39,81 @@ The output will be similar to the following containing the requested price updat "binary": { "encoding": "hex", "data": [ - "504e41550100000003b801000000040d002188e98ad3de530647c3aee42f8bb2cdb3b3b6fa9a9382aab2acf2528a4493c73360498d7ece59862d637e93d263f1a088c9a2ba5d32df7357f809878a02f3210002b8f66b286c196eacf577551fde3f05132931e4f5dd62a9d628dbe0eb850cd1e62cddf370f1a7367bc271702ca7b4a57c312cceae4d8324995820b5f37980c22e000359154db70a32fd77a29820bdb9aa41be7b913eca2ab0c9078e318647466426b766492e6dfa313ce02849a5808ffdf15b20923d98c36766e2c4e765e60d614d780006a00999c38d23c84e42837abc1215d29610939d3ccf8a4c9970ec1d5d41175d693482908685825898d5691504d24bfc5174ae329bd75eeda336559c057419107300071870315e009141af927ef98bea6a35f35f9da25b87569ce5354d4550de67417a3055c323ab98ea7e1ba114a4840de6e5810444d6c7033cfa3a0c25128f56d5c20108e5fffa3969de52f08444b662bd10a1a49e63af0a4a3eea5213a1ce252de090576877540a2b1276a0586dde42c3cecc600eb4197eb0efa04b483f8957de29024e010a66c8fad6fdb0cc85fa399c9640fdf5f60a0f0ca80bd788ddc1c20e19ace0c2171bc49876915d5ae54795e90aff7832a3a2db5b69ebd0849b7b2e3a03c7f3e13e010b7d0749e1afe455bf0f0997df8fa378cd451cfd02e3fdbadced1014a797a220e54eb429a4aa67b04888d9478a1e5e3e70c6c5bc2c5d7972af57e19c37fde9fb3b000c0e0061af49d17002356cc9e5c067d0b0871345dbcaa379e19c05ff1c229a27345f19a3683165443d814b0c652927a8f81ebfb22c4b35e309ea73472a50e4cf10000dde4b497454cb1b37edfc05e485d2544a0ac657f2b8b1c71fe0e07aad631430725328d176053f642a696fc738afe42b5914fa9f90421e8bfb54eedcf4d5ca0912000e2a117d0cf720a6277d38f1ce886993c3318b92b319b999a90ac7a2d58a9f11e40b91c1f5e74cc2a6bb3a0a69e8d445fe0647b2b8bcae7ef087850d3d1b243bda000fdba3db2e120d29b9a986bf0c2eebcb5bc481ab3fb4574f6c97685a3b096e7fb073c09fcb40fbce2e62a0c7059dbbdd6083d063b88c41f36efc610c258034a70c0012144cc9947356370db75b365ac7dd279fd1d94f9939099abbd75c273fe69527186982c1ff4eb96bf60f5866b7d63a9652e257442d5374bdcaa3242d22df89724e006633a8dd00000000001ae101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa7100000000033a30b10141555756000000000008442d8b00002710a3ead17e8fc983eb6a9089bc4772a2a5395949fb01005500e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b430000055dfa01811f000000011651312bfffffff8000000006633a8dc000000006633a8dc00000555bb770ae000000000eb8c19000a56d089dceea392760aadb468ea4545ac175b2c51f0285865a623489a35bbc8d74c627f8719e3a1ce2cc32be9e3fb046da02e358d581a780b9cbc37432b7dd3f6c83f64ef143c6147c5d3a28e7eff2347842ea15c23e7eabd9cab0cd34c8c45d88393c42fe94a49149db8ab4237bbd92637f4506ad11853586555d58ad6480887029e4acc65eb8f4a375c5c7b0ad11813b5a9b1202912c6adcfe92fffb703f04e99abb6de8a588446bd86c6d62e27356e38c479ca143259195a4d38355dee36b9f45a0825af1c2b2b" + "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 exposes an API to stream price updates. +Use the `/v2/updates/price/stream` endpoint of the Hermes REST API to stream price updates. + +For example, to stream price updates for the BTC/USD, run + +```bash copy +curl -N 'https://hermes.pyth.network/v2/updates/price/stream?ids[]=0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43' +``` + +The output should be streamed data containing the requested price updates. +It will be 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. This SDK allows you to read the real-time price updates to consume it on- and off-chain applications. -```typescript +```typescript copy const connection = new PriceServiceConnection("https://hermes.pyth.network", { priceFeedRequestConfig: { // Provide this option to retrieve signed price updates for on-chain contracts. @@ -70,7 +133,23 @@ const priceIds = [ const priceUpdates = await connection.getLatestVaas(priceIds); ``` -## Websocket +`PriceServiceConnection` client initialized in the above code snippet also allows subscribing to real-time price updates via WebSocket connection: -Hermes also provides a WebSocket API to fetch the latest price updates. -Consult the [Schedule Price Updates](/price-feeds/schedule-price-updates) guide to learn how to subscribe to Hermes websocket for streaming price updates. +```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); +``` From d13abf5bf1620e64cc303386e498465bb7c2e8c8 Mon Sep 17 00:00:00 2001 From: Jayant Krishnamurthy Date: Fri, 3 May 2024 10:00:34 -0700 Subject: [PATCH 6/6] minor edits --- pages/price-feeds/fetch-price-updates.mdx | 38 +++++++++++------------ 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/pages/price-feeds/fetch-price-updates.mdx b/pages/price-feeds/fetch-price-updates.mdx index 7dba1836..3ac23bf1 100644 --- a/pages/price-feeds/fetch-price-updates.mdx +++ b/pages/price-feeds/fetch-price-updates.mdx @@ -2,30 +2,30 @@ import { Callout } from "nextra/components"; # How To Fetch Price Updates -The following guides explain 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. - -Price updates are fetched from [Hermes](/price-feeds/how-pyth-works/hermes) which are verified on-chain. Please see [What is a Pull Oracle?](/price-feeds/pull-updates) to learn more. -There are three different ways to fetch price updates: +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) - Fetching a price from Hermes requires a price feed ID. These IDs are unique - identifiers for price feeds (e.g., BTC/USD). The complete list of Pyth price - feed IDs is available at https://pyth.network/developers/price-feed-ids + 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 ## REST API -Hermes exposes APIs to fetch the latest price updates. -Use the `/v2/updates/price/latest` endpoint of the Hermes REST API to fetch the latest price update for a feed. -This API also allows you to fetch the latest price updates for multiple feeds in a single request. -For example, to retrieve the latest BTC/USD & ETH/USD price updates, run +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' \ @@ -91,17 +91,16 @@ Hermes offers several other endpoints for retrieving price updates. For more inf ## Streaming -Hermes also exposes an API to stream price updates. -Use the `/v2/updates/price/stream` endpoint of the Hermes REST API to stream price updates. +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 the BTC/USD, run +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 should be streamed data containing the requested price updates. -It will be similar to the following: +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}}]} @@ -111,12 +110,13 @@ data:{"binary":{"encoding":"hex","data":["504e41550100000003b801000000040d00c225 ## SDK -Pyth provides a typescript SDK to fetch price updates. This SDK allows you to read the real-time price updates to consume it on- and off-chain applications. +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 signed price updates for on-chain contracts. + // Provide this option to retrieve binary price updates for on-chain contracts. // Ignore this option for off-chain use. binary: true, }, @@ -133,7 +133,7 @@ const priceIds = [ const priceUpdates = await connection.getLatestVaas(priceIds); ``` -`PriceServiceConnection` client initialized in the above code snippet also allows subscribing to real-time price updates via WebSocket connection: +`PriceServiceConnection` also allows subscribing to price updates via a WebSocket connection: ```typescript copy // priceIds here is the one declared in the above code snippet.