Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

4.1 KB
Raw Blame
// The retry classifier that keeps the public RPC's load shedding from aborting
// a cold-start backfill. The -32602 case is the one measured live: the public
// RPC answers a valid eth_getLogs with "Missing or invalid parameters" under
// load, and the identical request succeeds on the next attempt.

import { test } from 'node:test';
import assert from 'node:assert/strict';

import { isTransientRpcError, withRpcRetry } from '../src/rpc.js';

test('isTransientRpcError: the public RPC load-shed and throttle responses retry', () => {
	assert.equal(isTransientRpcError({ code: -32602, message: 'Missing or invalid parameters.' }), true);
	assert.equal(isTransientRpcError({ code: -32603, message: 'Internal error' }), true);
	assert.equal(isTransientRpcError({ code: 429, message: 'Too Many Requests' }), true);
	assert.equal(isTransientRpcError({ status: 503, message: 'Service Unavailable' }), true);
	assert.equal(isTransientRpcError(new TypeError('fetch failed')), true);
	assert.equal(isTransientRpcError({ code: 'ECONNRESET', message: 'socket hang up' }), true);
});

test('isTransientRpcError: a caller mistake is not retried', () => {
	assert.equal(isTransientRpcError(new Error('Encoded error signature not found on ABI')), false);
	assert.equal(isTransientRpcError({ code: 3, message: 'execution reverted' }), false);
	assert.equal(isTransientRpcError(null), false);
	const aborted = new Error('aborted');
	aborted.name = 'AbortError';
	assert.equal(isTransientRpcError(aborted), false);
});

test('isTransientRpcError: unwraps a nested cause (viem wraps the transport error)', () => {
	const wrapped = new Error('RPC Request failed.');
	wrapped.cause = { code: -32602, message: 'Missing or invalid parameters.' };
	assert.equal(isTransientRpcError(wrapped), true);
});

test('withRpcRetry: retries a transient failure and returns the eventual result', async () => {
	let calls = 0;
	const result = await withRpcRetry(async () => {
		calls++;
		if (calls < 3) throw { code: -32602, message: 'Missing or invalid parameters.' };
		return ['log'];
	}, { baseDelayMs: 1 });

	assert.deepEqual(result, ['log']);
	assert.equal(calls, 3);
});

test('withRpcRetry: reports each retry with an increasing attempt number', async () => {
	const seen = [];
	await withRpcRetry(async () => {
		if (seen.length < 2) throw { code: 429, message: 'Too Many Requests' };
		return 'ok';
	}, { baseDelayMs: 1, onRetry: (info) => seen.push(info.attempt) });

	assert.deepEqual(seen, [1, 2]);
});

test('withRpcRetry: gives up after the attempt cap and rethrows the last error', async () => {
	let calls = 0;
	await assert.rejects(
		() => withRpcRetry(async () => { calls++; throw { code: -32602, message: 'shed' }; }, { attempts: 3, baseDelayMs: 1 }),
		(err) => err.message === 'shed',
	);
	assert.equal(calls, 3);
});

test('withRpcRetry: a non-transient error throws immediately, no retries', async () => {
	let calls = 0;
	await assert.rejects(
		() => withRpcRetry(async () => { calls++; throw new Error('bad ABI'); }, { baseDelayMs: 1 }),
		/bad ABI/,
	);
	assert.equal(calls, 1);
});

test('rateLimitWaitMs reads the provider reset window, caps it, and ignores other errors', async () => {
	const { rateLimitWaitMs } = await import('../src/rpc.js');
	assert.equal(rateLimitWaitMs(Object.assign(new Error('x'), { cause: { code: 429, message: 'Rate Limit Hit, limit will reset in 60 seconds' } })), 60_000);
	assert.equal(rateLimitWaitMs(new Error('Rate Limit Hit')), 10_000);
	assert.equal(rateLimitWaitMs(new Error('limit will reset in 900 seconds, rate limit')), 65_000);
	assert.equal(rateLimitWaitMs(new Error('execution reverted')), null);
});

test('withRpcPatience waits out a rate limit window and then succeeds', async () => {
	const { withRpcPatience } = await import('../src/rpc.js');
	let calls = 0;
	const waits = [];
	const out = await withRpcPatience(async () => {
		calls += 1;
		if (calls <= 4) throw Object.assign(new Error('Rate Limit Hit, limit will reset in 30 seconds'), { status: 429 });
		return 'ok';
	}, { baseDelayMs: 1, sleep: async (ms) => { waits.push(ms); } });
	assert.equal(out, 'ok');
	assert.deepEqual(waits, [30_000]);
});