bug: fast zap donate loop

This commit is contained in:
Kieran 2023-03-12 19:25:22 +00:00
parent b0efe36c7a
commit 59ae02207c
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941

View File

@ -153,14 +153,15 @@ export default function NoteFooter(props: NoteFooterProps) {
} }
} }
async function fastZap(e: React.MouseEvent) { async function fastZap(e?: React.MouseEvent) {
if (zapping || e.isPropagationStopped()) return; if (zapping || e?.isPropagationStopped()) return;
const lnurl = author?.lud16 || author?.lud06; const lnurl = author?.lud16 || author?.lud06;
if (wallet?.isReady() && lnurl) { if (wallet?.isReady() && lnurl) {
setZapping(true); setZapping(true);
try { try {
await fastZapInner(lnurl, prefs.defaultZapAmount, ev.PubKey, ev.Id); await fastZapInner(lnurl, prefs.defaultZapAmount, ev.PubKey, ev.Id);
fastZapDonate();
} catch (e) { } catch (e) {
console.warn("Fast zap failed", e); console.warn("Fast zap failed", e);
if (!(e instanceof Error) || e.message !== "User rejected") { if (!(e instanceof Error) || e.message !== "User rejected") {
@ -175,16 +176,18 @@ export default function NoteFooter(props: NoteFooterProps) {
} }
async function fastZapInner(lnurl: string, amount: number, key: HexKey, id?: u256) { async function fastZapInner(lnurl: string, amount: number, key: HexKey, id?: u256) {
if (wallet?.isReady() && lnurl) { // only allow 1 invoice req/payment at a time to avoid hitting rate limits
// only allow 1 invoice req/payment at a time to avoid hitting rate limits await barrierZapper(async () => {
await barrierZapper(async () => { const handler = new LNURL(lnurl);
const handler = new LNURL(lnurl); await handler.load();
await handler.load(); const zap = handler.canZap ? await publisher.zap(amount * 1000, key, id) : undefined;
const zap = handler.canZap ? await publisher.zap(amount * 1000, key, id) : undefined; const invoice = await handler.getInvoice(amount, undefined, zap);
const invoice = await handler.getInvoice(amount, undefined, zap); await wallet?.payInvoice(unwrap(invoice.pr));
await wallet.payInvoice(unwrap(invoice.pr)); });
}); }
function fastZapDonate() {
queueMicrotask(async () => {
if (prefs.fastZapDonate > 0) { if (prefs.fastZapDonate > 0) {
// spin off donate // spin off donate
const donateAmount = Math.floor(prefs.defaultZapAmount * prefs.fastZapDonate); const donateAmount = Math.floor(prefs.defaultZapAmount * prefs.fastZapDonate);
@ -195,18 +198,21 @@ export default function NoteFooter(props: NoteFooterProps) {
.catch(() => console.debug("Failed to donate")); .catch(() => console.debug("Failed to donate"));
} }
} }
} });
} }
useEffect(() => { useEffect(() => {
if (prefs.autoZap) { if (prefs.autoZap && !ZapCache.has(ev.Id) && !isMine && !zapping) {
const lnurl = author?.lud16 || author?.lud06; const lnurl = author?.lud16 || author?.lud06;
if (wallet?.isReady() && lnurl && !ZapCache.has(ev.Id) && !zapping && !isMine) { if (wallet?.isReady() && lnurl) {
setZapping(true); setZapping(true);
queueMicrotask(async () => { queueMicrotask(async () => {
try { try {
await fastZapInner(lnurl, prefs.defaultZapAmount, ev.PubKey, ev.Id); await fastZapInner(lnurl, prefs.defaultZapAmount, ev.PubKey, ev.Id);
ZapCache.add(ev.Id); ZapCache.add(ev.Id);
fastZapDonate();
} catch {
// ignored
} finally { } finally {
setZapping(false); setZapping(false);
} }