From 37eb6bffd87aee2230983dae37d47e079c869126 Mon Sep 17 00:00:00 2001 From: nicholaspai Date: Tue, 17 Dec 2024 18:39:27 -0500 Subject: [PATCH 1/2] feat(API): Add gas-prices endpoint Can be used to determine what gas price the API is computing --- 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..d85fb4f52 --- /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]]) + ); + + 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; From 42363328c4ad4084b9c18cbbb57cf78ffef2d261 Mon Sep 17 00:00:00 2001 From: nicholaspai Date: Tue, 17 Dec 2024 18:49:28 -0500 Subject: [PATCH 2/2] Update gas-prices.ts --- api/gas-prices.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/gas-prices.ts b/api/gas-prices.ts index d85fb4f52..0b7a10534 100644 --- a/api/gas-prices.ts +++ b/api/gas-prices.ts @@ -24,7 +24,7 @@ const handler = async ( }) ); const responseJson = Object.fromEntries( - chains.map(({ chainId }, i) => [chainId, gasPrices[i]]) + chains.map(({ chainId }, i) => [chainId, gasPrices[i].toString()]) ); logger.debug({