Fix zapper loading

This commit is contained in:
Jon Staab
2025-12-05 09:44:21 -08:00
parent d81ec62d2b
commit 2f6875cca6
2 changed files with 36 additions and 43 deletions
+34 -40
View File
@@ -39,59 +39,53 @@ export const fetchZapper = batcher(800, async (lnurls: string[]) => {
const base = appContext.dufflepudUrl
const result = new Map<string, Zapper>()
for (const lnurl of lnurls) {
if (!lnurl.startsWith("lnurl1")) {
throw new Error(`Invalid lnurl ${lnurl}`)
}
}
const addZapper = (lnurl: string, info: any) => {
if (info) {
try {
result.set(lnurl, {...info, lnurl})
} catch (e) {
// pass
}
}
}
// Use dufflepud if we it's set up to protect user privacy, otherwise fetch directly
if (base) {
const hexUrls = removeUndefined(lnurls.map(lnurl => tryCatch(() => bech32ToHex(lnurl))))
const hexUrls = lnurls.map(bech32ToHex)
const res = await tryCatch(() => postJson(`${base}/zapper/info`, {lnurls: hexUrls}))
if (hexUrls.length > 0) {
const res: any = await tryCatch(
async () => await postJson(`${base}/zapper/info`, {lnurls: hexUrls}),
)
for (const {hexUrl, info} of res?.data || []) {
if (info) {
const lnurl = hexToBech32("lnurl", hexUrl)
tryCatch(() => result.set(lnurl, {...info, lnurl}))
}
}
for (const {lnurl, info} of res?.data || []) {
addZapper(hexToBech32("lnurl", lnurl), info)
}
} else {
const results = await Promise.all(
await Promise.all(
lnurls.map(async lnurl => {
const hexUrl = tryCatch(() => bech32ToHex(lnurl))
const info = hexUrl ? await tryCatch(async () => await fetchJson(hexUrl)) : undefined
return {lnurl, hexUrl, info}
addZapper(lnurl, await tryCatch(() => fetchJson(bech32ToHex(lnurl))))
}),
)
}
for (const {lnurl, info} of results) {
if (info) {
result.set(lnurl, {...info, lnurl})
if (result.size > 0) {
zappersByLnurl.update($zappersByLnurl => {
for (const [lnurl, zapper] of result) {
$zappersByLnurl.set(lnurl, zapper)
}
return $zappersByLnurl
})
for (const zapper of result.values()) {
notifyZapper(zapper)
}
}
zappersByLnurl.update($zappersByLnurl => {
for (const [nip05, info] of result) {
$zappersByLnurl.set(nip05, info)
}
return $zappersByLnurl
})
for (const info of result.values()) {
notifyZapper(info)
}
return lnurls.map(lnurl => {
const info = result.get(lnurl)
if (info) {
return {...info, lnurl}
}
})
return lnurls.map(lnurl => result.get(lnurl))
})
export const forceLoadZapper = makeForceLoadItem(fetchZapper, getZapper)
+2 -3
View File
@@ -58,11 +58,12 @@ export const getInvoiceAmount = (bolt11: string) => {
export const getLnUrl = (address: string) => {
address = address.toLowerCase()
// If it's already a lud06 we're good
if (address.startsWith("lnurl1")) {
return address
}
// If it's a regular url, just encode it
// If it's a regular url, encode it
if (address.includes("://")) {
return hexToBech32("lnurl", address)
}
@@ -75,8 +76,6 @@ export const getLnUrl = (address: string) => {
return hexToBech32("lnurl", `https://${domain}/.well-known/lnurlp/${name}`)
}
}
return undefined
}
export type Zapper = {