From 7449e8c3e33f6d997af229050a44bfd12e2b2ce6 Mon Sep 17 00:00:00 2001 From: nicholaspai Date: Sat, 11 Jan 2025 14:18:50 -0700 Subject: [PATCH] Add native gas cost caching Keep cache warm --- api/cron-cache-gas-prices.ts | 56 ++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/api/cron-cache-gas-prices.ts b/api/cron-cache-gas-prices.ts index 4c472961c..6e5f5cb92 100644 --- a/api/cron-cache-gas-prices.ts +++ b/api/cron-cache-gas-prices.ts @@ -31,12 +31,18 @@ const updateIntervalsSecPerChain = { default: 5, }; -// Set lower than TTL in getCachedOpStackL1DataFee and getCachedNativeGasCost. +// Set lower than TTL in getCachedOpStackL1DataFee // Set lower than the L1 block time so we can try to get as up to date L1 data fees based on L1 base fees as possible. const updateL1DataFeeIntervalsSecPerChain = { default: 10, }; +// Set lower than TTL in getCachedNativeGasCost. This should rarely change so we should just make sure +// we keep this cache warm. +const updateNativeGasCostIntervalsSecPerChain = { + default: 30, +}; + const maxDurationSec = 60; const getDepositArgsForChainId = (chainId: number, tokenAddress: string) => { @@ -114,10 +120,9 @@ const handler = async ( }; /** - * @notice Updates the L1 data fee and L2 gas cost caches every `updateL1DataFeeIntervalsSecPerChain` seconds + * @notice Updates the L1 data fee gas cost cache every `updateL1DataFeeIntervalsSecPerChain` seconds * up to `maxDurationSec` seconds. - * @dev This function will also update the L2 gas costs because this value is required to get the L1 data fee. - * @param chainId Chain to estimate gas price for + * @param chainId Chain to estimate l1 data fee for * @param outputTokenAddress This output token will be used to construct a fill transaction to simulate * gas costs for. */ @@ -144,6 +149,32 @@ const handler = async ( } }; + /** + * @notice Updates the native gas cost cache every `updateNativeGasCostIntervalsSecPerChain` seconds + * up to `maxDurationSec` seconds. + * @param chainId Chain to estimate gas cost for + * @param outputTokenAddress This output token will be used to construct a fill transaction to simulate + * gas costs for. + */ + const updateNativeGasCostPromise = async ( + chainId: number, + outputTokenAddress: string + ): Promise => { + const secondsPerUpdate = updateNativeGasCostIntervalsSecPerChain.default; + const depositArgs = getDepositArgsForChainId(chainId, outputTokenAddress); + const cache = getCachedNativeGasCost(depositArgs); + + while (true) { + const diff = Date.now() - functionStart; + // Stop after `maxDurationSec` seconds + if (diff >= maxDurationSec * 1000) { + break; + } + await cache.set(); + await utils.delay(secondsPerUpdate); + } + }; + const lineaDestinationRoutes = availableRoutes.filter( ({ destinationChainId }) => destinationChainId === CHAIN_IDs.LINEA ); @@ -172,11 +203,18 @@ const handler = async ( const outputTokensForChain = routesToChain.map( ({ destinationToken }) => destinationToken ); - await Promise.all( - outputTokensForChain.map((outputToken) => - updateL1DataFeePromise(chain.chainId, outputToken) - ) - ); + await Promise.all([ + Promise.all( + outputTokensForChain.map((outputToken) => + updateNativeGasCostPromise(chain.chainId, outputToken) + ) + ), + Promise.all( + outputTokensForChain.map((outputToken) => + updateL1DataFeePromise(chain.chainId, outputToken) + ) + ), + ]); }) ), ]);