|
| 1 | +# Pyth Lazer JavaScript SDK Examples |
| 2 | + |
| 3 | +This directory contains JavaScript/TypeScript examples demonstrating how to use the Pyth Lazer SDK to interact with real-time price feeds and verify price updates on the Solana blockchain. |
| 4 | + |
| 5 | +## What is Pyth Lazer? |
| 6 | + |
| 7 | +Pyth Lazer is a high-performance, low-latency price feed service that provides real-time financial market data to blockchain applications. It supports multiple blockchain networks and offers both JSON and binary message formats for optimal performance. |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +Before running these examples, make sure you have the following installed: |
| 12 | + |
| 13 | +- **Node.js** (version 18 or higher) |
| 14 | +- **pnpm** package manager |
| 15 | +- A Lazer Access Token -- please see [How to Acquire an Access Token](https://docs.pyth.network/lazer/acquire-access-token) if you don't have one. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +1. Navigate to the `lazer/js` directory: |
| 20 | + ```bash |
| 21 | + cd lazer/js |
| 22 | + ``` |
| 23 | + |
| 24 | +2. Install dependencies: |
| 25 | + ```bash |
| 26 | + pnpm install |
| 27 | + ``` |
| 28 | +3. Configure your access token: |
| 29 | + ```bash |
| 30 | + export ACCESS_TOKEN=your_actual_token |
| 31 | + ``` |
| 32 | + |
| 33 | +## Basic Examples |
| 34 | + |
| 35 | +### 1. WebSocket Client Example (`src/index.ts`) |
| 36 | +Demonstrates how to connect to the Pyth Lazer WebSocket stream and receive real-time price updates. |
| 37 | + |
| 38 | +**What it does:** |
| 39 | +- Connects to the Pyth Lazer server |
| 40 | +- Subscribes to price feeds (IDs 1 and 2) |
| 41 | +- Listens for both JSON and binary messages |
| 42 | +- Displays received price data and connection status |
| 43 | + |
| 44 | +**How to run:** |
| 45 | +```bash |
| 46 | +pnpm run start |
| 47 | +``` |
| 48 | + |
| 49 | +## Solana Examples |
| 50 | + |
| 51 | +### Prerequisites |
| 52 | + |
| 53 | +The Solana examples require a funded Solana private key stored in `./keypair.json`. |
| 54 | + |
| 55 | +### 1. Post Solana Example (`src/solana/post_solana.ts`) |
| 56 | +Shows how to receive price data from Pyth Lazer and post it to a Solana smart contract. |
| 57 | + |
| 58 | +**What it does:** |
| 59 | +- Connects to Pyth Lazer and receives price feed data |
| 60 | +- Creates cryptographic instructions for Solana |
| 61 | +- Posts the price data to a deployed smart contract |
| 62 | +- Handles transaction confirmation |
| 63 | + |
| 64 | +**How to run:** |
| 65 | +```bash |
| 66 | +pnpm run start:post_solana |
| 67 | +``` |
| 68 | + |
| 69 | +### 2. ECDSA Message Verification (`src/solana/verify_ecdsa_message.ts`) |
| 70 | +Demonstrates how to verify ECDSA-signed messages using the Pyth Lazer Solana contract. |
| 71 | + |
| 72 | +**What it does:** |
| 73 | +- Takes a hex-encoded message as input |
| 74 | +- Verifies the message using ECDSA signature verification |
| 75 | +- Interacts with the Pyth Lazer Solana program |
| 76 | + |
| 77 | +**How to run:** |
| 78 | +```bash |
| 79 | +pnpm run start:verify_ecdsa_message \ |
| 80 | + --url 'https://api.devnet.solana.com' \ |
| 81 | + --keypair-path './keypair.json' \ |
| 82 | + --message "e4bd474dda8934550d660e6ef4ee6ec1557349e283090c0107cad8bb997e67783a68be5646a5c949a8deaa6bee6ec1fc8aceb5002d6808b1da8ce5e9d26fd1b56ebeaf9d001c0075d3c793403ab1a9b03706000301010000000100eaf83297b5090000" |
| 83 | +``` |
| 84 | + |
| 85 | +### 3. Ed25519 Message Verification (`src/solana/verify_ed25519_message.ts`) |
| 86 | +Shows how to verify Ed25519-signed messages using the Pyth Lazer Solana contract. |
| 87 | + |
| 88 | +**What it does:** |
| 89 | +- Takes a hex-encoded message as input |
| 90 | +- Creates Ed25519 verification instructions |
| 91 | +- Verifies the message using the Pyth Lazer Solana program |
| 92 | + |
| 93 | +**How to run:** |
| 94 | +```bash |
| 95 | +pnpm run start:verify_ed25519_message \ |
| 96 | + --url 'https://api.testnet.solana.com' \ |
| 97 | + --keypair-path './keypair.json' \ |
| 98 | + --message "b9011a82f3c5c2760beb0c78827c75b0b18f1d4a2dcddf9d3efb291e66de25927538deffd74606de833eff236022aaca7b8a79cf15d3c7b51a91b500b2b9e6ca64bcfa03f65210bee4fcf5b1cee1e537fabcfd95010297653b94af04d454fc473e94834f1c0075d3c793c03c26adb03706000301010000000100aa749416b4090000" |
| 99 | +``` |
| 100 | + |
| 101 | +## Troubleshooting |
| 102 | + |
| 103 | +### Common Issues |
| 104 | + |
| 105 | +1. **Connection Errors**: Make sure you have a stable internet connection and the Pyth Lazer service is accessible. |
| 106 | + |
| 107 | +2. **Solana Transaction Failures**: Ensure your wallet has sufficient SOL for transaction fees and the correct network is configured. |
| 108 | + |
| 109 | +3. **Keypair Issues**: Make sure your Solana keypair file is in the correct format (JSON array of numbers) and the path is correct. |
| 110 | + |
| 111 | +## License |
| 112 | + |
| 113 | +This project is licensed under the Apache-2.0 License. |
0 commit comments