From f8dc3d36415e3ec99184f62cad47d9ca6f43a455 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 26 Nov 2020 15:51:59 +0100 Subject: [PATCH] Introduce useInflation hook (#4084) * Introduce useInflation hook * linting --- packages/page-staking/src/index.tsx | 1 + packages/page-staking/src/useSortedTargets.ts | 13 ++++ packages/react-hooks/src/index.ts | 1 + packages/react-hooks/src/useInflation.ts | 62 +++++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 packages/react-hooks/src/useInflation.ts diff --git a/packages/page-staking/src/index.tsx b/packages/page-staking/src/index.tsx index 36a16c1946..e55a4c7467 100644 --- a/packages/page-staking/src/index.tsx +++ b/packages/page-staking/src/index.tsx @@ -42,6 +42,7 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement(api.derive.staking.overview); const isInElection = useCall(api.query.staking?.eraElectionStatus, undefined, transformElection); diff --git a/packages/page-staking/src/useSortedTargets.ts b/packages/page-staking/src/useSortedTargets.ts index f20a769d59..87da3041b4 100644 --- a/packages/page-staking/src/useSortedTargets.ts +++ b/packages/page-staking/src/useSortedTargets.ts @@ -32,7 +32,20 @@ function isWaitingDerive (derive: DeriveStakingElected | DeriveStakingWaiting): } function sortValidators (list: ValidatorInfo[]): ValidatorInfo[] { + const existing: string[] = []; + return list + .filter((a): boolean => { + const s = a.accountId.toString(); + + if (!existing.includes(s)) { + existing.push(s); + + return true; + } + + return false; + }) .filter((a) => a.bondTotal.gtn(0)) .sort((a, b) => b.commissionPer - a.commissionPer) .map(mapIndex('rankComm')) diff --git a/packages/react-hooks/src/index.ts b/packages/react-hooks/src/index.ts index c7d7511745..ca708fe2a2 100644 --- a/packages/react-hooks/src/index.ts +++ b/packages/react-hooks/src/index.ts @@ -15,6 +15,7 @@ export { useFormField } from './useFormField'; export { useDebounce } from './useDebounce'; export { useFavorites } from './useFavorites'; export { useIncrement } from './useIncrement'; +export { useInflation } from './useInflation'; export { useIpfs } from './useIpfs'; export { useIsMountedRef } from './useIsMountedRef'; export { useLoadingDelay } from './useLoadingDelay'; diff --git a/packages/react-hooks/src/useInflation.ts b/packages/react-hooks/src/useInflation.ts new file mode 100644 index 0000000000..7096ab07b6 --- /dev/null +++ b/packages/react-hooks/src/useInflation.ts @@ -0,0 +1,62 @@ +// Copyright 2017-2020 @polkadot/react-hooks authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import BN from 'bn.js'; +import { useEffect, useState } from 'react'; +import { KUSAMA_GENESIS, POLKADOT_GENESIS } from '@polkadot/apps-config'; + +import { useApi } from './useApi'; +import { useCall } from './useCall'; + +interface Config { + falloff: number; + idealStake: number; + maxInflation: number; + minInflation: number; +} + +interface State { + inflation: number; + stakedReturn: number; +} + +const DEFAULT_CONFIG: Config = { + falloff: 0.05, + idealStake: 0.5, + maxInflation: 0.1, + minInflation: 0.025 +}; + +const KNOWN_CONFIG: Record = { + [KUSAMA_GENESIS]: { ...DEFAULT_CONFIG, idealStake: 0.75 }, + [POLKADOT_GENESIS]: { ...DEFAULT_CONFIG, idealStake: 0.75 } +}; + +function getInflation ({ falloff, idealStake, maxInflation, minInflation }: Config = DEFAULT_CONFIG, totalStaked: BN, totalIssuance: BN): State { + const stakedFraction = totalStaked.muln(1_000_000).div(totalIssuance).toNumber() / 1_000_000; + const idealInterest = maxInflation / idealStake; + const inflation = 100 * (minInflation + ( + stakedFraction <= idealStake + ? (stakedFraction * (idealInterest - (minInflation / idealStake))) + : (((idealInterest * idealStake) - minInflation) * Math.pow(2, (idealStake - stakedFraction) / falloff)) + )); + + return { + inflation, + stakedReturn: inflation / stakedFraction + }; +} + +export function useInflation (totalStaked?: BN): State { + const { api } = useApi(); + const totalIssuance = useCall(api.query.balances.totalIssuance); + const [state, setState] = useState({ inflation: 0, stakedReturn: 0 }); + + useEffect((): void => { + totalIssuance && totalStaked && setState( + getInflation(KNOWN_CONFIG[api.genesisHash.toHex()], totalStaked, totalIssuance) + ); + }, [api, totalIssuance, totalStaked]); + + return state; +}