From 8123d65969d4c278347154ea28316e86d968c00d Mon Sep 17 00:00:00 2001 From: Jaco Date: Sat, 7 Jan 2023 13:13:55 +0200 Subject: [PATCH] Adjust default (non-locked) vote size (#8719) --- packages/page-council/src/Overview/Vote.tsx | 1 - packages/react-components/src/VoteValue.tsx | 40 ++++++++++++--------- packages/react-query/src/BalanceVoting.tsx | 8 ++--- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/page-council/src/Overview/Vote.tsx b/packages/page-council/src/Overview/Vote.tsx index a023d2dbd1..c12a711fa8 100644 --- a/packages/page-council/src/Overview/Vote.tsx +++ b/packages/page-council/src/Overview/Vote.tsx @@ -95,7 +95,6 @@ function Vote ({ electionsInfo }: Props): React.ReactElement | null { ('The value associated with this vote. The amount will be locked (not available for transfer) and used in all subsequent elections.')}> diff --git a/packages/react-components/src/VoteValue.tsx b/packages/react-components/src/VoteValue.tsx index 207a60f796..08ede412b5 100644 --- a/packages/react-components/src/VoteValue.tsx +++ b/packages/react-components/src/VoteValue.tsx @@ -8,7 +8,7 @@ import React, { useCallback, useEffect, useState } from 'react'; import { useApi, useCall } from '@polkadot/react-hooks'; import { BalanceVoting } from '@polkadot/react-query'; -import { BN_ZERO, bnMin } from '@polkadot/util'; +import { BN_ZERO } from '@polkadot/util'; import InputBalance from './InputBalance'; import { useTranslation } from './translate'; @@ -31,7 +31,7 @@ interface ValueState { const LOCKS_ORDERED = ['pyconvot', 'democrac', 'phrelect']; -function getValues (selectedId: string | null | undefined, isCouncil: boolean | undefined, noDefault: boolean | undefined, allBalances: DeriveBalancesAll, existential: BN): ValueState { +function getValues (selectedId: string | null | undefined, noDefault: boolean | undefined, allBalances: DeriveBalancesAll, existential: BN): ValueState { const sortedLocks = allBalances.lockedBreakdown // first sort by amount, so greatest value first .sort((a, b) => @@ -55,17 +55,24 @@ function getValues (selectedId: string | null | undefined, isCouncil: boolean | }) .map(({ amount }) => amount); - const value = sortedLocks.length - ? bnMin(sortedLocks[0], allBalances.lockedBalance) - : allBalances.lockedBalance; - const maxValue = allBalances.votingBalance.add(isCouncil ? allBalances.reservedBalance : BN_ZERO); - const defaultValue = noDefault - ? BN_ZERO - : value.isZero() - ? maxValue.gt(existential) - ? maxValue.sub(existential) - : BN_ZERO - : value; + const maxValue = allBalances.votingBalance; + let defaultValue: BN = sortedLocks[0] || allBalances.lockedBalance; + + if (noDefault) { + defaultValue = BN_ZERO; + } else if (defaultValue.isZero()) { + // NOTE As of now (7 Jan 2023) taking the max and subtracting existential is still too high + // (on Kusama) where the tx fees for a conviction vote is more than the existential. So try to + // adapt to get some sane default starting value + let withoutExist = maxValue.sub(existential); + + for (let i = 0; i < 3; i++) { + if (withoutExist.gt(existential)) { + defaultValue = withoutExist; + withoutExist = withoutExist.sub(existential); + } + } + } return { defaultValue, @@ -75,7 +82,7 @@ function getValues (selectedId: string | null | undefined, isCouncil: boolean | }; } -function VoteValue ({ accountId, autoFocus, isCouncil, label, noDefault, onChange }: Props): React.ReactElement | null { +function VoteValue ({ accountId, autoFocus, label, noDefault, onChange }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const allBalances = useCall(api.derive.balances?.all, [accountId]); @@ -85,10 +92,10 @@ function VoteValue ({ accountId, autoFocus, isCouncil, label, noDefault, onChang // if the set accountId changes and the new balances is for that id, set it allBalances && allBalances.accountId.eq(accountId) && setValue((state) => state.selectedId !== accountId - ? getValues(accountId, isCouncil, noDefault, allBalances, api.consts.balances.existentialDeposit) + ? getValues(accountId, noDefault, allBalances, api.consts.balances.existentialDeposit) : state ); - }, [allBalances, accountId, api, isCouncil, noDefault]); + }, [allBalances, accountId, api, noDefault]); // only do onChange to parent when the BN value comes in, not our formatted version useEffect((): void => { @@ -120,7 +127,6 @@ function VoteValue ({ accountId, autoFocus, isCouncil, label, noDefault, onChang label={label || t('vote value')} labelExtra={ {t('voting balance')}} params={accountId} /> diff --git a/packages/react-query/src/BalanceVoting.tsx b/packages/react-query/src/BalanceVoting.tsx index a7125f7a0d..6aa568cd93 100644 --- a/packages/react-query/src/BalanceVoting.tsx +++ b/packages/react-query/src/BalanceVoting.tsx @@ -13,12 +13,11 @@ import FormatBalance from './FormatBalance'; interface Props { children?: React.ReactNode; className?: string; - isCouncil?: boolean; label?: React.ReactNode; params?: AccountId | AccountIndex | Address | string | Uint8Array | null; } -function BalanceVoting ({ children, className = '', isCouncil, label, params }: Props): React.ReactElement { +function BalanceVoting ({ children, className = '', label, params }: Props): React.ReactElement { const { api } = useApi(); const allBalances = useCall(api.derive.balances?.all, [params]); @@ -26,10 +25,7 @@ function BalanceVoting ({ children, className = '', isCouncil, label, params }: {children}