feat: fast zap donate

This commit is contained in:
Kieran 2023-03-03 22:01:15 +00:00
parent f31f44400f
commit 3fd4e471f1
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
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) {

View File

@ -38,6 +38,8 @@ const Translators = [
bech32ToHex("npub10529hxckjm5t5mchss5lnpsqrmavulglxhrmu5quuu4hs6yuyh3qc9gxd5"), // aadbitcoin - ID
];
export const DonateLNURL = "donate@snort.social";
interface Splits {
pubKey: string;
split: number;
@ -105,7 +107,7 @@ const DonatePage = () => {
<div className="mr10">
<FormattedMessage defaultMessage="Lightning Donation: " />
</div>
<ZapButton pubkey={bech32ToHex(SnortPubKey)} lnurl={"donate@snort.social"} />
<ZapButton pubkey={bech32ToHex(SnortPubKey)} lnurl={DonateLNURL} />
</div>
{today && (
<small>

View File

@ -2,6 +2,7 @@ import "./Preferences.css";
import { useDispatch, useSelector } from "react-redux";
import { FormattedMessage, useIntl } from "react-intl";
import { Link } from "react-router-dom";
import emoji from "@jukben/emoji-search";
import { DefaultImgProxy, setPreferences, UserPreferences } from "State/Login";
@ -154,6 +155,42 @@ const PreferencesPage = () => {
/>
</div>
</div>
<div className="card flex">
<div className="flex f-col f-grow">
<div>
<FormattedMessage defaultMessage="Fast Zap Donation" />
</div>
<small>
<FormattedMessage
defaultMessage="For each Fast Zap an additional {percentage}% ({amount} sats) of the zap amount will be sent to the Snort developers as a donation."
values={{
percentage: perf.fastZapDonate * 100,
amount: Math.floor(perf.defaultZapAmount * perf.fastZapDonate),
}}
/>
<br />
<FormattedMessage
defaultMessage="For more information about donations see {link}."
values={{
link: (
<Link to="/donate">
<FormattedMessage defaultMessage="Donate Page" />
</Link>
),
}}
/>
</small>
</div>
<div>
<input
type="number"
value={perf.fastZapDonate * 100}
min={0}
max={100}
onChange={e => dispatch(setPreferences({ ...perf, fastZapDonate: parseInt(e.target.value) / 100 }))}
/>
</div>
</div>
<div className="card flex f-col">
<div className="flex w-max">
<div className="flex f-col f-grow">

View File

@ -80,6 +80,11 @@ export interface UserPreferences {
* Default zap amount
*/
defaultZapAmount: number;
/**
* For each fast zap an additional X% will be sent to Snort donate address
*/
fastZapDonate: number;
}
export interface LoginStore {
@ -234,6 +239,7 @@ export const InitState = {
imgProxyConfig: DefaultImgProxy,
defaultRootTab: "posts",
defaultZapAmount: 500,
fastZapDonate: 0.0,
},
} as LoginStore;