igneum/site/api/_neon.mjs

29 lines
1.7 KiB
JavaScript

// Shared by the public read endpoints (stats, supply, explorer): Neon's HTTP SQL endpoint over Node's fetch, the
// same pattern as live.mjs. Returns rows. A file starting with an underscore is not deployed as a function by Vercel.
export function neon(url = process.env.DATABASE_URL) {
if (!url) throw new Error('DATABASE_URL is not set');
const host = new URL(url).hostname.replace('-pooler', '');
return async (query, params = []) => {
const r = await fetch(`https://${host}/sql`, {
method: 'POST',
headers: { 'Neon-Connection-String': url, 'Content-Type': 'application/json' },
body: JSON.stringify({ query, params }),
});
const j = await r.json();
if (!r.ok) throw new Error(j.message || JSON.stringify(j));
return j.rows || [];
};
}
export const num = v => (v === null || v === undefined ? null : Number(v));
// LIVE_TABLE_PREFIX names the observer whose tables are read; unset means dn4_ (the Igneum 2.0 devnet, the igneum-observer-dn4 unit on the build
// box, the same default as live.mjs since 8 October 2026: before that the explorer, stats and supply read the first devnet's tables
// while /api/live read the earlier devnet). An empty string reads the unprefixed tables.
export const tablePrefix = () => (process.env.LIVE_TABLE_PREFIX === undefined ? 'dn4_' : process.env.LIVE_TABLE_PREFIX).replace(/[^a-z0-9_]/gi, '');
/** EVM chain id from the network name (docs/design/execution-layer.md 8.1: 4461 mainnet, 4462 testnet, 4463 devnet). */
export function chainIdOf(network) {
const n = String(network || '');
if (n.includes('devnet')) return 4463;
if (n.includes('testnet')) return 4462;
if (n.includes('simnet')) return null;
return 4461;
}