|
| 1 | +# Pyth Lazer SDK Example |
| 2 | + |
| 3 | +This example demonstrates how to use the Pyth Lazer SDK to subscribe to real-time price feed data from the Pyth network. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before running this example, make sure you have the following: |
| 8 | + |
| 9 | +- Node.js installed (version 14 or higher) |
| 10 | +- A Pyth Lazer access token (replace `"YOUR_ACCESS_TOKEN"` in the code with your actual token).Please fill out [this form](https://tally.so/r/nP2lG5) to contact the Pyth team and get the access token. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Install the dependencies: |
| 15 | +```bash |
| 16 | +npm install |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +Please put your access token in the `src/index.ts` file. |
| 22 | + |
| 23 | +```js |
| 24 | +const client = await PythLazerClient.create( |
| 25 | + ["wss://pyth-lazer-staging.dourolabs.app/v1/stream"], |
| 26 | + "YOUR_ACCESS_TOKEN", // replace with your access token |
| 27 | + ); |
| 28 | +``` |
| 29 | + |
| 30 | +To run the example, use the following command: |
| 31 | +```bash |
| 32 | +npm start |
| 33 | +``` |
| 34 | + |
| 35 | +The example will connect to the Pyth Lazer staging environment using the provided WebSocket URL and access token. It will then subscribe to price feed data for the specified `priceFeedIds` (in this case, IDs 1 and 2) and log the received messages to the console. Get the lazer price feed ids from the [Pyth Price Feeds](https://docs.pyth.network/lazer/price-feed-ids) documentation page. |
| 36 | + |
| 37 | +You can customize the `priceFeedIds`, `properties`, `chains`, and other subscription options according to your requirements. |
| 38 | +To know more about the subscription options, please refer to the [Pyth Lazer SDK Documentation](https://docs.pyth.network/lazer/subscribe-price-updates#2-adjust-subscription-parameters). |
| 39 | + |
| 40 | +## Output |
| 41 | + |
| 42 | +The example will output the received messages to the console, including: |
| 43 | +- JSON messages with the subscription ID and parsed price feed data |
| 44 | +- Binary messages with the raw binary data for Solana and EVM messages |
| 45 | + |
| 46 | +Example output: |
| 47 | +``` |
| 48 | +got message: { |
| 49 | + type: 'json', |
| 50 | + value: { |
| 51 | + type: 'streamUpdated', |
| 52 | + subscriptionId: 1, |
| 53 | + parsed: { timestampUs: '1737058486600000', priceFeeds: [Array] }, |
| 54 | + solana: { |
| 55 | + encoding: 'hex', |
| 56 | + data: 'b9011a8254e9654152b8a69a070649239be1f06b5eb40f951026ec963fb14cae3d58178457e8b5e0fd5b228e34282e4bf5d6b3cf946e1a5b9e2e93656b34512da4f8b00ff65210bee4fcf5b1cee1e537fabcfd95010297653b94af04d454fc473e94834f2a0075d3c79340410c72d82b06000302010000000100d22071f6210900000200000001007f66eec54d000000' |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +stream updated for subscription 1 : [ |
| 61 | + { priceFeedId: 1, price: '10041473179858' }, |
| 62 | + { priceFeedId: 2, price: '334033217151' } |
| 63 | +] |
| 64 | +``` |
| 65 | + |
| 66 | +## Resources |
| 67 | + |
| 68 | +- [Pyth Lazer Documentation](https://docs.pyth.network/lazer) |
| 69 | +- [Pyth Network Website](https://pyth.network/) |
| 70 | +- [Pyth Price Feeds](https://docs.pyth.network/lazer/price-feed-ids) |
| 71 | +- [Lazer API Reference](https://pyth-lazer.dourolabs.app/docs) |
0 commit comments