@@ -155,6 +155,28 @@ export class Listener implements PriceStore {
155155 }
156156 }
157157
158+ isNewPriceInfo (
159+ cachedInfo : PriceInfo | undefined ,
160+ observedInfo : PriceInfo
161+ ) : boolean {
162+ if ( cachedInfo === undefined ) {
163+ return true ;
164+ }
165+
166+ if ( cachedInfo . attestationTime < observedInfo . attestationTime ) {
167+ return true ;
168+ }
169+
170+ if (
171+ cachedInfo . attestationTime === observedInfo . attestationTime &&
172+ cachedInfo . seqNum < observedInfo . seqNum
173+ ) {
174+ return true ;
175+ }
176+
177+ return false ;
178+ }
179+
158180 async processVaa ( vaa : Buffer ) {
159181 const { parse_vaa } = await importCoreWasm ( ) ;
160182
@@ -184,55 +206,31 @@ export class Listener implements PriceStore {
184206 return ;
185207 }
186208
187- const isAnyPriceNew = batchAttestation . priceAttestations . some (
188- ( priceAttestation ) => {
189- const key = priceAttestation . priceId ;
190- const lastAttestationTime =
191- this . priceFeedVaaMap . get ( key ) ?. attestationTime ;
192- return (
193- lastAttestationTime === undefined ||
194- lastAttestationTime < priceAttestation . attestationTime
195- ) ;
196- }
197- ) ;
198-
199- if ( ! isAnyPriceNew ) {
200- return ;
201- }
202-
203209 for ( const priceAttestation of batchAttestation . priceAttestations ) {
204210 const key = priceAttestation . priceId ;
205211
206- const lastAttestationTime =
207- this . priceFeedVaaMap . get ( key ) ?. attestationTime ;
208-
209- if (
210- lastAttestationTime === undefined ||
211- lastAttestationTime < priceAttestation . attestationTime
212- ) {
213- const priceFeed = priceAttestationToPriceFeed ( priceAttestation ) ;
214- const priceInfo = {
215- seqNum : parsedVaa . sequence ,
216- vaa,
217- publishTime : priceAttestation . publishTime ,
218- attestationTime : priceAttestation . attestationTime ,
219- priceFeed,
220- emitterChainId : parsedVaa . emitter_chain ,
221- priceServiceReceiveTime : Math . floor ( new Date ( ) . getTime ( ) / 1000 ) ,
222- } ;
212+ const priceFeed = priceAttestationToPriceFeed ( priceAttestation ) ;
213+ const priceInfo = {
214+ seqNum : parsedVaa . sequence ,
215+ vaa,
216+ publishTime : priceAttestation . publishTime ,
217+ attestationTime : priceAttestation . attestationTime ,
218+ priceFeed,
219+ emitterChainId : parsedVaa . emitter_chain ,
220+ priceServiceReceiveTime : Math . floor ( new Date ( ) . getTime ( ) / 1000 ) ,
221+ } ;
222+
223+ const cachedPriceInfo = this . priceFeedVaaMap . get ( key ) ;
224+
225+ if ( this . isNewPriceInfo ( cachedPriceInfo , priceInfo ) ) {
223226 this . priceFeedVaaMap . set ( key , priceInfo ) ;
224227
225- if ( lastAttestationTime !== undefined ) {
228+ if ( cachedPriceInfo !== undefined ) {
226229 this . promClient ?. addPriceUpdatesAttestationTimeGap (
227- priceAttestation . attestationTime - lastAttestationTime
230+ priceAttestation . attestationTime - cachedPriceInfo . attestationTime
228231 ) ;
229- }
230-
231- const lastPublishTime = this . priceFeedVaaMap . get ( key ) ?. publishTime ;
232-
233- if ( lastPublishTime !== undefined ) {
234232 this . promClient ?. addPriceUpdatesPublishTimeGap (
235- priceAttestation . publishTime - lastPublishTime
233+ priceAttestation . publishTime - cachedPriceInfo . publishTime
236234 ) ;
237235 }
238236
0 commit comments