From 177ef0f3f3c40ffd10cb3ca80b959724327c756a Mon Sep 17 00:00:00 2001 From: nicholaspai <9457025+nicholaspai@users.noreply.github.com> Date: Fri, 3 Jan 2025 15:20:17 -0500 Subject: [PATCH] feat(API): Add gas-prices endpoint (#1335) * feat(API): Add gas-prices endpoint Can be used to determine what gas price the API is computing * Update gas-prices.ts --- api/gas-prices.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 api/gas-prices.ts diff --git a/api/gas-prices.ts b/api/gas-prices.ts new file mode 100644 index 000000000..0b7a10534 --- /dev/null +++ b/api/gas-prices.ts @@ -0,0 +1,43 @@ +import { VercelResponse } from "@vercel/node"; +import { + getLogger, + handleErrorCondition, + latestGasPriceCache, + sendResponse, +} from "./_utils"; +import { TypedVercelRequest } from "./_types"; + +import mainnetChains from "../src/data/chains_1.json"; + +const chains = mainnetChains; + +const handler = async ( + _: TypedVercelRequest>, + response: VercelResponse +) => { + const logger = getLogger(); + + try { + const gasPrices = await Promise.all( + chains.map(({ chainId }) => { + return latestGasPriceCache(chainId).get(); + }) + ); + const responseJson = Object.fromEntries( + chains.map(({ chainId }, i) => [chainId, gasPrices[i].toString()]) + ); + + logger.debug({ + at: "GasPrices", + message: "Response data", + responseJson, + }); + // Respond with a 200 status code and 10 seconds of cache with + // 45 seconds of stale-while-revalidate. + sendResponse(response, responseJson, 200, 10, 45); + } catch (error: unknown) { + return handleErrorCondition("gas-prices", response, logger, error); + } +}; + +export default handler;