@@ -3,9 +3,10 @@ import https from "node:https";
33import { performance } from "node:perf_hooks" ;
44import { Readable } from "node:stream" ;
55import { setTimeout as delay } from "node:timers/promises" ;
6- import { hermesFetchDuration } from "../metrics.ts" ;
7- import type { HermesResponse , PriceProducerFactoryOptions , PriceUpdate , PythPriceData } from "../types.ts" ;
8- import { validateEndpointUrl } from "../validation.ts" ;
6+ import { hermesFetchDuration } from "../../metrics.ts" ;
7+ import type { HermesResponse , PriceProducerFactoryOptions , PriceUpdate } from "../../types.ts" ;
8+ import { validateEndpointUrl } from "../../validation.ts" ;
9+ import { parsePriceUpdate } from "../utils.ts" ;
910
1011export async function * pollPriceStream ( options : PollPriceStreamOptions ) : AsyncGenerator < PriceUpdate > {
1112 if ( ! options . priceFeedId ) {
@@ -37,7 +38,6 @@ export async function *pollPriceStream(options: PollPriceStreamOptions): AsyncGe
3738 continue ;
3839 } finally {
3940 hermesFetchDuration . record ( performance . now ( ) - fetchStart , { status } ) ;
40- console . log ( `Fetch from Hermes completed with status ${ status } in ${ performance . now ( ) - fetchStart } ms` ) ;
4141 }
4242
4343 if ( ! response . ok ) {
@@ -48,32 +48,22 @@ export async function *pollPriceStream(options: PollPriceStreamOptions): AsyncGe
4848 continue ;
4949 }
5050
51- const data = await response . json ( ) as HermesResponse ;
52-
53- if ( ! data . parsed || data . parsed . length === 0 ) {
54- options . logger ?. error ( "No price data returned from Hermes" ) ;
51+ let parsedData : HermesResponse ;
52+ try {
53+ parsedData = await response . json ( ) as HermesResponse ;
54+ } catch ( error ) {
55+ options . logger ?. error ( `Error parsing JSON from Hermes: ${ ( error as Error ) . message } ` ) ;
5556 continue ;
5657 }
5758
58- if ( ! data . binary ?. data || data . binary . data . length === 0 ) {
59- options . logger ?. error ( "No VAA binary data returned from Hermes" ) ;
59+ const priceUpdateResult = parsePriceUpdate ( parsedData ) ;
60+
61+ if ( ! priceUpdateResult . ok ) {
62+ options . logger ?. error ( priceUpdateResult . message ) ;
6063 continue ;
6164 }
6265
63- const priceData : PythPriceData = data . parsed [ 0 ] ;
64- const vaa : string = data . binary . data [ 0 ] ;
65-
66- options . logger ?. log (
67- `Fetched price from Hermes: ${ priceData . price . price } (expo: ${ priceData . price . expo } )` ,
68- ) ;
69- options . logger ?. log (
70- ` Confidence: ${ priceData . price . conf } , Publish time: ${ priceData . price . publish_time } ` ,
71- ) ;
72- options . logger ?. log (
73- ` VAA size: ${ vaa . length } bytes (base64)` ,
74- ) ;
75-
76- yield { priceData, vaa } ;
66+ yield priceUpdateResult . value ;
7767 if ( options . pollingIntervalMs > 0 ) {
7868 await delay ( options . pollingIntervalMs , undefined , { signal : options . signal } )
7969 . catch ( ( error ) => options . logger ?. warn ( `Polling delay interrupted: ${ ( error as Error ) . message } ` ) ) ;
0 commit comments