-
Notifications
You must be signed in to change notification settings - Fork 146
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: tx calc unhandled errors, closes #4941 #5011
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useState } from 'react'; | ||
|
||
import { useField } from 'formik'; | ||
import { Stack } from 'leather-styles/jsx'; | ||
|
||
import { createMoney } from '@shared/models/money.model'; | ||
|
||
import { useOnMount } from '@app/common/hooks/use-on-mount'; | ||
import { satToBtc } from '@app/common/money/unit-conversion'; | ||
import { InsufficientFundsError } from '@app/common/transactions/bitcoin/coinselect/local-coin-selection'; | ||
import { Input } from '@app/ui/components/input/input'; | ||
|
||
import { ErrorLabel } from '../error-label'; | ||
import { BitcoinCustomFeeFiat } from './bitcoin-custom-fee-fiat'; | ||
import { useBitcoinCustomFee } from './hooks/use-bitcoin-custom-fee'; | ||
|
||
interface Props { | ||
onClick?(): void; | ||
amount: number; | ||
isSendingMax: boolean; | ||
recipient: string; | ||
hasInsufficientBalanceError: boolean; | ||
errorMessage?: string; | ||
setCustomFeeInitialValue?(value: string): void; | ||
customFeeInitialValue: string; | ||
} | ||
|
||
const feeInputLabel = 'sats/vB'; | ||
|
||
export function BitcoinCustomFeeInput({ | ||
onClick, | ||
amount, | ||
isSendingMax, | ||
recipient, | ||
hasInsufficientBalanceError, | ||
setCustomFeeInitialValue, | ||
customFeeInitialValue, | ||
}: Props) { | ||
const [field] = useField('feeRate'); | ||
|
||
const [feeValue, setFeeValue] = useState<null | { | ||
fee: number; | ||
fiatFeeValue: string; | ||
}>(null); | ||
|
||
const getCustomFeeValues = useBitcoinCustomFee({ | ||
amount: createMoney(amount, 'BTC'), | ||
isSendingMax, | ||
recipient, | ||
}); | ||
const [unknownError, setUnknownError] = useState(false); | ||
const [customInsufficientBalanceError, setCustomInsufficientBalanceError] = useState(false); | ||
|
||
const hasError = hasInsufficientBalanceError || unknownError || customInsufficientBalanceError; | ||
const errorMessage = | ||
hasInsufficientBalanceError || customInsufficientBalanceError | ||
? 'Insufficient funds' | ||
: 'Unknown error'; | ||
|
||
function processFeeValue(feeRate: string) { | ||
try { | ||
const feeValues = getCustomFeeValues(Number(feeRate)); | ||
setFeeValue(feeValues); | ||
|
||
setUnknownError(false); | ||
setCustomInsufficientBalanceError(false); | ||
} catch (err) { | ||
if (err instanceof InsufficientFundsError) { | ||
return setCustomInsufficientBalanceError(true); | ||
} | ||
|
||
setUnknownError(true); | ||
} | ||
} | ||
|
||
function onChange(e: React.ChangeEvent<HTMLInputElement>) { | ||
const value = e.target.value; | ||
setCustomFeeInitialValue?.(e.target.value); | ||
processFeeValue(value); | ||
} | ||
|
||
useOnMount(() => { | ||
processFeeValue(customFeeInitialValue); | ||
}); | ||
return ( | ||
<Stack gap="space.05"> | ||
<Stack> | ||
<Input.Root hasError={hasError}> | ||
<Input.Label>{feeInputLabel}</Input.Label> | ||
<Input.Field | ||
onClick={onClick} | ||
{...field} | ||
onChange={e => { | ||
field.onChange(e); | ||
onChange?.(e); | ||
}} | ||
/> | ||
</Input.Root> | ||
{hasError && <ErrorLabel>{errorMessage}</ErrorLabel>} | ||
</Stack> | ||
|
||
{!hasError && feeValue && ( | ||
<BitcoinCustomFeeFiat | ||
feeInBtc={satToBtc(feeValue.fee).toString()} | ||
fiatFeeValue={feeValue.fiatFeeValue} | ||
/> | ||
)} | ||
</Stack> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,14 @@ function getFeeForList( | |
determineUtxosForFeeArgs: DetermineUtxosForSpendArgs, | ||
isSendingMax?: boolean | ||
) { | ||
const { fee } = isSendingMax | ||
? determineUtxosForSpendAll(determineUtxosForFeeArgs) | ||
: determineUtxosForSpend(determineUtxosForFeeArgs); | ||
return fee; | ||
try { | ||
const { fee } = isSendingMax | ||
? determineUtxosForSpendAll(determineUtxosForFeeArgs) | ||
: determineUtxosForSpend(determineUtxosForFeeArgs); | ||
return fee; | ||
} catch (error) { | ||
return null; | ||
} | ||
} | ||
|
||
interface UseBitcoinFeesListArgs { | ||
|
@@ -75,36 +79,46 @@ export function useBitcoinFeesList({ | |
feeRate: feeRates.hourFee.toNumber(), | ||
}; | ||
|
||
const feesArr = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can spread values into this array instead?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made it so to improve readability. don't really like such constructions even if they save several lines of code |
||
|
||
const highFeeValue = getFeeForList(determineUtxosForHighFeeArgs, isSendingMax); | ||
const standardFeeValue = getFeeForList(determineUtxosForStandardFeeArgs, isSendingMax); | ||
const lowFeeValue = getFeeForList(determineUtxosForLowFeeArgs, isSendingMax); | ||
|
||
return [ | ||
{ | ||
if (highFeeValue) { | ||
feesArr.push({ | ||
label: BtcFeeType.High, | ||
value: highFeeValue, | ||
btcValue: formatMoneyPadded(createMoney(highFeeValue, 'BTC')), | ||
time: btcTxTimeMap.fastestFee, | ||
fiatValue: getFiatFeeValue(highFeeValue), | ||
feeRate: feeRates.fastestFee.toNumber(), | ||
}, | ||
{ | ||
}); | ||
} | ||
|
||
if (standardFeeValue) { | ||
feesArr.push({ | ||
label: BtcFeeType.Standard, | ||
value: standardFeeValue, | ||
btcValue: formatMoneyPadded(createMoney(standardFeeValue, 'BTC')), | ||
time: btcTxTimeMap.halfHourFee, | ||
fiatValue: getFiatFeeValue(standardFeeValue), | ||
feeRate: feeRates.halfHourFee.toNumber(), | ||
}, | ||
{ | ||
}); | ||
} | ||
|
||
if (lowFeeValue) { | ||
feesArr.push({ | ||
label: BtcFeeType.Low, | ||
value: lowFeeValue, | ||
btcValue: formatMoneyPadded(createMoney(lowFeeValue, 'BTC')), | ||
time: btcTxTimeMap.hourFee, | ||
fiatValue: getFiatFeeValue(lowFeeValue), | ||
feeRate: feeRates.hourFee.toNumber(), | ||
}, | ||
]; | ||
}); | ||
} | ||
|
||
return feesArr; | ||
}, [feeRates, utxos, isSendingMax, balance.amount, amount.amount, recipient, btcMarketData]); | ||
|
||
return { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Processing the fee on mount, then setting the values, like this seems kinda side-effecty. Okay for now as we're going to do fee work and refactor entirely, but I'm sure there's a nicer way to do this next time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I realise it could have been done way better