story-research-zapwall/lib/zapReceiptQueries.ts
2026-01-06 14:17:55 +01:00

46 lines
996 B
TypeScript

import { nostrService } from './nostr'
import { getPrimaryRelaySync } from './config'
import type { Event } from 'nostr-tools'
import { createSubscription } from '@/types/nostr-tools-extended'
/**
* Get zap receipt event by ID
*/
export async function getZapReceiptById(zapReceiptId: string): Promise<Event | null> {
const pool = nostrService.getPool()
if (!pool) {
return null
}
const filters = [
{
kinds: [9735],
ids: [zapReceiptId],
limit: 1,
},
]
const relayUrl = getPrimaryRelaySync()
const sub = createSubscription(pool, [relayUrl], filters)
return new Promise<Event | null>((resolve) => {
let resolved = false
const finalize = (value: Event | null) => {
if (resolved) {
return
}
resolved = true
sub.unsub()
resolve(value)
}
sub.on('event', (event: Event) => {
finalize(event)
})
sub.on('eose', () => finalize(null))
setTimeout(() => finalize(null), 5000)
})
}