Skip to content

Commit

Permalink
fix promise nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Jan 11, 2025
1 parent a63bf04 commit 2161690
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions api/cron-cache-gas-prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ const handler = async (
chainId: number,
outputTokenAddress?: string
): Promise<void> => {
const secondsPerUpdateForChain =
updateIntervalsSecPerChain[
chainId as keyof typeof updateIntervalsSecPerChain
] || updateIntervalsSecPerChain.default;
const secondsPerUpdateForChain = updateIntervalsSecPerChain.default;
const cache = latestGasPriceCache(
chainId,
outputTokenAddress
Expand Down Expand Up @@ -126,7 +123,7 @@ const handler = async (
const updateL1DataFeePromise = async (
chainId: number,
outputTokenAddress: string
) => {
): Promise<void> => {
const secondsPerUpdate = updateL1DataFeeIntervalsSecPerChain.default;
const depositArgs = getDepositArgsForChainId(chainId, outputTokenAddress);
const gasCostCache = getCachedNativeGasCost(depositArgs);
Expand All @@ -146,34 +143,41 @@ const handler = async (
}
};

const lineaDestinationRoutes = availableRoutes.filter(
({ destinationChainId }) => destinationChainId === CHAIN_IDs.LINEA
);
// The minimum interval for Vercel Serverless Functions cron jobs is 1 minute.
// But we want to update gas data more frequently than that.
// To circumvent this, we run the function in a loop and update gas prices every
// `secondsPerUpdateForChain` seconds and stop after `maxDurationSec` seconds (1 minute).
await Promise.all([
// @dev Linea gas prices are dependent on the L2 calldata to be submitted so compute one gas price for each output token,
// so we compute one gas price per output token for Linea
mainnetChains
.filter((chain) => chain.chainId !== CHAIN_IDs.LINEA)
.map((chain) => updateGasPricePromise(chain.chainId)),
availableRoutes
.filter(
({ destinationChainId }) => destinationChainId === CHAIN_IDs.LINEA
Promise.all(
mainnetChains
.filter((chain) => chain.chainId !== CHAIN_IDs.LINEA)
.map((chain) => updateGasPricePromise(chain.chainId))
),
Promise.all(
lineaDestinationRoutes.map(({ destinationToken }) =>
updateGasPricePromise(CHAIN_IDs.LINEA, destinationToken)
)
.map(({ destinationToken }) => {
updateGasPricePromise(CHAIN_IDs.LINEA, destinationToken);
}),
mainnetChains.map((chain) => {
const routesToChain = availableRoutes.filter(
({ destinationChainId }) => destinationChainId === chain.chainId
);
const outputTokensForChain = routesToChain.map(
({ destinationToken }) => destinationToken
);
outputTokensForChain.map((outputToken) =>
updateL1DataFeePromise(chain.chainId, outputToken)
);
}),
),
Promise.all(
mainnetChains.map((chain) => {
const routesToChain = availableRoutes.filter(
({ destinationChainId }) => destinationChainId === chain.chainId
);
const outputTokensForChain = routesToChain.map(
({ destinationToken }) => destinationToken
);
return Promise.all(
outputTokensForChain.map((outputToken) =>
updateL1DataFeePromise(chain.chainId, outputToken)
)
);
})
),
]);

logger.debug({
Expand Down

0 comments on commit 2161690

Please sign in to comment.