Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions lazer/js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Pyth Lazer JavaScript SDK Examples

This directory contains JavaScript/TypeScript examples demonstrating how to use the Pyth Lazer SDK to interact with real-time price feeds and verify cryptographic messages on the Solana blockchain.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just say verify prices or verify price updates rather cryptographic messsages

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe priceUpdates ? 🤔


## What is Pyth Lazer?

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.

## Prerequisites

Before running these examples, make sure you have the following installed:

- **Node.js** (version 18 or higher)
- **pnpm** package manager
- A Lazer Access Token -- please see the TODO.

## Installation

1. Navigate to the `lazer/js` directory:
```bash
cd lazer/js
```

2. Install dependencies:
```bash
pnpm install
```
3. Configure your access token:
```bash
export ACCESS_TOKEN=your_actual_token
```

## Basic Examples

### 1. WebSocket Client Example (`src/index.ts`)
Demonstrates how to connect to the Pyth Lazer WebSocket stream and receive real-time price feed updates.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Demonstrates how to connect to the Pyth Lazer WebSocket stream and receive real-time price feed updates.
Demonstrates how to connect to the Pyth Lazer WebSocket stream and receive real-time price updates.


**What it does:**
- Connects to the Pyth Lazer server
- Subscribes to price feeds (IDs 1 and 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a TODO to the link to price feeds ID table.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the thing to do is to actually use the client to query the symbols for these ids and then print it out nicely. Unfortunately that's not in the SDK right now but i'll push to get it added .

- Listens for both JSON and binary messages
- Displays received price data and connection status

**How to run:**
```bash
pnpm run start
```

## Solana Examples

### Prerequisites

The Solana examples require a funded Solana private key stored in `./keypair.json`.

### 1. Post Solana Example (`src/solana/post_solana.ts`)
Shows how to receive price data from Pyth Lazer and post it to a Solana smart contract.

**What it does:**
- Connects to Pyth Lazer and receives price feed data
- Creates cryptographic instructions for Solana
- Posts the price data to a deployed smart contract
- Handles transaction confirmation

**How to run:**
```bash
pnpm run start:post_solana
```

### 2. ECDSA Message Verification (`src/solana/verify_ecdsa_message.ts`)
Demonstrates how to verify ECDSA-signed messages using the Pyth Lazer Solana contract.

**What it does:**
- Takes a hex-encoded message as input
- Verifies the message using ECDSA signature verification
- Interacts with the Pyth Lazer Solana program

**How to run:**
```bash
pnpm run start:verify_ecdsa_message \
--url 'https://api.devnet.solana.com' \
--keypair-path './keypair.json' \
--message "e4bd474dda8934550d660e6ef4ee6ec1557349e283090c0107cad8bb997e67783a68be5646a5c949a8deaa6bee6ec1fc8aceb5002d6808b1da8ce5e9d26fd1b56ebeaf9d001c0075d3c793403ab1a9b03706000301010000000100eaf83297b5090000"
```

### 3. Ed25519 Message Verification (`src/solana/verify_ed25519_message.ts`)
Shows how to verify Ed25519-signed messages using the Pyth Lazer Solana contract.

**What it does:**
- Takes a hex-encoded message as input
- Creates Ed25519 verification instructions
- Verifies the message using the Pyth Lazer Solana program

**How to run:**
```bash
pnpm run start:verify_ed25519_message \
--url 'https://api.testnet.solana.com' \
--keypair-path './keypair.json' \
--message "b9011a82f3c5c2760beb0c78827c75b0b18f1d4a2dcddf9d3efb291e66de25927538deffd74606de833eff236022aaca7b8a79cf15d3c7b51a91b500b2b9e6ca64bcfa03f65210bee4fcf5b1cee1e537fabcfd95010297653b94af04d454fc473e94834f1c0075d3c793c03c26adb03706000301010000000100aa749416b4090000"
```

## Troubleshooting

### Common Issues

1. **Connection Errors**: Make sure you have a stable internet connection and the Pyth Lazer service is accessible.

2. **Solana Transaction Failures**: Ensure your wallet has sufficient SOL for transaction fees and the correct network is configured.

3. **Keypair Issues**: Make sure your Solana keypair file is in the correct format (JSON array of numbers) and the path is correct.

## License

This project is licensed under the Apache-2.0 License.
6 changes: 3 additions & 3 deletions lazer/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"type": "module",
"scripts": {
"start": "node --loader ts-node/esm src/index.js",
"start:publish": "node --loader ts-node/esm src/publish.js",
"start:verify_ecdsa_message": "node --loader ts-node/esm src/verify_ecdsa_message.js",
"start:verify_ed25519_message": "node --loader ts-node/esm src/verify_ed25519_message.js",
"start:post_solana": "node --loader ts-node/esm src/solana/post_solana.js",
"start:verify_ecdsa_message": "node --loader ts-node/esm src/solana/verify_ecdsa_message.js",
"start:verify_ed25519_message": "node --loader ts-node/esm src/solana/verify_ed25519_message.js",
"test": "pnpm run test:format && pnpm run test:types && pnpm run build:cjs && pnpm run build:esm",
"build:cjs": "tsc --project tsconfig.json --verbatimModuleSyntax false --module commonjs --outDir ./dist/cjs && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
"build:esm": "tsc --project tsconfig.json --outDir ./dist/esm && echo '{\"type\":\"module\"}' > dist/esm/package.json",
Expand Down
4 changes: 2 additions & 2 deletions lazer/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";

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

client.addMessageListener((message) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";

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

// data received from pyth lazer
Expand Down
Loading