@@ -54,15 +54,14 @@ export class EvmPriceListener implements PriceListener {
5454 this . startSubscription ( ) ;
5555 } else {
5656 console . log (
57- "The target network RPC endpoint is not Websocket. Using polling instead..."
57+ "The target network RPC endpoint is not Websocket. " +
58+ "Listening for updates only via polling...."
5859 ) ;
59- setInterval ( this . pollPrices . bind ( this ) , this . pollingFrequency * 1000 ) ;
6060 }
6161
62- // Poll the prices to have values in the beginning until updates arrive.
63- console . log (
64- "Polling the prices in the beginning in order to set the initial values."
65- ) ;
62+ console . log ( `Polling the prices every ${ this . pollingFrequency } seconds...` ) ;
63+ setInterval ( this . pollPrices . bind ( this ) , this . pollingFrequency * 1000 ) ;
64+
6665 await this . pollPrices ( ) ;
6766 }
6867
@@ -100,15 +99,15 @@ export class EvmPriceListener implements PriceListener {
10099 publishTime : Number ( event . returnValues . publishTime ) ,
101100 } ;
102101
103- this . latestPriceInfo . set ( priceId , priceInfo ) ;
102+ this . updateLatestPriceInfo ( priceId , priceInfo ) ;
104103 }
105104
106105 private async pollPrices ( ) {
107106 console . log ( "Polling evm prices..." ) ;
108107 for ( const priceId of this . priceIds ) {
109108 const currentPriceInfo = await this . getOnChainPriceInfo ( priceId ) ;
110109 if ( currentPriceInfo !== undefined ) {
111- this . latestPriceInfo . set ( priceId , currentPriceInfo ) ;
110+ this . updateLatestPriceInfo ( priceId , currentPriceInfo ) ;
112111 }
113112 }
114113 }
@@ -134,7 +133,23 @@ export class EvmPriceListener implements PriceListener {
134133 return {
135134 conf : priceRaw . conf ,
136135 price : priceRaw . price ,
137- publishTime : priceRaw . publishTime ,
136+ publishTime : Number ( priceRaw . publishTime ) ,
138137 } ;
139138 }
139+
140+ private updateLatestPriceInfo ( priceId : HexString , observedPrice : PriceInfo ) {
141+ const cachedLatestPriceInfo = this . getLatestPriceInfo ( priceId ) ;
142+
143+ // Ignore the observed price if the cache already has newer
144+ // price. This could happen because we are using polling and
145+ // subscription at the same time.
146+ if (
147+ cachedLatestPriceInfo !== undefined &&
148+ cachedLatestPriceInfo . publishTime > observedPrice . publishTime
149+ ) {
150+ return ;
151+ }
152+
153+ this . latestPriceInfo . set ( priceId , observedPrice ) ;
154+ }
140155}
0 commit comments