From 8928a68e85e93ffb2e2014df89e9e573c0dff04b Mon Sep 17 00:00:00 2001 From: forbesus Date: Wed, 2 Oct 2024 09:34:40 -0700 Subject: [PATCH] fix: add logging util --- apps/api/src/routes/v1/dashboardData.ts | 15 +++--------- .../src/services/external/apiNodeService.ts | 24 +++++-------------- apps/api/src/utils/logging.ts | 12 ++++++++++ 3 files changed, 21 insertions(+), 30 deletions(-) create mode 100644 apps/api/src/utils/logging.ts diff --git a/apps/api/src/routes/v1/dashboardData.ts b/apps/api/src/routes/v1/dashboardData.ts index 2be9beba8..99b600eed 100644 --- a/apps/api/src/routes/v1/dashboardData.ts +++ b/apps/api/src/routes/v1/dashboardData.ts @@ -1,15 +1,16 @@ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi"; -import { getSentry } from "@src/core/providers/sentry.provider"; import { LoggerService } from "@src/core/services/logger/logger.service"; import { getBlocks } from "@src/services/db/blocksService"; import { getNetworkCapacity } from "@src/services/db/providerStatusService"; import { getDashboardData, getProviderGraphData } from "@src/services/db/statsService"; import { getTransactions } from "@src/services/db/transactionsService"; import { getChainStats } from "@src/services/external/apiNodeService"; +import { createLoggingExecutor } from "@src/utils/logging"; const logger = new LoggerService({ context: "Dashboard" }); +const runOrLog = createLoggingExecutor(logger) const route = createRoute({ method: "get", @@ -179,14 +180,4 @@ export default new OpenAPIHono().openapi(route, async c => { latestBlocks, latestTransactions }); -}); - -async function runOrLog(cb: () => Promise, defaultValue?: T): Promise { - try { - return await cb(); - } catch (error) { - logger.error({ event: `Failed to fetch ${cb.name}`, error }); - getSentry().captureException(error); - return defaultValue; - } -} \ No newline at end of file +}); \ No newline at end of file diff --git a/apps/api/src/services/external/apiNodeService.ts b/apps/api/src/services/external/apiNodeService.ts index 5088b7487..2b41b09a4 100644 --- a/apps/api/src/services/external/apiNodeService.ts +++ b/apps/api/src/services/external/apiNodeService.ts @@ -6,7 +6,6 @@ import fetch from "node-fetch"; import { Op } from "sequelize"; import { cacheKeys, cacheResponse } from "@src/caching/helpers"; -import { getSentry } from "@src/core/providers/sentry.provider"; import { LoggerService } from "@src/core/services/logger/logger.service"; import { getTransactionByAddress } from "@src/services/db/transactionsService"; import { @@ -30,21 +29,12 @@ import { CosmosMintInflationResponse } from "@src/types/rest/cosmosMintInflation import { CosmosStakingPoolResponse } from "@src/types/rest/cosmosStakingPoolResponse"; import { coinToAsset } from "@src/utils/coin"; import { apiNodeUrl, averageBlockCountInAMonth, betaTypeVersion, betaTypeVersionMarket } from "@src/utils/constants"; +import { createLoggingExecutor } from "@src/utils/logging"; import { getDeploymentRelatedMessages } from "../db/deploymentService"; import { getProviderList } from "../db/providerStatusService"; export async function getChainStats() { - const createLoggingExecutor = (logger: LoggerService) => async (cb: () => Promise, defaultValue?: T): Promise => { - try { - return await cb(); - } catch (error) { - logger.error({ event: `Failed to fetch ${cb.name}`, error }); - getSentry().captureException(error); - return defaultValue; - } - } - - const logger = new LoggerService() + const logger = new LoggerService({ context: "ApiNode" }) const runOrLog = createLoggingExecutor(logger) const result = await cacheResponse( @@ -101,11 +91,9 @@ export async function getChainStats() { true ); - let stakingAPR; - if (result.bondedTokens && result.inflation && result.communityTax && result.totalSupply) { - if (result.bondedTokens > 0) { - stakingAPR = result.inflation * (1 - result.communityTax) * result.totalSupply / result.bondedTokens - } + let stakingAPR: number | undefined; + if (result.bondedTokens && result.bondedTokens > 0 && result.inflation && result.communityTax && result.totalSupply) { + stakingAPR = result.inflation * (1 - result.communityTax) * result.totalSupply / result.bondedTokens } return { @@ -113,7 +101,7 @@ export async function getChainStats() { totalSupply: result.totalSupply, communityPool: result.communityPool, inflation: result.inflation, - stakingAPR: stakingAPR, + stakingAPR, }; } diff --git a/apps/api/src/utils/logging.ts b/apps/api/src/utils/logging.ts new file mode 100644 index 000000000..7ccef6e91 --- /dev/null +++ b/apps/api/src/utils/logging.ts @@ -0,0 +1,12 @@ +import { getSentry } from "@src/core/providers/sentry.provider"; +import { LoggerService } from "@src/core/services/logger/logger.service"; + +export const createLoggingExecutor = (logger: LoggerService) => async (cb: () => Promise, defaultValue?: T): Promise => { + try { + return await cb(); + } catch (error) { + logger.error({ event: `Failed to fetch ${cb.name}`, error }); + getSentry().captureException(error); + return defaultValue; + } +} \ No newline at end of file