Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make donate spend from the wallet #331

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"keysetID": "Keyset ID",
"lnInvoice": "Lightning invoice",
"lnPayment": "Lightning payment",
"lowBalanceError": "Mint balance is not sufficiant to donate",
"mint": "Mint",
"minted": "minted",
"multimintSwap": "Multimint swap",
Expand Down
56 changes: 46 additions & 10 deletions src/components/modal/Zap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@ import { DONATION_ADDR } from '@consts/mints'
import { usePromptContext } from '@src/context/Prompt'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { IZapModalProps, IZapReturnData } from '@src/model/zap'
import { getMintsBalances } from '@src/storage/db'
import { getCustomMintNames } from '@store/mintStore'
import { globals } from '@styles'
import { formatSatStr, getInvoiceFromLnurl, isErr, openUrl } from '@util'
import { formatSatStr, getInvoiceFromLnurl, isErr } from '@util'
import { checkFees } from '@wallet'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Text, TouchableOpacity, View } from 'react-native'
import { s, ScaledSheet, vs } from 'react-native-size-matters'

import MyModal from '.'

interface IQuestionModalProps {
visible: boolean
close: () => void
}

interface IZap { amount: number, emoji: string, selected: boolean }

export function ZapModal({ visible, close }: IQuestionModalProps) {
export function ZapModal({ visible, close, onReturnData }: IZapModalProps) {
const { t } = useTranslation([NS.common])
const { color } = useThemeContext()
const { openPromptAutoClose } = usePromptContext()
Expand All @@ -48,11 +47,48 @@ export function ZapModal({ visible, close }: IQuestionModalProps) {
openPromptAutoClose({ msg: 'Zap error' })
return
}
const invoice = await getInvoiceFromLnurl(DONATION_ADDR, zap.amount)
stopLoading()

try {

const invoice = await getInvoiceFromLnurl(DONATION_ADDR, zap.amount)
const mintsWithBal = await getMintsBalances()
if (mintsWithBal.length > 0 ) {
const mints = await getCustomMintNames(mintsWithBal.map(m => ({ mintUrl: m.mintUrl })))
const nonEmptyMint = mintsWithBal.filter(m => m.amount > zap.amount)
if (nonEmptyMint.length > 0) {

const mintUsing = mints.find(m => m.mintUrl === nonEmptyMint[0].mintUrl) || { mintUrl: 'N/A', customName: 'N/A' }
const estFee = await checkFees(mintUsing.mintUrl, invoice)

const returnData: IZapReturnData = {
invoice,
amount: zap.amount,
mintUsing,
estFee,
balance: nonEmptyMint[0].amount

}
onReturnData(returnData)

} else {
openPromptAutoClose({ msg:t('lowBalanceError')})
}

} else {
openPromptAutoClose({ msg:t('lowBalanceError') })
}

} catch (e) {
openPromptAutoClose({ msg: isErr(e) ? e.message : t('lowBalanceError') })
}

stopLoading()
close()


/* commented to spend from the wallet
await openUrl(`lightning:${invoice}`)?.catch(e =>
openPromptAutoClose({ msg: isErr(e) ? e.message : t('deepLinkErr') }))
openPromptAutoClose({ msg: isErr(e) ? e.message : t('deepLinkErr') })) */
} catch (e) {
openPromptAutoClose({ msg: isErr(e) ? e.message : t('deepLinkErr') })
}
Expand Down
18 changes: 18 additions & 0 deletions src/model/zap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { IMintUrl } from './index'

export interface IZapReturnData {

invoice: string
amount: number
estFee: number
mintUsing: IMintUrl
balance: number

}

export interface IZapModalProps {
visible: boolean
close: () => void
onReturnData:(value: IZapReturnData) => void;

}
44 changes: 34 additions & 10 deletions src/screens/Settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { AboutIcon, HeartIcon, MintBoardIcon, OptionsIcon } from '@comps/Icons'
import { ZapModal } from '@comps/modal/Zap'
import Screen from '@comps/Screen'
import Txt from '@comps/Txt'
import { appVersion, env, isIOS } from '@consts/env'
import { appVersion } from '@consts/env'
import { BottomModal } from '@modal/Question'
import type { TSettingsPageProps } from '@model/nav'
import BottomNav from '@nav/BottomNav'
import { useThemeContext } from '@src/context/Theme'
import { NS } from '@src/i18n'
import { IZapReturnData } from '@src/model/zap'
import { dropAllData } from '@src/storage/dev'
import { globals } from '@styles'
// import * as Updates from 'expo-updates'
Expand All @@ -32,6 +33,29 @@ export default function Settings({ navigation, route }: TSettingsPageProps) {
// void Updates.reloadAsync()
}

const handleReturnData = (returnData: IZapReturnData) => {
setZapModal(false)
if (returnData.amount > 0) {

navigation.navigate('coinSelection', {
mint: returnData.mintUsing,
balance: returnData.balance,
amount: returnData.amount,
estFee: returnData.estFee,
recipient: returnData.invoice,
isMelt: true,
scanned: true
})

}

}


const handleCloseZap = () => {
setZapModal(false)
}

return (
<Screen
screenName={t('settings', { ns: NS.topNav })}
Expand Down Expand Up @@ -59,14 +83,14 @@ export default function Settings({ navigation, route }: TSettingsPageProps) {
onPress={() => navigation.navigate('About settings')}
hasSeparator={__DEV__}
/>
{(__DEV__ || env.isExpoBeta || !isIOS) &&
<MenuItem
txt={t('donateLn')}
icon={<HeartIcon color={color.TEXT} />}
onPress={() => setZapModal(true)}
hasSeparator={__DEV__}
/>
}

<MenuItem
txt={t('donateLn')}
icon={<HeartIcon color={color.TEXT} />}
onPress={() => setZapModal(true)}
hasSeparator={__DEV__}
/>

{__DEV__ &&
<MenuItem
txt={t('factoryReset')}
Expand All @@ -78,7 +102,7 @@ export default function Settings({ navigation, route }: TSettingsPageProps) {
<Txt txt={appVersion} bold center />
</ScrollView>
<BottomNav navigation={navigation} route={route} />
<ZapModal visible={zapModal} close={() => setZapModal(false)} />
<ZapModal visible={zapModal} close={() => handleCloseZap()} onReturnData={handleReturnData} />
{/* confirm factory reset */}
<BottomModal
header={t('resetQ')}
Expand Down