Use rates

This commit is contained in:
2023-09-16 23:03:11 +01:00
parent c926586647
commit 8c0afbe63d
38 changed files with 54 additions and 38 deletions

View File

@ -13,7 +13,8 @@ import QrCode from "./qr-code";
import { useLogin } from "hooks/login";
import Copy from "./copy";
import { defaultRelays } from "const";
import { FormattedMessage } from "react-intl";
import { FormattedMessage, FormattedNumber } from "react-intl";
import { useRates } from "hooks/rates";
export interface LNURLLike {
get name(): string;
@ -33,8 +34,6 @@ export interface SendZapsProps {
}
export function SendZaps({ lnurl, pubkey, aTag, eTag, targetName, onFinish }: SendZapsProps) {
const UsdRate = 28_000;
const satsAmounts = [
21, 69, 121, 420, 1_000, 2_100, 4_200, 10_000, 21_000, 42_000, 69_000, 100_000, 210_000, 500_000, 1_000_000,
];
@ -45,6 +44,7 @@ export function SendZaps({ lnurl, pubkey, aTag, eTag, targetName, onFinish }: Se
const [comment, setComment] = useState("");
const [invoice, setInvoice] = useState("");
const login = useLogin();
const rate = useRates("BTCUSD");
const relays = Object.keys(defaultRelays);
const name = targetName ?? svc?.name;
async function loadService(lnurl: string) {
@ -52,6 +52,7 @@ export function SendZaps({ lnurl, pubkey, aTag, eTag, targetName, onFinish }: Se
await s.load();
setSvc(s);
}
const usdRate = rate.time ? rate.ask : 26_000;
useEffect(() => {
if (!svc) {
@ -72,7 +73,7 @@ export function SendZaps({ lnurl, pubkey, aTag, eTag, targetName, onFinish }: Se
isAnon = true;
}
const amountInSats = isFiat ? Math.floor((amount / UsdRate) * 1e8) : amount;
const amountInSats = isFiat ? Math.floor((amount / usdRate) * 1e8) : amount;
let zap: NostrEvent | undefined;
if (pubkey) {
zap = await pub.zap(amountInSats * 1000, pubkey, relays, undefined, comment, eb => {
@ -132,6 +133,18 @@ export function SendZaps({ lnurl, pubkey, aTag, eTag, targetName, onFinish }: Se
defaultMessage="Zap amount in {currency}"
values={{ currency: isFiat ? "USD" : "SATS" }}
/>
{isFiat && (
<>
&nbsp;
<FormattedMessage
defaultMessage="@ {rate}"
description="Showing zap amount in USD @ rate"
values={{
rate: <FormattedNumber value={usdRate} />,
}}
/>
</>
)}
</small>
<div className="amounts">
{(isFiat ? usdAmounts : satsAmounts).map(a => (

32
src/hooks/rates.ts Normal file
View File

@ -0,0 +1,32 @@
import { bech32ToHex } from "@snort/shared";
import { EventKind, ReplaceableNoteStore, RequestBuilder } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { useMemo } from "react";
// Snort backend publishes rates
const SnortPubkey = "npub1sn0rtcjcf543gj4wsg7fa59s700d5ztys5ctj0g69g2x6802npjqhjjtws";
export function useRates(symbol: string, leaveOpen = true) {
const sub = useMemo(() => {
const rb = new RequestBuilder(`rates:${symbol}`);
rb.withOptions({
leaveOpen,
});
rb.withFilter()
.kinds([1009 as EventKind])
.authors([bech32ToHex(SnortPubkey)])
.tag("d", [symbol]);
return rb;
}, [symbol]);
const data = useRequestBuilder(ReplaceableNoteStore, sub);
const tag = data?.data?.tags.find(a => a[0] === "d" && a[1] === symbol);
return {
time: data.data?.created_at,
ask: Number(tag?.[2]),
bid: Number(tag?.[3]),
low: Number(tag?.[4]),
hight: Number(tag?.[5]),
};
}

View File

@ -239,6 +239,10 @@
"X2PZ7D": {
"defaultMessage": "Create Goal"
},
"YPh5Nq": {
"defaultMessage": "@ {rate}",
"description": "Showing zap amount in USD @ rate"
},
"Z8ZOEY": {
"defaultMessage": "This method is insecure. We recommend using a {nostrlink}"
},

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Зап Тревога"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "জ্যাপ অ্যালার্ট"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -79,6 +79,7 @@
"VA/Z1S": "Hide",
"W9355R": "Unmute",
"X2PZ7D": "Create Goal",
"YPh5Nq": "@ {rate}",
"Z8ZOEY": "This method is insecure. We recommend using a {nostrlink}",
"ZmqxZs": "You can change this later",
"acrOoz": "Continue",

View File

@ -414,4 +414,3 @@
"defaultMessage": "Alerta de Zap"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "هشدار زپ"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap-hälytys"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Alerte Zap"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Riadó"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "ザップアラート"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Alerta Zap"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Оповещение о запе"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Tahadhari ya Zap"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "Zap Alert"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "打闪提示"
}
}

View File

@ -414,4 +414,3 @@
"defaultMessage": "打閃提示"
}
}