Introduce useInflation hook (#4084)
* Introduce useInflation hook * linting
This commit is contained in:
parent
2bcbf67e36
commit
f8dc3d3641
@ -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);
|
||||
|
||||
|
||||
@ -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'))
|
||||
|
||||
@ -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';
|
||||
|
||||
62
packages/react-hooks/src/useInflation.ts
Normal file
62
packages/react-hooks/src/useInflation.ts
Normal 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;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user