Skip to content

Commit

Permalink
update UI & rename functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KKA11010 committed Mar 26, 2024
1 parent 63eeb02 commit 8742b34
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 84 deletions.
9 changes: 9 additions & 0 deletions src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export interface ILnUrl {
callback: string
pr: string
}

// TODO This interface is missing some properties?
export interface ILnUrlPayRequest {
tag: string
cb: string
minSendable: number
maxSendable: number
metadata: string
}
export interface IMint {
id: string
mintUrl: string
Expand Down
17 changes: 15 additions & 2 deletions src/model/nav.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type { EventArg } from '@react-navigation/core'
import type { NativeStackScreenProps } from '@react-navigation/native-stack'

import type { IHistoryEntry, IMintUrl, IMintWithBalance, IProofSelection, ITokenInfo } from '.'
import type { IHistoryEntry, ILnUrlPayRequest, IMintUrl, IMintWithBalance, IProofSelection, ITokenInfo } from '.'
import type { HexKey, IContact } from './nostr'

export interface INostrSendData {
senderName: string
contact?: IContact
}

interface ILnurlNavData {
userInput: string
url?: string
data?: ILnUrlPayRequest
}
/**
* Stack Navigator
*/
Expand Down Expand Up @@ -48,6 +54,7 @@ export type RootStackParamList = {
invoice?: string
invoiceAmount?: number
estFee?: number
lnurl?: ILnurlNavData
scanned?: boolean
},
selectTarget: {
Expand All @@ -74,7 +81,7 @@ export type RootStackParamList = {
nostr?: INostrSendData
isSwap?: boolean
balance: number
lnurl?: string
lnurl?: ILnurlNavData
targetMint?: IMintUrl
}
selectNostrAmount: {
Expand Down Expand Up @@ -124,6 +131,12 @@ export type RootStackParamList = {
balance?: number
amount: number
}
lnurl?: {
mint?: IMintUrl
balance?: number
url: string
data: string
}
}
'mint confirm': {
mintUrl: string
Expand Down
3 changes: 1 addition & 2 deletions src/screens/Addressbook/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ export default function AddressbookPage({ navigation, route }: TAddressBookPageP
openPromptAutoClose({ msg: t('receiverNoLnurl', { ns: NS.addrBook }) })
return
}
navigation.navigate('selectAmount', { isMelt, lnurl: contact.lud16, mint, balance })
return
return navigation.navigate('selectAmount', { isMelt, lnurl: { userInput: contact.lud16 }, mint, balance })
}
if (!nostrRef.current) { return }
// mint has already been selected
Expand Down
5 changes: 3 additions & 2 deletions src/screens/Payment/Processing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import { useInitialURL } from '@src/context/Linking'
import { useNostrContext } from '@src/context/Nostr'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { isLnurlOrAddress } from '@src/util/lnurl'
import { addLnPaymentToHistory } from '@store/HistoryStore'
import { addToHistory, updateLatestHistory } from '@store/latestHistoryEntries'
import { getDefaultMint } from '@store/mintStore'
import { globals } from '@styles'
import { decodeLnInvoice, getInvoiceFromLnurl, isErr, isLnurlOrAddress, isNum, uniqByIContacts } from '@util'
import { decodeLnInvoice, getInvoiceFromLnurl, isErr, isNum, uniqByIContacts } from '@util'
import { autoMintSwap, checkFees, fullAutoMintSwap, getHighestBalMint, payLnInvoice, requestMint, sendToken } from '@wallet'
import { useEffect, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -98,7 +99,7 @@ export default function ProcessingScreen({ navigation, route }: TProcessingPageP

const handleMelting = async () => {
let invoice = ''
// recipient can be a LNURL (address) or a LN invoice
// recipient can be a LNURL or a LN invoice
if (recipient?.length && isLnurlOrAddress(recipient)) {
try {
invoice = await getInvoiceFromLnurl(recipient, +amount)
Expand Down
34 changes: 22 additions & 12 deletions src/screens/Payment/SelectAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { usePromptContext } from '@src/context/Prompt'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { globals, highlight as hi, mainColors } from '@styles'
import { cleanUpNumericStr, formatSatStr, getInvoiceFromLnurl, vib } from '@util'
import { cleanUpNumericStr, formatInt, formatSatStr, getInvoiceFromLnurl, vib } from '@util'
import { getLnurlIdentifierFromMetadata, isLightningAddress } from '@util/lnurl'
import { checkFees, requestMint } from '@wallet'
import { createRef, useCallback, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -53,14 +54,14 @@ export default function SelectAmountScreen({ navigation, route }: TSelectAmountP
return 'createInvoice'
}

const handleFeeEstimation = async (lnurl: string) => {
const handleFeeEstimation = async () => {
setFee(prev => ({ ...prev, isCalculating: true }))
try {
// check fee for payment to lnurl
if (lnurl.length) {
const lnurlInvoice = await getInvoiceFromLnurl(lnurl, +amount)
if (lnurl) {
const lnurlInvoice = await getInvoiceFromLnurl(lnurl.userInput, +amount)
if (!lnurlInvoice?.length) {
openPromptAutoClose({ msg: t('feeErr', { ns: NS.common, input: lnurl }) })
openPromptAutoClose({ msg: t('feeErr', { ns: NS.common, input: lnurl.url }) })
return setFee(prev => ({ ...prev, isCalculating: false }))
}
const estFee = await checkFees(mint.mintUrl, lnurlInvoice)
Expand Down Expand Up @@ -106,11 +107,12 @@ export default function SelectAmountScreen({ navigation, route }: TSelectAmountP
return
}
// estimate melting/swap fee
if (!isSendEcash && shouldEstimate && (lnurl?.length || isSwap)) {
return handleFeeEstimation(lnurl || '')
if (!isSendEcash && shouldEstimate && (lnurl || isSwap)) {
return handleFeeEstimation()
}
// send ecash / melt / swap
if (isSendingTX) {
const recipient = isLightningAddress(lnurl?.userInput || '') ? lnurl?.userInput : lnurl?.data ? getLnurlIdentifierFromMetadata(lnurl.data?.metadata) : undefined
// Check if user melts/swaps his whole mint balance, so there is no need for coin selection and that can be skipped here
if (!isSendEcash && isSendingWholeMintBal()) {
return navigation.navigate('processing', {
Expand All @@ -121,7 +123,7 @@ export default function SelectAmountScreen({ navigation, route }: TSelectAmountP
isSendEcash,
isSwap,
targetMint,
recipient: lnurl
recipient
})
}
return navigation.navigate('coinSelection', {
Expand All @@ -135,7 +137,7 @@ export default function SelectAmountScreen({ navigation, route }: TSelectAmountP
isSendEcash,
isSwap,
targetMint,
recipient: lnurl
recipient
})
}
// request new token from mint
Expand Down Expand Up @@ -181,11 +183,19 @@ export default function SelectAmountScreen({ navigation, route }: TSelectAmountP
/>
}
<View style={[styles.overviewWrap, { marginTop: isMelt || isSwap ? 0 : vs(20) }]}>
{lnurl && lnurl.length > 0 &&
{lnurl && (lnurl.data || lnurl.userInput) &&
<Txt
txt={lnurl}
txt={
isLightningAddress(lnurl.userInput) ?
lnurl.userInput
:
lnurl.data ?
`${getLnurlIdentifierFromMetadata(lnurl.data.metadata)} requests ${lnurl.data.minSendable / 1000} to ${formatInt(lnurl.data.maxSendable / 1000)} Sats.`
:
''
}
bold
styles={[styles.sats, { color: color.TEXT_SECONDARY, marginBottom: vs(5), fontSize: s(10) }]}
styles={[styles.sats, { marginBottom: vs(5), fontSize: s(10) }]}
/>
}
<Animated.View style={[styles.amountWrap, { transform: [{ translateX: anim.current }] }]}>
Expand Down
5 changes: 3 additions & 2 deletions src/screens/Payment/Send/CoinSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { useInitialURL } from '@src/context/Linking'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { globals } from '@styles'
import { formatInt, formatMintUrl, formatSatStr, getSelectedAmount, isLnurlOrAddress, isNum } from '@util'
import { formatInt, formatMintUrl, formatSatStr, getSelectedAmount, isNum } from '@util'
import { isLightningAddress } from '@util/lnurl'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { ScrollView, View } from 'react-native'
Expand Down Expand Up @@ -60,7 +61,7 @@ export default function CoinSelectionScreen({ navigation, route }: TCoinSelectio

const getRecipient = () => {
if (recipient) {
return !isLnurlOrAddress(recipient) ? truncateStr(recipient, 16) : recipient
return !isLightningAddress(recipient) ? truncateStr(recipient, 16) : recipient
}
const npub = npubEncode(nostr?.contact?.hex ?? '')
const receiverName = getNostrUsername(nostr?.contact)
Expand Down
16 changes: 12 additions & 4 deletions src/screens/Payment/Send/Inputfield.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { globals } from '@styles'
import { decodeLnInvoice, getStrFromClipboard, isErr, openUrl } from '@util'
import { isLnurlOrAddress } from '@util/lnurl'
import { decodeUrlOrAddress, getLnurlData, isLnurlOrAddress } from '@util/lnurl'
import { checkFees } from '@wallet'
import { createRef, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
Expand Down Expand Up @@ -66,16 +66,24 @@ export default function InputfieldScreen({ navigation, route }: TMeltInputfieldP
}
}

const handleBtnPress = () => {
const handleBtnPress = async () => {
if (loading) { return }
// open user LN wallet
if (!input.length) {
return openUrl('lightning://')?.catch(e =>
openPromptAutoClose({ msg: isErr(e) ? e.message : t('deepLinkErr') }))
}
// user pasted a LNURL, we need to get the amount by the user
// user pasted an encoded LNURL, we need to get the amount by the user
if (isLnurlOrAddress(input)) {
return navigation.navigate('selectAmount', { mint, balance, isMelt: true, lnurl: input })
const decoded = decodeUrlOrAddress(input)
if (!decoded) { return openPromptAutoClose({ msg: 'Could not decode LNURL!' }) }
try {
const lnurlData = await getLnurlData(decoded)
if (!lnurlData) { return openPromptAutoClose({ msg: 'Could not fetch data from LNURL' }) }
return navigation.navigate('selectAmount', { mint, balance, isMelt: true, lnurl: { userInput: input, url: decoded, data: lnurlData } })
} catch (e) {
return openPromptAutoClose({ msg: 'Could not fetch data from LNURL' })
}
}
// not enough funds
if (decodedAmount + estFee > balance) {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Payment/Send/SelectTarget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function SelectTargetScreen({ navigation, route }: TSelectTargetP
mint,
balance,
isMelt: true,
lnurl: lud16
lnurl: { userInput: lud16 }
})
}}
hasSeparator
Expand Down
Loading

0 comments on commit 8742b34

Please sign in to comment.