diff --git a/components/generate-invoice.tsx b/components/generate-invoice.tsx index 742a820b..b3dcaaed 100644 --- a/components/generate-invoice.tsx +++ b/components/generate-invoice.tsx @@ -26,17 +26,37 @@ const LN_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT = gql` } ` -const INVOICE_STALE_CHECK_INTERVAL = 5 * 60 * 1000 +const LN_USD_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT = gql` + mutation lnUsdInvoiceCreateOnBehalfOfRecipient( + $walletId: WalletId! + $amount: CentAmount! + ) { + mutationData: lnUsdInvoiceCreateOnBehalfOfRecipient( + input: { recipientWalletId: $walletId, amount: $amount } + ) { + errors { + message + } + invoice { + paymentRequest + } + } + } +` + +const INVOICE_STALE_CHECK_INTERVAL = 2 * 60 * 1000 const INVOICE_EXPIRE_INTERVAL = 60 * 60 * 1000 function GenerateInvoice({ recipientWalletId, - amountInSats, + recipientWalletCurrency, + amountInBase, regenerate, currency, }: { recipientWalletId: string - amountInSats: number + recipientWalletCurrency: string + amountInBase: number regenerate: () => void currency: string }) { @@ -44,6 +64,10 @@ function GenerateInvoice({ "loading" | "new" | "need-update" | "expired" >("loading") + const INVOICE_CREATION_MUTATION = + recipientWalletCurrency === "USD" + ? LN_USD_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT + : LN_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT const timerIds = useRef([]) const [createInvoice, { loading, error, data }] = useMutation<{ @@ -51,7 +75,7 @@ function GenerateInvoice({ errors: OperationError[] invoice?: LnInvoiceObject } - }>(LN_INVOICE_CREATE_ON_BEHALF_OF_RECIPIENT, { + }>(INVOICE_CREATION_MUTATION, { onError: console.error, onCompleted: () => setInvoiceStatus("new"), }) @@ -59,12 +83,11 @@ function GenerateInvoice({ const clearAllTimers = () => { timerIds.current.forEach((timerId) => clearTimeout(timerId)) } - useEffect(() => { createInvoice({ - variables: { walletId: recipientWalletId, amount: amountInSats }, + variables: { walletId: recipientWalletId, amount: amountInBase }, }) - if (currency !== "SATS") { + if (currency !== "SATS" || recipientWalletCurrency === "USD") { timerIds.current.push( window.setTimeout( () => setInvoiceStatus("need-update"), @@ -76,7 +99,7 @@ function GenerateInvoice({ window.setTimeout(() => setInvoiceStatus("expired"), INVOICE_EXPIRE_INTERVAL), ) return clearAllTimers - }, [recipientWalletId, amountInSats, currency, createInvoice]) + }, [recipientWalletId, amountInBase, currency, createInvoice]) let errorString: string | null = error?.message || null let invoice diff --git a/components/receive-amount.tsx b/components/receive-amount.tsx index b01e3e8d..0c307229 100644 --- a/components/receive-amount.tsx +++ b/components/receive-amount.tsx @@ -19,8 +19,10 @@ const satsFormatter = new Intl.NumberFormat("en-US", { export default function ReceiveAmount({ recipientWalletId, + recipientWalletCurrency, }: { recipientWalletId: string + recipientWalletCurrency: string }) { const router = useRouter() const { satsToUsd, usdToSats } = useSatPrice() @@ -41,14 +43,23 @@ export default function ReceiveAmount({ const getSatsForInvoice = React.useCallback(() => { return Math.round(currency === "SATS" ? amount : Math.round(usdToSats(amount))) }, [amount, currency, usdToSats]) + const [satsForInvoice, setSatsForInvoice] = useState(() => getSatsForInvoice()) + + const getCentsForInvoice = React.useCallback(() => { + return Math.round(currency === "USD" ? amount * 100 : satsToUsd(amount) * 100) + }, [amount, currency, satsToUsd]) + const [centsForInvoice, setCentsForInvoice] = useState(() => getCentsForInvoice()) + + function triggerRegenerateUsdInvoice() { + setCentsForInvoice(0) + setTimeout(() => setCentsForInvoice(getCentsForInvoice())) + } - function triggerRegenerateInvoice() { + function triggerRegenerateBtcInvoice() { setSatsForInvoice(0) setTimeout(() => setSatsForInvoice(getSatsForInvoice())) } - const [satsForInvoice, setSatsForInvoice] = useState(() => getSatsForInvoice()) - const convertedValue = currency === "SATS" ? usdFormatter.format(satsToUsd(amount)) @@ -57,8 +68,16 @@ export default function ReceiveAmount({ useEffect(() => { const newSats = getSatsForInvoice() if (newSats !== satsForInvoice) setSatsForInvoice(newSats) - }, [amount, currency, getSatsForInvoice, satsForInvoice, usdToSats]) - + const newCents = getCentsForInvoice() + if (newCents !== centsForInvoice) setCentsForInvoice(newCents) + }, [getSatsForInvoice, satsForInvoice, centsForInvoice, getCentsForInvoice]) + + const amountInBase = + recipientWalletCurrency === "USD" ? centsForInvoice : satsForInvoice + const triggerRegenerateInvoice = + recipientWalletCurrency === "USD" + ? triggerRegenerateUsdInvoice + : triggerRegenerateBtcInvoice return ( <>
@@ -76,10 +95,11 @@ export default function ReceiveAmount({
≈ {convertedValue}
- {satsForInvoice > 0 && ( + {amountInBase > 0 && ( diff --git a/pages/[username].tsx b/pages/[username].tsx index 04528802..e4878250 100644 --- a/pages/[username].tsx +++ b/pages/[username].tsx @@ -12,8 +12,11 @@ import ReceiveNoAmount from "../components/receive-no-amount" import { getOS, playStoreLink, appStoreLink } from "../lib/download" const RECIPIENT_WALLET_ID = gql` - query userDefaultWalletId($username: Username!) { - recipientWalletId: userDefaultWalletId(username: $username) + query accountDefaultWallet($username: Username!) { + accountDefaultWallet(username: $username) { + id + walletCurrency + } } ` @@ -33,7 +36,8 @@ export default function Receive() { if (loading) return
Loading...
if (!data) return null - const { recipientWalletId } = data + const { id: recipientWalletId, walletCurrency: recipientWalletCurrency } = + data.accountDefaultWallet const isAmountInvoice = amount !== undefined @@ -50,7 +54,10 @@ export default function Receive() { Pay {username} {isAmountInvoice ? ( - + ) : (