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" ;
35import fs from "fs" ;
46
57import {
@@ -15,7 +17,7 @@ import { SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
1517
1618const SOLANA_RPC_URL = "https://api.devnet.solana.com" ;
1719const 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
2123const PROGRAM_ID = "HU64YGK66e1wdxD83D3snGuZEvfhM4YDdYShTfQvf6nm" ;
@@ -35,16 +37,17 @@ const connection = new Connection(SOLANA_RPC_URL, "confirmed");
3537const 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
4548client . 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