/* eslint-disable @typescript-eslint/camelcase */ // Copyright 2017-2019 @polkadot/react-components authors & contributors // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. import { DerivedBalances, DerivedStaking } from '@polkadot/api-derive/types'; import { ValidatorPrefs0to145 } from '@polkadot/types/interfaces'; import { BareProps, I18nProps } from './types'; import BN from 'bn.js'; import React from 'react'; import styled from 'styled-components'; import { formatBalance, formatNumber } from '@polkadot/util'; import { Icon, Tooltip, TxButton } from '@polkadot/react-components'; import { withCalls, withMulti } from '@polkadot/react-api'; import CryptoType from './CryptoType'; import Label from './Label'; import translate from './translate'; // true to display, or (for bonded) provided values [own, ...all extras] export interface BalanceActiveType { available?: boolean; bonded?: boolean | BN[]; locked?: boolean; redeemable?: boolean; reserved?: boolean; total?: boolean; unlocking?: boolean; } export interface CryptoActiveType { crypto?: boolean; nonce?: boolean; } export interface ValidatorPrefsType { unstakeThreshold?: boolean; validatorPayment?: boolean; } interface Props extends BareProps, I18nProps { address: string; balances_all?: DerivedBalances; children?: React.ReactNode; staking_info?: DerivedStaking; withBalance?: boolean | BalanceActiveType; withExtended?: boolean | CryptoActiveType; withHexSessionId: string | null; withRewardDestination?: boolean; withValidatorPrefs?: boolean | ValidatorPrefsType; } const DEFAULT_BALANCES: BalanceActiveType = { available: true, bonded: true, locked: true, redeemable: true, reserved: true, total: true, unlocking: true }; const DEFAULT_EXTENDED = { crypto: true, nonce: true }; const DEFAULT_PREFS = { unstakeThreshold: true, validatorPayment: true }; // calculates the bonded, first being the own, the second being nominated function calcBonded (staking_info?: DerivedStaking, bonded?: boolean | BN[]): [BN, BN[]] { let other: BN[] = []; let own = new BN(0); if (Array.isArray(bonded)) { other = bonded .filter((_, index): boolean => index !== 0) .filter((value): boolean => value.gtn(0)); own = bonded[0]; } else if (staking_info && staking_info.stakingLedger && staking_info.accountId.eq(staking_info.stashId)) { own = staking_info.stakingLedger.active.unwrap(); } return [own, other]; } function renderExtended ({ balances_all, t, address, withExtended }: Props): React.ReactNode { const extendedDisplay = withExtended === true ? DEFAULT_EXTENDED : withExtended || undefined; if (!extendedDisplay || !balances_all) { return null; } return (
{extendedDisplay.nonce && ( <>
); } function renderUnlocking ({ staking_info, t }: Props): React.ReactNode { if (!staking_info || !staking_info.unlocking || !staking_info.unlocking.length) { return null; } const total = staking_info.unlocking.reduce((total, { value }): BN => total.add(value), new BN(0)); if (total.eqn(0)) { return null; } return (
{formatBalance(total)} (
{t('{{value}}, {{remaining}} blocks left', { replace: { remaining: formatNumber(remainingBlocks), value: formatBalance(value) } })}
))} trigger='unlocking-trigger' />
); } function renderValidatorPrefs ({ staking_info, t, withValidatorPrefs = false }: Props): React.ReactNode { const validatorPrefsDisplay = withValidatorPrefs === true ? DEFAULT_PREFS : withValidatorPrefs; if (!validatorPrefsDisplay || !staking_info || !staking_info.validatorPrefs) { return null; } return ( <>
{validatorPrefsDisplay.unstakeThreshold && (staking_info.validatorPrefs as ValidatorPrefs0to145).unstakeThreshold && ( <>