Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Identity for delegation #3140

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/renderer/domains/identity/model/identity/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { identityPallet } from '@/shared/pallet/identity';
import { type AccountId } from '@/shared/polkadotjs-schemas';
import { networkModel } from '@/entities/network';

import { identityService } from './service';
import { type AccountIdentity } from './types';

type IdentityData = Record<AccountId, AccountIdentity>;
Expand Down Expand Up @@ -79,14 +78,11 @@ const request = attach({
effect: requestIdentity,
source: { apis: $apis, chains: $chains },
mapParams: ({ chainId, accounts }: RequestParams, { apis, chains }) => {
const identityChain = identityService.findIdentityChain(chains, chainId);
if (nullable(identityChain)) {
throw new Error(`Chain path from ${chainId} is broken, trace chain.parentId fields in config.`);
}
const identityChainId = chains[chainId]?.additional?.identityChain ?? chainId;

const api = apis[identityChain.chainId];
const api = apis[identityChainId];
if (nullable(api)) {
throw new Error(`ApiPromise for chain ${identityChain.chainId} not found`);
throw new Error(`ApiPromise for chain ${identityChainId} not found`);
}

return {
Expand Down
38 changes: 0 additions & 38 deletions src/renderer/domains/identity/model/identity/service.ts

This file was deleted.

31 changes: 30 additions & 1 deletion src/renderer/features/governance/aggregates/delegateRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { readonly } from 'patronum';

import { type DelegateAccount, delegationService } from '@/shared/api/governance';
import { type Chain } from '@/shared/core';
import { MONTH, getBlockTimeAgo, nonNullable } from '@/shared/lib/utils';
import { MONTH, getBlockTimeAgo, nonNullable, toAccountId } from '@/shared/lib/utils';
import { type AccountId } from '@/shared/polkadotjs-schemas';
import { identityDomain } from '@/domains/identity';
import { networkSelectorModel } from '../model/networkSelector';

const requestDelegateRegistry = createEvent();
Expand Down Expand Up @@ -63,6 +65,33 @@ sample({
target: $delegateRegistry,
});

sample({
clock: $delegateRegistry,
source: {
chain: networkSelectorModel.$governanceChain,
identity: identityDomain.identity.$list,
},
filter: ({ chain }) => nonNullable(chain),
fn: ({ chain, identity }, delegates) => {
const accounts = new Set<AccountId>();

for (const delegate of delegates) {
if (nonNullable(delegate.name)) continue;

const accountId = toAccountId(delegate.address ?? delegate.accountId);
if (nonNullable(identity[chain!.chainId]?.[accountId])) continue;

accounts.add(accountId);
}

return {
chainId: chain!.chainId,
accounts: Array.from(accounts),
};
},
target: identityDomain.identity.request,
});

export const delegateRegistryAggregate = {
$delegateRegistry: readonly($delegateRegistry),
$isRegistryLoading: requestDelegateRegistryFx.pending,
Expand Down
35 changes: 33 additions & 2 deletions src/renderer/features/governance/aggregates/voting.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { combine, createEvent, sample } from 'effector';

import { type Address, type TrackId, type VotingMap } from '@/shared/core';
import { nonNullable, nullable } from '@/shared/lib/utils';
import { votingModel } from '@/entities/governance';
import { nonNullable, nullable, toAccountId } from '@/shared/lib/utils';
import { type AccountId } from '@/shared/polkadotjs-schemas';
import { identityDomain } from '@/domains/identity';
import { votingModel, votingService } from '@/entities/governance';
import { accountUtils, walletModel, walletUtils } from '@/entities/wallet';
import { networkSelectorModel } from '../model/networkSelector';

Expand Down Expand Up @@ -89,6 +91,35 @@ sample({
target: requestVoting,
});

sample({
clock: $activeWalletVotes,
source: {
chain: networkSelectorModel.$governanceChain,
identity: identityDomain.identity.$list,
},
filter: ({ chain }) => nonNullable(chain),
fn: ({ chain, identity }, activeVotes) => {
const accounts = new Set<AccountId>();

for (const voteList of Object.values(activeVotes)) {
for (const vote of Object.values(voteList)) {
if (!votingService.isDelegating(vote)) continue;

const accountId = toAccountId(vote.target);
if (nonNullable(identity[chain!.chainId]?.[accountId])) continue;

accounts.add(accountId);
}
}

return {
chainId: chain!.chainId,
accounts: Array.from(accounts),
};
},
target: identityDomain.identity.request,
});

export const votingAggregate = {
$activeWalletVotes,
$possibleAccountsForVoting,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ export const DelegateIcon = ({ delegate, className }: Props) => {
if (!delegate.name) return <Identicon address={delegate.accountId} size={46} />;

if (isDefaultImage(delegate.image)) {
<div
className={cnTw(
'flex h-11.5 w-11.5 items-center justify-center rounded-full',
delegate.isOrganization ? 'bg-badge-orange-background-default' : 'bg-badge-background',
)}
>
{delegate.isOrganization ? (
<Icon className="text-icon-warning" name="organization" />
) : (
<Icon className="text-icon-accent" name="individual" />
)}
</div>;
return (
<div
className={cnTw(
'flex h-11.5 w-11.5 items-center justify-center rounded-full',
delegate.isOrganization ? 'bg-badge-orange-background-default' : 'bg-badge-background',
)}
>
{delegate.isOrganization ? (
<Icon className="text-icon-warning" name="organization" />
) : (
<Icon className="text-icon-accent" name="individual" />
)}
</div>
);
}

return <img src={delegate.image} alt={delegate.name} className={cnTw('h-11.5 w-11.5 rounded-full', className)} />;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useUnit } from 'effector-react';
import { useStoreMap, useUnit } from 'effector-react';

import { type DelegateAccount } from '@/shared/api/governance';
import { cnTw, nullable } from '@/shared/lib/utils';
import { cnTw, nullable, toAccountId } from '@/shared/lib/utils';
import { HeadlineText } from '@/shared/ui';
import { Account } from '@/shared/ui-entities';
import { identityDomain } from '@/domains/identity';
import { networkSelectorModel } from '../../model/networkSelector';

type Props = {
Expand All @@ -13,11 +14,22 @@ type Props = {

export const DelegateTitle = ({ delegate, className }: Props) => {
const chain = useUnit(networkSelectorModel.$governanceChain);

const delegateName = useStoreMap({
store: identityDomain.identity.$list,
keys: [chain?.chainId, delegate.accountId, delegate.address],
fn: (identity, [chainId, accountId, address]) => {
if (nullable(chainId)) return null;

return delegate.name ?? identity[chainId]?.[toAccountId(address ?? accountId)]?.name ?? null;
},
});

if (nullable(chain) || nullable(delegate.accountId)) return null;

return (
<HeadlineText className={cnTw('w-full', className)}>
<Account hideAddress hideIcon title={delegate.name} accountId={delegate.accountId} chain={chain} />
<Account hideAddress hideIcon title={delegateName || undefined} accountId={delegate.accountId} chain={chain} />
</HeadlineText>
);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useUnit } from 'effector-react';

import { useI18n } from '@/shared/i18n';
import { toAccountId } from '@/shared/lib/utils';
import { Button, Loader } from '@/shared/ui';
import { Modal, SearchInput } from '@/shared/ui-kit';
import { OperationTitle } from '@/entities/chain';
Expand Down Expand Up @@ -45,16 +46,20 @@ export const CurrentDelegationModal = () => {

<div className="scrollbar-stable flex flex-1 flex-col items-center overflow-y-auto">
<ul className="flex w-[400px] flex-col gap-y-2">
{delegationList.map((delegate) => (
<button key={delegate.accountId} onClick={() => delegateDetailsModel.events.flowStarted(delegate)}>
<DelegationCard
key={delegate.accountId}
delegate={delegate}
votes={Object.values(activeDelegations[delegate.accountId] || {})}
tracks={[...new Set(Object.values(activeTracks[delegate.accountId] || {}).flat())]}
/>
</button>
))}
{delegationList.map((delegate) => {
const accountId = toAccountId(delegate.address ?? delegate.accountId);

return (
<button key={accountId} onClick={() => delegateDetailsModel.events.flowStarted(delegate)}>
<DelegationCard
key={accountId}
delegate={delegate}
votes={Object.values(activeDelegations[delegate.accountId] || {})}
tracks={[...new Set(Object.values(activeTracks[delegate.accountId] || {}).flat())]}
/>
</button>
);
})}
</ul>
</div>
</>
Expand Down
15 changes: 10 additions & 5 deletions src/renderer/widgets/DelegationModal/components/DelegationList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useUnit } from 'effector-react';

import { type DelegateAccount } from '@/shared/api/governance';
import { useI18n } from '@/shared/i18n';
import { toAccountId } from '@/shared/lib/utils';
import { Button, Loader } from '@/shared/ui';
import { Box, SearchInput, Select } from '@/shared/ui-kit';
import { SortType } from '../common/constants';
Expand Down Expand Up @@ -68,11 +69,15 @@ export const DelegationList = ({ onClick, onAddCustomClick }: Props) => {

<div className="scrollbar-stable flex h-full flex-col items-center overflow-y-auto">
<ul className="flex w-[400px] flex-col gap-y-2">
{delegationList.map((delegate) => (
<button key={delegate.accountId} onClick={() => onClick(delegate)}>
<DelegationCard key={delegate.accountId} delegate={delegate} />
</button>
))}
{delegationList.map((delegate) => {
const accountId = toAccountId(delegate.address ?? delegate.accountId);
tuul-wq marked this conversation as resolved.
Show resolved Hide resolved

return (
<button key={accountId} onClick={() => onClick(delegate)}>
<DelegationCard key={accountId} delegate={delegate} />
</button>
);
})}
</ul>

{delegationList.length === 0 && <EmptyState />}
Expand Down
Loading