From 7d4b3a71dc2a6bcfdae0370d3bb0013aba1bc144 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 30 Jul 2020 16:11:01 +0200 Subject: [PATCH] De-dupe nominations, favor different (#3292) --- .../page-staking/src/Actions/partials/Nominate.tsx | 12 +++++++++++- packages/react-components/src/AccountName.tsx | 10 ++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/page-staking/src/Actions/partials/Nominate.tsx b/packages/page-staking/src/Actions/partials/Nominate.tsx index 25b025eb26..1e7fa16554 100644 --- a/packages/page-staking/src/Actions/partials/Nominate.tsx +++ b/packages/page-staking/src/Actions/partials/Nominate.tsx @@ -8,6 +8,7 @@ import { SortedTargets } from '../../types'; import React, { useCallback, useEffect, useState } from 'react'; import styled from 'styled-components'; import { AddressMini, InputAddress, InputAddressMulti, Modal, Static, Toggle } from '@polkadot/react-components'; +import { getParentAccount } from '@polkadot/react-components/AccountName'; import { useApi, useFavorites } from '@polkadot/react-hooks'; import { MAX_NOMINATIONS, MAX_PAYOUTS, STORE_FAVS_BASE } from '../../constants'; @@ -31,10 +32,19 @@ interface Selected { } function autoPick (targets: SortedTargets): string[] { + const parents: string[] = []; + return (targets.validators || []).reduce((result: string[], { isElected, isFavorite, key, numNominators, rewardPayout }): string[] => { if (result.length < MAX_NOMINATIONS) { if (numNominators && (numNominators < MAX_PAYOUTS) && (isElected || isFavorite) && !rewardPayout.isZero()) { - result.push(key); + const parent = getParentAccount(key); + + if (isFavorite || !parent || (!parents.includes(parent) && !parents.includes(key))) { + parents.push(key); + parent && parents.push(parent); + + result.push(key); + } } } diff --git a/packages/react-components/src/AccountName.tsx b/packages/react-components/src/AccountName.tsx index 504b467984..daa2ce3e45 100644 --- a/packages/react-components/src/AccountName.tsx +++ b/packages/react-components/src/AccountName.tsx @@ -38,6 +38,12 @@ const KNOWN: [AccountId, string][] = [ const displayCache = new Map(); const indexCache = new Map(); +const parentCache = new Map(); + +export function getParentAccount (value: string): string | undefined { + return parentCache.get(value); +} + function defaultOrAddr (defaultName = '', _address: AccountId | AccountIndex | Address | string | Uint8Array, _accountIndex?: AccountIndex | null): [React.ReactNode, boolean, boolean, boolean] { const known = KNOWN.find(([known]) => known.eq(_address)); @@ -136,6 +142,10 @@ function AccountName ({ children, className = '', defaultName, label, noLookup, const { accountId, accountIndex, identity, nickname } = info || {}; const cacheAddr = (accountId || value || '').toString(); + if (identity?.parent) { + parentCache.set(cacheAddr, identity.parent.toString()); + } + if (isFunction(api.query.identity?.identityOf)) { setName(() => identity?.display