@@ -30,6 +30,7 @@ export class PythConnection {
3030 connection : Connection
3131 pythProgramKey : PublicKey
3232 commitment : Commitment
33+ feedIds ?: PublicKey [ ]
3334
3435 productAccountKeyToProduct : Record < string , AccountUpdate < ProductData > > = { }
3536 priceAccountKeyToProductAccountKey : Record < string , string > = { }
@@ -104,29 +105,49 @@ export class PythConnection {
104105 /** Create a PythConnection that reads its data from an underlying solana web3 connection.
105106 * pythProgramKey is the public key of the Pyth program running on the chosen solana cluster.
106107 */
107- constructor ( connection : Connection , pythProgramKey : PublicKey , commitment : Commitment = 'finalized' ) {
108+ constructor ( connection : Connection , pythProgramKey : PublicKey , commitment : Commitment = 'finalized' , feedIds ?: PublicKey [ ] ) {
108109 this . connection = connection
109110 this . pythProgramKey = pythProgramKey
110111 this . commitment = commitment
112+ this . feedIds = feedIds
111113 }
112114
113115 /** Start receiving price updates. Once this method is called, any registered callbacks will be invoked
114116 * each time a Pyth price account is updated.
115117 */
116118 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 )
118120 const currentSlot = await this . connection . getSlot ( this . commitment )
121+ // Handle all accounts once since we need to handle product accounts
122+ // at least once
119123 for ( const account of accounts ) {
120124 this . handleAccount ( account . pubkey , account . account , true , currentSlot )
121125 }
122126
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+ }
130151 }
131152
132153 /** Register callback to receive price updates. */
0 commit comments