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.

fix(solana): stop a deaf subscription from looking like a quiet market

nirholas3825e98c112f
diff --git a/src/proof-of-custody/verifier.js b/src/proof-of-custody/verifier.js
index 10c02c3c0..031676dd3 100644
--- a/src/proof-of-custody/verifier.js
+++ b/src/proof-of-custody/verifier.js
@@ -27,6 +27,11 @@ const PUBLIC_RPC = {
mainnet: ['https://api.mainnet-beta.solana.com', 'https://solana-rpc.publicnode.com'],
};
+// Per-endpoint deadline. Generous enough for a cold getTransaction on a free
+// public node, short enough that walking all three endpoints stays inside the
+// patience of someone watching a verification badge.
+const RPC_ATTEMPT_TIMEOUT_MS = 10_000;
+
function rpcEndpoints(network) {
const base = PUBLIC_RPC[network] || PUBLIC_RPC.mainnet;
return [...base, `/api/solana-rpc?network=${encodeURIComponent(network)}`];
@@ -47,6 +52,11 @@ async function fetchTransaction(signature, network) {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body),
+ // Without a per-attempt deadline the ordered endpoint list is
+ // decorative: the first host to accept the connection and never
+ // answer holds the whole chain behind it, and the verifier sits on
+ // "Checking the chain…" instead of moving to the next endpoint.
+ signal: AbortSignal.timeout(RPC_ATTEMPT_TIMEOUT_MS),
});
if (!res.ok) { lastErr = new Error(`rpc ${res.status}`); continue; }
const j = await res.json();
diff --git a/src/pump/pumpkit-whale.js b/src/pump/pumpkit-whale.js
index e3e1e9fe2..4ac9a767b 100644
--- a/src/pump/pumpkit-whale.js
+++ b/src/pump/pumpkit-whale.js
@@ -12,13 +12,91 @@ import { getSolPriceUsd } from '../shared/usd-price.js';
// whale feed used to be dead on arrival with nothing on screen saying why. Every
// other browser Solana caller goes through our own proxy, which fronts the
// keyed, rotating server chain; do the same here and keep the public endpoint
-// only for a non-browser caller that has no origin to proxy from.
-const RPC_MAINNET = (() => {
+// as the fallback for a non-browser caller that has no origin to proxy from,
+// and for the case where our own proxy is the thing that is down.
+const RPC_HTTP_ENDPOINTS = (() => {
+ const list = [];
try {
- if (typeof location !== 'undefined' && location.origin) return `${location.origin}/api/solana-rpc`;
- } catch { /* fall through to the public endpoint */ }
- return 'https://api.mainnet-beta.solana.com';
+ if (typeof location !== 'undefined' && location.origin) list.push(`${location.origin}/api/solana-rpc`);
+ } catch { /* no origin to proxy from; the public endpoint below still works */ }
+ list.push('https://api.mainnet-beta.solana.com');
+ return list;
})();
+
+// The subscription leg is a SEPARATE list, and it has to be. /api/solana-rpc is
+// an HTTP-only proxy, so web3.js deriving its wsEndpoint from the http one
+// (wss://<origin>/api/solana-rpc) can never complete a logsSubscribe: the whale
+// feed would look calm forever instead of reporting that it is deaf. These are
+// the same free WS hosts src/pump/wallet-monitor.js rotates across.
+const WS_ENDPOINTS = [
+ 'wss://api.mainnet-beta.solana.com',
+ 'wss://solana-rpc.publicnode.com',
+ 'wss://solana.drpc.org',
+];
+
+const RPC_ATTEMPT_TIMEOUT_MS = 10_000;
+const WS_PROBE_TIMEOUT_MS = 6_000;
+
+// A fetch for the Connection that walks RPC_HTTP_ENDPOINTS in order with a
+// per-attempt deadline. Without it a single stalled proxy hung every RPC read
+// the whale feed makes, with no second chance and no way to time out.
+function rotatingRpcFetch(endpoints) {
+ return async function rpcFetch(_url, init) {
+ let lastErr;
+ for (const endpoint of endpoints) {
+ try {
+ const res = await fetch(endpoint, { ...init, signal: AbortSignal.timeout(RPC_ATTEMPT_TIMEOUT_MS) });
+ if (res.ok) return res;
+ lastErr = new Error(`${endpoint} responded ${res.status}`);
+ } catch (err) {
+ lastErr = err;
+ }
+ }
+ throw new Error(`all ${endpoints.length} Solana RPC endpoints failed (${lastErr?.message || 'unknown'})`, {
+ cause: lastErr,
+ });
+ };
+}
+
+/**
+ * The first WS endpoint that completes a handshake, or null when none does.
+ *
+ * Only runs in a browser: outside one there is no blocked-origin problem to
+ * route around, the first endpoint is reachable, and a probe would turn every
+ * test run into a live network call.
+ *
+ * @param {string[]} endpoints
+ * @param {(url: string) => WebSocket} [factory] Injectable for tests.
+ */
+export async function pickWsEndpoint(endpoints, factory) {
+ const open = factory || (typeof WebSocket !== 'undefined' ? (url) => new WebSocket(url) : null);
+ if (!open) return endpoints[0] ?? null;
+ for (const url of endpoints) {
+ const reached = await new Promise((resolve) => {
+ let settled = false;
+ let sock;
+ const done = (ok) => {
+ if (settled) return;
+ settled = true;
+ clearTimeout(timer);
+ if (!ok) { try { sock?.close(); } catch { /* already closing */ } }
+ resolve(ok);
+ };
+ const timer = setTimeout(() => done(false), WS_PROBE_TIMEOUT_MS);
+ try {
+ sock = open(url);
+ } catch {
+ done(false);
+ return;
+ }
+ sock.onopen = () => { try { sock.close(); } catch { /* already closing */ } done(true); };
+ sock.onerror = () => done(false);
+ sock.onclose = () => done(false);
+ });
+ if (reached) return url;
+ }
+ return null;
+}
const PUMP_PROGRAM = '6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P';
const LAMPORTS_PER_SOL = 1_000_000_000;
const NATIVE_SOL = 'So11111111111111111111111111111111111111112';
@@ -59,7 +137,25 @@ export async function watchWhaleTrades({ mint, minUsd = 5000, onTrade, onStatus,
if (signal?.aborted) return;
- const connection = new Connection(RPC_MAINNET, 'confirmed');
+ // In a browser, find a WS host that actually answers before subscribing; on
+ // the server the first entry is reachable and no probe is needed.
+ const wsEndpoint = (typeof window !== 'undefined')
+ ? await pickWsEndpoint(WS_ENDPOINTS)
+ : WS_ENDPOINTS[0];
+ if (signal?.aborted) return;
+ if (!wsEndpoint) {
+ onStatus?.({
+ code: 'subscription_unavailable',
+ message: 'No Solana WebSocket endpoint accepted a connection, so live whale trades cannot be streamed right now.',
+ });
+ return;
+ }
+
+ const connection = new Connection(RPC_HTTP_ENDPOINTS[0], {
+ commitment: 'confirmed',
+ wsEndpoint,
+ fetch: rotatingRpcFetch(RPC_HTTP_ENDPOINTS),
+ });
const coder = new BorshCoder(pumpIdl);
const parser = new EventParser(PUMP_PROGRAM_ID, coder);
const mintStr = mint instanceof PublicKey ? mint.toBase58() : String(mint);
diff --git a/src/pump/wallet-monitor.js b/src/pump/wallet-monitor.js
index a96b42cc6..b5d95e17c 100644
--- a/src/pump/wallet-monitor.js
+++ b/src/pump/wallet-monitor.js
@@ -27,6 +27,15 @@ const WS_URLS = {
],
};
+// How long an endpoint gets to open its socket AND confirm the logsSubscribe.
+// Rotation used to be driven entirely by onclose, so a host that accepted the
+// TCP connection and then never answered the subscribe (a captive portal, a
+// rate-limited node holding the connection open, a stalled proxy) left the
+// monitor connected, silent, and permanently blind: no error, no rotation, no
+// trades. The deadline force-closes that socket, which routes it through the
+// existing unsubscribed-rotation path.
+const SUBSCRIBE_TIMEOUT_MS = 12_000;
+
// Parse pump.fun structured log line: "Program log: {...json...}"
function extractLogJson(log) {
if (!log.startsWith('Program log: {')) return null;
@@ -83,17 +92,31 @@ export class WalletMonitor extends EventTarget {
stop() {
this._closed = true;
- this._ws?.close();
+ try { this._ws?.close(); } catch { /* already closing */ }
this._ws = null;
}
_connect() {
if (this._closed) return;
- const ws = new WebSocket(this._wsUrls[this._urlIdx % this._wsUrls.length]);
+ const url = this._wsUrls[this._urlIdx % this._wsUrls.length];
+ const ws = new WebSocket(url);
this._ws = ws;
let subscribed = false;
+ const subscribeDeadline = setTimeout(() => {
+ if (subscribed || this._closed) return;
+ log.warn('[wallet-monitor] no subscription confirmation from', url, `within ${SUBSCRIBE_TIMEOUT_MS}ms; rotating endpoint`);
+ // close() fires onclose, which rotates and reconnects. Guard against a
+ // socket wedged in CONNECTING that never emits either event.
+ try { ws.close(); } catch { /* already closing */ }
+ if (this._ws === ws) {
+ this._ws = null;
+ this._urlIdx++;
+ setTimeout(() => this._connect(), 0);
+ }
+ }, SUBSCRIBE_TIMEOUT_MS);
+
ws.onopen = () => {
this._reconnectMs = 1000;
ws.send(JSON.stringify({
@@ -115,6 +138,7 @@ export class WalletMonitor extends EventTarget {
if (msg.id != null && typeof msg.result === 'number') {
this._subId = msg.result;
subscribed = true;
+ clearTimeout(subscribeDeadline);
return;
}
@@ -137,12 +161,14 @@ export class WalletMonitor extends EventTarget {
// diagnostics only, so a dead endpoint is visible instead of silent.
log.warn(
'[wallet-monitor] websocket error on',
- this._wsUrls[this._urlIdx % this._wsUrls.length],
+ url,
subscribed ? '(subscribed; will back off and reconnect)' : '(unsubscribed; rotating endpoint)',
);
};
ws.onclose = () => {
+ clearTimeout(subscribeDeadline);
+ if (this._ws !== ws) return; // the deadline already rotated past this socket
this._ws = null;
// Never got a confirmed subscription on this endpoint — try the next
// one immediately rather than backing off against a dead host.
diff --git a/tests/pump-ws-subscription-failover.test.js b/tests/pump-ws-subscription-failover.test.js
new file mode 100644
index 000000000..991899023
--- /dev/null
+++ b/tests/pump-ws-subscription-failover.test.js
@@ -0,0 +1,147 @@
+// Two browser Solana subscriptions had no way to notice they were deaf.
+//
+// WalletMonitor rotated endpoints only from `onclose`, so a host that accepted
+// the socket and then never answered the logsSubscribe left the monitor
+// connected, silent and permanently blind: no error, no rotation, no trades. A
+// rate-limited node holding a connection open does exactly that.
+//
+// watchWhaleTrades had the mirror-image problem. It builds its Connection on
+// /api/solana-rpc, which is an HTTP-only proxy, so web3.js derived a wsEndpoint
+// of wss://<origin>/api/solana-rpc that can never complete a subscribe. The
+// feed looked like a quiet market forever.
+//
+// Both fixes are timing/rotation logic with no network in them, so both are
+// pinned here.
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
+
+// A WebSocket stub whose lifecycle the test drives by hand.
+class FakeSocket {
+ constructor(url) {
+ this.url = url;
+ this.sent = [];
+ this.closed = false;
+ this.onopen = null;
+ this.onmessage = null;
+ this.onerror = null;
+ this.onclose = null;
+ FakeSocket.created.push(this);
+ }
+ send(data) { this.sent.push(data); }
+ close() {
+ if (this.closed) return;
+ this.closed = true;
+ // Real sockets deliver onclose asynchronously; mirror that so the code
+ // under test is exercised the way a browser runs it.
+ setTimeout(() => this.onclose?.(), 0);
+ }
+ /** Simulate the node answering the subscribe request. */
+ confirmSubscription(id = 7) {
+ this.onmessage?.({ data: JSON.stringify({ jsonrpc: '2.0', id: 1, result: id }) });
+ }
+}
+FakeSocket.created = [];
+
+describe('WalletMonitor rotates off a socket that never confirms its subscription', () => {
+ let realWebSocket;
+
+ beforeEach(() => {
+ FakeSocket.created = [];
+ realWebSocket = globalThis.WebSocket;
+ globalThis.WebSocket = FakeSocket;
+ vi.useFakeTimers();
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ globalThis.WebSocket = realWebSocket;
+ });
+
+ it('opens the next endpoint when the subscribe is never answered', async () => {
+ const { WalletMonitor } = await import('../src/pump/wallet-monitor.js');
+ const mon = new WalletMonitor('Wa11etAddress1111111111111111111111111111');
+ mon.start();
+
+ expect(FakeSocket.created).toHaveLength(1);
+ const first = FakeSocket.created[0];
+ first.onopen();
+ expect(first.sent).toHaveLength(1);
+ expect(JSON.parse(first.sent[0]).method).toBe('logsSubscribe');
+
+ // The node accepted the socket and then said nothing at all.
+ await vi.advanceTimersByTimeAsync(20_000);
+
+ expect(first.closed).toBe(true);
+ expect(FakeSocket.created.length).toBeGreaterThan(1);
+ expect(FakeSocket.created[1].url).not.toBe(first.url);
+ mon.stop();
+ });
+
+ it('leaves a confirmed subscription alone once the deadline passes', async () => {
+ const { WalletMonitor } = await import('../src/pump/wallet-monitor.js');
+ const mon = new WalletMonitor('Wa11etAddress1111111111111111111111111111');
+ mon.start();
+
+ const first = FakeSocket.created[0];
+ first.onopen();
+ first.confirmSubscription();
+
+ await vi.advanceTimersByTimeAsync(60_000);
+
+ expect(first.closed).toBe(false);
+ expect(FakeSocket.created).toHaveLength(1);
+ mon.stop();
+ });
+
+ it('rotates only once per dead socket, never double-counting the endpoint', async () => {
+ const { WalletMonitor } = await import('../src/pump/wallet-monitor.js');
+ const mon = new WalletMonitor('Wa11etAddress1111111111111111111111111111');
+ mon.start();
+
+ FakeSocket.created[0].onopen();
+ await vi.advanceTimersByTimeAsync(20_000);
+
+ // The deadline closed the socket, and the resulting onclose must not
+ // advance the endpoint a second time and skip a healthy host.
+ const second = FakeSocket.created[1];
+ expect(second.url).toBe('wss://solana-rpc.publicnode.com');
+ mon.stop();
+ });
+});
+
+describe('pickWsEndpoint finds a WS host that actually answers', () => {
+ it('returns the first endpoint whose socket opens', async () => {
+ const { pickWsEndpoint } = await import('../src/pump/pumpkit-whale.js');
+ const attempted = [];
+ const factory = (url) => {
+ attempted.push(url);
+ const sock = new FakeSocket(url);
+ // The first host refuses; the second one answers.
+ setTimeout(() => (attempted.length === 1 ? sock.onerror?.() : sock.onopen?.()), 0);
+ return sock;
+ };
+ const picked = await pickWsEndpoint(['wss://dead.example', 'wss://live.example'], factory);
+ expect(picked).toBe('wss://live.example');
+ expect(attempted).toEqual(['wss://dead.example', 'wss://live.example']);
+ });
+
+ it('returns null when no endpoint answers, so the caller can say the feed is deaf', async () => {
+ const { pickWsEndpoint } = await import('../src/pump/pumpkit-whale.js');
+ const factory = (url) => {
+ const sock = new FakeSocket(url);
+ setTimeout(() => sock.onerror?.(), 0);
+ return sock;
+ };
+ expect(await pickWsEndpoint(['wss://a.example', 'wss://b.example'], factory)).toBeNull();
+ });
+
+ it('treats a socket that throws on construction as a dead endpoint', async () => {
+ const { pickWsEndpoint } = await import('../src/pump/pumpkit-whale.js');
+ const factory = (url) => {
+ if (url === 'wss://blocked.example') throw new Error('blocked by content security policy');
+ const sock = new FakeSocket(url);
+ setTimeout(() => sock.onopen?.(), 0);
+ return sock;
+ };
+ expect(await pickWsEndpoint(['wss://blocked.example', 'wss://ok.example'], factory)).toBe('wss://ok.example');
+ });
+});