Skip to content

Commit 1d13c8d

Browse files
authored
Clean up lazer JS examples (#63)
* tidy up * docs
1 parent 851f948 commit 1d13c8d

File tree

6 files changed

+121
-8
lines changed

6 files changed

+121
-8
lines changed

lazer/js/README.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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.

lazer/js/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"type": "module",
77
"scripts": {
88
"start": "node --loader ts-node/esm src/index.js",
9-
"start:publish": "node --loader ts-node/esm src/publish.js",
10-
"start:verify_ecdsa_message": "node --loader ts-node/esm src/verify_ecdsa_message.js",
11-
"start:verify_ed25519_message": "node --loader ts-node/esm src/verify_ed25519_message.js",
9+
"start:post_solana": "node --loader ts-node/esm src/solana/post_solana.js",
10+
"start:verify_ecdsa_message": "node --loader ts-node/esm src/solana/verify_ecdsa_message.js",
11+
"start:verify_ed25519_message": "node --loader ts-node/esm src/solana/verify_ed25519_message.js",
1212
"test": "pnpm run test:format && pnpm run test:types && pnpm run build:cjs && pnpm run build:esm",
1313
"build:cjs": "tsc --project tsconfig.json --verbatimModuleSyntax false --module commonjs --outDir ./dist/cjs && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
1414
"build:esm": "tsc --project tsconfig.json --outDir ./dist/esm && echo '{\"type\":\"module\"}' > dist/esm/package.json",

lazer/js/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";
22

33
const main = async () => {
44
const client = await PythLazerClient.create({
5-
urls: ["wss://pyth-lazer-staging.dourolabs.app/v1/stream"],
6-
token: "my_token",
5+
urls: ["wss://pyth-lazer.dourolabs.app/v1/stream"],
6+
token: process.env.ACCESS_TOKEN!,
77
});
88

99
client.addMessageListener((message) => {

lazer/js/src/publish.ts renamed to lazer/js/src/solana/post_solana.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
1515

1616
const SOLANA_RPC_URL = "https://api.devnet.solana.com";
1717
const PAYER_SECRET_KEY = Uint8Array.from(
18-
JSON.parse(fs.readFileSync("/path/to/private-key.json", "utf8")) as number[],
18+
JSON.parse(fs.readFileSync("./keypair.json", "utf8")) as number[],
1919
);
2020
// Program ID of the example contract on devnet
2121
const PROGRAM_ID = "HU64YGK66e1wdxD83D3snGuZEvfhM4YDdYShTfQvf6nm";
@@ -35,8 +35,8 @@ const connection = new Connection(SOLANA_RPC_URL, "confirmed");
3535
const payer = Keypair.fromSecretKey(PAYER_SECRET_KEY);
3636
const main = async () => {
3737
const client = await PythLazerClient.create({
38-
urls: ["wss://pyth-lazer-staging.dourolabs.app/v1/stream"],
39-
token: "my_token",
38+
urls: ["wss://pyth-lazer.dourolabs.app/v1/stream"],
39+
token: process.env.ACCESS_TOKEN!,
4040
});
4141

4242
// data received from pyth lazer
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)