@@ -30,6 +30,7 @@ export class PythConnection {
30
30
connection : Connection
31
31
pythProgramKey : PublicKey
32
32
commitment : Commitment
33
+ feedIds ?: PublicKey [ ]
33
34
34
35
productAccountKeyToProduct : Record < string , AccountUpdate < ProductData > > = { }
35
36
priceAccountKeyToProductAccountKey : Record < string , string > = { }
@@ -104,29 +105,49 @@ export class PythConnection {
104
105
/** Create a PythConnection that reads its data from an underlying solana web3 connection.
105
106
* pythProgramKey is the public key of the Pyth program running on the chosen solana cluster.
106
107
*/
107
- constructor ( connection : Connection , pythProgramKey : PublicKey , commitment : Commitment = 'finalized' ) {
108
+ constructor ( connection : Connection , pythProgramKey : PublicKey , commitment : Commitment = 'finalized' , feedIds ?: PublicKey [ ] ) {
108
109
this . connection = connection
109
110
this . pythProgramKey = pythProgramKey
110
111
this . commitment = commitment
112
+ this . feedIds = feedIds
111
113
}
112
114
113
115
/** Start receiving price updates. Once this method is called, any registered callbacks will be invoked
114
116
* each time a Pyth price account is updated.
115
117
*/
116
118
public async start ( ) {
117
- const accounts = await this . connection . getProgramAccounts ( this . pythProgramKey , this . commitment )
119
+ let accounts = await this . connection . getProgramAccounts ( this . pythProgramKey , this . commitment )
118
120
const currentSlot = await this . connection . getSlot ( this . commitment )
121
+ // Handle all accounts once since we need to handle product accounts
122
+ // at least once
119
123
for ( const account of accounts ) {
120
124
this . handleAccount ( account . pubkey , account . account , true , currentSlot )
121
125
}
122
126
123
- this . connection . onProgramAccountChange (
124
- this . pythProgramKey ,
125
- ( keyedAccountInfo , context ) => {
126
- this . handleAccount ( keyedAccountInfo . accountId , keyedAccountInfo . accountInfo , false , context . slot )
127
- } ,
128
- this . commitment ,
129
- )
127
+ if ( this . feedIds ) {
128
+ // Filter down to only the feeds we want
129
+ const rawIDs = this . feedIds . map ( ( feed ) => feed . toString ( ) )
130
+ accounts = accounts . filter ( ( feed ) => rawIDs . includes ( feed . pubkey . toString ( ) ) )
131
+ for ( const account of accounts ) {
132
+ this . connection . onAccountChange (
133
+ account . pubkey ,
134
+
135
+ ( accountInfo , context ) => {
136
+ this . handleAccount ( account . pubkey , accountInfo , false , context . slot )
137
+ } ,
138
+ this . commitment ,
139
+ )
140
+ }
141
+
142
+ } else {
143
+ this . connection . onProgramAccountChange (
144
+ this . pythProgramKey ,
145
+ ( keyedAccountInfo , context ) => {
146
+ this . handleAccount ( keyedAccountInfo . accountId , keyedAccountInfo . accountInfo , false , context . slot )
147
+ } ,
148
+ this . commitment ,
149
+ )
150
+ }
130
151
}
131
152
132
153
/** Register callback to receive price updates. */
0 commit comments