Skip to content

Commit

Permalink
fix: balance validation in sendMax for LN tx
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Jan 12, 2023
1 parent 51df848 commit 3afd7f6
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 36 deletions.
12 changes: 5 additions & 7 deletions src/screens/Wallets/NumberPadButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import { SwitchIcon } from '../../styles/icons';
import { updateSettings } from '../../store/actions/settings';
import useDisplayValues from '../../hooks/displayValues';
import { IColors } from '../../styles/colors';
import {
onChainBalanceSelector,
transactionMaxSelector,
} from '../../store/reselect/wallet';
import { transactionMaxSelector } from '../../store/reselect/wallet';
import {
bitcoinUnitSelector,
unitPreferenceSelector,
} from '../../store/reselect/settings';
import { EBitcoinUnit } from '../../store/types/wallet';
import { useBalance } from '../../hooks/wallet';

type NumberPadButtons = {
color?: keyof IColors;
Expand All @@ -31,12 +29,12 @@ const NumberPadButtons = ({
onMaxPress,
onDone,
}: NumberPadButtons): ReactElement => {
const balance = useSelector(onChainBalanceSelector);
const { satoshis } = useBalance({ onchain: true, lightning: true });
const bitcoinUnit = useSelector(bitcoinUnitSelector);
const unitPreference = useSelector(unitPreferenceSelector);
const isMaxSendAmount = useSelector(transactionMaxSelector);

const displayValues = useDisplayValues(balance);
const displayValues = useDisplayValues(satoshis);

// BTC -> satoshi -> fiat
const nextUnit = useMemo(() => {
Expand All @@ -60,7 +58,7 @@ const NumberPadButtons = ({
<TouchableOpacity
style={styles.button}
color="white08"
disabled={balance <= 0}
disabled={satoshis <= 0}
onPress={onMaxPress}>
<Text02B size="12px" color={isMaxSendAmount ? 'orange' : color}>
MAX
Expand Down
95 changes: 66 additions & 29 deletions src/utils/wallet/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1561,39 +1561,72 @@ export const sendMax = ({
address = outputs[index]?.address ?? '';
}

const inputTotal = getTransactionInputValue({
selectedNetwork,
selectedWallet,
inputs: transaction.inputs,
});
let inputTotal = 0;
const max = transaction?.max;
if (
!max &&
inputTotal > 0 &&
transaction?.fee &&
inputTotal / 2 > transaction.fee
) {
const newFee = getTotalFee({
satsPerByte: transaction.satsPerByte ?? 1,
message: transaction.message,
});
const _transaction: IBitcoinTransactionData = {
fee: newFee,
outputs: [{ address, value: inputTotal - newFee, index }],
max: !max,
};
updateBitcoinTransaction({

if (transaction?.lightningInvoice) {
// lightning transaction
const { satoshis } = getBalance({
lightning: true,
selectedWallet,
selectedNetwork,
transaction: _transaction,
}).then();
});
inputTotal = satoshis;

if (!max && inputTotal > 0) {
const _transaction: IBitcoinTransactionData = {
outputs: [{ address, value: inputTotal, index }],
max: !max,
};
updateBitcoinTransaction({
selectedWallet,
selectedNetwork,
transaction: _transaction,
}).then();
} else {
updateBitcoinTransaction({
selectedWallet,
selectedNetwork,
transaction: { max: !max },
}).then();
}
} else {
updateBitcoinTransaction({
selectedWallet,
// onchain transaction
inputTotal = getTransactionInputValue({
selectedNetwork,
transaction: { max: !max },
}).then();
selectedWallet,
inputs: transaction.inputs,
});

if (
!max &&
inputTotal > 0 &&
transaction?.fee &&
inputTotal / 2 > transaction.fee
) {
const newFee = getTotalFee({
satsPerByte: transaction.satsPerByte ?? 1,
message: transaction.message,
});
const _transaction: IBitcoinTransactionData = {
fee: newFee,
outputs: [{ address, value: inputTotal - newFee, index }],
max: !max,
};
updateBitcoinTransaction({
selectedWallet,
selectedNetwork,
transaction: _transaction,
}).then();
} else {
updateBitcoinTransaction({
selectedWallet,
selectedNetwork,
transaction: { max: !max },
}).then();
}
}

return ok('Successfully setup max send transaction.');
} catch (e) {
return err(e);
Expand Down Expand Up @@ -1704,7 +1737,11 @@ export const updateAmount = async ({

if (transaction?.lightningInvoice) {
// lightning transaction
const { satoshis } = getBalance({ lightning: true });
const { satoshis } = getBalance({
lightning: true,
selectedWallet,
selectedNetwork,
});
inputTotal = satoshis;

if (newAmount === inputTotal) {
Expand Down Expand Up @@ -2002,7 +2039,7 @@ export const setupCpfp = async ({
}

// Construct the tx to send funds back to ourselves using the assigned inputs, receive address and fee.
const sendMaxResponse = await sendMax({
const sendMaxResponse = sendMax({
selectedWallet,
selectedNetwork,
transaction: {
Expand Down

0 comments on commit 3afd7f6

Please sign in to comment.