@@ -35,140 +35,142 @@ const connection = new Connection(SOLANA_RPC_URL, "confirmed");
35
35
36
36
// Load the payer's keypair
37
37
const payer = Keypair . fromSecretKey ( PAYER_SECRET_KEY ) ;
38
+ const main = async ( ) => {
39
+ const client = await PythLazerClient . create (
40
+ [ "wss://pyth-lazer-staging.dourolabs.app/v1/stream" ] ,
41
+ "my_token" ,
42
+ ) ;
38
43
39
- /* eslint-disable no-console */
40
- const client = await PythLazerClient . create (
41
- [ "wss://pyth-lazer-staging.dourolabs.app/v1/stream" ] ,
42
- "my_token" ,
43
- ) ;
44
-
45
- // data received from pyth lazer
46
- let rawData : string | undefined = undefined ;
47
-
48
- client . addMessageListener ( async ( message ) => {
49
- // avoid processing multiple messages
50
- if ( rawData ) {
51
- return ;
52
- }
53
-
54
- // received message from server
55
- console . log ( "got message:" , message ) ;
56
-
57
- // We are expecting a JSON messages
58
- if ( message . type !== "json" ) {
59
- console . log ( "unexpected message type:" , message . type ) ;
60
- return ;
61
- }
62
-
63
- // ignore the subscribed message
64
- if ( message . value . type === "subscribed" ) {
65
- return ;
66
- }
67
-
68
- // close the ws connection
69
- // we only need 1 message
70
- client . shutdown ( ) ;
71
-
72
- // We are expecting a streamUpdated message
73
- if ( message . value . type !== "streamUpdated" ) {
74
- console . log ( "unexpected message value type:" , message . value . type ) ;
75
- return ;
76
- }
77
-
78
- // Extract the base64 encoded data
79
- rawData = message . value . solana ?. data ;
80
- console . log ( "rawData:" , rawData ) ;
81
-
82
- // Decode the base64 encoded data
83
- const instructionMessage = Buffer . from ( rawData ! , "hex" ) ;
84
- console . log ( "instructionMessage:" , instructionMessage ) ;
85
-
86
- // Create the ed25519 instruction
87
- // The instruction index is 1
88
- // the starting offset is 9 (1 byte for the instruction index, 8 bytes for the program data)
89
- // the rest of the data is the message
90
- const ed25519Instr = createEd25519Instruction ( instructionMessage , 1 , 9 ) ;
91
- console . log ( "ed25519Instr:" , ed25519Instr ) ;
92
-
93
- // concatenate the message to the end of the instruction
94
- const verifyMessageData = Buffer . from ( [
95
- 1 ,
96
- 42 ,
97
- 0 ,
98
- 0 ,
99
- 0 ,
100
- 0 ,
101
- 0 ,
102
- 0 ,
103
- 0 ,
104
- ...instructionMessage ,
105
- ] ) ;
106
- console . log ( "verifyMessageData:" , verifyMessageData ) ;
107
-
108
- // Create the verify message instruction
109
- const verifyMessageInstr = new TransactionInstruction ( {
110
- keys : [
111
- { pubkey : payer . publicKey , isSigner : true , isWritable : true } ,
112
- {
113
- pubkey : new PublicKey ( DATA_PDA_KEY ) ,
114
- isSigner : false ,
115
- isWritable : true ,
116
- } ,
117
- {
118
- pubkey : new PublicKey ( PYTH_LAZER_SOLANA_CONTRACT_ID ) ,
119
- isSigner : false ,
120
- isWritable : true ,
121
- } ,
44
+ // data received from pyth lazer
45
+ let rawData : string | undefined = undefined ;
46
+
47
+ client . addMessageListener ( async ( message ) => {
48
+ // avoid processing multiple messages
49
+ if ( rawData ) {
50
+ return ;
51
+ }
52
+
53
+ // received message from server
54
+ console . log ( "got message:" , message ) ;
55
+
56
+ // We are expecting a JSON messages
57
+ if ( message . type !== "json" ) {
58
+ console . log ( "unexpected message type:" , message . type ) ;
59
+ return ;
60
+ }
61
+
62
+ // ignore the subscribed message
63
+ if ( message . value . type === "subscribed" ) {
64
+ return ;
65
+ }
66
+
67
+ // close the ws connection
68
+ // we only need 1 message
69
+ client . shutdown ( ) ;
70
+
71
+ // We are expecting a streamUpdated message
72
+ if ( message . value . type !== "streamUpdated" ) {
73
+ console . log ( "unexpected message value type:" , message . value . type ) ;
74
+ return ;
75
+ }
76
+
77
+ // Extract the base64 encoded data
78
+ rawData = message . value . solana ?. data ;
79
+ console . log ( "rawData:" , rawData ) ;
80
+
81
+ // Decode the base64 encoded data
82
+ const instructionMessage = Buffer . from ( rawData ! , "hex" ) ;
83
+ console . log ( "instructionMessage:" , instructionMessage ) ;
84
+
85
+ // Create the ed25519 instruction
86
+ // The instruction index is 1
87
+ // the starting offset is 9 (1 byte for the instruction index, 8 bytes for the program data)
88
+ // the rest of the data is the message
89
+ const ed25519Instr = createEd25519Instruction ( instructionMessage , 1 , 9 ) ;
90
+ console . log ( "ed25519Instr:" , ed25519Instr ) ;
91
+
92
+ // concatenate the message to the end of the instruction
93
+ const verifyMessageData = Buffer . from ( [
94
+ 1 ,
95
+ 42 ,
96
+ 0 ,
97
+ 0 ,
98
+ 0 ,
99
+ 0 ,
100
+ 0 ,
101
+ 0 ,
102
+ 0 ,
103
+ ...instructionMessage ,
104
+ ] ) ;
105
+ console . log ( "verifyMessageData:" , verifyMessageData ) ;
106
+
107
+ // Create the verify message instruction
108
+ const verifyMessageInstr = new TransactionInstruction ( {
109
+ keys : [
110
+ { pubkey : payer . publicKey , isSigner : true , isWritable : true } ,
111
+ {
112
+ pubkey : new PublicKey ( DATA_PDA_KEY ) ,
113
+ isSigner : false ,
114
+ isWritable : true ,
115
+ } ,
116
+ {
117
+ pubkey : new PublicKey ( PYTH_LAZER_SOLANA_CONTRACT_ID ) ,
118
+ isSigner : false ,
119
+ isWritable : true ,
120
+ } ,
121
+ {
122
+ pubkey : new PublicKey ( PYTH_LAZER_SOLANA_CONTRACT_STORAGE_ID ) ,
123
+ isSigner : false ,
124
+ isWritable : false ,
125
+ } ,
126
+ {
127
+ pubkey : new PublicKey ( PYTH_LAZER_SOLANA_CONTRACT_TREASURY_ID ) ,
128
+ isSigner : false ,
129
+ isWritable : true ,
130
+ } ,
131
+ // system program
132
+ { pubkey : SystemProgram . programId , isSigner : false , isWritable : false } ,
133
+ // sysvar
134
+ {
135
+ pubkey : SYSVAR_INSTRUCTIONS_PUBKEY ,
136
+ isSigner : false ,
137
+ isWritable : false ,
138
+ } ,
139
+ ] ,
140
+ programId : new PublicKey ( PROGRAM_ID ) ,
141
+ data : verifyMessageData ,
142
+ } ) ;
143
+ console . log ( "verifyMessageInstr:" , verifyMessageInstr ) ;
144
+
145
+ // Create the transaction
146
+ // 1st instruction is the ed25519 instruction
147
+ // 2nd instruction is the verify message instruction
148
+ const transaction = new Transaction ( ) . add ( ed25519Instr , verifyMessageInstr ) ;
149
+ console . log ( "transaction:" , transaction ) ;
150
+
151
+ const signature = await sendAndConfirmTransaction (
152
+ connection ,
153
+ transaction ,
154
+ [ payer ] ,
122
155
{
123
- pubkey : new PublicKey ( PYTH_LAZER_SOLANA_CONTRACT_STORAGE_ID ) ,
124
- isSigner : false ,
125
- isWritable : false ,
156
+ skipPreflight : true ,
126
157
} ,
127
- {
128
- pubkey : new PublicKey ( PYTH_LAZER_SOLANA_CONTRACT_TREASURY_ID ) ,
129
- isSigner : false ,
130
- isWritable : true ,
131
- } ,
132
- // system program
133
- { pubkey : SystemProgram . programId , isSigner : false , isWritable : false } ,
134
- // sysvar
135
- {
136
- pubkey : SYSVAR_INSTRUCTIONS_PUBKEY ,
137
- isSigner : false ,
138
- isWritable : false ,
139
- } ,
140
- ] ,
141
- programId : new PublicKey ( PROGRAM_ID ) ,
142
- data : verifyMessageData ,
158
+ ) ;
159
+
160
+ console . log ( "Transaction confirmed with signature:" , signature ) ;
143
161
} ) ;
144
- console . log ( "verifyMessageInstr:" , verifyMessageInstr ) ;
145
-
146
- // Create the transaction
147
- // 1st instruction is the ed25519 instruction
148
- // 2nd instruction is the verify message instruction
149
- const transaction = new Transaction ( ) . add ( ed25519Instr , verifyMessageInstr ) ;
150
- console . log ( "transaction:" , transaction ) ;
151
-
152
- const signature = await sendAndConfirmTransaction (
153
- connection ,
154
- transaction ,
155
- [ payer ] ,
156
- {
157
- skipPreflight : true ,
158
- } ,
159
- ) ;
160
162
161
- console . log ( "Transaction confirmed with signature:" , signature ) ;
162
- } ) ;
163
-
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" ,
174
- } ) ;
163
+ client . subscribe ( {
164
+ type : "subscribe" ,
165
+ subscriptionId : 1 ,
166
+ // Example contract receives ETH/USD price
167
+ priceFeedIds : [ 2 ] ,
168
+ properties : [ "price" ] ,
169
+ chains : [ "solana" ] ,
170
+ deliveryFormat : "json" ,
171
+ channel : "real_time" ,
172
+ jsonBinaryEncoding : "hex" ,
173
+ } ) ;
174
+ } ;
175
+
176
+ main ( ) ;
0 commit comments