Files
the-nexus/modules/data/bitcoin.js

33 lines
1018 B
JavaScript
Raw Normal View History

// modules/data/bitcoin.js — Blockstream block height polling
// Writes to S: lastKnownBlockHeight, _starPulseIntensity (legacy)
// Writes to state: blockHeight, lastBlockHeight, newBlockDetected, starPulseIntensity
import { S } from '../state.js';
import { state } from '../core/state.js';
const BITCOIN_REFRESH_MS = 60 * 1000;
export async function fetchBlockHeight() {
try {
const res = await fetch('https://blockstream.info/api/blocks/tip/height');
if (!res.ok) return null;
const height = parseInt(await res.text(), 10);
if (isNaN(height)) return null;
const prev = S.lastKnownBlockHeight;
const isNew = prev !== null && height > prev;
S.lastKnownBlockHeight = height;
if (isNew) S._starPulseIntensity = 1.0;
state.blockHeight = height;
state.lastBlockHeight = prev || 0;
state.newBlockDetected = isNew;
if (isNew) state.starPulseIntensity = 1.0;
return { height, isNewBlock: isNew };
} catch {
return null;
}
}
export { BITCOIN_REFRESH_MS };