-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: tx calc unhandled errors, closes #4941
- Loading branch information
1 parent
5b37691
commit 7bdabba
Showing
15 changed files
with
262 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 5 additions & 36 deletions
41
src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-fiat.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.