@@ -321,22 +321,46 @@ export class StatsService implements OnModuleInit {
321321 return cachedHmtPrice ;
322322 }
323323
324+ const headers = this . envConfigService . hmtPriceSourceApiKey
325+ ? { 'x-cg-demo-api-key' : this . envConfigService . hmtPriceSourceApiKey }
326+ : { } ;
327+
324328 const { data } = await lastValueFrom (
325- this . httpService . get ( this . envConfigService . hmtPriceSource , {
326- headers : {
327- 'x-cg-demo-api-key' : this . envConfigService . hmtPriceSourceApiKey ,
328- } ,
329- } ) ,
329+ this . httpService . get ( this . envConfigService . hmtPriceSource , { headers } ) ,
330330 ) ;
331- const hmtPrice =
332- data [ this . envConfigService . hmtPriceFromKey ] [
333- this . envConfigService . hmtPriceToKey
334- ] ;
331+
332+ let hmtPrice : number ;
333+
334+ if ( this . envConfigService . hmtPriceSource . includes ( 'coingecko' ) ) {
335+ if (
336+ ! data ||
337+ ! data [ this . envConfigService . hmtPriceFromKey ] ||
338+ ! data [ this . envConfigService . hmtPriceFromKey ] [
339+ this . envConfigService . hmtPriceToKey
340+ ]
341+ ) {
342+ throw new Error ( 'Failed to fetch HMT price from CoinGecko API' ) ;
343+ }
344+ hmtPrice = parseFloat (
345+ data [ this . envConfigService . hmtPriceFromKey ] [
346+ this . envConfigService . hmtPriceToKey
347+ ] ,
348+ ) ;
349+ } else if ( this . envConfigService . hmtPriceSource . includes ( 'coinlore' ) ) {
350+ if ( ! data || ! data [ 0 ] || ! data [ 0 ] . price_usd || data [ 0 ] . symbol !== 'HMT' ) {
351+ throw new Error ( 'Failed to fetch HMT price from Coinlore API' ) ;
352+ }
353+ hmtPrice = parseFloat ( data [ 0 ] . price_usd ) ;
354+ } else {
355+ throw new Error ( 'Unsupported HMT price source' ) ;
356+ }
357+
335358 await this . cacheManager . set (
336359 this . redisConfigService . hmtPriceCacheKey ,
337360 hmtPrice ,
338361 this . redisConfigService . cacheHmtPriceTTL ,
339362 ) ;
363+
340364 return hmtPrice ;
341365 }
342366
0 commit comments