Skip to content

Commit

Permalink
feat(API): Add gas-prices endpoint
Browse files Browse the repository at this point in the history
Can be used to determine what gas price the API is computing
  • Loading branch information
nicholaspai committed Dec 17, 2024
1 parent 7a57b12 commit 37eb6bf
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions api/gas-prices.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, never>>,
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;

0 comments on commit 37eb6bf

Please sign in to comment.