From 9a58882cac08d302b77f0124a21608905d3dad79 Mon Sep 17 00:00:00 2001 From: JCNoguera Date: Wed, 29 Nov 2023 13:48:58 +0100 Subject: [PATCH] cleanup code and use correct formulas --- .../ManaCalculator/components/RoleSection.tsx | 10 ++-- .../components/ValidatorCard.tsx | 12 +++-- .../components/ValidatorSettings.tsx | 9 +++- .../ManaCalculator/hooks/useManaState.ts | 54 +++++++++---------- src/components/ManaCalculator/utils.ts | 21 ++++++++ 5 files changed, 69 insertions(+), 37 deletions(-) diff --git a/src/components/ManaCalculator/components/RoleSection.tsx b/src/components/ManaCalculator/components/RoleSection.tsx index 45e554e6eca..9ace248c180 100644 --- a/src/components/ManaCalculator/components/RoleSection.tsx +++ b/src/components/ManaCalculator/components/RoleSection.tsx @@ -8,8 +8,12 @@ import { DelegatorSettings } from './DelegatorSettings'; import { ManaCalculatorInput } from '.'; export function RoleSection() { - const { state, handleUserChange, handleOwnHoldChange, maxTotalSupply } = - useManaState(); + const { + state, + handleUserChange, + handleOwnHoldChange, + maxAvailableOwnedAmount, + } = useManaState(); return (

Role configuration

@@ -31,7 +35,7 @@ export function RoleSection() {
Validator {id + 1}
@@ -27,7 +33,7 @@ export function ValidatorCard({ handleStakeChange(value, id)} /> @@ -36,7 +42,7 @@ export function ValidatorCard({ handleDelegatedStakeChange(value, id)} /> diff --git a/src/components/ManaCalculator/components/ValidatorSettings.tsx b/src/components/ManaCalculator/components/ValidatorSettings.tsx index 5efe5b2979d..f39a62f4ac6 100644 --- a/src/components/ManaCalculator/components/ValidatorSettings.tsx +++ b/src/components/ManaCalculator/components/ValidatorSettings.tsx @@ -11,8 +11,13 @@ export function ValidatorSettings() { handleOwnFCChange, handleOwnStakeChange, handleAttractedNewDelegatedStakeChange, - maxTotalSupply, + maxAvailableSupply, } = useManaState(); + + const maxAttractedNewDelegatedStake = + fromMicro(maxAvailableSupply) + + fromMicro(state.validator.attractedNewDelegatedStake); + return (
a + b.lockedStake, 0) + - state.validators.reduce((a, b) => a + b.delegatedStake, 0); + const networkSupply = getNetworkSupply(state.network); + + const userOwnedTokens = state.heldTokens; + + const totalValidatorsLockedStake = state.validators.reduce( + (a, b) => a + b.lockedStake, + 0, + ); + const totalValidatorsDelegatedStake = state.validators.reduce( + (a, b) => a + b.delegatedStake, + 0, + ); + + const totalValidatorsStake = + totalValidatorsLockedStake + totalValidatorsDelegatedStake; + + const totalStake = userOwnedTokens + totalValidatorsStake; + + const maxAvailableOwnedAmount = networkSupply - totalValidatorsStake; + + const maxAvailableSupply = Math.max(networkSupply - totalStake, 0); return { state: { @@ -200,7 +218,8 @@ export function useGivenManaState( generationPerSlot, stakedOrDelegatedTokens, } as ManaState, - maxTotalSupply, + maxAvailableOwnedAmount, + maxAvailableSupply, handleDelete, handleStakeChange, handleAddValidator, @@ -221,29 +240,6 @@ export function useGivenManaState( }; } -function getValidInputValue( - num: string, - transformNumber?: (number) => number, -): number { - let value = inputValuetoNumber(num); - - if (transformNumber) { - value = transformNumber(value); - } - - const isInvalid = isNaN(value); - - if (isInvalid) { - throw new Error('Invalid number'); - } - - return value; -} - -function inputValuetoNumber(value: string) { - return Number(Number(value).toString()); -} - export function getDefaultParameters( network: NetworkType, ): ManaCalculatorProps { diff --git a/src/components/ManaCalculator/utils.ts b/src/components/ManaCalculator/utils.ts index 5d1f2f870f8..326ac8f9560 100644 --- a/src/components/ManaCalculator/utils.ts +++ b/src/components/ManaCalculator/utils.ts @@ -89,3 +89,24 @@ export function getStakedOrDelegated(userType: UserType) { export const roundMax = function (num: number, places: number) { return +(Math.round(Number(num + 'e+' + places)) + 'e-' + places); }; + + +export function getValidInputValue( + num: string, + transformNumber?: (number: number) => number, +): number { + let value = Number(Number(num).toString()); + + if (transformNumber) { + value = transformNumber(value); + } + + const isInvalid = isNaN(value); + + if (isInvalid) { + throw new Error('Invalid number'); + } + + return value; +} +