Skip to content

Commit

Permalink
feat: enable send payment when fee probe fails (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
daviroo authored Jul 15, 2022
1 parent 5ec9617 commit b9d0574
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ const styles = StyleSheet.create({
color: palette.red,
textAlign: "center",
},
maxFeeWarningText: {
color: palette.midGrey,
fontWeight: "bold",
},
disabledButtonStyle: {
backgroundColor: "rgba(83, 111, 242, 0.1)",
},
Expand Down Expand Up @@ -198,7 +202,7 @@ const SendBitcoinConfirmationScreen = ({
}, [convertPaymentAmount, paymentAmount, payerWalletDescriptor.currency])

const { usdWalletBalance, btcWalletBalance, btcWalletValueInUsd } = useWalletBalance()
const [error, setError] = useState<string | undefined>(undefined)
const [paymentError, setPaymentError] = useState<string | undefined>(undefined)

const [intraLedgerPaymentSend, { loading: intraledgerLoading }] =
useMutation.intraLedgerPaymentSend()
Expand Down Expand Up @@ -364,55 +368,55 @@ const SendBitcoinConfirmationScreen = ({
}

if (status === "ALREADY_PAID") {
setError("Invoice is already paid")
setPaymentError("Invoice is already paid")
return
}

setError(errorsMessage || "Something went wrong")
setPaymentError(errorsMessage || "Something went wrong")
} catch (err) {
setStatus(Status.ERROR)
setError(err.message || err.toString())
setPaymentError(err.message || err.toString())
}
}

let validAmount = false
let errorMessage

if (fee.amount && payerWalletDescriptor.currency === WalletCurrency.BTC) {
if (paymentAmount.currency === WalletCurrency.USD) {
validAmount =
usdPaymentAmount.amount + fee.amount.amount <= 100 * btcWalletValueInUsd
if (!validAmount) {
errorMessage = translate("SendBitcoinScreen.amountExceed", {
balance: usdAmountDisplay(btcWalletValueInUsd),
})
setPaymentError(
translate("SendBitcoinScreen.amountExceed", {
balance: usdAmountDisplay(btcWalletValueInUsd),
}),
)
}
}

if (paymentAmount.currency === WalletCurrency.BTC) {
validAmount = paymentAmount.amount + fee.amount.amount <= btcWalletBalance
if (!validAmount) {
errorMessage = translate("SendBitcoinScreen.amountExceed", {
balance: satAmountDisplay(btcWalletBalance),
})
setPaymentError(
translate("SendBitcoinScreen.amountExceed", {
balance: satAmountDisplay(btcWalletBalance),
}),
)
}
}
}

if (fee.amount && payerWalletDescriptor.currency === WalletCurrency.USD) {
validAmount = paymentAmount.amount + fee.amount.amount <= usdWalletBalance
if (!validAmount) {
errorMessage = translate("SendBitcoinScreen.amountExceed", {
balance: usdAmountDisplay(usdWalletBalance / 100),
})
setPaymentError(
translate("SendBitcoinScreen.amountExceed", {
balance: usdAmountDisplay(usdWalletBalance / 100),
}),
)
}
}

errorMessage =
errorMessage ??
error ??
(fee.status === "error" && translate("SendBitcoinScreen.feeCalculationUnsuccessful"))

return (
<ScrollView
showsVerticalScrollIndicator={false}
Expand Down Expand Up @@ -561,31 +565,38 @@ const SendBitcoinConfirmationScreen = ({
</View>
</>
) : null}

<Text style={styles.fieldTitleText}>
{translate("SendBitcoinConfirmationScreen.feeLabel")}
</Text>
<View style={styles.fieldBackground}>
<Text style={styles.destinationText}>
{fee.status === "loading" ? <ActivityIndicator /> : feeDisplayText}
</Text>
<View style={styles.destinationText}>
{fee.status === "loading" && <ActivityIndicator />}
{fee.status === "set" && <Text>{feeDisplayText}</Text>}
{fee.status === "error" && Boolean(feeDisplayText) && (
<Text>{feeDisplayText} *</Text>
)}
</View>
</View>
{fee.status === "error" && Boolean(feeDisplayText) && (
<Text style={styles.maxFeeWarningText}>
{"*" + translate("SendBitcoinConfirmationScreen.maxFeeSelected")}
</Text>
)}

{errorMessage && (
{paymentError && (
<View style={styles.errorContainer}>
<Text style={styles.errorText}>{translate(errorMessage)}</Text>
<Text style={styles.errorText}>{paymentError}</Text>
</View>
)}

<View style={styles.buttonContainer}>
{isLoading && <ActivityIndicator />}
<Button
loading={isLoading}
title={translate("SendBitcoinConfirmationScreen.title")}
buttonStyle={styles.button}
titleStyle={styles.buttonTitleStyle}
disabledStyle={[styles.button, styles.disabledButtonStyle]}
disabledTitleStyle={styles.disabledButtonTitleStyle}
disabled={fee.status === "error" || !validAmount}
disabled={fee.status === "loading" || isLoading || !validAmount}
onPress={sendPayment}
/>
</View>
Expand Down
19 changes: 11 additions & 8 deletions app/screens/send-bitcoin-screen/use-fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const useFee = ({
}

if (paymentType === "lightning" || paymentType === "lnurl") {
let feeProbeFailed = false

if (sameNode || (isNoAmountInvoice && paymentAmount.amount === 0)) {
return setFee({
amount: { amount: 0, currency: walletDescriptor.currency },
Expand All @@ -80,33 +82,34 @@ const useFee = ({
},
},
})
if (errorsMessage) {
throw new Error("Error returned from API while calculating fee.")
}

feeValue =
"lnNoAmountInvoiceFeeProbe" in data
? data.lnNoAmountInvoiceFeeProbe.amount
: data.lnNoAmountUsdInvoiceFeeProbe.amount

if (errorsMessage && feeValue) {
feeProbeFailed = true
}
} else {
const { data, errorsMessage } = await getLightningFees({
variables: {
input: { walletId: walletDescriptor.id, paymentRequest: invoice },
},
})

if (errorsMessage) {
throw new Error("Error returned from API while calculating fee.")
}

feeValue =
"lnInvoiceFeeProbe" in data
? data.lnInvoiceFeeProbe.amount
: data.lnUsdInvoiceFeeProbe.amount
if (errorsMessage && feeValue) {
feeProbeFailed = true
}
}

setFee({
amount: { amount: feeValue, currency: walletDescriptor.currency },
status: "set",
status: feeProbeFailed ? "error" : "set",
})
} catch (err) {
console.debug({ err, message: "error getting lightning fees" })
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1881,9 +1881,9 @@
integrity sha512-Doz2bfiPndXYFPMRwPyGa1k5QaKDVpY806UJj570epIiMzWaYyCtobasyfC++qfIXVb5Ocy7r3tP9d62hAQ7IQ==

"@galoymoney/client@^0.1.33":
version "0.1.35"
resolved "https://registry.yarnpkg.com/@galoymoney/client/-/client-0.1.35.tgz#5d5065936b33c594eed5ea19ed9ce5958c7df013"
integrity sha512-S9jQjxUffU16zQO4vj8uo96+qlgmjab0M7HW8TqlK/dA4OA/W1i2nZc8YWoyT1xlOpDlVvfmSQVpsOm5jsmuxw==
version "0.1.37"
resolved "https://registry.yarnpkg.com/@galoymoney/client/-/client-0.1.37.tgz#8b80e80e5322720735a872a3ec449a16c8a2d9ef"
integrity sha512-DRRUWKKM5SR9onJknnGI2eRUf1xbBVQvvGi8yvll7t7hZDAzkFngY+MRZXPSi53g48yXiP/pnu8LA699E4jHtg==

"@galoymoney/react-native-geetest-module@^0.1.3":
version "0.1.3"
Expand Down

0 comments on commit b9d0574

Please sign in to comment.