Skip to content

Commit

Permalink
fix: handle the empty value check before parsing it into a num
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Aug 14, 2024
1 parent e125d02 commit 608bb22
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/app/components/Staking/Form/StakingAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const StakingAmount: React.FC<StakingAmountProps> = ({
}) => {
const [value, setValue] = useState("");
const [error, setError] = useState("");
const [blured, setBlured] = useState(false);

// Track if the input field has been interacted with
const [touched, setTouched] = useState(false);
Expand Down Expand Up @@ -54,9 +53,8 @@ export const StakingAmount: React.FC<StakingAmountProps> = ({
};

useEffect(() => {
if (btcWalletBalanceSat === undefined) return;
if (btcWalletBalanceSat === undefined || value === "") return;
const numValue = parseFloat(value);
if (!numValue) return;
const satoshis = btcToSatoshi(numValue);

// Run all validations
Expand Down Expand Up @@ -105,7 +103,6 @@ export const StakingAmount: React.FC<StakingAmountProps> = ({
value,
onStakingAmountSatChange,
coinName,
blured,
]);

const handleBlur = (_e: FocusEvent<HTMLInputElement>) => {
Expand All @@ -115,7 +112,6 @@ export const StakingAmount: React.FC<StakingAmountProps> = ({
return;
}
setTouched(true);
setBlured(!blured);
};

const minStakeAmount = maxDecimals(satoshiToBtc(minStakingAmountSat), 8);
Expand Down
6 changes: 5 additions & 1 deletion src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,11 @@ export const Staking: React.FC<StakingProps> = ({
return stakingFeeSat;
} catch (error: Error | any) {
let errorMsg = error?.message;
if (error?.message.includes("Insufficient funds")) {
// Turn the error message into a user-friendly message
// The btc-staking-ts lib will be improved to return propert error types
// in the future. For now, we need to handle the errors manually by
// matching the error message.
if (errorMsg.includes("Insufficient funds")) {
errorMsg =
"Not enough balance to cover staking amount and fees, please lower the staking amount";
}
Expand Down

0 comments on commit 608bb22

Please sign in to comment.