Skip to content

Commit 109e878

Browse files
committed
add solana ix example
1 parent 8e4758a commit 109e878

File tree

5 files changed

+67
-124
lines changed

5 files changed

+67
-124
lines changed

lazer/js/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"author": "",
1717
"license": "Apache-2.0",
1818
"dependencies": {
19-
"@pythnetwork/pyth-lazer-sdk": "^0.1.1",
20-
"@solana/buffer-layout": "^4.0.1",
19+
"@pythnetwork/pyth-lazer-sdk": "^0.3.1",
2120
"@solana/web3.js": "^1.98.0"
2221
},
2322
"devDependencies": {

lazer/js/pnpm-lock.yaml

Lines changed: 21 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lazer/js/src/ed25519.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

lazer/js/src/index.ts

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

33
/* eslint-disable no-console */
4-
const client = new PythLazerClient(
5-
"wss://pyth-lazer-staging.dourolabs.app/v1/stream",
6-
"my_token",
4+
const client = await PythLazerClient.create(
5+
["wss://pyth-lazer-staging.dourolabs.app/v1/stream"],
6+
"my_token"
77
);
8+
89
client.addMessageListener((message) => {
910
console.log("got message:", message);
1011
switch (message.type) {
@@ -14,7 +15,7 @@ client.addMessageListener((message) => {
1415
"stream updated for subscription",
1516
message.value.subscriptionId,
1617
":",
17-
message.value.parsed?.priceFeeds,
18+
message.value.parsed?.priceFeeds
1819
);
1920
}
2021
break;
@@ -30,15 +31,21 @@ client.addMessageListener((message) => {
3031
}
3132
}
3233
});
33-
client.ws.addEventListener("open", () => {
34-
client.send({
35-
type: "subscribe",
36-
subscriptionId: 1,
37-
priceFeedIds: [1, 2],
38-
properties: ["price"],
39-
chains: ["solana"],
40-
deliveryFormat: "json",
41-
channel: "fixed_rate@200ms",
42-
jsonBinaryEncoding: "hex",
43-
});
34+
35+
client.addAllConnectionsDownListener(() => {
36+
console.log("All connections are down");
4437
});
38+
39+
client.subscribe({
40+
type: "subscribe",
41+
subscriptionId: 1,
42+
priceFeedIds: [1, 2],
43+
properties: ["price"],
44+
chains: ["solana"],
45+
deliveryFormat: "json",
46+
channel: "fixed_rate@200ms",
47+
jsonBinaryEncoding: "hex",
48+
});
49+
50+
// shutdown client after 10 seconds
51+
setTimeout(() => client.shutdown(), 10000);

lazer/js/src/publish.ts

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";
2-
import { createEd25519Instruction } from "./ed25519.js";
1+
import {
2+
PythLazerClient,
3+
createEd25519Instruction,
4+
} from "@pythnetwork/pyth-lazer-sdk";
35
import fs from "fs";
46

57
import {
@@ -15,7 +17,7 @@ import { SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
1517

1618
const SOLANA_RPC_URL = "https://api.devnet.solana.com";
1719
const PAYER_SECRET_KEY = Uint8Array.from(
18-
JSON.parse(fs.readFileSync("/path/to/secret/key.json", "utf8")) as number[]
20+
JSON.parse(fs.readFileSync("/path/to/private-key.json", "utf8")) as number[]
1921
);
2022
// Program ID of the example contract on devnet
2123
const PROGRAM_ID = "HU64YGK66e1wdxD83D3snGuZEvfhM4YDdYShTfQvf6nm";
@@ -35,16 +37,17 @@ const connection = new Connection(SOLANA_RPC_URL, "confirmed");
3537
const payer = Keypair.fromSecretKey(PAYER_SECRET_KEY);
3638

3739
/* eslint-disable no-console */
38-
const client = new PythLazerClient(
39-
"wss://pyth-lazer-staging.dourolabs.app/v1/stream",
40-
"{access_token}"
40+
const client = await PythLazerClient.create(
41+
["wss://pyth-lazer-staging.dourolabs.app/v1/stream"],
42+
"my_token"
4143
);
4244

43-
let received = false;
45+
// data received from pyth lazer
46+
let rawData: string | undefined = undefined;
4447

4548
client.addMessageListener(async (message) => {
4649
// avoid processing multiple messages
47-
if (received) {
50+
if (rawData) {
4851
return;
4952
}
5053

@@ -64,18 +67,16 @@ client.addMessageListener(async (message) => {
6467

6568
// close the ws connection
6669
// we only need 1 message
67-
client.ws.close();
70+
client.shutdown();
6871

6972
// We are expecting a streamUpdated message
7073
if (message.value.type !== "streamUpdated") {
7174
console.log("unexpected message value type:", message.value.type);
7275
return;
7376
}
7477

75-
received = true;
76-
7778
// Extract the base64 encoded data
78-
const rawData = message.value.solana?.data;
79+
rawData = message.value.solana?.data;
7980
console.log("rawData:", rawData);
8081

8182
// Decode the base64 encoded data
@@ -160,16 +161,14 @@ client.addMessageListener(async (message) => {
160161
console.log("Transaction confirmed with signature:", signature);
161162
});
162163

163-
client.ws.addEventListener("open", () => {
164-
client.send({
165-
type: "subscribe",
166-
subscriptionId: 1,
167-
// Example contract receives ETH/USD price
168-
priceFeedIds: [2],
169-
properties: ["price"],
170-
chains: ["solana"],
171-
deliveryFormat: "json",
172-
channel: "real_time",
173-
jsonBinaryEncoding: "hex",
174-
});
164+
client.subscribe({
165+
type: "subscribe",
166+
subscriptionId: 1,
167+
// Example contract receives ETH/USD price
168+
priceFeedIds: [2],
169+
properties: ["price"],
170+
chains: ["solana"],
171+
deliveryFormat: "json",
172+
channel: "real_time",
173+
jsonBinaryEncoding: "hex",
175174
});

0 commit comments

Comments
 (0)