Avoind NaN for returns (#4899)

This commit is contained in:
Jaco Greeff 2021-03-22 10:26:07 +01:00 committed by GitHub
parent c23745f352
commit bfd6e387d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,8 @@ import { BN_MILLION } from '@polkadot/util';
import { useApi } from './useApi';
import { useCall } from './useCall';
const EMPTY: Inflation = { inflation: 0, stakedReturn: 0 };
export function calcInflation (api: ApiPromise, totalStaked: BN, totalIssuance: BN): Inflation {
const { falloff, idealStake, maxInflation, minInflation } = getInflationParams(api);
const stakedFraction = totalStaked.mul(BN_MILLION).div(totalIssuance).toNumber() / BN_MILLION.toNumber();
@ -25,14 +27,16 @@ export function calcInflation (api: ApiPromise, totalStaked: BN, totalIssuance:
return {
inflation,
stakedReturn: inflation / stakedFraction
stakedReturn: stakedFraction
? (inflation / stakedFraction)
: 0
};
}
export function useInflation (totalStaked?: BN): Inflation {
const { api } = useApi();
const totalIssuance = useCall<BN>(api.query.balances.totalIssuance);
const [state, setState] = useState<Inflation>({ inflation: 0, stakedReturn: 0 });
const [state, setState] = useState<Inflation>(EMPTY);
useEffect((): void => {
totalIssuance && totalStaked && setState(