Skip to content

Commit

Permalink
fix: add error handling durin CJIT flow
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Oct 18, 2024
1 parent ff1cb6b commit 1f0b7e4
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
13 changes: 11 additions & 2 deletions src/screens/Wallets/Receive/ReceiveAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { updateInvoice } from '../../../store/slices/receive';
import { receiveSelector } from '../../../store/reselect/receive';
import { estimateOrderFee } from '../../../utils/blocktank';
import { getNumberPadText } from '../../../utils/numberpad';
import { showToast } from '../../../utils/notifications';
import { blocktankInfoSelector } from '../../../store/reselect/blocktank';
import { refreshBlocktankInfo } from '../../../store/utils/blocktank';
import {
Expand Down Expand Up @@ -63,11 +64,17 @@ const ReceiveAmount = ({
// add 10% buffer and round up to the nearest 1000 to avoid fee fluctuations
const minimum = Math.ceil((feeResult.value * 1.1) / 1000) * 1000;
setMinimumAmount(minimum);
} else {
showToast({
type: 'error',
title: t('receive_cjit_error'),
description: feeResult.error.message,
});
}
};

getFeeEstimation();
}, [channelSize]);
}, [t, channelSize]);

const onMinimum = (): void => {
const result = getNumberPadText(minimumAmount, denomination, unit);
Expand All @@ -89,7 +96,9 @@ const ReceiveAmount = ({
};

const continueDisabled =
invoice.amount < minimumAmount || invoice.amount > channelSize;
invoice.amount < minimumAmount ||
invoice.amount > channelSize ||
minimumAmount === 0;

return (
<GradientView style={styles.container}>
Expand Down
85 changes: 55 additions & 30 deletions src/screens/Wallets/Receive/ReceiveConnect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { memo, ReactElement, useEffect, useState } from 'react';
import { StyleSheet, View, Image } from 'react-native';
import { StyleSheet, View, Image, ActivityIndicator } from 'react-native';
import { Trans, useTranslation } from 'react-i18next';

import { Caption13Up, BodyMB, BodyM } from '../../../styles/text';
Expand Down Expand Up @@ -33,6 +33,7 @@ const ReceiveConnect = ({
const lightningBalance = useLightningBalance(true);
const [feeEstimate, setFeeEstimate] = useState(0);
const [isLoading, setIsLoading] = useState(false);
const [isLoadingFee, setIsLoadingFee] = useState(false);
const dispatch = useAppDispatch();
const blocktank = useAppSelector(blocktankInfoSelector);
const { amount, message } = useAppSelector(receiveSelector);
Expand All @@ -47,14 +48,22 @@ const ReceiveConnect = ({

useEffect(() => {
const getFeeEstimation = async (): Promise<void> => {
setIsLoadingFee(true);
const estimate = await estimateOrderFee({ lspBalance });
if (estimate.isOk()) {
setFeeEstimate(estimate.value);
} else {
showToast({
type: 'error',
title: t('receive_cjit_error'),
description: estimate.error.message,
});
}
setIsLoadingFee(false);
};

getFeeEstimation();
}, [lspBalance]);
}, [t, lspBalance]);

const onMore = (): void => {
navigation.navigate('Liquidity', {
Expand Down Expand Up @@ -95,34 +104,44 @@ const ReceiveConnect = ({
<View style={styles.content}>
<AmountToggle amount={amount} />

<BodyM style={styles.text} color="secondary">
<Trans
t={t}
i18nKey={
isInitial
? 'receive_connect_initial'
: 'receive_connect_additional'
}
components={{ accent: <BodyMB color="white" /> }}
values={{ lspFee: `${fiatSymbol}${displayFee.fiatFormatted}` }}
/>
</BodyM>

<View style={styles.payAmount}>
<Caption13Up style={styles.payAmountText} color="secondary">
{t('receive_will')}
</Caption13Up>
<Money
sats={payAmount}
size="title"
symbol={true}
testID="AvailableAmount"
/>
</View>

<View style={styles.imageContainer}>
<Image style={styles.image} source={imageSrc} />
</View>
{feeEstimate > 0 && (
<>
<BodyM style={styles.text} color="secondary">
<Trans
t={t}
i18nKey={
isInitial
? 'receive_connect_initial'
: 'receive_connect_additional'
}
components={{ accent: <BodyMB color="white" /> }}
values={{ lspFee: `${fiatSymbol}${displayFee.fiatFormatted}` }}
/>
</BodyM>

<View style={styles.payAmount}>
<Caption13Up style={styles.payAmountText} color="secondary">
{t('receive_will')}
</Caption13Up>
<Money
sats={payAmount}
size="title"
symbol={true}
testID="AvailableAmount"
/>
</View>

<View style={styles.imageContainer}>
<Image style={styles.image} source={imageSrc} />
</View>
</>
)}

{isLoadingFee && (
<View style={styles.indicator}>
<ActivityIndicator color="white" size="large" />
</View>
)}

<View style={styles.buttonContainer}>
<Button
Expand All @@ -132,6 +151,7 @@ const ReceiveConnect = ({
variant="secondary"
testID="ReceiveConnectMore"
onPress={onMore}
disabled={feeEstimate === 0}
/>
<Button
style={styles.button}
Expand All @@ -140,6 +160,7 @@ const ReceiveConnect = ({
loading={isLoading}
testID="ReceiveConnectContinue"
onPress={onContinue}
disabled={feeEstimate === 0}
/>
</View>
</View>
Expand Down Expand Up @@ -177,6 +198,10 @@ const styles = StyleSheet.create({
flex: 1,
resizeMode: 'contain',
},
indicator: {
marginTop: 'auto',
marginBottom: 'auto',
},
buttonContainer: {
flexDirection: 'row',
justifyContent: 'center',
Expand Down

0 comments on commit 1f0b7e4

Please sign in to comment.