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

fix: insufficient funds in send max btc flow #5978

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
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
Loading