Skip to content

Commit

Permalink
fix: incorrect ui with psbt listing tx, closes #4428
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Oct 26, 2023
1 parent 190868a commit bd92a7e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,46 @@ import { PsbtAddressTotalItem } from './psbt-address-total-item';
import { PsbtInscription } from './psbt-inscription';

interface PsbtAddressTotalsProps {
showNativeSegwitTotal: boolean;
showTaprootTotal: boolean;
}
export function PsbtAddressReceiveTotals({ showTaprootTotal }: PsbtAddressTotalsProps) {
const { accountInscriptionsBeingReceived, addressTaproot, addressTaprootTotal } =
usePsbtSignerContext();
export function PsbtAddressReceiveTotals({
showNativeSegwitTotal,
showTaprootTotal,
}: PsbtAddressTotalsProps) {
const {
accountInscriptionsBeingReceived,
addressNativeSegwit,
addressNativeSegwitTotal,
addressTaproot,
addressTaprootTotal,
} = usePsbtSignerContext();
const calculateBitcoinFiatValue = useCalculateBitcoinFiatValue();

const isReceivingInscriptions = accountInscriptionsBeingReceived?.length;
const isReceivingInscriptions = accountInscriptionsBeingReceived?.length > 0;

return (
<>
{!isReceivingInscriptions && showNativeSegwitTotal ? (
<PsbtAddressTotalItem
hoverLabel={addressNativeSegwit}
subtitle={truncateMiddle(addressNativeSegwit)}
subValue={i18nFormatCurrency(calculateBitcoinFiatValue(addressNativeSegwitTotal)).replace(
'-',
''
)}
value={formatMoney(addressNativeSegwitTotal).replace('-', '')}
/>
) : null}
{!isReceivingInscriptions && showTaprootTotal ? (
<PsbtAddressTotalItem
hoverLabel={addressTaproot}
subtitle={truncateMiddle(addressTaproot)}
subValue={i18nFormatCurrency(calculateBitcoinFiatValue(addressTaprootTotal))}
value={formatMoney(addressTaprootTotal)}
subValue={i18nFormatCurrency(calculateBitcoinFiatValue(addressTaprootTotal)).replace(
'-',
''
)}
value={formatMoney(addressTaprootTotal).replace('-', '')}
/>
) : null}
{isReceivingInscriptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function PsbtAddressTransferTotals({ showNativeSegwitTotal }: PsbtAddress
usePsbtSignerContext();
const calculateBitcoinFiatValue = useCalculateBitcoinFiatValue();

const isTransferringInscriptions = accountInscriptionsBeingTransferred?.length;
const isTransferringInscriptions = accountInscriptionsBeingTransferred?.length > 0;

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function PsbtInscription({ inscription }: PsbtInscriptionProps) {
/>
);

//
return (
<PsbtAddressTotalItem
image={<InscriptionPreview inscription={supportedInscription} height="40px" width="40px" />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export function PsbtInputsOutputsTotals() {
{isReceiving ? (
<Box p="space.05">
<PsbtRequestDetailsSectionHeader title="You'll receive" />
<PsbtAddressReceiveTotals showTaprootTotal={isTaprootTotalLessThanZero} />
<PsbtAddressReceiveTotals
showNativeSegwitTotal={isNativeSegwitTotalLessThanZero}
showTaprootTotal={isTaprootTotalLessThanZero}
/>
</Box>
) : null}
</PsbtRequestDetailsSectionLayout>
Expand Down
12 changes: 10 additions & 2 deletions src/app/features/psbt-signer/hooks/use-parsed-psbt.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { useCallback } from 'react';
import { useCallback, useMemo } from 'react';

import * as btc from '@scure/btc-signer';

import { createMoney } from '@shared/models/money.model';

import { subtractMoney } from '@app/common/money/calculate-money';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentAccountTaprootIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
Expand Down Expand Up @@ -50,12 +52,18 @@ export function useParsedPsbt({ inputs, indexesToSign, outputs }: UseParsedPsbtA
return noInputs || noOutputs;
}, [inputs.length, outputs.length]);

const fee = useMemo(() => {
if (psbtInputsTotal.amount.isGreaterThan(psbtOutputsTotal.amount))
return subtractMoney(psbtInputsTotal, psbtOutputsTotal);
return createMoney(0, 'BTC');
}, [psbtInputsTotal, psbtOutputsTotal]);

return {
accountInscriptionsBeingTransferred,
accountInscriptionsBeingReceived,
addressNativeSegwitTotal: subtractMoney(inputsTotalNativeSegwit, outputsTotalNativeSegwit),
addressTaprootTotal: subtractMoney(inputsTotalTaproot, outputsTotalTaproot),
fee: subtractMoney(psbtInputsTotal, psbtOutputsTotal),
fee,
isPsbtMutable,
psbtInputs: parsedInputs,
psbtOutputs: parsedOutputs,
Expand Down
49 changes: 29 additions & 20 deletions src/app/features/psbt-signer/hooks/use-psbt-inscriptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,33 @@ export function usePsbtInscriptions(psbtInputs: PsbtInput[], psbtOutputs: PsbtOu
[psbtInputs, psbtOutputs]
);

return useMemo(
() => ({
accountInscriptionsBeingTransferred: psbtInputs
.filter(
input =>
input.address === bitcoinAddressNativeSegwit || input.address === bitcoinAddressTaproot
)
.map(input => input.inscription)
.filter(isDefined),
accountInscriptionsBeingReceived: outputsReceivingInscriptions
.filter(
outputWithInscription =>
outputWithInscription.address === bitcoinAddressNativeSegwit ||
outputWithInscription.address === bitcoinAddressTaproot
)
.map(input => input.inscription)
.filter(isDefined),
}),
[bitcoinAddressNativeSegwit, bitcoinAddressTaproot, outputsReceivingInscriptions, psbtInputs]
);
return useMemo(() => {
const accountInscriptionsBeingTransferred = psbtInputs
.filter(
input =>
input.address === bitcoinAddressNativeSegwit || input.address === bitcoinAddressTaproot
)
.map(input => input.inscription)
.filter(isDefined);

const accountInscriptionsBeingReceived = outputsReceivingInscriptions
.filter(
outputWithInscription =>
outputWithInscription.address === bitcoinAddressNativeSegwit ||
outputWithInscription.address === bitcoinAddressTaproot
)
.map(input => input.inscription)
.filter(
inscription =>
!accountInscriptionsBeingTransferred.find(
transferInscription => inscription.id === transferInscription.id
)
)
.filter(isDefined);

return {
accountInscriptionsBeingTransferred,
accountInscriptionsBeingReceived,
};
}, [bitcoinAddressNativeSegwit, bitcoinAddressTaproot, outputsReceivingInscriptions, psbtInputs]);
}

0 comments on commit bd92a7e

Please sign in to comment.