diff --git a/packages/page-claims/src/Warning.tsx b/packages/page-claims/src/Warning.tsx index 3a026b0bcc..a2b76c825b 100644 --- a/packages/page-claims/src/Warning.tsx +++ b/packages/page-claims/src/Warning.tsx @@ -8,7 +8,7 @@ import React from 'react'; import styled from 'styled-components'; import { useTranslation } from './translate'; -import usePreclaimPolkadotAddresses from './usePreclaimPolkadotAddresses'; +import usePolkadotPreclaims from './usePolkadotPreclaims'; export interface Props{ className?: string; @@ -16,9 +16,9 @@ export interface Props{ function Warning ({ className }: Props): React.ReactElement | null { const { t } = useTranslation(); - const needsAttestArray = usePreclaimPolkadotAddresses(); + const needsAttest = usePolkadotPreclaims(); - if (!needsAttestArray || !needsAttestArray.length) { + if (!needsAttest.length) { return null; } @@ -26,11 +26,11 @@ function Warning ({ className }: Props): React.ReactElement | null {
{ - needsAttestArray.length > 1 + needsAttest.length > 1 ? t('You need to sign an attestation for the following accounts:') : t('You need to sign an attestation for the following account:') }{ - needsAttestArray.map((address) => ( + needsAttest.map((address) => ( ([]); + + // find all own preclaims + const preclaims = useCall<[string, EthereumAddress][]>(api.query.claims?.preclaims?.multi, [allAccounts], { + transform: (preclaims: Option[]) => + preclaims + .map((opt, index): [string, Option] => [allAccounts[index], opt]) + .filter(([, opt]) => opt.isSome) + .map(([address, opt]) => [address, opt.unwrap()]) + }); + + // Filter the accounts that need attest. They are accounts that + // - already preclaimed + // - has a balance, either vested or normal + useEffect((): void => { + preclaims && api.queryMulti( + preclaims.reduce((result: [QueryableStorageEntry<'promise'>, EthereumAddress][], [, ethAddr]) => + result.concat([ + [api.query.claims.claims, ethAddr], + [api.query.claims.vesting, ethAddr] + ]), + []), (opts: Option[]): void => { + // filter the cases where either claims or vesting has a value + mountedRef.current && setNeedsAttest( + preclaims + .filter((_, index) => opts[index * 2].isSome || opts[(index * 2) + 1].isSome) + .map(([address]) => address) + ); + } + ); + }, [api, allAccounts, mountedRef, preclaims]); + + return needsAttest; +} diff --git a/packages/page-claims/src/usePreclaimPolkadotAddresses.ts b/packages/page-claims/src/usePreclaimPolkadotAddresses.ts deleted file mode 100644 index 58c7da4f24..0000000000 --- a/packages/page-claims/src/usePreclaimPolkadotAddresses.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2017-2020 @polkadot/app-settings authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -import { Option } from '@polkadot/types'; -import { EthereumAddress } from '@polkadot/types/interfaces'; - -import { useAccounts, useApi, useCall } from '@polkadot/react-hooks'; - -export default function usePreclaimPolkadotAddresses (): string[] { - const { allAccounts } = useAccounts(); - const { api } = useApi(); - - // Find accounts that need attest. They are accounts that - // - already preclaimed, - // - didn't sign the attest yet. - // `claims.preclaims` returns the ethereum address for these accounts. - const needAttest = useCall(api.query.claims?.preclaims?.multi, [allAccounts], { - transform: (preclaims: Option[]) => - preclaims - .map((opt, index): [boolean, string] => [opt.isSome, allAccounts[index]]) - .filter(([isSome]) => isSome) - .map(([, address]) => address) - }); - - return needAttest || []; -}