Adjust default (non-locked) vote size (#8719)
This commit is contained in:
parent
ab2acb92e7
commit
8123d65969
@ -95,7 +95,6 @@ function Vote ({ electionsInfo }: Props): React.ReactElement<Props> | null {
|
||||
<Modal.Columns hint={t<string>('The value associated with this vote. The amount will be locked (not available for transfer) and used in all subsequent elections.')}>
|
||||
<VoteValue
|
||||
accountId={accountId}
|
||||
isCouncil
|
||||
onChange={setVoteValue}
|
||||
/>
|
||||
</Modal.Columns>
|
||||
|
||||
@ -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<Props> | null {
|
||||
function VoteValue ({ accountId, autoFocus, label, noDefault, onChange }: Props): React.ReactElement<Props> | null {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const allBalances = useCall<DeriveBalancesAll>(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<string>('vote value')}
|
||||
labelExtra={
|
||||
<BalanceVoting
|
||||
isCouncil={isCouncil}
|
||||
label={<label>{t<string>('voting balance')}</label>}
|
||||
params={accountId}
|
||||
/>
|
||||
|
||||
@ -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<Props> {
|
||||
function BalanceVoting ({ children, className = '', label, params }: Props): React.ReactElement<Props> {
|
||||
const { api } = useApi();
|
||||
const allBalances = useCall<DeriveBalancesAll>(api.derive.balances?.all, [params]);
|
||||
|
||||
@ -26,10 +25,7 @@ function BalanceVoting ({ children, className = '', isCouncil, label, params }:
|
||||
<FormatBalance
|
||||
className={className}
|
||||
label={label}
|
||||
value={isCouncil
|
||||
? allBalances?.votingBalance.add(allBalances?.reservedBalance)
|
||||
: allBalances?.votingBalance
|
||||
}
|
||||
value={allBalances?.votingBalance}
|
||||
>
|
||||
{children}
|
||||
</FormatBalance>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user