feat: fast zap donate

This commit is contained in:
2023-03-03 22:01:15 +00:00
parent f31f44400f
commit 3fd4e471f1
4 changed files with 68 additions and 7 deletions

View File

@ -21,7 +21,7 @@ import { UserPreferences, setPinned, setBookmarked } from "State/Login";
import useModeration from "Hooks/useModeration";
import { TranslateHost } from "Const";
import { LNURL } from "LNURL";
import { DonateLNURL } from "Pages/DonatePage";
import { useWallet } from "Wallet";
import messages from "./messages";
@ -114,11 +114,17 @@ export default function NoteFooter(props: NoteFooterProps) {
if (wallet?.isReady() && lnurl) {
setZapping(true);
try {
const handler = new LNURL(lnurl);
await handler.load();
const zap = handler.canZap ? await publisher.zap(prefs.defaultZapAmount * 1000, ev.PubKey, ev.Id) : undefined;
const invoice = await handler.getInvoice(prefs.defaultZapAmount, undefined, zap);
await wallet.payInvoice(unwrap(invoice.pr));
if (prefs.fastZapDonate > 0) {
// spin off donate
const donateAmount = Math.floor(prefs.defaultZapAmount * prefs.fastZapDonate);
if (donateAmount > 0) {
console.debug(`Donating ${donateAmount} sats to ${DonateLNURL}`);
fastZapInner(DonateLNURL, donateAmount)
.then(() => console.debug("Donation sent! Thank You!"))
.catch(() => console.debug("Failed to donate"));
}
}
await fastZapInner(lnurl, prefs.defaultZapAmount);
} catch (e) {
console.warn("Fast zap failed", e);
if (!(e instanceof Error) || e.message !== "User rejected") {
@ -132,6 +138,16 @@ export default function NoteFooter(props: NoteFooterProps) {
}
}
async function fastZapInner(lnurl: string, amount: number) {
if (wallet?.isReady() && lnurl) {
const handler = new LNURL(lnurl);
await handler.load();
const zap = handler.canZap ? await publisher.zap(amount * 1000, ev.PubKey, ev.Id) : undefined;
const invoice = await handler.getInvoice(amount, undefined, zap);
await wallet.payInvoice(unwrap(invoice.pr));
}
}
function tipButton() {
const service = author?.lud16 || author?.lud06;
if (service) {