From c5ddd17364103c8c6ad94301130c6e1fb6818e42 Mon Sep 17 00:00:00 2001
From: JCNoguera <88061365+VmMad@users.noreply.github.com>
Date: Fri, 8 Dec 2023 14:25:49 +0100
Subject: [PATCH] Improve input validation from ManaCalculator (#1337)
---
.../components/DelegatorSettings.tsx | 11 +-
.../components/OtherParametersSection.tsx | 17 +--
.../ManaCalculator/components/RoleSection.tsx | 15 ++-
.../components/ValidatorCard.tsx | 73 ++++++++-----
.../components/ValidatorSettings.tsx | 50 +++++----
.../ManaCalculator/hooks/useManaState.ts | 102 +++++++++++++-----
src/components/ManaCalculator/styles.css | 41 +++++++
src/components/ManaCalculator/utils.ts | 19 ++++
.../ValidatedInput/ValidatedInput.tsx | 81 ++++++++++++++
9 files changed, 319 insertions(+), 90 deletions(-)
create mode 100644 src/components/ValidatedInput/ValidatedInput.tsx
diff --git a/src/components/ManaCalculator/components/DelegatorSettings.tsx b/src/components/ManaCalculator/components/DelegatorSettings.tsx
index 37c8f0f3f10..d4e9284f516 100644
--- a/src/components/ManaCalculator/components/DelegatorSettings.tsx
+++ b/src/components/ManaCalculator/components/DelegatorSettings.tsx
@@ -1,8 +1,9 @@
import React from 'react';
import { Details } from '@docusaurus/theme-common/Details';
import { useManaState } from '../hooks';
-import { fromMicro, toMicro } from '../utils';
+import { fromMicro } from '../utils';
import Select from 'react-select';
+import { ValidatedInput } from '../../ValidatedInput/ValidatedInput';
export function DelegatorSettings() {
const { state, handleOwnStakeChange, handleValidatorChange } = useManaState();
@@ -30,11 +31,13 @@ export function DelegatorSettings() {
- handleOwnStakeChange(toMicro(Number(e.target.value)))}
- >
+ min={0}
+ max={fromMicro(state.heldTokens)}
+ onChange={handleOwnStakeChange}
+ />
);
}
diff --git a/src/components/ManaCalculator/components/OtherParametersSection.tsx b/src/components/ManaCalculator/components/OtherParametersSection.tsx
index 197e68d5ddd..aff21606c24 100644
--- a/src/components/ManaCalculator/components/OtherParametersSection.tsx
+++ b/src/components/ManaCalculator/components/OtherParametersSection.tsx
@@ -4,6 +4,7 @@ import Select from 'react-select';
import { UserType } from '../enums';
import { CongestionType } from '../enums';
import { AdvancedSettingsValidator } from './AdvancedSettingsValidator';
+import { ValidatedInput } from '../../ValidatedInput/ValidatedInput';
export function OtherParametersSection() {
const {
@@ -34,18 +35,22 @@ export function OtherParametersSection() {
/>
- handleInitialEpochChange(Number(e.target.value))}
- >
+ onChange={handleInitialEpochChange}
+ />
- handleFinalEpochChange(Number(e.target.value))}
- >
+ onChange={handleFinalEpochChange}
+ />
{state.userType === UserType.HOLDER ? (
<>>
) : (
diff --git a/src/components/ManaCalculator/components/RoleSection.tsx b/src/components/ManaCalculator/components/RoleSection.tsx
index 6897fdcb869..7e0b2d27b65 100644
--- a/src/components/ManaCalculator/components/RoleSection.tsx
+++ b/src/components/ManaCalculator/components/RoleSection.tsx
@@ -2,12 +2,15 @@ import React from 'react';
import { useManaState } from '../hooks';
import Select from 'react-select';
import { UserType } from '../enums';
-import { fromMicro, toMicro } from '../utils';
+import { fromMicro } from '../utils';
import { ValidatorSettings } from './ValidatorSettings';
import { DelegatorSettings } from './DelegatorSettings';
+import { ValidatedInput } from '../../ValidatedInput/ValidatedInput';
export function RoleSection() {
- const { state, handleUserChange, handleOwnHoldChange } = useManaState();
+ const { state, handleUserChange, handleOwnHoldChange, maxAvailableSupply } =
+ useManaState();
+
return (
Role configuration
@@ -27,11 +30,13 @@ export function RoleSection() {
/>
-
handleOwnHoldChange(toMicro(Number(e.target.value)))}
- >
+ onChange={handleOwnHoldChange}
+ />
{state.userType === UserType.VALIDATOR ? (
diff --git a/src/components/ManaCalculator/components/ValidatorCard.tsx b/src/components/ManaCalculator/components/ValidatorCard.tsx
index 517a5293a46..139ee1b5695 100644
--- a/src/components/ManaCalculator/components/ValidatorCard.tsx
+++ b/src/components/ManaCalculator/components/ValidatorCard.tsx
@@ -1,7 +1,8 @@
import React from 'react';
import { useManaState } from '../hooks';
import { ValidatorProps } from '../types';
-import { fromMicro, toMicro } from '../utils';
+import { fromMicro } from '../utils';
+import { ValidatedInput } from '../../ValidatedInput/ValidatedInput';
export function ValidatorCard({
validator,
@@ -16,36 +17,54 @@ export function ValidatorCard({
handleDelegatedStakeChange,
handlePFChange,
handleFCChange,
+ maxAvailableSupply,
} = useManaState();
+
+ const maxValidatorLockedStake =
+ fromMicro(maxAvailableSupply) + fromMicro(validator.lockedStake);
+
+ const maxValidatorDelegatedStake =
+ fromMicro(maxAvailableSupply) + fromMicro(validator.delegatedStake);
+
return (
<>
Validator {id + 1}
-
handleStakeChange(toMicro(Number(e.target.value)), id)}
- >
-
- handleDelegatedStakeChange(toMicro(Number(e.target.value)), id)
- }
- >
-
handlePFChange(Number(e.target.value), id)}
- >
-
handleFCChange(Number(e.target.value), id)}
- >
+
+ handleStakeChange(value, id)}
+ />
+
+
+ handleDelegatedStakeChange(value, id)}
+ />
+
+
+ handlePFChange(value, id)}
+ />
+
+
+ handleFCChange(value, id)}
+ />
+