Skip to content

Commit

Permalink
Derive values on input changes in Mana Calculator (#1354)
Browse files Browse the repository at this point in the history
  • Loading branch information
VmMad authored Dec 7, 2023
1 parent b074433 commit aae6af2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export function ValidatorSettings() {
0,
)}
onChange={(e) =>
handleAttractedNewDelegatedStakeChange(Number(e.target.value))
handleAttractedNewDelegatedStakeChange(
toMicro(Number(e.target.value)),
)
}
></input>
</Details>
Expand Down
43 changes: 37 additions & 6 deletions src/components/ManaCalculator/hooks/useManaState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ export function useGivenManaState(
}

function handleStakeChange(value: number, id: number) {
setState({
const newStateWithValidators = {
...state,
validators: state.validators.map((validator, i) => {
return {
...validator,
lockedStake: i === id ? value : validator.lockedStake,
};
}),
};
setState({
...newStateWithValidators,
...getDerivedRoleValues(newStateWithValidators, state.stakedTokens),
});
}

Expand Down Expand Up @@ -87,10 +91,11 @@ export function useGivenManaState(
});
}

function handleOwnStakeChange(value: number) {
function handleOwnStakeChange(stakedTokens: number) {
setState({
...state,
[getStakedOrDelegated(state.userType)]: value,
[getStakedOrDelegated(state.userType)]: stakedTokens,
...getDerivedRoleValues(state, stakedTokens),
});
}

Expand Down Expand Up @@ -149,10 +154,12 @@ export function useGivenManaState(
});
}

function handleUserChange(value: UserType) {
function handleUserChange(newUserType: UserType) {
setState({
...state,
userType: value,
userType: newUserType,
[getStakedOrDelegated(newUserType)]: state.heldTokens,
...getDerivedRoleValues(state, state.heldTokens, newUserType),
});
}

Expand All @@ -162,7 +169,13 @@ export function useGivenManaState(
}

function handleOwnHoldChange(value: number) {
setState({ ...state, heldTokens: value });
const newState = {
...state,
heldTokens: value,
[getStakedOrDelegated(state.userType)]: value,
};

setState({ ...newState, ...getDerivedRoleValues(newState, value) });
}

const congestionAmount = getNetworkCongestion(
Expand Down Expand Up @@ -242,6 +255,24 @@ export function getDefaultParameters(
} as ManaCalculatorProps;
}

function getDerivedRoleValues(
state: ManaCalculatorProps,
stakedTokens: number,
userType: UserType = state.userType,
) {
return userType === UserType.VALIDATOR
? {
validator: {
...state.validator,
attractedNewDelegatedStake:
(stakedTokens *
state.validators.reduce((a, b) => a + b.delegatedStake, 0)) /
state.validators.reduce((a, b) => a + b.lockedStake, 0),
},
}
: {};
}

export function getValidators(network: NetworkType): ValidatorProps[] {
const supply = getNetworkSupply(network);

Expand Down

0 comments on commit aae6af2

Please sign in to comment.