Skip to content

Commit

Permalink
fix: multishard voting and etc. (#2429)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Zhuravlev <[email protected]>
  • Loading branch information
johnthecat and Sergey Zhuravlev authored Oct 9, 2024
1 parent 9f703df commit 1318e15
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 42 deletions.
8 changes: 8 additions & 0 deletions src/renderer/features/governance/aggregates/voteHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { votingAssetModel } from '../model/votingAsset';
import { type AggregatedVoteHistory } from '../types/structs';
import { votingPowerSorting } from '../utils/votingPowerSorting';

import { listAggregate } from './list';
import { proposerIdentityAggregate } from './proposerIdentity';

const flow = createGate<{ referendum: Referendum }>();
Expand Down Expand Up @@ -81,6 +82,13 @@ sample({
target: requestVoteHistory,
});

sample({
clock: listAggregate.$referendums,
source: flow.state,
filter: flow.status,
target: requestVoteHistory,
});

export const voteHistoryAggregate = {
$voteHistory,
$isLoading: voteHistoryModel.$isLoading,
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/features/governance/lib/votingListService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const getDecoupledVotesFromVotingHistory = (voting: VoteHistoryRecord) => {
});

res.push({
decision: 'aye',
decision: 'nay',
voter: voting.voter,
votingPower: votingService.calculateVotingPower(voting.vote.nay, conviction),
conviction: votingService.getConvictionMultiplier(conviction),
Expand Down
1 change: 1 addition & 0 deletions src/renderer/pages/Governance/ui/Governance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export const Governance = () => {
nonNullable(network) &&
referendumService.isOngoing(selectedReferendum) && (
<RemoveVotesModal
single
votes={selectedReferendum.voting.votes.map(({ voter, vote }) => ({
vote,
voter,
Expand Down
12 changes: 9 additions & 3 deletions src/renderer/widgets/DelegateModal/model/form-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BN } from '@polkadot/util';
import { BN, BN_ZERO } from '@polkadot/util';
import { combine, createEvent, createStore, restore, sample } from 'effector';
import { createForm } from 'effector-forms';
import { spread } from 'patronum';
Expand All @@ -13,6 +13,7 @@ import {
transferableAmountBN,
} from '@shared/lib/utils';
import { balanceModel, balanceUtils } from '@entities/balance';
import { locksService } from '@entities/governance';
import { networkModel } from '@entities/network';
import { walletModel, walletUtils } from '@entities/wallet';
import { locksAggregate } from '@/features/governance/aggregates/locks';
Expand Down Expand Up @@ -81,13 +82,18 @@ const $accounts = combine(
const address = toAddress(shard.accountId, { prefix: network!.chain.addressPrefix });
const lock = getLocksForAddress(address, trackLocks);

return { account: shard, balance: transferableAmountBN(balance), lock };
return {
account: shard,
balance: transferableAmountBN(balance),
lock,
available: balance ? locksService.getAvailableBalance(balance) : BN_ZERO,
};
});
},
);

const $accountsBalances = $accounts.map((accounts) => {
return accounts.map(({ balance, lock }) => lock.add(balance).toString());
return accounts.map(({ available }) => available.toString());
});

const $delegateForm = createForm<FormParams>({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type ApiPromise } from '@polkadot/api';
import { combine, createEvent, createStore, sample } from 'effector';
import { combine, createEvent, createStore, restore, sample } from 'effector';
import { createGate } from 'effector-react';
import uniq from 'lodash/uniq';

import {
type Account,
Expand Down Expand Up @@ -46,28 +47,23 @@ const flow = createGate<{

const selectAccount = createEvent<Account>();

const $account = createStore<Account | null>(null);
const $pickedAccount = restore(selectAccount, null).reset(flow.close);

const $accounts = combine(walletModel.$activeWallet, flow.state, (wallet, { votes, chain }) => {
if (nullable(wallet) || nullable(chain)) return [];
const $availableAccounts = combine(walletModel.$activeWallet, flow.state, (wallet, { votes, chain }) => {
if (nullable(wallet) || nullable(votes.length) || nullable(chain)) return [];

return walletUtils.getAccountsBy([wallet], (a) => {
if (!accountUtils.isChainAndCryptoMatch(a, chain)) return false;
const accounts = uniq(votes.map((vote) => vote.voter).filter(nonNullable));

return votes.some(({ voter }) => toAddress(a.accountId, { prefix: chain.addressPrefix }) === voter);
});
return accounts.map(
(address) =>
walletUtils.getAccountBy([wallet], (a) => toAddress(a.accountId, { prefix: chain.addressPrefix }) === address)!,
);
});

sample({
clock: $accounts,
filter: $accounts.map((x) => x.length < 2),
fn: (s) => s.at(0) ?? null,
target: $account,
});
const $accounts = combine($availableAccounts, $pickedAccount, (availableAccounts, pickedAccount) => {
if (nonNullable(pickedAccount)) return [pickedAccount];

sample({
clock: selectAccount,
target: $account,
return availableAccounts;
});

const $initiatorWallet = combine($accounts, walletModel.$wallets, (accounts, wallets) => {
Expand Down Expand Up @@ -291,7 +287,7 @@ sample({
sample({
clock: removeVoteConfirmModel.events.submitFinished,
source: {
accounts: $accounts,
accounts: $availableAccounts,
chain: flow.state.map(({ chain }) => chain),
},
fn: ({ accounts, chain }) => {
Expand All @@ -315,19 +311,20 @@ export const removeVotesModalAggregate = {
$initiatorWallet,
$lockPeriods: lockPeriodsModel.$lockPeriods,

$availableAccounts,
$pickedAccount,
$accounts,

$step,
$signatory,
$signatories,
$votesList,

$account,
$accounts,

events: {
txSaved,
setStep,
selectSignatory,
selectAccount,
selectSignatory,
},

gates: {
Expand Down
26 changes: 16 additions & 10 deletions src/renderer/widgets/RemoveVotesModal/ui/RemoveVotesModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import { SignatorySelectModal } from '@/pages/Operations/components/modals/Signa
import { removeVotesModalAggregate } from '../aggregates/removeVotesModal';

type Props = {
/**
* Adds account select in case of multiple votes with different accounts.
*/
single?: boolean;
votes: {
voter: Address;
referendum: ReferendumId;
Expand All @@ -42,7 +46,7 @@ type Props = {
onClose: VoidFunction;
};

export const RemoveVotesModal = ({ votes, chain, asset, api, onClose }: Props) => {
export const RemoveVotesModal = ({ single, votes, chain, asset, api, onClose }: Props) => {
useGate(removeVotesModalAggregate.gates.flow, {
votes,
chain,
Expand Down Expand Up @@ -134,12 +138,14 @@ export const RemoveVotesModal = ({ votes, chain, asset, api, onClose }: Props) =
onCancel={closeModal}
/>

<VoteAccountSelect
asset={asset}
chain={chain}
onSelect={removeVotesModalAggregate.events.selectAccount}
onCancel={closeModal}
/>
{single ? (
<VoteAccountSelect
asset={asset}
chain={chain}
onSelect={removeVotesModalAggregate.events.selectAccount}
onCancel={closeModal}
/>
) : null}
</>
);
};
Expand Down Expand Up @@ -198,9 +204,9 @@ type AccountProps = {
const VoteAccountSelect = ({ asset, chain, onCancel, onSelect }: AccountProps) => {
const { t } = useI18n();

const account = useUnit(removeVotesModalAggregate.$account);
const accounts = useUnit(removeVotesModalAggregate.$accounts);
const shouldPickAccount = nullable(account) && accounts.length > 0;
const account = useUnit(removeVotesModalAggregate.$pickedAccount);
const accounts = useUnit(removeVotesModalAggregate.$availableAccounts);
const shouldPickAccount = nullable(account) && accounts.length > 1;
const [isSelectAccountOpen, setIsSelectAccountOpen] = useState(shouldPickAccount);
const [isSelectAccountClosed, setIsSelectAccountClosed] = useState(false);

Expand Down
11 changes: 5 additions & 6 deletions src/renderer/widgets/VoteModal/aggregates/voteModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type AccountVote, type Address, type BasketTransaction, type OngoingRef
import { Step, isStep, nonNullable, nullable, toAddress } from '@shared/lib/utils';
import { basketModel } from '@entities/basket';
import { votingService } from '@entities/governance';
import { walletModel } from '@entities/wallet';
import {
delegationAggregate,
lockPeriodsModel,
Expand Down Expand Up @@ -244,14 +245,12 @@ sample({
clock: voteConfirmModel.events.submitFinished,
source: {
status: flow.status,
form: voteFormAggregate.transactionForm.form.$values,
wallet: walletModel.$activeWallet,
chain: networkSelectorModel.$governanceChain,
},
filter: ({ chain, status }) => status && nonNullable(chain),
fn: ({ form, chain }) => {
const addresses = [form.signatory, form.account]
.filter(nonNullable)
.map((account) => toAddress(account.accountId, { prefix: chain?.addressPrefix }));
filter: ({ chain, status, wallet }) => status && nonNullable(chain) && nonNullable(wallet),
fn: ({ wallet, chain }) => {
const addresses = wallet!.accounts.map((account) => toAddress(account.accountId, { prefix: chain?.addressPrefix }));

return { addresses };
},
Expand Down

0 comments on commit 1318e15

Please sign in to comment.