-

@ dan wedge
2025-02-19 19:45:12
Fyi i can read through but i didn’t code it myself. If this looks like hot garbage please don’t waste your time.
import { getEventHash, nip19 } from 'nostr-tools';
async function fetchAndVisualizeZaps(relayUrl, dvmContainerId) { // Added dvmContainerId
try {
const pool = new nip19.SimplePool(relayUrl);
const zapEvents = await pool.list([
{
kinds: [5400],
since: Math.floor(Date.now() / 1000) - 24 * 60 * 60,
},
]);
const processedEvents = zapEvents.map(event => {
try {
if (!event.id || !event.pubkey || !event.created_at || !event.tags) {
throw new Error('Invalid event structure');
}
let totalZapAmount = 0;
const zapTags = event.tags.filter(tag => tag[0] === 'zap');
zapTags.forEach(zapTag => {
try {
totalZapAmount += parseInt(zapTag[1]);
} catch (error) {
console.error("Error parsing zap amount for event", event.id, error);
}
});
let weightedAmount = 1;
if (totalZapAmount > 0) {
weightedAmount += Math.floor(Math.log10(totalZapAmount)) * 0.1;
}
return {
id: event.id,
pubkey: event.pubkey,
created_at: event.created_at,
zapAmount: totalZapAmount,
weightedAmount: weightedAmount,
};
} catch (error) {
console.error('Error processing event:', event, error);
return null;
}
}).filter(event => event !== null);
// Sort by weightedAmount (descending)
const sortedEvents = processedEvents.sort((a, b) => b.weightedAmount - a.weightedAmount);
// Placeholder DVM implementation (replace with your actual visualization)
class DVM {
constructor(options) {
this.options = options;
this.container = document.getElementById(dvmContainerId); // Get container by ID
if (!this.container) {
throw new Error("DVM container element not found.");
}
}
render() {
// Clear previous content
this.container.innerHTML = '';
this.options.data.forEach(event => {
const element = document.createElement('div'); // Simple div for each note
element.style.fontSize = `${10 + event.weightedAmount * 5}px`; // Example size scaling
element.title = `Event ID: ${event.id} - Zap Amount: ${event.zapAmount} - Weighted Amount: ${event.weightedAmount}`; // Tooltip
element.textContent = `Note: ${event.id.slice(0, 20)}... (Zaps: ${event.zapAmount})`; // Display a snippet
this.container.appendChild(element);
});
}
}
const dvm = new DVM({
data: sortedEvents,
});
dvm.render();
pool.close();
} catch (error) {
console.error('Error fetching or visualizing zaps:', error);
}
}
// Example usage (you'll need to adapt this for the DMV site's execution environment):
// 1. Create a div with the ID "dvm-container" in your HTML
// 2. Call the function:
fetchAndVisualizeZaps('wss://relay.example.com', 'dvm-container'); // Replace with your relay URL and container ID