ghost-portal/packages/react-hooks/src/useInflation.ts
Ewa Kowalska 541213f233
Ek extract type only imports into separate group (#4168)
* Plug new beta of eslint plugin simple import sort

* Apply new configuration to existing imports
2020-12-07 16:04:05 +01:00

44 lines
1.5 KiB
TypeScript

// Copyright 2017-2020 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiPromise } from '@polkadot/api';
import type { Inflation } from './types';
import BN from 'bn.js';
import { useEffect, useState } from 'react';
import { getInflationParams } from '@polkadot/apps-config';
import { useApi } from './useApi';
import { useCall } from './useCall';
export function calcInflation (api: ApiPromise, totalStaked: BN, totalIssuance: BN): Inflation {
const { falloff, idealStake, maxInflation, minInflation } = getInflationParams(api);
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): Inflation {
const { api } = useApi();
const totalIssuance = useCall<BN>(api.query.balances.totalIssuance);
const [state, setState] = useState<Inflation>({ inflation: 0, stakedReturn: 0 });
useEffect((): void => {
totalIssuance && totalStaked && setState(
calcInflation(api, totalStaked, totalIssuance)
);
}, [api, totalIssuance, totalStaked]);
return state;
}