diff --git a/package.json b/package.json index 974fb12e08..aaa729d38d 100644 --- a/package.json +++ b/package.json @@ -8,12 +8,12 @@ "packages/*" ], "resolutions": { - "@polkadot/api": "^1.12.0-beta.9", - "@polkadot/api-contract": "^1.12.0-beta.9", - "@polkadot/keyring": "^2.9.0-beta.1", - "@polkadot/types": "^1.12.0-beta.9", - "@polkadot/util": "^2.9.0-beta.1", - "@polkadot/util-crypto": "^2.9.0-beta.1", + "@polkadot/api": "^1.12.0-beta.11", + "@polkadot/api-contract": "^1.12.0-beta.11", + "@polkadot/keyring": "^2.9.0-beta.3", + "@polkadot/types": "^1.12.0-beta.11", + "@polkadot/util": "^2.9.0-beta.3", + "@polkadot/util-crypto": "^2.9.0-beta.3", "babel-core": "^7.0.0-bridge.0", "typescript": "^3.8.3" }, diff --git a/packages/page-contracts/package.json b/packages/page-contracts/package.json index fd72a8bfe0..adb05b3df8 100644 --- a/packages/page-contracts/package.json +++ b/packages/page-contracts/package.json @@ -12,6 +12,6 @@ "license": "Apache-2.0", "dependencies": { "@babel/runtime": "^7.9.2", - "@polkadot/api-contract": "^1.12.0-beta.9" + "@polkadot/api-contract": "^1.12.0-beta.11" } } diff --git a/packages/page-democracy/src/Overview/Referendum.tsx b/packages/page-democracy/src/Overview/Referendum.tsx index e59c70b8fa..83efbd310b 100644 --- a/packages/page-democracy/src/Overview/Referendum.tsx +++ b/packages/page-democracy/src/Overview/Referendum.tsx @@ -7,7 +7,7 @@ import { BlockNumber } from '@polkadot/types/interfaces'; import React, { useMemo } from 'react'; import styled from 'styled-components'; -import { Button, LinkExternal, Tag } from '@polkadot/react-components'; +import { Badge, Button, Icon, LinkExternal } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; import { BlockToTime } from '@polkadot/react-query'; import { formatNumber, isBoolean } from '@polkadot/util'; @@ -72,12 +72,13 @@ function Referendum ({ className, value: { allAye, allNay, image, imageHash, ind total={votedNay} votes={allNay} /> - + {isBoolean(isPassing) && ( - } + isTooltip + type={isPassing ? 'green' : 'brown'} /> )} diff --git a/packages/page-staking/src/Actions/Account/index.tsx b/packages/page-staking/src/Actions/Account/index.tsx index 2edae530bf..6f97f439b1 100644 --- a/packages/page-staking/src/Actions/Account/index.tsx +++ b/packages/page-staking/src/Actions/Account/index.tsx @@ -3,15 +3,13 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { DeriveBalancesAll, DeriveStakingAccount } from '@polkadot/api-derive/types'; -import { AccountId, EraIndex, Exposure, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces'; -import { Codec, ITuple } from '@polkadot/types/types'; +import { EraIndex } from '@polkadot/types/interfaces'; +import { StakerState } from '@polkadot/react-hooks/types'; -import BN from 'bn.js'; import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import { AddressInfo, AddressMini, AddressSmall, Button, Expander, Menu, Popup, StakingBonded, StakingRedeemable, StakingUnbonding, TxButton } from '@polkadot/react-components'; -import { useAccounts, useApi, useCall, useToggle } from '@polkadot/react-hooks'; -import { u8aConcat, u8aToHex } from '@polkadot/util'; +import { useApi, useCall, useToggle } from '@polkadot/react-hooks'; import { useTranslation } from '../../translate'; import useInactives from '../useInactives'; @@ -24,82 +22,21 @@ import SetSessionKey from './SetSessionKey'; import Unbond from './Unbond'; import Validate from './Validate'; -type ValidatorInfo = ITuple<[ValidatorPrefs, Codec]> | ValidatorPrefs; - interface Props { activeEra?: EraIndex; - allStashes?: string[]; className?: string; isDisabled?: boolean; - isOwnStash: boolean; + info: StakerState; next?: string[]; - onUpdateType: (stashId: string, type: 'validator' | 'nominator' | 'started' | 'other') => void; - setBonded: (account: string, bonded: BN) => void; stashId: string; validators?: string[]; } -interface StakeState { - controllerId: string | null; - destination?: string; - destinationId: number; - exposure?: Exposure; - hexSessionIdNext: string | null; - hexSessionIdQueue: string | null; - isLoading: boolean; - isOwnController: boolean; - isStashNominating: boolean; - isStashValidating: boolean; - nominating?: string[]; - sessionIds: string[]; - stakingLedger?: StakingLedger; - validatorPrefs?: ValidatorPrefs; -} - -function toIdString (id?: AccountId | null): string | null { - return id - ? id.toString() - : null; -} - -function getStakeState (allAccounts: string[], allStashes: string[] | undefined, { controllerId: _controllerId, exposure, nextSessionIds, nominators, rewardDestination, sessionIds, stakingLedger, validatorPrefs }: DeriveStakingAccount, stashId: string, validateInfo: ValidatorInfo): StakeState { - const isStashNominating = !!(nominators?.length); - const isStashValidating = !(Array.isArray(validateInfo) ? validateInfo[1].isEmpty : validateInfo.isEmpty) || !!allStashes?.includes(stashId); - const nextConcat = u8aConcat(...nextSessionIds.map((id): Uint8Array => id.toU8a())); - const currConcat = u8aConcat(...sessionIds.map((id): Uint8Array => id.toU8a())); - const controllerId = toIdString(_controllerId); - - return { - controllerId, - destination: rewardDestination?.toString().toLowerCase(), - destinationId: rewardDestination?.toNumber() || 0, - exposure, - hexSessionIdNext: u8aToHex(nextConcat, 48), - hexSessionIdQueue: u8aToHex(currConcat.length ? currConcat : nextConcat, 48), - isLoading: false, - isOwnController: allAccounts.includes(controllerId || ''), - isStashNominating, - isStashValidating, - // we assume that all ids are non-null - nominating: nominators?.map(toIdString) as string[], - sessionIds: ( - nextSessionIds.length - ? nextSessionIds - : sessionIds - ).map(toIdString) as string[], - stakingLedger, - validatorPrefs - }; -} - -function Account ({ allStashes, className, isDisabled, isOwnStash, next, onUpdateType, setBonded, stashId, validators }: Props): React.ReactElement { +function Account ({ className, info: { controllerId, destination, destinationId, hexSessionIdNext, hexSessionIdQueue, isLoading, isOwnController, isOwnStash, isStashNominating, isStashValidating, nominating, sessionIds, stakingLedger, stashId }, isDisabled, next, validators }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); - const { allAccounts } = useAccounts(); - const validateInfo = useCall(api.query.staking.validators, [stashId]); const balancesAll = useCall(api.derive.balances.all, [stashId]); const stakingAccount = useCall(api.derive.staking.account, [stashId]); - const [{ controllerId, destination, destinationId, hexSessionIdNext, hexSessionIdQueue, isLoading, isOwnController, isStashNominating, isStashValidating, nominating, sessionIds, stakingLedger }, setStakeState] = useState({ controllerId: null, destinationId: 0, hexSessionIdNext: null, hexSessionIdQueue: null, isLoading: true, isOwnController: false, isStashNominating: false, isStashValidating: false, sessionIds: [] }); const [activeNoms, setActiveNoms] = useState([]); const inactiveNoms = useInactives(stashId, nominating); const [isBondExtraOpen, toggleBondExtra] = useToggle(); @@ -112,26 +49,6 @@ function Account ({ allStashes, className, isDisabled, isOwnStash, next, onUpdat const [isUnbondOpen, toggleUnbond] = useToggle(); const [isValidateOpen, toggleValidate] = useToggle(); - useEffect((): void => { - if (stakingAccount && validateInfo) { - const state = getStakeState(allAccounts, allStashes, stakingAccount, stashId, validateInfo); - - if (state.stakingLedger) { - setBonded(stashId, state.stakingLedger.total.unwrap()); - } - - setStakeState(state); - onUpdateType( - stashId, - state.isStashValidating - ? 'validator' - : state.isStashNominating - ? 'nominator' - : 'other' - ); - } - }, [allAccounts, allStashes, onUpdateType, stakingAccount, setBonded, stashId, validateInfo]); - useEffect((): void => { nominating && setActiveNoms( nominating.filter((id): boolean => !inactiveNoms.includes(id)) diff --git a/packages/page-staking/src/Actions/index.tsx b/packages/page-staking/src/Actions/index.tsx index 0208e4ced8..03d930c305 100644 --- a/packages/page-staking/src/Actions/index.tsx +++ b/packages/page-staking/src/Actions/index.tsx @@ -3,11 +3,12 @@ // of the Apache-2.0 license. See the LICENSE file for details. import { ActiveEraInfo, EraIndex } from '@polkadot/types/interfaces'; +import { StakerState } from '@polkadot/react-hooks/types'; import BN from 'bn.js'; -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import { Table } from '@polkadot/react-components'; -import { useCall, useApi, useOwnStashes } from '@polkadot/react-hooks'; +import { useCall, useApi } from '@polkadot/react-hooks'; import { FormatBalance } from '@polkadot/react-query'; import { Option } from '@polkadot/types'; @@ -17,60 +18,38 @@ import Account from './Account'; import NewStake from './NewStake'; interface Props { - allStashes?: string[]; className?: string; isInElection?: boolean; + ownStashes?: StakerState[]; next?: string[]; validators?: string[]; } -interface Balances { - accounts: Record; +interface State { bondedTotal?: BN; + foundStashes?: StakerState[]; } -function Actions ({ allStashes, className, isInElection, next, validators }: Props): React.ReactElement { +function Actions ({ className, isInElection, next, ownStashes, validators }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const activeEra = useCall(api.query.staking?.activeEra, [], { transform: (activeEra: Option) => activeEra.unwrapOr({ index: undefined }).index }); - const ownStashes = useOwnStashes(); - const [{ bondedTotal }, setBonded] = useState({ accounts: {} }); - const [foundStashes, setFoundStashes] = useState<[string, boolean][] | null>(null); - const [stashTypes, setStashTypes] = useState>({}); + const [{ bondedTotal, foundStashes }, setState] = useState({}); useEffect((): void => { - ownStashes && setFoundStashes( - ownStashes.sort((a, b) => (stashTypes[a[0]] || 99) - (stashTypes[b[0]] || 99)) - ); - }, [ownStashes, stashTypes]); - - const _setBonded = useCallback( - (account: string, bonded: BN) => - setBonded(({ accounts }: Balances): Balances => { - accounts[account] = bonded; - - return { - accounts, - bondedTotal: Object.values(accounts).reduce((total: BN, value: BN) => total.add(value), new BN(0)) - }; - }), - [] - ); - - const _onUpdateType = useCallback( - (stashId: string, type: 'validator' | 'nominator' | 'started' | 'other'): void => - setStashTypes((stashTypes: Record) => ({ - ...stashTypes, - [stashId]: type === 'validator' - ? 1 - : type === 'nominator' - ? 5 - : 9 - })), - [] - ); + ownStashes && setState({ + bondedTotal: ownStashes.reduce((total: BN, { stakingLedger }) => + stakingLedger + ? total.add(stakingLedger.total.unwrap()) + : total, + new BN(0)), + foundStashes: ownStashes.sort((a, b) => + (a.isStashValidating ? 1 : (a.isStashNominating ? 5 : 99)) - (b.isStashValidating ? 1 : (b.isStashNominating ? 5 : 99)) + ) + }); + }, [ownStashes]); const header = useMemo(() => [ [t('stashes'), 'start'], @@ -95,21 +74,17 @@ function Actions ({ allStashes, className, isInElection, next, validators }: Pro - {foundStashes?.map(([stashId, isOwnStash]): React.ReactNode => ( + {foundStashes?.map((info): React.ReactNode => ( ))} diff --git a/packages/page-staking/src/Targets/Nominate.tsx b/packages/page-staking/src/Targets/Nominate.tsx new file mode 100644 index 0000000000..41500cafab --- /dev/null +++ b/packages/page-staking/src/Targets/Nominate.tsx @@ -0,0 +1,95 @@ +// Copyright 2017-2020 @polkadot/app-staking 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 { StakerState } from '@polkadot/react-hooks/types'; + +import React, { useCallback, useEffect, useState } from 'react'; +import { AddressMini, Button, InputAddress, Modal, Static, TxButton } from '@polkadot/react-components'; +import { useToggle } from '@polkadot/react-hooks'; + +import { useTranslation } from '../translate'; + +interface Props { + className?: string; + ownNominators?: StakerState[]; + targets: string[]; +} + +function Nominate ({ ownNominators, targets }: Props): React.ReactElement { + const { t } = useTranslation(); + const [ids, setIds] = useState<{ controllerId?: string | null; stashId: string } | null>(null); + const [filter, setFilter] = useState([]); + const [isOpen, toggleOpen] = useToggle(); + + useEffect((): void => { + ownNominators && setFilter( + ownNominators.map(({ stashId }) => stashId) + ); + }, [ownNominators]); + + const _onChangeStash = useCallback( + (accountId?: string | null): void => { + const acc = ownNominators?.find(({ stashId }) => stashId === accountId); + + setIds( + acc + ? { controllerId: acc.controllerId, stashId: acc.stashId } + : null + ); + }, + [ownNominators] + ); + + return ( + <> + +
+ {(canSelect || isSelected) && ( + + )} + a.isFavorite === b.isFavorite + ? 0 + : (a.isFavorite ? -1 : 1) + ); } function extractInfo (allAccounts: string[], amount: BN = new BN(0), electedInfo: DeriveStakingElected, favorites: string[], lastReward = new BN(1)): AllInfo { @@ -157,7 +166,7 @@ function extractInfo (allAccounts: string[], amount: BN = new BN(0), electedInfo } function sort (sortBy: SortBy, sortFromMax: boolean, validators: ValidatorInfo[]): ValidatorInfo[] { - return validators + return [...validators] .sort((a, b): number => sortFromMax ? a[sortBy] - b[sortBy] : b[sortBy] - a[sortBy] @@ -168,12 +177,12 @@ function sort (sortBy: SortBy, sortFromMax: boolean, validators: ValidatorInfo[] ); } -function Targets ({ className }: Props): React.ReactElement { +function Targets ({ className, ownStashes }: Props): React.ReactElement { const { t } = useTranslation(); const { api } = useApi(); const { allAccounts } = useAccounts(); + const ownNominators = useOwnNominators(ownStashes); const lastEra = useCall(api.derive.session.indexes, [], { - defaultValue: new BN(0), transform: ({ activeEra }: DeriveSessionIndexes) => activeEra.gtn(0) ? activeEra.subn(1) : new BN(0) }) || new BN(0); const lastReward = useCall(api.query.staking.erasValidatorReward, [lastEra], { @@ -182,6 +191,7 @@ function Targets ({ className }: Props): React.ReactElement { const [_amount, setAmount] = useState(new BN(1_000)); const electedInfo = useCall(api.derive.staking.electedInfo, []); const [favorites, toggleFavorite] = useFavorites(STORE_FAVS_BASE); + const [selected, setSelected] = useState([]); const [{ nominators, sorted, totalStaked, validators }, setWorkable] = useState({ nominators: [], validators: [] }); const [{ sortBy, sortFromMax }, setSortBy] = useState<{ sortBy: SortBy; sortFromMax: boolean }>({ sortBy: 'rankOverall', sortFromMax: true }); const amount = useDebounce(_amount); @@ -201,6 +211,24 @@ function Targets ({ className }: Props): React.ReactElement { [] ); + const _toggleSelected = useCallback( + (address: string) => setSelected( + selected.includes(address) + ? selected.filter((accountId): boolean => address !== accountId) + : [...selected, address] + ), + [selected] + ); + + const _selectProfitable = useCallback( + (): void => setSelected( + validators + .filter((_, index) => index < MAX_NOMINATIONS) + .map(({ key }) => key) + ), + [validators] + ); + useEffect((): void => { if (electedInfo) { const { nominators, totalStaked, validators } = extractInfo(allAccounts, amount, electedInfo, favorites, lastReward); @@ -224,7 +252,7 @@ function Targets ({ className }: Props): React.ReactElement { const filter = useMemo(() => ( { numValidators={validators.length} totalStaked={totalStaked} /> + +