Optimize finding loan account (hex-only encoding) (#5524)
This commit is contained in:
parent
e42f8c66c3
commit
09343fba3f
@ -12,7 +12,6 @@ import { AddressMini, Icon, ParaLink, TxButton } from '@polkadot/react-component
|
||||
import { useAccounts, useApi, useEventTrigger, useParaEndpoints } from '@polkadot/react-hooks';
|
||||
import { BlockToTime, FormatBalance } from '@polkadot/react-query';
|
||||
import { formatNumber } from '@polkadot/util';
|
||||
import { encodeAddress } from '@polkadot/util-crypto';
|
||||
|
||||
import { useTranslation } from '../translate';
|
||||
import Contribute from './Contribute';
|
||||
@ -29,26 +28,26 @@ interface Props {
|
||||
|
||||
interface Contributions {
|
||||
contributors: string[];
|
||||
myAccounts: string[];
|
||||
myAccountsHex: string[];
|
||||
}
|
||||
|
||||
const NO_CONTRIB: Contributions = { contributors: [], myAccounts: [] };
|
||||
const NO_CONTRIB: Contributions = { contributors: [], myAccountsHex: [] };
|
||||
|
||||
function extractContributors (allAccounts: string[], keys: StorageKey[], ss58Format?: number): Contributions {
|
||||
const contributors = keys.map((k) => encodeAddress(k, ss58Format));
|
||||
function extractContributors (allAccountsHex: string[], keys: StorageKey[]): Contributions {
|
||||
const contributors = keys.map((k) => k.toHex());
|
||||
|
||||
return {
|
||||
contributors,
|
||||
myAccounts: contributors.filter((c) => allAccounts.includes(c))
|
||||
myAccountsHex: contributors.filter((c) => allAccountsHex.includes(c))
|
||||
};
|
||||
}
|
||||
|
||||
function Fund ({ bestNumber, className, isOdd, isOngoing, leasePeriod, value: { childKey, info: { cap, depositor, end, firstPeriod, lastPeriod, raised, verifier }, isCapped, isEnded, isWinner, paraId } }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const { api } = useApi();
|
||||
const { allAccounts, isAccount } = useAccounts();
|
||||
const { allAccountsHex, isAccount } = useAccounts();
|
||||
const endpoints = useParaEndpoints(paraId);
|
||||
const [{ contributors, myAccounts }, setContributors] = useState<Contributions>(NO_CONTRIB);
|
||||
const [{ contributors, myAccountsHex }, setContributors] = useState<Contributions>(NO_CONTRIB);
|
||||
const trigger = useEventTrigger([api.events.crowdloan.Contributed, api.events.crowdloan.Withdrew, api.events.crowdloan.AllRefunded, api.events.crowdloan.PartiallyRefunded], useCallback(
|
||||
({ event: { data } }: EventRecord) =>
|
||||
((data.length === 1
|
||||
@ -63,10 +62,10 @@ function Fund ({ bestNumber, className, isOdd, isOngoing, leasePeriod, value: {
|
||||
api.rpc.childstate
|
||||
.getKeys(childKey, '0x')
|
||||
.then((keys) => setContributors(
|
||||
extractContributors(allAccounts, keys, api.registry.chainSS58))
|
||||
extractContributors(allAccountsHex, keys))
|
||||
)
|
||||
.catch(console.error);
|
||||
}, [allAccounts, api, childKey, trigger]);
|
||||
}, [allAccountsHex, api, childKey, trigger]);
|
||||
|
||||
const isDepositor = useMemo(
|
||||
() => isAccount(depositor.toString()),
|
||||
@ -143,7 +142,7 @@ function Fund ({ bestNumber, className, isOdd, isOngoing, leasePeriod, value: {
|
||||
</td>
|
||||
<td className='badge media--1000'>
|
||||
<Icon
|
||||
color={myAccounts.length ? 'green' : 'gray'}
|
||||
color={myAccountsHex.length ? 'green' : 'gray'}
|
||||
icon='asterisk'
|
||||
/>
|
||||
</td>
|
||||
|
||||
@ -4,17 +4,20 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import { keyring } from '@polkadot/ui-keyring';
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { decodeAddress } from '@polkadot/util-crypto';
|
||||
|
||||
import { useIsMountedRef } from './useIsMountedRef';
|
||||
|
||||
interface UseAccounts {
|
||||
allAccounts: string[];
|
||||
allAccountsHex: string[];
|
||||
areAccountsLoaded: boolean
|
||||
hasAccounts: boolean;
|
||||
isAccount: (address?: string | null) => boolean;
|
||||
}
|
||||
|
||||
const EMPTY: UseAccounts = { allAccounts: [], areAccountsLoaded: false, hasAccounts: false, isAccount: () => false };
|
||||
const EMPTY: UseAccounts = { allAccounts: [], allAccountsHex: [], areAccountsLoaded: false, hasAccounts: false, isAccount: () => false };
|
||||
|
||||
export function useAccounts (): UseAccounts {
|
||||
const mountedRef = useIsMountedRef();
|
||||
@ -24,10 +27,11 @@ export function useAccounts (): UseAccounts {
|
||||
const subscription = keyring.accounts.subject.subscribe((accounts): void => {
|
||||
if (mountedRef.current) {
|
||||
const allAccounts = accounts ? Object.keys(accounts) : [];
|
||||
const allAccountsHex = allAccounts.map((a) => u8aToHex(decodeAddress(a)));
|
||||
const hasAccounts = allAccounts.length !== 0;
|
||||
const isAccount = (address?: string | null) => !!address && allAccounts.includes(address);
|
||||
|
||||
setState({ allAccounts, areAccountsLoaded: true, hasAccounts, isAccount });
|
||||
setState({ allAccounts, allAccountsHex, areAccountsLoaded: true, hasAccounts, isAccount });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user