Skip to content

Commit

Permalink
fix: insufficient funds in send max btc flow
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo authored and kyranjamie committed Nov 25, 2024
1 parent 3360686 commit 531fd94
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function calculateMaxBitcoinSpend({
utxos: filteredUtxos,
feeRate: currentFeeRate,
recipients: [{ address, amount: createMoney(0, 'BTC') }],
isSendMax: true,
});

return {
Expand Down
20 changes: 15 additions & 5 deletions src/app/common/transactions/bitcoin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,25 @@ export function getBitcoinTxValue(address: string, transaction?: BitcoinTx) {
return '';
}

interface GetSpendableAmountArgs {
utxos: UtxoResponseItem[];
feeRate: number;
recipients: TransferRecipient[];
isSendMax?: boolean;
}

export function getSpendableAmount({
utxos,
feeRate,
recipients,
}: {
utxos: UtxoResponseItem[];
feeRate: number;
recipients: TransferRecipient[];
}) {
isSendMax,
}: GetSpendableAmountArgs) {
const balance = utxos.map(utxo => utxo.value).reduce((prevVal, curVal) => prevVal + curVal, 0);

const size = getSizeInfo({
inputLength: utxos.length,
recipients,
isSendMax,
});
const fee = Math.ceil(size.txVBytes * feeRate);
const bigNumberBalance = BigNumber(balance);
Expand Down Expand Up @@ -169,6 +174,11 @@ export function getSizeInfo(payload: {

const outputTypesCount = getTxOutputsLengthByPaymentType();

// If no outputs, e.g. when recipient is not provided, set default output to p2wpkh
if (Object.values(outputTypesCount).length === 0) {
outputTypesCount[AddressType.p2wpkh] = 1;
}

// Add a change address if not sending max (defaults to p2wpkh)
if (!isSendMax) {
outputTypesCount[AddressType.p2wpkh] = (outputTypesCount[AddressType.p2wpkh] || 0) + 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface AccountListItemProps {
onClose(): void;
}
export const AccountListItem = memo(({ index, stacksAccount, onClose }: AccountListItemProps) => {
const { setFieldValue, values, setFieldTouched } = useFormikContext<
const { setFieldValue, values } = useFormikContext<
BitcoinSendFormValues | StacksSendFormValues
>();
const stacksAddress = stacksAccount?.address || '';
Expand All @@ -30,7 +30,6 @@ export const AccountListItem = memo(({ index, stacksAccount, onClose }: AccountL
const onSelectAccount = () => {
const isBitcoin = values.symbol === 'BTC';
void setFieldValue('recipient', isBitcoin ? bitcoinAddress : stacksAddress, false);
void setFieldTouched('recipient', false);
onClose();
};

Expand Down

0 comments on commit 531fd94

Please sign in to comment.