import {
METHOD_NOT_FOUND,
INVALID_PARAMS,
} from '@tevm/voltaire/JsonRpcError';
async function fetchWithRetry<T>(
fn: () => Promise<T>,
maxRetries: number = 3
): Promise<T> {
const NON_RETRYABLE_ERRORS = new Set([
METHOD_NOT_FOUND, // Method will never exist
INVALID_PARAMS, // Parameters are wrong
]);
for (let i = 0; i < maxRetries; i++) {
try {
return await fn();
} catch (error) {
// Don't retry if error is non-retryable or last attempt
if (NON_RETRYABLE_ERRORS.has(error.code) || i === maxRetries - 1) {
throw error;
}
// Exponential backoff: 1s, 2s, 4s
await new Promise(resolve => setTimeout(resolve, 1000 * Math.pow(2, i)));
}
}
throw new Error('Max retries exceeded');
}