De-dupe nominations, favor different (#3292)

This commit is contained in:
Jaco Greeff 2020-07-30 16:11:01 +02:00 committed by GitHub
parent 3f6b5dded1
commit 7d4b3a71dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -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);
}
}
}

View File

@ -38,6 +38,12 @@ const KNOWN: [AccountId, string][] = [
const displayCache = new Map<string, React.ReactNode>();
const indexCache = new Map<string, string>();
const parentCache = new Map<string, string>();
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