mirror of
https://github.com/zhigang1992/xverse-web-extension.git
synced 2026-01-12 18:02:19 +08:00
fix: handle btc fee error
This commit is contained in:
@@ -187,7 +187,7 @@ function EditBtcFee({
|
||||
const { ordinals } = useOrdinalsByAddress(btcAddress);
|
||||
const ordinalsUtxos = useMemo(() => ordinals?.map((ord) => ord.utxo), [ordinals]);
|
||||
const btcClient = useBtcClient();
|
||||
const feeData = useBtcFees({
|
||||
const { feeData, highFeeError, mediumFeeError } = useBtcFees({
|
||||
isRestoreFlow: !!isRestoreFlow,
|
||||
nonOrdinalUtxos,
|
||||
btcRecipients,
|
||||
@@ -341,6 +341,7 @@ function EditBtcFee({
|
||||
setFeeMode('high');
|
||||
}}
|
||||
selected={feeMode === 'high'}
|
||||
error={highFeeError}
|
||||
/>
|
||||
<FeeItem
|
||||
priority="medium"
|
||||
@@ -358,6 +359,7 @@ function EditBtcFee({
|
||||
setFeeMode('medium');
|
||||
}}
|
||||
selected={feeMode === 'medium'}
|
||||
error={mediumFeeError}
|
||||
/>
|
||||
<FeeItemContainer
|
||||
isSelected={feeMode === 'custom'}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Bicycle, CarProfile, RocketLaunch } from '@phosphor-icons/react';
|
||||
import { ErrorCodes } from '@secretkeylabs/xverse-core';
|
||||
import { StyledP } from '@ui-library/common.styled';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { MoonLoader } from 'react-spinners';
|
||||
@@ -54,7 +55,6 @@ const StyledHeading = styled(StyledP)`
|
||||
`;
|
||||
|
||||
const StyledSubText = styled(StyledP)`
|
||||
color: ${(props) => props.theme.colors.white_200};
|
||||
margin-bottom: ${(props) => props.theme.space.xxs};
|
||||
`;
|
||||
|
||||
@@ -75,9 +75,19 @@ interface FeeItemProps {
|
||||
fiat: string | JSX.Element;
|
||||
selected: boolean;
|
||||
onClick?: () => void;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
function FeeItem({ priority, time, feeRate, totalFee, fiat, selected, onClick }: FeeItemProps) {
|
||||
function FeeItem({
|
||||
priority,
|
||||
time,
|
||||
feeRate,
|
||||
totalFee,
|
||||
fiat,
|
||||
selected,
|
||||
error,
|
||||
onClick,
|
||||
}: FeeItemProps) {
|
||||
const { t } = useTranslation('translation');
|
||||
const getIcon = () => {
|
||||
switch (priority) {
|
||||
@@ -105,25 +115,50 @@ function FeeItem({ priority, time, feeRate, totalFee, fiat, selected, onClick }:
|
||||
}
|
||||
};
|
||||
|
||||
const getErrorMessage = (btcError: string) => {
|
||||
if (
|
||||
Number(btcError) === ErrorCodes.InSufficientBalance ||
|
||||
Number(btcError) === ErrorCodes.InSufficientBalanceWithTxFee
|
||||
) {
|
||||
return t('SEND.ERRORS.INSUFFICIENT_BALANCE');
|
||||
}
|
||||
return btcError;
|
||||
};
|
||||
|
||||
return (
|
||||
<FeeItemContainer onClick={onClick} isSelected={selected} disabled={!totalFee}>
|
||||
<FeeItemContainer onClick={onClick} isSelected={selected} disabled={!totalFee || !!error}>
|
||||
<IconContainer>{getIcon()}</IconContainer>
|
||||
<TextsContainer>
|
||||
<ColumnsTexts>
|
||||
<StyledHeading typography="body_medium_m" color="white_0">
|
||||
{getLabel()}
|
||||
</StyledHeading>
|
||||
<StyledSubText typography="body_medium_s">{time}</StyledSubText>
|
||||
<StyledSubText typography="body_medium_s">{`${feeRate} Sats/ vByte`}</StyledSubText>
|
||||
<StyledSubText typography="body_medium_s" color="white_200">
|
||||
{time}
|
||||
</StyledSubText>
|
||||
<StyledSubText
|
||||
typography="body_medium_s"
|
||||
color="white_200"
|
||||
>{`${feeRate} Sats/ vByte`}</StyledSubText>
|
||||
</ColumnsTexts>
|
||||
{totalFee ? (
|
||||
<EndColumnTexts>
|
||||
|
||||
<EndColumnTexts>
|
||||
{totalFee && (
|
||||
<StyledHeading typography="body_medium_m" color="white_0">
|
||||
{`${totalFee} Sats`}
|
||||
</StyledHeading>
|
||||
<StyledSubText typography="body_medium_s">{fiat}</StyledSubText>
|
||||
</EndColumnTexts>
|
||||
) : (
|
||||
)}
|
||||
<StyledSubText typography="body_medium_s" color="white_200">
|
||||
{fiat}
|
||||
</StyledSubText>
|
||||
{error && (
|
||||
<StyledSubText typography="body_medium_s" color="danger_medium">
|
||||
{getErrorMessage(error)}
|
||||
</StyledSubText>
|
||||
)}
|
||||
</EndColumnTexts>
|
||||
|
||||
{!totalFee && !error && (
|
||||
<LoaderContainer>
|
||||
<MoonLoader color="white" size={20} />
|
||||
</LoaderContainer>
|
||||
|
||||
@@ -31,13 +31,15 @@ const useBtcFees = ({
|
||||
btcRecipients,
|
||||
type,
|
||||
ordinalTxUtxo,
|
||||
}: Params): FeeData => {
|
||||
}: Params): { feeData: FeeData; highFeeError?: string; mediumFeeError?: string } => {
|
||||
const [feeData, setFeeData] = useState<FeeData>({
|
||||
standardFeeRate: '',
|
||||
standardTotalFee: '',
|
||||
highFeeRate: '',
|
||||
highTotalFee: '',
|
||||
});
|
||||
const [highFeeError, setHighFeeError] = useState<string>('');
|
||||
const [standardFeeError, setStandardFeeError] = useState<string>('');
|
||||
const { network, btcAddress, ordinalsAddress } = useWalletSelector();
|
||||
const btcClient = useBtcClient();
|
||||
const { ordinals } = useOrdinalsByAddress(btcAddress);
|
||||
@@ -46,6 +48,8 @@ const useBtcFees = ({
|
||||
useEffect(() => {
|
||||
async function fetchFees(mode: 'standard' | 'high') {
|
||||
try {
|
||||
setStandardFeeError('');
|
||||
setHighFeeError('');
|
||||
let feeInfo;
|
||||
if (isRestoreFlow) {
|
||||
feeInfo = await getBtcFeesForNonOrdinalBtcSend(
|
||||
@@ -55,7 +59,7 @@ const useBtcFees = ({
|
||||
network.type,
|
||||
mode,
|
||||
);
|
||||
} else if (type !== 'Ordinals' && btcRecipients) {
|
||||
} else if (type === 'BTC' && btcRecipients) {
|
||||
feeInfo = await getBtcFees(btcRecipients, btcAddress, btcClient, network.type, mode);
|
||||
} else if (type === 'Ordinals' && btcRecipients && ordinalTxUtxo) {
|
||||
feeInfo = await getBtcFeesForOrdinalSend(
|
||||
@@ -68,12 +72,13 @@ const useBtcFees = ({
|
||||
mode,
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
fee: feeInfo?.fee.toString() || '',
|
||||
feeRate: feeInfo?.selectedFeeRate.toString() || '',
|
||||
};
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
if (mode === 'standard') setStandardFeeError(error.toString());
|
||||
else if (mode === 'high') setHighFeeError(error.toString());
|
||||
return { fee: '', feeRate: '' };
|
||||
}
|
||||
}
|
||||
@@ -103,8 +108,7 @@ const useBtcFees = ({
|
||||
ordinalsUtxos,
|
||||
ordinalsAddress,
|
||||
]);
|
||||
|
||||
return feeData;
|
||||
return { feeData, highFeeError, mediumFeeError: standardFeeError };
|
||||
};
|
||||
|
||||
export default useBtcFees;
|
||||
|
||||
Reference in New Issue
Block a user