Improve rewards rendering
This commit is contained in:
parent
fc80d5c008
commit
644fd0099a
@ -9,7 +9,7 @@ import React from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { registry } from '@polkadot/react-api';
|
||||
import { Button } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { createType } from '@polkadot/types';
|
||||
|
||||
import Candidates from './Candidates';
|
||||
@ -33,9 +33,9 @@ const NULL_INFO: DerivedElectionsInfo = {
|
||||
|
||||
export default function Overview ({ className }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const bestNumber = trackStream<BlockNumber>(api.derive.chain.bestNumber, []);
|
||||
const _electionsInfo = trackStream<DerivedElectionsInfo>(api.derive.elections.info, []);
|
||||
const allVotes = trackStream<Record<string, AccountId[]>>(api.query.electionsPhragmen?.votesOf, [], {
|
||||
const bestNumber = useStream<BlockNumber>(api.derive.chain.bestNumber, []);
|
||||
const _electionsInfo = useStream<DerivedElectionsInfo>(api.derive.elections.info, []);
|
||||
const allVotes = useStream<Record<string, AccountId[]>>(api.query.electionsPhragmen?.votesOf, [], {
|
||||
transform: ([voters, casted]: [AccountId[], AccountId[][]]): Record<string, AccountId[]> =>
|
||||
voters.reduce((result: Record<string, AccountId[]>, voter, index): Record<string, AccountId[]> => {
|
||||
casted[index].forEach((candidate): void => {
|
||||
|
||||
@ -10,7 +10,7 @@ import { Route, Switch } from 'react-router';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import styled from 'styled-components';
|
||||
import { Tabs } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
import Overview from './Overview';
|
||||
import Motions from './Motions';
|
||||
@ -23,7 +23,7 @@ interface Props extends AppProps, BareProps, I18nProps {}
|
||||
function App ({ basePath, className, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const { pathname } = useLocation();
|
||||
const motions = trackStream<Hash[]>(api.query.council.proposals, []);
|
||||
const motions = useStream<Hash[]>(api.query.council.proposals, []);
|
||||
|
||||
return (
|
||||
<main className={className}>
|
||||
|
||||
@ -5,11 +5,11 @@
|
||||
import { Hash } from '@polkadot/types/interfaces';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
export default function useCounter (): number {
|
||||
const { api, isApiReady } = useApi();
|
||||
const motions = trackStream<Hash[]>(isApiReady ? api.query.council?.proposals : undefined, []);
|
||||
const motions = useStream<Hash[]>(isApiReady ? api.query.council?.proposals : undefined, []);
|
||||
const [counter, setCounter] = useState(0);
|
||||
|
||||
useEffect((): void => {
|
||||
|
||||
@ -7,7 +7,7 @@ import { AccountId, Balance, BlockNumber, Hash, Proposal, ReferendumIndex } from
|
||||
import { ITuple } from '@polkadot/types/types';
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { Bytes, Option } from '@polkadot/types';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
@ -22,7 +22,7 @@ interface Props extends I18nProps {
|
||||
|
||||
function DispatchEntry ({ blockNumber, hash, referendumIndex, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const preimage = trackStream<Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>
|
||||
const preimage = useStream<Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>
|
||||
>(api.query.democracy.preimages, [hash]);
|
||||
const [proposal, setProposal] = useState<Proposal | undefined>();
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import { ITuple } from '@polkadot/types/types';
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import { Table } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { Option, StorageKey, Vec } from '@polkadot/types';
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
|
||||
@ -18,7 +18,7 @@ import DispatchBlock from './DispatchBlock';
|
||||
function DispatchQueue ({ className, t }: Props): React.ReactElement<Props> | null {
|
||||
const { api } = useApi();
|
||||
const [keyPrefix] = useState(u8aToHex(api.query.democracy.dispatchQueue.creator.iterKey));
|
||||
const queued = trackStream<[StorageKey, Option<Vec<Option<ITuple<[Hash, ReferendumIndex]>>>>
|
||||
const queued = useStream<[StorageKey, Option<Vec<Option<ITuple<[Hash, ReferendumIndex]>>>>
|
||||
][]>(api.query.democracy.dispatchQueue.entries as any, []);
|
||||
|
||||
if (!queued?.length) {
|
||||
|
||||
@ -8,7 +8,7 @@ import { I18nProps as Props } from '@polkadot/react-components/types';
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { AddressSmall, Table } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
import { Bytes, Option } from '@polkadot/types';
|
||||
|
||||
@ -17,10 +17,10 @@ import ProposalCell from './ProposalCell';
|
||||
|
||||
function Externals ({ className, t }: Props): React.ReactElement<Props> | null {
|
||||
const { api } = useApi();
|
||||
const external = trackStream<Option<ITuple<[Hash, VoteThreshold]>>>(api.query.democracy.nextExternal, []);
|
||||
const external = useStream<Option<ITuple<[Hash, VoteThreshold]>>>(api.query.democracy.nextExternal, []);
|
||||
const [hash, setHash] = useState<Hash | null>(null);
|
||||
const [expanded, setExpanded] = useState<{ at: BlockNumber; balance: Balance; proposer: AccountId; proposal: Proposal } | null>(null);
|
||||
const preimage = trackStream<Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>>(api.query.democracy.preimages, [hash]);
|
||||
const preimage = useStream<Option<ITuple<[Bytes, AccountId, Balance, BlockNumber]>>>(api.query.democracy.preimages, [hash]);
|
||||
|
||||
useEffect((): void => {
|
||||
setHash(
|
||||
|
||||
@ -7,14 +7,14 @@ import { I18nProps as Props } from '@polkadot/react-components/types';
|
||||
|
||||
import React from 'react';
|
||||
import { Table } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
import ProposalDisplay from './Proposal';
|
||||
import translate from '../translate';
|
||||
|
||||
function Proposals ({ className, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const proposals = trackStream<DeriveProposal[]>(api.derive.democracy.proposals, []);
|
||||
const proposals = useStream<DeriveProposal[]>(api.derive.democracy.proposals, []);
|
||||
|
||||
return (
|
||||
<div className={`proposalSection ${className}`}>
|
||||
|
||||
@ -7,14 +7,14 @@ import { I18nProps as Props } from '@polkadot/react-components/types';
|
||||
|
||||
import React from 'react';
|
||||
import { Table } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
import Referendum from './Referendum';
|
||||
import translate from '../translate';
|
||||
|
||||
function Referendums ({ className, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const referendums = trackStream<DerivedReferendum[]>(api.derive.democracy.referendums, []);
|
||||
const referendums = useStream<DerivedReferendum[]>(api.derive.democracy.referendums, []);
|
||||
|
||||
return (
|
||||
<div className={`proposalSection ${className}`}>
|
||||
|
||||
@ -8,7 +8,7 @@ import { I18nProps } from '@polkadot/react-components/types';
|
||||
import BN from 'bn.js';
|
||||
import React from 'react';
|
||||
import { SummaryBox, CardSummary } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { withCalls } from '@polkadot/react-api';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
@ -23,7 +23,7 @@ interface Props extends I18nProps {
|
||||
|
||||
function Summary (props: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const activeProposals = trackStream<any[]>(api.derive.democracy.proposals, []);
|
||||
const activeProposals = useStream<any[]>(api.derive.democracy.proposals, []);
|
||||
|
||||
const {
|
||||
chain_bestNumber,
|
||||
|
||||
@ -3,12 +3,12 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
export default function useCounter (): number {
|
||||
const { api, isApiReady } = useApi();
|
||||
const proposals = trackStream<any[]>(isApiReady ? api.derive.democracy.proposals : undefined, []);
|
||||
const referenda = trackStream<any[]>(isApiReady ? api.derive.democracy.referendums : undefined, []);
|
||||
const proposals = useStream<any[]>(isApiReady ? api.derive.democracy.proposals : undefined, []);
|
||||
const referenda = useStream<any[]>(isApiReady ? api.derive.democracy.referendums : undefined, []);
|
||||
const [counter, setCounter] = useState(0);
|
||||
|
||||
useEffect((): void => {
|
||||
|
||||
@ -10,7 +10,7 @@ import { ComponentProps } from '../types';
|
||||
import React, { useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { Button, CardGrid } from '@polkadot/react-components';
|
||||
import { trackStream, useApi } from '@polkadot/react-hooks';
|
||||
import { useStream, useApi } from '@polkadot/react-hooks';
|
||||
import { AccountName } from '@polkadot/react-query';
|
||||
import { Option } from '@polkadot/types';
|
||||
import createOption from '@polkadot/ui-keyring/options/item';
|
||||
@ -46,8 +46,8 @@ function getStashes (allAccounts: string[], queryBonded?: Option<AccountId>[], q
|
||||
|
||||
function Actions ({ allAccounts, allStashes, className, recentlyOnline, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const queryBonded = trackStream<Option<AccountId>[]>(api.query.staking.bonded.multi as any, [allAccounts]);
|
||||
const queryLedger = trackStream<Option<StakingLedger>[]>(api.query.staking.ledger.multi as any, [allAccounts]);
|
||||
const queryBonded = useStream<Option<AccountId>[]>(api.query.staking.bonded.multi as any, [allAccounts]);
|
||||
const queryLedger = useStream<Option<StakingLedger>[]>(api.query.staking.ledger.multi as any, [allAccounts]);
|
||||
const [isNewStakeOpen, setIsNewStateOpen] = useState(false);
|
||||
const foundStashes = getStashes(allAccounts, queryBonded, queryLedger);
|
||||
const stashOptions = allStashes.map((stashId): KeyringSectionOption =>
|
||||
|
||||
@ -11,7 +11,7 @@ import BN from 'bn.js';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { AddressMini, AddressSmall, Badge, Icon } from '@polkadot/react-components';
|
||||
import { trackStream, useApi } from '@polkadot/react-hooks';
|
||||
import { useStream, useApi } from '@polkadot/react-hooks';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
@ -51,7 +51,7 @@ interface StakingState {
|
||||
function Address ({ address, authorsMap, className, filter, hasQueries, isElected, isFavorite, lastAuthors, myAccounts, points, recentlyOnline, t, toggleFavorite, withNominations = true }: Props): React.ReactElement<Props> | null {
|
||||
const { api } = useApi();
|
||||
// FIXME Any horrors, caused by derive type mismatches
|
||||
const stakingInfo = trackStream<DerivedStaking>(api.derive.staking.info as any, [address]);
|
||||
const stakingInfo = useStream<DerivedStaking>(api.derive.staking.info as any, [address]);
|
||||
const [hasActivity, setHasActivity] = useState(true);
|
||||
const [{ commission, hasNominators, isNominatorMe, nominators, stashId, stakeOwn, stakeOther, validatorPayment }, setStakingState] = useState<StakingState>({
|
||||
hasNominators: false,
|
||||
|
||||
@ -8,7 +8,7 @@ import { I18nProps } from '@polkadot/react-components/types';
|
||||
import BN from 'bn.js';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { SummaryBox, CardSummary } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { formatBalance } from '@polkadot/util';
|
||||
|
||||
import translate from '../translate';
|
||||
@ -25,7 +25,7 @@ interface StakeInfo {
|
||||
|
||||
function Summary ({ lastReward, t, totalStaked }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const totalInsurance = trackStream<Balance>(api.query.balances.totalIssuance, []);
|
||||
const totalInsurance = useStream<Balance>(api.query.balances.totalIssuance, []);
|
||||
const [{ percentage, staked }, setStakeInfo] = useState<StakeInfo>({ percentage: '-', staked: null });
|
||||
const [total, setTotal] = useState<string | null>(null);
|
||||
|
||||
|
||||
@ -4,15 +4,15 @@
|
||||
|
||||
import { DerivedStakingElected } from '@polkadot/api-derive/types';
|
||||
import { I18nProps } from '@polkadot/react-components/types';
|
||||
import { ValidatorPrefs, ValidatorPrefsTo196 } from '@polkadot/types/interfaces';
|
||||
import { AccountId, Exposure, ValidatorPrefs, ValidatorPrefsTo196 } from '@polkadot/types/interfaces';
|
||||
import { ComponentProps } from '../types';
|
||||
import { ValidatorInfo } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import React, { useEffect, useRef, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { InputBalance, Table } from '@polkadot/react-components';
|
||||
import { useAccounts, useApi, useFavorites, trackStream } from '@polkadot/react-hooks';
|
||||
import { useAccounts, useApi, useFavorites, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
import { STORE_FAVS_BASE } from '../constants';
|
||||
import translate from '../translate';
|
||||
@ -29,6 +29,12 @@ interface AllInfo {
|
||||
validators: ValidatorInfo[];
|
||||
}
|
||||
|
||||
interface BaseInfo {
|
||||
accountId: AccountId;
|
||||
stakers?: Exposure;
|
||||
validatorPrefs?: ValidatorPrefs;
|
||||
}
|
||||
|
||||
function sortValidators (list: ValidatorInfo[]): ValidatorInfo[] {
|
||||
return list
|
||||
.sort((a, b): number => b.bondTotal.cmp(a.bondTotal))
|
||||
@ -86,9 +92,11 @@ function Targets ({ className, sessionRewards, t }: Props): React.ReactElement<P
|
||||
const { api } = useApi();
|
||||
const { allAccounts } = useAccounts();
|
||||
const [amount, setAmount] = useState<BN | undefined>(new BN(1000));
|
||||
const electedInfo = trackStream<DerivedStakingElected>(api.derive.staking.electedInfo, []);
|
||||
const electedInfo = useStream<DerivedStakingElected>(api.derive.staking.electedInfo, []);
|
||||
const [favorites, toggleFavorite] = useFavorites(STORE_FAVS_BASE);
|
||||
const [lastReward, setLastReward] = useState(new BN(0));
|
||||
const lastBase = useRef<number>(0);
|
||||
const [baseInfo, setBaseInfo] = useState<BaseInfo[]>([]);
|
||||
const [{ validators, totalStaked }, setWorkable] = useState<AllInfo>({ totalStaked: new BN(0), validators: [] });
|
||||
|
||||
useEffect((): void => {
|
||||
@ -104,63 +112,73 @@ function Targets ({ className, sessionRewards, t }: Props): React.ReactElement<P
|
||||
}, [sessionRewards]);
|
||||
|
||||
useEffect((): void => {
|
||||
if (electedInfo) {
|
||||
let totalStaked = new BN(0);
|
||||
const numValidators = electedInfo.info.length;
|
||||
const validators = sortValidators(
|
||||
electedInfo.info.map(({ accountId, stakers, validatorPrefs }): ValidatorInfo => {
|
||||
const exposure = stakers || {
|
||||
total: api.createType('Compact<Balance>'),
|
||||
own: api.createType('Compact<Balance>'),
|
||||
others: api.createType('Vec<IndividualExposure>')
|
||||
};
|
||||
const prefs = (validatorPrefs as (ValidatorPrefs | ValidatorPrefsTo196)) || {
|
||||
commission: api.createType('Compact<Perbill>')
|
||||
};
|
||||
const bondOwn = exposure.own.unwrap();
|
||||
const bondTotal = exposure.total.unwrap();
|
||||
const perValidatorReward = lastReward.divn(numValidators);
|
||||
const validatorPayment = (prefs as ValidatorPrefsTo196).validatorPayment
|
||||
? (prefs as ValidatorPrefsTo196).validatorPayment.unwrap() as BN
|
||||
: (prefs as ValidatorPrefs).commission.unwrap().mul(perValidatorReward).div(PERBILL);
|
||||
const key = accountId.toString();
|
||||
const rewardSplit = perValidatorReward.sub(validatorPayment);
|
||||
const calcAmount = amount || new BN(0);
|
||||
const rewardPayout = rewardSplit.gtn(0)
|
||||
? calcAmount.mul(rewardSplit).div(calcAmount.add(bondTotal))
|
||||
: new BN(0);
|
||||
const isNominating = exposure.others.reduce((isNominating, indv): boolean => {
|
||||
return isNominating || allAccounts.includes(indv.who.toString());
|
||||
}, allAccounts.includes(key));
|
||||
if (electedInfo && lastBase.current !== electedInfo.info.length) {
|
||||
lastBase.current = electedInfo.info.length;
|
||||
|
||||
totalStaked = totalStaked.add(bondTotal);
|
||||
|
||||
return {
|
||||
accountId,
|
||||
bondOther: bondTotal.sub(bondOwn),
|
||||
bondOwn,
|
||||
bondShare: 0,
|
||||
bondTotal,
|
||||
isCommission: !!(prefs as ValidatorPrefs).commission,
|
||||
isFavorite: favorites.includes(key),
|
||||
isNominating,
|
||||
key,
|
||||
commissionPer: (((prefs as ValidatorPrefs).commission?.unwrap() || new BN(0)).muln(10000).div(PERBILL).toNumber() / 100),
|
||||
numNominators: exposure.others.length,
|
||||
rankBonded: 0,
|
||||
rankOverall: 0,
|
||||
rankPayment: 0,
|
||||
rankReward: 0,
|
||||
rewardPayout,
|
||||
rewardSplit,
|
||||
validatorPayment
|
||||
};
|
||||
})
|
||||
setBaseInfo(
|
||||
electedInfo.info.map(({ accountId, stakers, validatorPrefs }) => ({
|
||||
accountId, stakers, validatorPrefs
|
||||
}))
|
||||
);
|
||||
|
||||
setWorkable({ totalStaked, validators });
|
||||
}
|
||||
}, [allAccounts, amount, electedInfo, favorites, lastReward]);
|
||||
}, [electedInfo]);
|
||||
|
||||
useEffect((): void => {
|
||||
let totalStaked = new BN(0);
|
||||
const numValidators = baseInfo.length;
|
||||
const validators = sortValidators(
|
||||
baseInfo.map(({ accountId, stakers, validatorPrefs }): ValidatorInfo => {
|
||||
const exposure = stakers || {
|
||||
total: api.createType('Compact<Balance>'),
|
||||
own: api.createType('Compact<Balance>'),
|
||||
others: api.createType('Vec<IndividualExposure>')
|
||||
};
|
||||
const prefs = (validatorPrefs as (ValidatorPrefs | ValidatorPrefsTo196)) || {
|
||||
commission: api.createType('Compact<Perbill>')
|
||||
};
|
||||
const bondOwn = exposure.own.unwrap();
|
||||
const bondTotal = exposure.total.unwrap();
|
||||
const perValidatorReward = lastReward.divn(numValidators);
|
||||
const validatorPayment = (prefs as ValidatorPrefsTo196).validatorPayment
|
||||
? (prefs as ValidatorPrefsTo196).validatorPayment.unwrap() as BN
|
||||
: (prefs as ValidatorPrefs).commission.unwrap().mul(perValidatorReward).div(PERBILL);
|
||||
const key = accountId.toString();
|
||||
const rewardSplit = perValidatorReward.sub(validatorPayment);
|
||||
const calcAmount = amount || new BN(0);
|
||||
const rewardPayout = rewardSplit.gtn(0)
|
||||
? calcAmount.mul(rewardSplit).div(calcAmount.add(bondTotal))
|
||||
: new BN(0);
|
||||
const isNominating = exposure.others.reduce((isNominating, indv): boolean => {
|
||||
return isNominating || allAccounts.includes(indv.who.toString());
|
||||
}, allAccounts.includes(key));
|
||||
|
||||
totalStaked = totalStaked.add(bondTotal);
|
||||
|
||||
return {
|
||||
accountId,
|
||||
bondOther: bondTotal.sub(bondOwn),
|
||||
bondOwn,
|
||||
bondShare: 0,
|
||||
bondTotal,
|
||||
isCommission: !!(prefs as ValidatorPrefs).commission,
|
||||
isFavorite: favorites.includes(key),
|
||||
isNominating,
|
||||
key,
|
||||
commissionPer: (((prefs as ValidatorPrefs).commission?.unwrap() || new BN(0)).muln(10000).div(PERBILL).toNumber() / 100),
|
||||
numNominators: exposure.others.length,
|
||||
rankBonded: 0,
|
||||
rankOverall: 0,
|
||||
rankPayment: 0,
|
||||
rankReward: 0,
|
||||
rewardPayout,
|
||||
rewardSplit,
|
||||
validatorPayment
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
setWorkable({ totalStaked, validators });
|
||||
}, [allAccounts, amount, baseInfo, favorites, lastReward]);
|
||||
|
||||
return (
|
||||
<div className={className}>
|
||||
|
||||
@ -14,7 +14,7 @@ import styled from 'styled-components';
|
||||
import { Option } from '@polkadot/types';
|
||||
import { HelpOverlay } from '@polkadot/react-components';
|
||||
import Tabs from '@polkadot/react-components/Tabs';
|
||||
import { trackStream, useAccounts, useApi } from '@polkadot/react-hooks';
|
||||
import { useStream, useAccounts, useApi } from '@polkadot/react-hooks';
|
||||
|
||||
import basicMd from './md/basic.md';
|
||||
import Actions from './Actions';
|
||||
@ -44,10 +44,10 @@ function App ({ basePath, className, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const { allAccounts, hasAccounts } = useAccounts();
|
||||
const { pathname } = useLocation();
|
||||
const stakingControllers = trackStream<[string[], string[]]>(api.derive.staking.controllers, [], { transform: transformStakingControllers });
|
||||
const bestNumber = trackStream<BlockNumber>(api.derive.chain.bestNumber, []);
|
||||
const recentlyOnline = trackStream<DerivedHeartbeats>(api.derive.imOnline.receivedHeartbeats, []);
|
||||
const stakingOverview = trackStream<DerivedStakingOverview>(api.derive.staking.overview, []);
|
||||
const stakingControllers = useStream<[string[], string[]]>(api.derive.staking.controllers, [], { transform: transformStakingControllers });
|
||||
const bestNumber = useStream<BlockNumber>(api.derive.chain.bestNumber, []);
|
||||
const recentlyOnline = useStream<DerivedHeartbeats>(api.derive.imOnline.receivedHeartbeats, []);
|
||||
const stakingOverview = useStream<DerivedStakingOverview>(api.derive.staking.overview, []);
|
||||
const sessionRewards = useSessionRewards(MAX_SESSIONS);
|
||||
|
||||
const hasQueries = hasAccounts && !!(api.query.imOnline?.authoredBlocks);
|
||||
|
||||
@ -6,15 +6,15 @@ import { SessionIndex } from '@polkadot/types/interfaces';
|
||||
import { SessionRewards } from './types';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { u32 } from '@polkadot/types';
|
||||
|
||||
export default function useBlockCounts (accountId: string, sessionRewards: SessionRewards[]): u32[] {
|
||||
const { api } = useApi();
|
||||
const [counts, setCounts] = useState<u32[]>([]);
|
||||
const [historic, setHistoric] = useState<u32[]>([]);
|
||||
const sessionIndex = trackStream<SessionIndex>(api.query.session.currentIndex, []);
|
||||
const current = trackStream<u32>(api.query.imOnline?.authoredBlocks, [sessionIndex, accountId]);
|
||||
const sessionIndex = useStream<SessionIndex>(api.query.session.currentIndex, []);
|
||||
const current = useStream<u32>(api.query.imOnline?.authoredBlocks, [sessionIndex, accountId]);
|
||||
|
||||
useEffect((): void => {
|
||||
if (api.query.imOnline?.authoredBlocks && sessionRewards && sessionRewards.length) {
|
||||
|
||||
@ -9,7 +9,7 @@ import { ComponentProps } from './types';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { Route, Switch } from 'react-router';
|
||||
import { Icon, Tabs } from '@polkadot/react-components';
|
||||
import { trackStream, useAccounts, useApi } from '@polkadot/react-hooks';
|
||||
import { useStream, useAccounts, useApi } from '@polkadot/react-hooks';
|
||||
|
||||
import SetKey from './SetKey';
|
||||
import Sudo from './Sudo';
|
||||
@ -21,7 +21,7 @@ interface Props extends AppProps, I18nProps {
|
||||
|
||||
function App ({ basePath, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const sudoKey = trackStream<string>(api.query.sudo.key, [], { transform: (k): string => k.toString() });
|
||||
const sudoKey = useStream<string>(api.query.sudo.key, [], { transform: (k): string => k.toString() });
|
||||
const { allAccounts } = useAccounts();
|
||||
const [isMine, setIsMine] = useState(false);
|
||||
|
||||
|
||||
@ -8,7 +8,7 @@ import { ComponentProps } from '../types';
|
||||
|
||||
import React from 'react';
|
||||
import { SummaryBox, CardSummary } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { u32 } from '@polkadot/types';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
|
||||
@ -18,7 +18,7 @@ interface Props extends ComponentProps, I18nProps {}
|
||||
|
||||
function Summary ({ className, members, proposals, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const proposalCount = trackStream<u32>(api.query.technicalCommittee.proposalCount, []);
|
||||
const proposalCount = useStream<u32>(api.query.technicalCommittee.proposalCount, []);
|
||||
|
||||
return (
|
||||
<SummaryBox className={className}>
|
||||
|
||||
@ -7,7 +7,7 @@ import { I18nProps } from '@polkadot/react-components/types';
|
||||
|
||||
import React from 'react';
|
||||
import { AddressMini } from '@polkadot/react-components';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import ProposalCell from '@polkadot/app-democracy/Overview/ProposalCell';
|
||||
import { Option } from '@polkadot/types';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
@ -21,8 +21,8 @@ interface Props extends I18nProps {
|
||||
|
||||
function Proposal ({ className, hash, t }: Props): React.ReactElement<Props> | null {
|
||||
const { api } = useApi();
|
||||
const optProposal = trackStream<Option<ProposalType>>(api.query.technicalCommittee.proposalOf, [hash]);
|
||||
const votes = trackStream<Option<Votes>>(api.query.technicalCommittee.voting, [hash]);
|
||||
const optProposal = useStream<Option<ProposalType>>(api.query.technicalCommittee.proposalOf, [hash]);
|
||||
const votes = useStream<Option<Votes>>(api.query.technicalCommittee.voting, [hash]);
|
||||
|
||||
if (!optProposal?.isSome || !votes?.isSome) {
|
||||
return null;
|
||||
|
||||
@ -7,7 +7,7 @@ import { AppProps, BareProps, I18nProps } from '@polkadot/react-components/types
|
||||
|
||||
import React from 'react';
|
||||
import { Route, Switch } from 'react-router';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
import { Tabs } from '@polkadot/react-components';
|
||||
|
||||
import Overview from './Overview';
|
||||
@ -20,8 +20,8 @@ interface Props extends AppProps, BareProps, I18nProps {}
|
||||
|
||||
function App ({ basePath, className, t }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const members = trackStream<AccountId[]>(api.query.technicalCommittee.members, []);
|
||||
const proposals = trackStream<Hash[]>(api.query.technicalCommittee.proposals, []);
|
||||
const members = useStream<AccountId[]>(api.query.technicalCommittee.members, []);
|
||||
const proposals = useStream<Hash[]>(api.query.technicalCommittee.proposals, []);
|
||||
|
||||
return (
|
||||
<main className={className}>
|
||||
|
||||
@ -5,11 +5,11 @@
|
||||
import { Hash } from '@polkadot/types/interfaces';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
export default function useCounter (): number {
|
||||
const { api, isApiReady } = useApi();
|
||||
const proposals = trackStream<Hash[]>(isApiReady ? api.query.technicalCommittee && api.query.technicalCommittee.proposals : undefined, []);
|
||||
const proposals = useStream<Hash[]>(isApiReady ? api.query.technicalCommittee && api.query.technicalCommittee.proposals : undefined, []);
|
||||
const [counter, setCounter] = useState(0);
|
||||
|
||||
useEffect((): void => {
|
||||
|
||||
@ -13,7 +13,7 @@ import styled from 'styled-components';
|
||||
import { Responsive } from 'semantic-ui-react';
|
||||
import routing from '@polkadot/apps-routing';
|
||||
import { Button, ChainImg, Icon, Menu, media } from '@polkadot/react-components';
|
||||
import { trackStream, useApi } from '@polkadot/react-hooks';
|
||||
import { useStream, useApi } from '@polkadot/react-hooks';
|
||||
import { classes } from '@polkadot/react-components/util';
|
||||
import { BestNumber, Chain } from '@polkadot/react-query';
|
||||
|
||||
@ -33,7 +33,7 @@ interface Props extends I18nProps {
|
||||
|
||||
function SideBar ({ className, collapse, handleResize, isCollapsed, toggleMenu, menuOpen }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const runtimeVersion = trackStream<RuntimeVersion>(api.rpc.state.subscribeRuntimeVersion, []);
|
||||
const runtimeVersion = useStream<RuntimeVersion>(api.rpc.state.subscribeRuntimeVersion, []);
|
||||
const [modals, setModals] = useState<Record<string, boolean>>(
|
||||
routing.routes.reduce((result: Record<string, boolean>, route): Record<string, boolean> => {
|
||||
if (route && route.Modal) {
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export { default as trackPromise } from './track/trackPromise';
|
||||
export { default as trackStream } from './track/trackStream';
|
||||
export { default as usePromise } from './track/usePromise';
|
||||
export { default as useStream } from './track/useStream';
|
||||
export { default as useAccounts } from './useAccounts';
|
||||
export { default as useAddresses } from './useAddresses';
|
||||
export { default as useApi } from './useApi';
|
||||
|
||||
@ -18,7 +18,7 @@ interface TrackFn <T> {
|
||||
// tracks a promise, typically an api.* call (query, query.at, rpc) that
|
||||
// - returns a promise with the value
|
||||
// FIXME The typings here need some serious TLC
|
||||
export default function trackPromise <T> (fn: TrackFn<T> | undefined, params: Params, { paramMap = transformIdentity, transform = transformIdentity }: Options<T> = {}): T | undefined {
|
||||
export default function usePromise <T> (fn: TrackFn<T> | undefined, params: Params, { paramMap = transformIdentity, transform = transformIdentity }: Options<T> = {}): T | undefined {
|
||||
const [value, setValue] = useState<T | undefined>();
|
||||
const tracker = useRef<{ serialized: string | null }>({ serialized: null });
|
||||
|
||||
@ -32,17 +32,6 @@ export default function trackPromise <T> (fn: TrackFn<T> | undefined, params: Pa
|
||||
});
|
||||
};
|
||||
|
||||
// initial round, subscribe once
|
||||
useEffect((): void => {
|
||||
const [serialized, mappedParams] = extractParams(fn, params, paramMap);
|
||||
|
||||
tracker.current.serialized = serialized;
|
||||
|
||||
if (mappedParams) {
|
||||
_subscribe(mappedParams);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// on changes, re-get
|
||||
useEffect((): void => {
|
||||
const [serialized, mappedParams] = extractParams(fn, params, paramMap);
|
||||
@ -32,7 +32,7 @@ interface TrackFn {
|
||||
// - returns a promise with an unsubscribe function
|
||||
// - has a callback to set the value
|
||||
// FIXME The typings here need some serious TLC
|
||||
export default function trackStream <T> (fn: TrackFn | undefined, params: Params, { paramMap = transformIdentity, transform = transformIdentity }: Options<T> = {}): T | undefined {
|
||||
export default function useStream <T> (fn: TrackFn | undefined, params: Params, { paramMap = transformIdentity, transform = transformIdentity }: Options<T> = {}): T | undefined {
|
||||
const [value, setValue] = useState<T | undefined>();
|
||||
const tracker = useRef<{ serialized: string | null; subscriber: TrackFnResult }>({ serialized: null, subscriber: dummyPromise });
|
||||
|
||||
@ -54,19 +54,6 @@ export default function trackStream <T> (fn: TrackFn | undefined, params: Params
|
||||
});
|
||||
};
|
||||
|
||||
// initial round, subscribe once
|
||||
useEffect((): () => void => {
|
||||
const [serialized, mappedParams] = extractParams(fn, params, paramMap);
|
||||
|
||||
tracker.current.serialized = serialized;
|
||||
|
||||
if (mappedParams) {
|
||||
_subscribe(mappedParams);
|
||||
}
|
||||
|
||||
return _unsubscribe;
|
||||
}, []);
|
||||
|
||||
// on changes, re-subscribe
|
||||
useEffect((): void => {
|
||||
const [serialized, mappedParams] = extractParams(fn, params, paramMap);
|
||||
@ -7,7 +7,7 @@ import { DeriveAccountInfo } from '@polkadot/api-derive/types';
|
||||
import { BareProps } from '@polkadot/react-api/types';
|
||||
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { useApi, trackStream } from '@polkadot/react-hooks';
|
||||
import { useApi, useStream } from '@polkadot/react-hooks';
|
||||
|
||||
interface Props extends BareProps {
|
||||
children?: React.ReactNode;
|
||||
@ -18,7 +18,7 @@ interface Props extends BareProps {
|
||||
|
||||
export default function AccountIndexDisplay ({ children, className, defaultValue = '-', label = '', params, style }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const info = trackStream<DeriveAccountInfo>(api.derive.accounts.info as any, [params]);
|
||||
const info = useStream<DeriveAccountInfo>(api.derive.accounts.info as any, [params]);
|
||||
const [accountIndex, setAccountIndex] = useState<string | null>(null);
|
||||
|
||||
useEffect((): void => {
|
||||
|
||||
@ -8,7 +8,7 @@ import { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces';
|
||||
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { getAddressName } from '@polkadot/react-components/util';
|
||||
import { trackStream, useApi } from '@polkadot/react-hooks';
|
||||
import { useStream, useApi } from '@polkadot/react-hooks';
|
||||
|
||||
interface Props extends BareProps {
|
||||
children?: React.ReactNode;
|
||||
@ -42,7 +42,7 @@ function defaultOrAddr (defaultName = '', _address?: AccountId | AccountIndex |
|
||||
|
||||
export default function AccountName ({ children, className, defaultName, label = '', params, style, withShort }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const info = trackStream<DeriveAccountInfo>(api.derive.accounts.info as any, [params]);
|
||||
const info = useStream<DeriveAccountInfo>(api.derive.accounts.info as any, [params]);
|
||||
const [name, setName] = useState(defaultOrAddr(defaultName, params));
|
||||
|
||||
useEffect((): void => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user