Skip to content

Commit

Permalink
Add native gas cost caching
Browse files Browse the repository at this point in the history
Keep cache warm
  • Loading branch information
nicholaspai committed Jan 11, 2025
1 parent 035ef1b commit 7449e8c
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions api/cron-cache-gas-prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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<void> => {
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
);
Expand Down Expand Up @@ -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)
)
),
]);
})
),
]);
Expand Down

0 comments on commit 7449e8c

Please sign in to comment.