Skip to content

Commit

Permalink
cleanup code and use correct formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
VmMad committed Nov 29, 2023
1 parent f1a56cf commit 9a58882
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 37 deletions.
10 changes: 7 additions & 3 deletions src/components/ManaCalculator/components/RoleSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className='mana_calculator__card'>
<h4>Role configuration</h4>
Expand All @@ -31,7 +35,7 @@ export function RoleSection() {
<label className='inlined-label'>Owned amount ({state.network})</label>
<ManaCalculatorInput
min={0}
max={maxTotalSupply}
max={fromMicro(maxAvailableOwnedAmount)}
className='mana_calculator__compact inlined'
value={fromMicro(state.heldTokens)}
onChange={handleOwnHoldChange}
Expand Down
12 changes: 9 additions & 3 deletions src/components/ManaCalculator/components/ValidatorCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ export function ValidatorCard({
handleDelegatedStakeChange,
handlePFChange,
handleFCChange,
maxTotalSupply,
maxAvailableSupply,
} = useManaState();

const maxValidatorLockedStake =
fromMicro(maxAvailableSupply) + fromMicro(validator.lockedStake);

const maxValidatorDelegatedStake =
fromMicro(maxAvailableSupply) + fromMicro(validator.delegatedStake);

return (
<>
<div className='col col--2'>Validator {id + 1}</div>
<div className='validator_card col col--2 align-right horizontal-spaced'>
<ManaCalculatorInput
className='w-full'
min={0}
max={maxTotalSupply}
max={maxValidatorLockedStake}
value={fromMicro(validator.lockedStake)}
onChange={(value: string) => handleStakeChange(value, id)}
/>
Expand All @@ -36,7 +42,7 @@ export function ValidatorCard({
<ManaCalculatorInput
className='w-full'
min={0}
max={maxTotalSupply}
max={maxValidatorDelegatedStake}
value={fromMicro(validator.delegatedStake)}
onChange={(value: string) => handleDelegatedStakeChange(value, id)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ export function ValidatorSettings() {
handleOwnFCChange,
handleOwnStakeChange,
handleAttractedNewDelegatedStakeChange,
maxTotalSupply,
maxAvailableSupply,
} = useManaState();

const maxAttractedNewDelegatedStake =
fromMicro(maxAvailableSupply) +
fromMicro(state.validator.attractedNewDelegatedStake);

return (
<Details
summary='Validator Settings'
Expand Down Expand Up @@ -52,7 +57,7 @@ export function ValidatorSettings() {
<ManaCalculatorInput
className='mana_calculator__compact input--vertical-spaced'
min={0}
max={maxTotalSupply}
max={maxAttractedNewDelegatedStake}
value={roundMax(
fromMicro(state.validator.attractedNewDelegatedStake),
0,
Expand Down
54 changes: 25 additions & 29 deletions src/components/ManaCalculator/hooks/useManaState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import {
import { CongestionType, NetworkType, UserType } from '../enums';
import { ManaCalculatorProps, ManaState, ValidatorProps } from '../types';
import {
fromMicro,

Check warning on line 15 in src/components/ManaCalculator/hooks/useManaState.ts

View workflow job for this annotation

GitHub Actions / consistency

'fromMicro' is defined but never used
getNetworkCongestion,
getNetworkGenerationPerSlot,
getNetworkSupply,
getStakedOrDelegated,
getValidInputValue,
toMicro,
} from '../utils';

Expand Down Expand Up @@ -187,11 +189,27 @@ export function useGivenManaState(
const generationPerSlot = getNetworkGenerationPerSlot(state.network);
const stakedOrDelegatedTokens = state[getStakedOrDelegated(state.userType)];

const userOwned = state.heldTokens + stakedOrDelegatedTokens;
const maxTotalSupply =
userOwned +
state.validators.reduce((a, b) => 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: {
Expand All @@ -200,7 +218,8 @@ export function useGivenManaState(
generationPerSlot,
stakedOrDelegatedTokens,
} as ManaState,
maxTotalSupply,
maxAvailableOwnedAmount,
maxAvailableSupply,
handleDelete,
handleStakeChange,
handleAddValidator,
Expand All @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions src/components/ManaCalculator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 9a58882

Please sign in to comment.