Skip to content

Commit

Permalink
fix: validate staking amount while filling the form
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwbabylonlab committed Aug 13, 2024
1 parent 3b571bb commit e125d02
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-staking",
"version": "0.2.32",
"version": "0.2.33",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
33 changes: 23 additions & 10 deletions src/app/components/Staking/Form/StakingAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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 @@ -51,17 +53,10 @@ export const StakingAmount: React.FC<StakingAmountProps> = ({
}
};

const handleBlur = (_e: FocusEvent<HTMLInputElement>) => {
if (!btcWalletBalanceSat) return;
setTouched(true);

if (value === "") {
onStakingAmountSatChange(0);
setError(generalErrorMessage);
return;
}

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

// Run all validations
Expand Down Expand Up @@ -103,6 +98,24 @@ export const StakingAmount: React.FC<StakingAmountProps> = ({
onStakingAmountSatChange(satoshis);
setValue(maxDecimals(satoshiToBtc(satoshis), 8).toString());
}
}, [
btcWalletBalanceSat,
minStakingAmountSat,
maxStakingAmountSat,
value,
onStakingAmountSatChange,
coinName,
blured,
]);

const handleBlur = (_e: FocusEvent<HTMLInputElement>) => {
if (value === "") {
onStakingAmountSatChange(0);
setError(generalErrorMessage);
return;
}
setTouched(true);
setBlured(!blured);
};

const minStakeAmount = maxDecimals(satoshiToBtc(minStakingAmountSat), 8);
Expand Down
7 changes: 6 additions & 1 deletion src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,15 @@ export const Staking: React.FC<StakingProps> = ({
);
return stakingFeeSat;
} catch (error: Error | any) {
let errorMsg = error?.message;
if (error?.message.includes("Insufficient funds")) {
errorMsg =
"Not enough balance to cover staking amount and fees, please lower the staking amount";
}
// fees + staking amount can be more than the balance
showError({
error: {
message: error.message,
message: errorMsg,
errorState: ErrorState.STAKING,
errorTime: new Date(),
},
Expand Down

0 comments on commit e125d02

Please sign in to comment.