Display inflation & stakedReturn info (#4085)

* Introduce useInflation hook

* linting

* Display inflation & stakedReturns
This commit is contained in:
Jaco Greeff 2020-11-28 14:29:51 +01:00 committed by GitHub
parent c9d9b5022d
commit 4add11e7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 15 deletions

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { DeriveStakingOverview } from '@polkadot/api-derive/types';
import type { Inflation } from '@polkadot/react-hooks/types';
import React, { useContext } from 'react';
import styled from 'styled-components';
@ -13,13 +14,14 @@ import { useTranslation } from '../translate';
interface Props {
className?: string;
inflation: Inflation;
isVisible: boolean;
next?: string[];
nominators?: string[];
stakingOverview?: DeriveStakingOverview;
}
function Summary ({ className = '', isVisible, next, nominators, stakingOverview }: Props): React.ReactElement<Props> {
function Summary ({ className = '', inflation, isVisible, next, nominators, stakingOverview }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { lastBlockAuthors, lastBlockNumber } = useContext(BlockAuthorsContext);
@ -47,6 +49,14 @@ function Summary ({ className = '', isVisible, next, nominators, stakingOverview
{nominators.length}
</CardSummary>
)}
{(inflation.inflation > 0) && (
<CardSummary
className='media--1200'
label={t<string>('inflation')}
>
{inflation.inflation.toFixed(1)}%
</CardSummary>
)}
</section>
<section>
<CardSummary

View File

@ -1,7 +1,8 @@
// Copyright 2017-2020 @polkadot/app-staking authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Balance } from '@polkadot/types/interfaces';
import type { Inflation } from '@polkadot/react-hooks/types';
import type { Balance } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import React, { useMemo } from 'react';
@ -13,6 +14,7 @@ import { useTranslation } from '../translate';
interface Props {
avgStaked?: BN;
inflation: Inflation;
lowStaked?: BN;
lastReward?: BN;
numNominators?: number;
@ -20,7 +22,7 @@ interface Props {
totalStaked?: BN;
}
function Summary ({ avgStaked, lastReward, lowStaked, numNominators, numValidators, totalStaked }: Props): React.ReactElement<Props> {
function Summary ({ avgStaked, inflation, lastReward, lowStaked, numNominators, numValidators, totalStaked }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const totalIssuance = useCall<Balance>(api.query.balances?.totalIssuance);
@ -73,6 +75,14 @@ function Summary ({ avgStaked, lastReward, lowStaked, numNominators, numValidato
</div>
</CardSummary>
)}
{(inflation.stakedReturn > 0) && (
<CardSummary
className='media--1200'
label={t<string>('returns')}
>
{inflation.stakedReturn.toFixed(1)}%
</CardSummary>
)}
</section>
{avgStaked && lowStaked && (
<CardSummary

View File

@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { DeriveStakingOverview } from '@polkadot/api-derive/types';
import type { StakerState } from '@polkadot/react-hooks/types';
import type { Inflation, StakerState } from '@polkadot/react-hooks/types';
import type { SortedTargets, TargetSortBy, ValidatorInfo } from '../types';
import BN from 'bn.js';
@ -23,6 +23,7 @@ import useOwnNominators from './useOwnNominators';
interface Props {
className?: string;
inflation: Inflation;
isInElection: boolean;
ownStashes?: StakerState[];
stakingOverview?: DeriveStakingOverview;
@ -98,7 +99,7 @@ function selectProfitable (maxPaid: BN |undefined, list: ValidatorInfo[], { with
return result;
}
function Targets ({ className = '', isInElection, ownStashes, targets: { avgStaked, calcWith, lastReward, lowStaked, nominators, setCalcWith, totalStaked, validators }, toggleFavorite }: Props): React.ReactElement<Props> {
function Targets ({ className = '', inflation, isInElection, ownStashes, targets: { avgStaked, calcWith, lastReward, lowStaked, nominators, setCalcWith, totalStaked, validators }, toggleFavorite }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const allSlashes = useAvailableSlashes();
@ -226,6 +227,7 @@ function Targets ({ className = '', isInElection, ownStashes, targets: { avgStak
<div className={className}>
<Summary
avgStaked={avgStaked}
inflation={inflation}
lastReward={lastReward}
lowStaked={lowStaked}
numNominators={nominators?.length}

View File

@ -11,7 +11,7 @@ import { useLocation } from 'react-router-dom';
import styled from 'styled-components';
import { HelpOverlay } from '@polkadot/react-components';
import Tabs from '@polkadot/react-components/Tabs';
import { useAccounts, useApi, useAvailableSlashes, useCall, useFavorites, useOwnStashInfos, useStashIds } from '@polkadot/react-hooks';
import { useAccounts, useApi, useAvailableSlashes, useCall, useFavorites, useInflation, useOwnStashInfos, useStashIds } from '@polkadot/react-hooks';
import { isFunction } from '@polkadot/util';
import basicMd from './md/basic.md';
@ -42,7 +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 inflation = useInflation(targets?.totalStaked);
const stakingOverview = useCall<DeriveStakingOverview>(api.derive.staking.overview);
const isInElection = useCall<boolean>(api.query.staking?.eraElectionStatus, undefined, transformElection);
@ -115,6 +115,7 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement<Pr
/>
</header>
<Summary
inflation={inflation}
isVisible={pathname === basePath}
next={next}
nominators={targets.nominators}
@ -138,6 +139,7 @@ function StakingApp ({ basePath, className = '' }: Props): React.ReactElement<Pr
</Route>
<Route path={`${basePath}/targets`}>
<Targets
inflation={inflation}
isInElection={isInElection}
ownStashes={ownStashes}
stakingOverview={stakingOverview}

View File

@ -32,6 +32,11 @@ export interface ModalState {
onClose: () => void;
}
export interface Inflation {
inflation: number;
stakedReturn: number;
}
export interface Slash {
accountId: AccountId;
amount: Balance;

View File

@ -1,6 +1,8 @@
// Copyright 2017-2020 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Inflation } from './types';
import BN from 'bn.js';
import { useEffect, useState } from 'react';
import { KUSAMA_GENESIS, POLKADOT_GENESIS } from '@polkadot/apps-config';
@ -15,11 +17,6 @@ interface Config {
minInflation: number;
}
interface State {
inflation: number;
stakedReturn: number;
}
const DEFAULT_CONFIG: Config = {
falloff: 0.05,
idealStake: 0.5,
@ -32,7 +29,7 @@ const KNOWN_CONFIG: Record<string, Config> = {
[POLKADOT_GENESIS]: { ...DEFAULT_CONFIG, idealStake: 0.75 }
};
function getInflation ({ falloff, idealStake, maxInflation, minInflation }: Config = DEFAULT_CONFIG, totalStaked: BN, totalIssuance: BN): State {
function getInflation ({ falloff, idealStake, maxInflation, minInflation }: Config = DEFAULT_CONFIG, totalStaked: BN, totalIssuance: BN): Inflation {
const stakedFraction = totalStaked.muln(1_000_000).div(totalIssuance).toNumber() / 1_000_000;
const idealInterest = maxInflation / idealStake;
const inflation = 100 * (minInflation + (
@ -47,10 +44,10 @@ function getInflation ({ falloff, idealStake, maxInflation, minInflation }: Conf
};
}
export function useInflation (totalStaked?: BN): State {
export function useInflation (totalStaked?: BN): Inflation {
const { api } = useApi();
const totalIssuance = useCall<BN>(api.query.balances.totalIssuance);
const [state, setState] = useState<State>({ inflation: 0, stakedReturn: 0 });
const [state, setState] = useState<Inflation>({ inflation: 0, stakedReturn: 0 });
useEffect((): void => {
totalIssuance && totalStaked && setState(