Introduce useInflation hook (#4084)

* Introduce useInflation hook

* linting
This commit is contained in:
Jaco Greeff 2020-11-26 15:51:59 +01:00 committed by GitHub
parent 2bcbf67e36
commit f8dc3d3641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 0 deletions

View File

@ -42,6 +42,7 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement<Pr
const ownStashes = useOwnStashInfos();
const slashes = useAvailableSlashes();
const targets = useSortedTargets(favorites);
// const inflation = useInflation(targets?.totalStaked);
const stakingOverview = useCall<DeriveStakingOverview>(api.derive.staking.overview);
const isInElection = useCall<boolean>(api.query.staking?.eraElectionStatus, undefined, transformElection);

View File

@ -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'))

View File

@ -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';

View File

@ -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<string, Config> = {
[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<BN>(api.query.balances.totalIssuance);
const [state, setState] = useState<State>({ inflation: 0, stakedReturn: 0 });
useEffect((): void => {
totalIssuance && totalStaked && setState(
getInflation(KNOWN_CONFIG[api.genesisHash.toHex()], totalStaked, totalIssuance)
);
}, [api, totalIssuance, totalStaked]);
return state;
}