Skip to content

Commit

Permalink
fix melt via invoice scan, pass cashu err msg #175
Browse files Browse the repository at this point in the history
  • Loading branch information
KKA11010 committed Sep 9, 2023
1 parent 60bfe8d commit cef4bae
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions assets/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"noCamAccess": "Kein Zugriff auf die Kamera",
"noEnoughFunds": "Nicht genügend Guthaben",
"noFunds": "Nicht genügend Guthaben",
"noFundsForFee": "Nicht genug Guthaben, bitte lassen Sie Platz für eventuelle Gebühren: ~{{fee}} Sat.",
"noMint": "Keine Mint gefunden",
"noProofsToBackup": "Keine Proofs verfügbar für eine Sicherung.",
"notClaim": "Wenn Sie \"Nein\" wählen, wird der Token nicht beansprucht",
Expand Down
1 change: 1 addition & 0 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"noCamAccess": "No access to camera",
"noEnoughFunds": "Chosen mint has not enough funds",
"noFunds": "Not enough funds",
"noFundsForFee": "Not enough funds, please leave room for potential fees: ~{{fee}} Sat.",
"noMint": "Found no mint",
"noProofsToBackup": "Found no proofs to create a backup.",
"notClaim": "If you choose \"No\", the token will not be claimed",
Expand Down
1 change: 1 addition & 0 deletions assets/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
"noCamAccess": "Accès à la caméra non autorisé",
"noEnoughFunds": "La Mint choisie n'a pas suffisamment de fonds",
"noFunds": "Fonds insuffisants",
"noFundsForFee": "Solde insuffisant, veuillez prévoir de la marge pour les frais éventuels: ~{{fee}} Sat.",
"noMint": "Aucune Mint trouvée",
"noProofsToBackup": "Aucune preuve trouvée pour créer une sauvegarde.",
"notClaim": "Si vous choisissez \"Non\", le token ne sera pas réclamé",
Expand Down
30 changes: 21 additions & 9 deletions src/screens/Payment/Processing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { TProcessingPageProps } from '@model/nav'
import { relay } from '@nostr/class/Relay'
import { EventKind } from '@nostr/consts'
import { encrypt } from '@nostr/crypto'
import { useFocusEffect } from '@react-navigation/core'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { updateNostrDmUsers } from '@src/storage/store/nostrDms'
Expand All @@ -19,9 +20,9 @@ import { addToHistory, updateLatestHistory } from '@store/latestHistoryEntries'
import { globals } from '@styles'
import { formatMintUrl, getInvoiceFromLnurl, isErr, isLnurl } from '@util'
import { autoMintSwap, payLnInvoice, requestMint, requestToken, sendToken } from '@wallet'
import { useEffect } from 'react'
import { useCallback, useEffect } from 'react'
import { useTranslation } from 'react-i18next'
import { StyleSheet, View } from 'react-native'
import { BackHandler, StyleSheet, View } from 'react-native'

interface IErrorProps {
e?: unknown
Expand Down Expand Up @@ -107,10 +108,9 @@ export default function ProcessingScreen({ navigation, route }: TProcessingPageP
}

const handleMeltingProcess = async () => {
if (!recipient?.length) { return }
let invoice = ''
// recipient can be a LNURL or a LN invoice
if (isLnurl(recipient)) {
if (recipient?.length && isLnurl(recipient)) {
try {
invoice = await getInvoiceFromLnurl(recipient, +amount)
if (!invoice?.length) {
Expand All @@ -123,24 +123,25 @@ export default function ProcessingScreen({ navigation, route }: TProcessingPageP
}
}
try {
const res = await payLnInvoice(mint.mintUrl, invoice || recipient, estFee || 0, proofs || [])
if (!res.result?.isPaid) {
const target = invoice || recipient || ''
const res = await payLnInvoice(mint.mintUrl, target, estFee || 0, proofs || [])
if (!res?.result?.isPaid) {
// here it could be a routing path finding issue
handleError({ customMsg: 'generalMeltingErr' })
handleError({ e: isErr(res.error) ? res.error : undefined })
return
}
// payment success, add as history entry
await addLnPaymentToHistory(
res,
[mint.mintUrl],
-amount,
invoice || recipient
target
)
// update latest 3 history entries
await updateLatestHistory({
amount: -amount,
type: 2,
value: invoice || recipient,
value: target,
mints: [mint.mintUrl],
timestamp: Math.ceil(Date.now() / 1000)
})
Expand Down Expand Up @@ -250,6 +251,17 @@ export default function ProcessingScreen({ navigation, route }: TProcessingPageP
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isMelt, isSwap, isSendEcash])

// prevent android back button to go back to previous nav screen
useFocusEffect(
useCallback(() => {
const onBackPress = () => true
BackHandler.addEventListener('hardwareBackPress', onBackPress)
return () => {
BackHandler.removeEventListener('hardwareBackPress', onBackPress)
}
}, [])
)

return (
<View style={[globals(color).container, styles.container]}>
<Loading size={40} nostr={!!nostr} />
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Payment/SelectMint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function SelectMintScreen({ navigation, route }: TSelectMintPageP
if (invoice && invoiceAmount) {
const estFee = await checkFees(mint.mintUrl, invoice)
if (invoiceAmount + estFee > mint.amount) {
openPromptAutoClose({ msg: t('noFunds', { ns: NS.common }) })
openPromptAutoClose({ msg: t('noFundsForFee', { ns: NS.common, fee: estFee }), ms: 4000 })
return
}
navigation.navigate('coinSelection', {
Expand Down
15 changes: 10 additions & 5 deletions src/screens/QRScan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ export default function QRScanPage({ navigation, route }: TQRScanPageProps) {
// user already has selected the mint in the previous screens
if (mint && balance) {
// check if invoice amount is higher than the selected mint balance to avoid navigating
if (amount > balance) {
openPromptAutoClose({ msg: t('noFunds') })
const estFee = await checkFees(mint.mintUrl, invoice)
if (amount + estFee > balance) {
openPromptAutoClose({ msg: t('noFundsForFee', { fee: estFee }), ms: 4000 })
return
}
const estFee = await checkFees(mint.mintUrl, invoice)
navigation.navigate('coinSelection', {
mint,
balance,
Expand All @@ -169,17 +169,22 @@ export default function QRScanPage({ navigation, route }: TQRScanPageProps) {
})
return
}
// user has funds, select his first mint for the case that he has only one (2 lines later)
// user has funds, select his first mint for the case that he has only one
const mintUsing = mints.find(m => m.mintUrl === nonEmptyMint[0].mintUrl) || { mintUrl: 'N/A', customName: 'N/A' }
const estFee = await checkFees(mintUsing.mintUrl, invoice)
if (nonEmptyMint.length === 1 && amount + estFee > nonEmptyMint[0].amount) {
openPromptAutoClose({ msg: t('noFundsForFee', { fee: estFee }), ms: 4000 })
return
}
// user has only 1 mint with enough balance, he can directly navigate to the payment overview page
if (nonEmptyMint.length === 1) {
navigation.navigate('coinSelection', {
mint: mintUsing,
balance: nonEmptyMint[0].amount,
amount,
estFee,
isMelt: true
isMelt: true,
recipient: invoice
})
return
}
Expand Down

0 comments on commit cef4bae

Please sign in to comment.