Skip to content

Commit

Permalink
math check
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviasaa committed Nov 22, 2023
1 parent 5aecbbc commit 0b4faa0
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 40 deletions.
30 changes: 30 additions & 0 deletions src/components/ManaCalculator/components/DelegatorSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { Details } from '@docusaurus/theme-common/Details';
import { useManaState } from '../hooks';
import { fromMicro, toMicro, roundMax } from '../utils';

Check warning on line 4 in src/components/ManaCalculator/components/DelegatorSettings.tsx

View workflow job for this annotation

GitHub Actions / consistency

'roundMax' is defined but never used

export function DelegatorSettings() {
const {
state,
handleOwnPFChange,

Check warning on line 9 in src/components/ManaCalculator/components/DelegatorSettings.tsx

View workflow job for this annotation

GitHub Actions / consistency

'handleOwnPFChange' is assigned a value but never used
handleOwnFCChange,

Check warning on line 10 in src/components/ManaCalculator/components/DelegatorSettings.tsx

View workflow job for this annotation

GitHub Actions / consistency

'handleOwnFCChange' is assigned a value but never used
handleOwnStakeChange,
handleAttractedNewDelegatedStakeChange,

Check warning on line 12 in src/components/ManaCalculator/components/DelegatorSettings.tsx

View workflow job for this annotation

GitHub Actions / consistency

'handleAttractedNewDelegatedStakeChange' is assigned a value but never used
} = useManaState();
return (
<Details
summary='Delegator Settings'
className='mana_calculator__card mana_calculator_inner__card'
>
<label className='inlined-long-label'>Delegated amount ({state.network})</label>
<input
className='mana_calculator__compact inlined'
value={fromMicro(state.stakedOrDelegatedTokens)}
onChange={(e) =>
handleOwnStakeChange(toMicro(Number(e.target.value)))
}
></input>
<br />
</Details>
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { useManaState } from '../hooks/useManaState';
import Select from 'react-select';
import { UserType } from '../enums';
import { CongestionType } from '../enums';
import { AdvancedSettingsValidator } from './AdvancedSettingsValidator';

Expand Down Expand Up @@ -45,7 +46,14 @@ export function OtherParametersSection() {
value={state.finalEpoch}
onChange={(e) => handleFinalEpochChange(Number(e.target.value))}
></input>
<AdvancedSettingsValidator />
{state.userType === UserType.HOLDER ? (
<>
</>
) : (
<>
< AdvancedSettingsValidator />
</>
)}
</div>
);
}
61 changes: 53 additions & 8 deletions src/components/ManaCalculator/components/OutputForm.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import React from 'react';
import { useManaState, useResults } from '../hooks';
import { fromMicro, roundMax } from '../utils';
import { UserType } from '../enums';
import humanizeDuration from 'humanize-duration';

export function OutputForm() {
const { state } = useManaState();
const results = useResults(state);
const passiveRewards = roundMax(fromMicro(results.passiveRewards), 6);
const manaGenerated = roundMax(fromMicro(results.generatedRewards), 6);
const totalBPS = roundMax(results.totalBPS, 2);
const blockAllowance = roundMax(results.blockAllowance, 2);
const totalBPS = roundMax(results.totalBPS, 20);

Check warning on line 13 in src/components/ManaCalculator/components/OutputForm.tsx

View workflow job for this annotation

GitHub Actions / consistency

'totalBPS' is assigned a value but never used
const humanizer = humanizeDuration.humanizer({
units: ['y', 'mo', 'w', 'd', 'h', 'm', 's', 'ms'],
maxDecimalPoints: 3,
Expand All @@ -18,27 +20,70 @@ export function OutputForm() {

return (
<div>
{ state.userType === UserType.VALIDATOR ? (
<>
<div className='col'>
Mana generation (by holding):{' '}
<b className='output-result'>{passiveRewards}</b>
Generation (by just holding) in the input period:{' '}
<b className='output-result'>{passiveRewards} Mana</b>
</div>
<div className='col'>
Mana rewards: <b className='output-result'>{manaGenerated}</b>
Mana rewards from staking in the input period: <b className='output-result'>{manaGenerated} Mana</b>
</div>
<div className='col'>
Total BPS granted: <b className='output-result'>{totalBPS}</b>
Total Block Allowance granted after the input period: <b className='output-result'>{roundMax(blockAllowance,3)} standard blocks</b>
</div>
<div className='col'>
Time it takes to accumulate enough mana for a standard block...
</div>
<div className='col'>
...as a holder (owning <b className='output-result'>{fromMicro(state.heldTokens)}</b> tokens and not staking any of them):{' '}
<b className='output-result'>{passiveMsToTransaction}</b>
</div>
<div className='col'>
...as a validator (owning <b className='output-result'>{fromMicro(state.heldTokens)}</b> tokens and staking <b className='output-result'>{fromMicro(state.stakedOrDelegatedTokens)}</b> of them):{' '}
<b className='output-result'>{msToTransaction}</b>
</div>
</>
) :
state.userType === UserType.DELEGATOR ? (
<>
<div className='col'>
Generation (by just holding) in the input period:{' '}
<b className='output-result'>{passiveRewards} Mana</b>
</div>
<div className='col'>
Mana rewards from delegating in the input period: <b className='output-result'>{manaGenerated} Mana</b>
</div>
<div className='col'>
Total Block Allowance granted after the input period: <b className='output-result'>{roundMax(blockAllowance,3)} standard blocks</b>
</div>
<div className='col'>
Time it takes to accumulate enough mana for a standard block...
</div>
<div className='col'>
Time it takes to accumulate enough mana for a standard transaction...
...as a holder (owning <b className='output-result'>{fromMicro(state.heldTokens)}</b> tokens and not delegating any of them):{' '}
<b className='output-result'>{passiveMsToTransaction}</b>
</div>
<div className='col'>
...as a delegator/validator:{' '}
...as a delegator (owning <b className='output-result'>{fromMicro(state.heldTokens)}</b> tokens and delegating <b className='output-result'>{fromMicro(state.stakedOrDelegatedTokens)}</b> of them):{' '}
<b className='output-result'>{msToTransaction}</b>
</div>
</>
): (
<>
<div className='col'>
Generation in the input period:{' '}
<b className='output-result'>{passiveRewards} Mana</b>
</div>
<div className='col'>
Total Block Allowance granted after the input period: <b className='output-result'>{roundMax(blockAllowance,3)} standard blocks</b>
</div>
<div className='col'>
...as a holder:{' '}
Time it takes to accumulate enough mana for a standard block:{' '}
<b className='output-result'>{passiveMsToTransaction}</b>
</div>
</>
)}
</div>
);
}
26 changes: 12 additions & 14 deletions src/components/ManaCalculator/components/RoleSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Select from 'react-select';
import { UserType } from '../enums';
import { fromMicro, toMicro } from '../utils';
import { ValidatorSettings } from './ValidatorSettings';
import { DelegatorSettings } from './DelegatorSettings';

Check warning on line 7 in src/components/ManaCalculator/components/RoleSection.tsx

View workflow job for this annotation

GitHub Actions / consistency

'DelegatorSettings' is defined but never used

export function RoleSection() {
const {
Expand Down Expand Up @@ -31,30 +32,24 @@ export function RoleSection() {
options={[
{ value: UserType.DELEGATOR, label: `Delegator` },
{ value: UserType.VALIDATOR, label: `Validator` },
{ value: UserType.HOLDER, label: `Holder` },
]}
/>
<br />
<label className='inlined-label'>Held amount ({state.network})</label>
<label className='inlined-label'>Owned amount ({state.network})</label>
<input
className='mana_calculator__compact inlined'
value={fromMicro(state.heldTokens)}
onChange={(e) => handleOwnHoldChange(toMicro(Number(e.target.value)))}
></input>
<br />
{state.userType === UserType.VALIDATOR ? (
{
state.userType === UserType.VALIDATOR ? (
<>
<label className='inlined-label'>Stake ({state.network})</label>
<input
className='mana_calculator__compact inlined'
value={fromMicro(state.stakedOrDelegatedTokens)}
onChange={(e) =>
handleOwnStakeChange(toMicro(Number(e.target.value)))
}
></input>
<br />
<ValidatorSettings />
<ValidatorSettings />
</>
) : (
) :
state.userType === UserType.DELEGATOR ? (
<>
<label className='inlined-label'>Delegating to</label>
<Select
Expand All @@ -78,7 +73,10 @@ export function RoleSection() {
}
></input>
</>
)}
): (
<>
</>
)}
</div>
);
}
23 changes: 16 additions & 7 deletions src/components/ManaCalculator/components/ValidatorSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import React from 'react';
import { Details } from '@docusaurus/theme-common/Details';
import { useManaState } from '../hooks';
import { fromMicro, toMicro, roundMax } from '../utils';

export function ValidatorSettings() {
const {
state: {
validator: { performanceFactor, fixedCost, attractedNewDelegatedStake },
},
state,
handleOwnPFChange,
handleOwnFCChange,
handleOwnStakeChange,
handleAttractedNewDelegatedStakeChange,
} = useManaState();
return (
<Details
summary='Validator Settings'
className='mana_calculator__card mana_calculator_inner__card'
>
<label className='inlined-long-label'>Staked amount ({state.network})</label>
<input
className='mana_calculator__compact inlined'
value={fromMicro(state.stakedOrDelegatedTokens)}
onChange={(e) =>
handleOwnStakeChange(toMicro(Number(e.target.value)))
}
></input>
<br />
<label className='inlined-long-label'>Performance factor</label>
<input
className='mana_calculator__compact input--vertical-spaced'
type='number'
step='0.01'
value={performanceFactor}
value={state.validator.performanceFactor}
onChange={(e) => handleOwnPFChange(Number(e.target.value))}
></input>
<br />
Expand All @@ -30,15 +39,15 @@ export function ValidatorSettings() {
className='mana_calculator__compact input--vertical-spaced'
type='number'
step='0.01'
value={fixedCost}
value={state.validator.fixedCost}
onChange={(e) => handleOwnFCChange(Number(e.target.value))}
></input>
<label className='inlined-long-label'>
Attracted new delegated stake
Attracted new delegated stake ({state.network})
</label>
<input
className='mana_calculator__compact input--vertical-spaced'
value={attractedNewDelegatedStake}
value={roundMax(fromMicro(state.validator.attractedNewDelegatedStake),0)}
onChange={(e) =>
handleAttractedNewDelegatedStakeChange(Number(e.target.value))
}
Expand Down
1 change: 1 addition & 0 deletions src/components/ManaCalculator/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './ManaCalculator';
export * from './OtherParametersSection';
export * from './ValidatorCard';
export * from './ValidatorSettings';
export * from './DelegatorSettings';
export * from './RoleSection';
export * from './OutputForm';
export * from './NetworkSection';
Expand Down
2 changes: 1 addition & 1 deletion src/components/ManaCalculator/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CongestionType } from './enums/parameters.enum';
// GENERIC

export const INITIAL_EPOCH = 1;
export const FINAL_EPOCH = 1000;
export const FINAL_EPOCH = 10;
export const SLOT_DURATION = 10;
export const SLOTS_IN_EPOCH = 8192;
export const EPOCH_DURATION = SLOTS_IN_EPOCH * SLOT_DURATION;
Expand Down
1 change: 1 addition & 0 deletions src/components/ManaCalculator/enums/parameters.enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum UserType {
DELEGATOR,
VALIDATOR,
HOLDER,
}

export enum NetworkType {
Expand Down
4 changes: 2 additions & 2 deletions src/components/ManaCalculator/hooks/useManaState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ export function getDefaultParameters(
shareOfYourStakeLocked: 1.0,
attractedNewDelegatedStake:
(finalNetworkParams.stakedTokens *
validators.reduce((a, b) => a + b.lockedStake, 0)) /
validators.reduce((a, b) => a + b.delegatedStake, 0),
validators.reduce((a, b) => a + b.delegatedStake, 0)) /
validators.reduce((a, b) => a + b.lockedStake, 0),
attractedDelegatedStakeFromOtherPools: 0,
},
network,
Expand Down
42 changes: 36 additions & 6 deletions src/components/ManaCalculator/hooks/useResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,51 @@ export function useResults(state: ManaState) {
state.generationPerSlot,
);

const yourBlocksPerEpoch = passiveRewards / state.congestionAmount;
const yourAdditionalBlocksPerEpoch =
const passiveRewardsInTheFirstEpoch = calculatePassiveRewards(
state.heldTokens,
state.initialEpoch,
state.initialEpoch + 1,
state.generationPerSlot,
);


const generatedRewardsInTheFirstEpoch = calculateManaRewards(
state.stakedOrDelegatedTokens,
state.delegator.validator,
validatorParameters,
state.validators,
state.initialEpoch,
state.initialEpoch + 1,
state.userType,
state.network,
state.generationPerSlot,
);

const yourPassiveBlocksInPeriod = passiveRewards / state.congestionAmount;
const yourAdditionalBlocksInPeriod =
generatedRewards / state.congestionAmount;

const yourTPS = yourBlocksPerEpoch / EPOCH_DURATION;
const yourAdditionalTPS = yourAdditionalBlocksPerEpoch / EPOCH_DURATION;
const totalBPS = yourTPS + yourAdditionalTPS;
const yourPassiveBlocksInTheFirstEpoch = passiveRewardsInTheFirstEpoch / state.congestionAmount;
const yourAdditionalBlocksInTheFirstEpoch =
generatedRewardsInTheFirstEpoch / state.congestionAmount;

const yourTotalBlocksInPeriod = yourPassiveBlocksInPeriod + yourAdditionalBlocksInPeriod

const yourTotalBlocksInTheFirstEpoch = yourPassiveBlocksInTheFirstEpoch + yourAdditionalBlocksInTheFirstEpoch

const blockAllowance = yourTotalBlocksInPeriod;

const yourBPSasHolder = yourPassiveBlocksInTheFirstEpoch / EPOCH_DURATION;
const totalBPS = yourTotalBlocksInTheFirstEpoch / EPOCH_DURATION;

const msToTransaction = (1 / totalBPS) * 1_000;
const passiveMsToTransaction = (1 / yourTPS) * 1_000;
const passiveMsToTransaction = (1 / yourBPSasHolder) * 1_000;

return {
generatedRewards,
passiveRewards,
totalBPS,
blockAllowance,
msToTransaction,
passiveMsToTransaction,
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/ManaCalculator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SHIMMER_GENERATION_PER_SLOT,
SHIMMER_SUPPLY,
SHIMMER_THROUGHPUT,
SLOT_DURATION,
SLOTS_IN_EPOCH,
} from './constants';
import { CongestionType, NetworkType, UserType } from './enums';
Expand Down Expand Up @@ -51,7 +52,7 @@ export function getNetworkCongestion(
const generation = getNetworkGenerationPerSlot(network);
const throughput = getNetworkThroughput(network);

return (supply * generation) / (10 * throughput);
return (supply * generation) / (SLOT_DURATION * throughput);
} else {
if (network == NetworkType.IOTA) {
return IOTA_CONGESTION[congestionType];
Expand Down

0 comments on commit 0b4faa0

Please sign in to comment.