diff --git a/packages/page-addresses/src/Contacts/Address.tsx b/packages/page-addresses/src/Contacts/Address.tsx index d40252ff9d..7f5f7719af 100644 --- a/packages/page-addresses/src/Contacts/Address.tsx +++ b/packages/page-addresses/src/Contacts/Address.tsx @@ -1,7 +1,6 @@ // Copyright 2017-2021 @polkadot/app-addresses authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { DeriveAccountInfo } from '@polkadot/api-derive/types'; import type { ActionStatus } from '@polkadot/react-components/Status/types'; import type { ThemeDef } from '@polkadot/react-components/types'; import type { KeyringAddress } from '@polkadot/ui-keyring/types'; @@ -11,7 +10,7 @@ import styled, { ThemeContext } from 'styled-components'; import Transfer from '@polkadot/app-accounts/modals/Transfer'; import { AddressInfo, AddressSmall, Button, ChainLock, ExpandButton, Forget, Icon, LinkExternal, Menu, Popup, Tags } from '@polkadot/react-components'; -import { useApi, useBalancesAll, useCall, useToggle } from '@polkadot/react-hooks'; +import { useApi, useBalancesAll, useDeriveAccountInfo, useToggle } from '@polkadot/react-hooks'; import { keyring } from '@polkadot/ui-keyring'; import { BN_ZERO, formatNumber, isFunction } from '@polkadot/util'; @@ -31,7 +30,7 @@ function Address ({ address, className = '', filter, isFavorite, toggleFavorite const { t } = useTranslation(); const { theme } = useContext(ThemeContext as React.Context); const api = useApi(); - const info = useCall(api.api.derive.accounts.info, [address]); + const info = useDeriveAccountInfo(address); const balancesAll = useBalancesAll(address); const [tags, setTags] = useState([]); const [accName, setAccName] = useState(''); diff --git a/packages/page-staking/src/Overview/Address/index.tsx b/packages/page-staking/src/Overview/Address/index.tsx index 0afedfeed6..9a7700bd4d 100644 --- a/packages/page-staking/src/Overview/Address/index.tsx +++ b/packages/page-staking/src/Overview/Address/index.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type BN from 'bn.js'; -import type { DeriveAccountInfo, DeriveHeartbeatAuthor } from '@polkadot/api-derive/types'; +import type { DeriveHeartbeatAuthor } from '@polkadot/api-derive/types'; import type { Option } from '@polkadot/types'; import type { SlashingSpans, ValidatorPrefs } from '@polkadot/types/interfaces'; import type { NominatedBy as NominatedByType, ValidatorInfo } from '../../types'; @@ -13,7 +13,7 @@ import React, { useMemo } from 'react'; import { ApiPromise } from '@polkadot/api'; import { AddressSmall, Icon, LinkExternal } from '@polkadot/react-components'; import { checkVisibility } from '@polkadot/react-components/util'; -import { useApi, useCall } from '@polkadot/react-hooks'; +import { useApi, useCall, useDeriveAccountInfo } from '@polkadot/react-hooks'; import { FormatBalance } from '@polkadot/react-query'; import { BN_ZERO } from '@polkadot/util'; @@ -78,7 +78,7 @@ const transformSlashes = { function useAddressCalls (api: ApiPromise, address: string, isMain?: boolean) { const params = useMemo(() => [address], [address]); - const accountInfo = useCall(api.derive.accounts.info, params); + const accountInfo = useDeriveAccountInfo(address); const slashingSpans = useCall(!isMain && api.query.staking.slashingSpans, params, transformSlashes); return { accountInfo, slashingSpans }; diff --git a/packages/page-staking/src/Targets/Validator.tsx b/packages/page-staking/src/Targets/Validator.tsx index aad15af152..27f3eed342 100644 --- a/packages/page-staking/src/Targets/Validator.tsx +++ b/packages/page-staking/src/Targets/Validator.tsx @@ -2,7 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 import type BN from 'bn.js'; -import type { DeriveAccountInfo } from '@polkadot/api-derive/types'; import type { UnappliedSlash } from '@polkadot/types/interfaces'; import type { NominatedBy, ValidatorInfo } from '../types'; @@ -10,7 +9,7 @@ import React, { useCallback, useMemo } from 'react'; import { AddressSmall, Badge, Checkbox, Icon } from '@polkadot/react-components'; import { checkVisibility } from '@polkadot/react-components/util'; -import { useApi, useBlockTime, useCall } from '@polkadot/react-hooks'; +import { useApi, useBlockTime, useDeriveAccountInfo } from '@polkadot/react-hooks'; import { FormatBalance } from '@polkadot/react-query'; import { formatNumber } from '@polkadot/util'; @@ -37,7 +36,7 @@ function queryAddress (address: string): void { function Validator ({ allSlashes, canSelect, filterName, info, isNominated, isSelected, nominatedBy = [], toggleFavorite, toggleSelected }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); - const accountInfo = useCall(api.derive.accounts.info, [info.accountId]); + const accountInfo = useDeriveAccountInfo(info.accountId); const [,, time] = useBlockTime(info.lastPayout); const isVisible = useMemo( diff --git a/packages/react-components/src/AccountIndex.tsx b/packages/react-components/src/AccountIndex.tsx index 244c2fe81c..998a529207 100644 --- a/packages/react-components/src/AccountIndex.tsx +++ b/packages/react-components/src/AccountIndex.tsx @@ -7,7 +7,7 @@ import type { AccountId, Address } from '@polkadot/types/interfaces'; import React, { useMemo } from 'react'; import styled from 'styled-components'; -import { useApi, useCall } from '@polkadot/react-hooks'; +import { useDeriveAccountInfo, useSystemApi } from '@polkadot/react-hooks'; interface Props { children?: React.ReactNode; @@ -24,8 +24,8 @@ function extractIndex ({ accountIndex }: Partial = {}): strin } function AccountIndex ({ children, className = '', defaultValue, label, value }: Props): React.ReactElement | null { - const { api } = useApi(); - const info = useCall(api.derive.accounts.info, [value]); + const api = useSystemApi(); + const info = useDeriveAccountInfo(value); const accountIndex = useMemo( () => extractIndex(info), diff --git a/packages/react-components/src/AccountName.tsx b/packages/react-components/src/AccountName.tsx index 07d333d43f..12a7ede7ad 100644 --- a/packages/react-components/src/AccountName.tsx +++ b/packages/react-components/src/AccountName.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { IconName } from '@fortawesome/fontawesome-svg-core'; -import type { DeriveAccountInfo, DeriveAccountRegistration } from '@polkadot/api-derive/types'; +import type { DeriveAccountRegistration } from '@polkadot/api-derive/types'; import type { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces'; import React, { useCallback, useContext, useEffect, useState } from 'react'; @@ -10,7 +10,7 @@ import styled from 'styled-components'; import { AccountSidebarToggle } from '@polkadot/app-accounts/Sidebar'; import registry from '@polkadot/react-api/typeRegistry'; -import { useApi, useCall } from '@polkadot/react-hooks'; +import { useDeriveAccountInfo, useSystemApi } from '@polkadot/react-hooks'; import { isFunction, stringToU8a } from '@polkadot/util'; import Badge from './Badge'; @@ -131,8 +131,8 @@ function extractIdentity (address: string, identity: DeriveAccountRegistration): } function AccountName ({ children, className = '', defaultName, label, onClick, override, toggle, value, withSidebar }: Props): React.ReactElement { - const { api } = useApi(); - const info = useCall(api.derive.accounts.info, [value]); + const api = useSystemApi(); + const info = useDeriveAccountInfo(value); const [name, setName] = useState(() => extractName((value || '').toString(), undefined, defaultName)); const toggleSidebar = useContext(AccountSidebarToggle); diff --git a/packages/react-components/src/AddressToggle.tsx b/packages/react-components/src/AddressToggle.tsx index 3a1aa44ac2..f26ace0d9c 100644 --- a/packages/react-components/src/AddressToggle.tsx +++ b/packages/react-components/src/AddressToggle.tsx @@ -1,12 +1,10 @@ // Copyright 2017-2021 @polkadot/react-components authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { DeriveAccountInfo } from '@polkadot/api-derive/types'; - import React, { useCallback, useMemo } from 'react'; import styled from 'styled-components'; -import { useApi, useCall } from '@polkadot/react-hooks'; +import { useApi, useDeriveAccountInfo } from '@polkadot/react-hooks'; import AddressMini from './AddressMini'; import Toggle from './Toggle'; @@ -24,7 +22,7 @@ interface Props { function AddressToggle ({ address, className = '', filter, isHidden, noToggle, onChange, value }: Props): React.ReactElement | null { const { api } = useApi(); - const info = useCall(api.derive.accounts.info, [address]); + const info = useDeriveAccountInfo(address); const isVisible = useMemo( () => info ? checkVisibility(api, address, info, filter, false) : true, diff --git a/packages/react-hooks/src/index.ts b/packages/react-hooks/src/index.ts index d1a69da13c..9b7951955b 100644 --- a/packages/react-hooks/src/index.ts +++ b/packages/react-hooks/src/index.ts @@ -22,6 +22,9 @@ export { useCollectiveMembers } from './useCollectiveMembers'; export { useFormField } from './useFormField'; export { useDebounce } from './useDebounce'; export { useDelegations } from './useDelegations'; +export { useSystemApi } from './useSystemApi'; +export { useDeriveAccountFlags } from './useDeriveAccountFlags'; +export { useDeriveAccountInfo } from './useDeriveAccountInfo'; export { useEventTrigger } from './useEventTrigger'; export { useExtrinsicTrigger } from './useExtrinsicTrigger'; export { useFavorites } from './useFavorites'; diff --git a/packages/react-hooks/src/useAccountInfo.ts b/packages/react-hooks/src/useAccountInfo.ts index b2c2158810..697435dbc7 100644 --- a/packages/react-hooks/src/useAccountInfo.ts +++ b/packages/react-hooks/src/useAccountInfo.ts @@ -1,7 +1,6 @@ // Copyright 2017-2021 @polkadot/react-hooks authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { DeriveAccountFlags, DeriveAccountInfo } from '@polkadot/api-derive/types'; import type { Nominations, ValidatorPrefs } from '@polkadot/types/interfaces'; import type { KeyringJson$Meta } from '@polkadot/ui-keyring/types'; import type { AddressFlags, AddressIdentity, UseAccountInfo } from './types'; @@ -16,6 +15,8 @@ import { useAccounts } from './useAccounts'; import { useAddresses } from './useAddresses'; import { useApi } from './useApi'; import { useCall } from './useCall'; +import { useDeriveAccountFlags } from './useDeriveAccountFlags'; +import { useDeriveAccountInfo } from './useDeriveAccountInfo'; import { useToggle } from './useToggle'; const IS_NONE = { @@ -42,8 +43,8 @@ function useAccountInfoImpl (value: string | null, isContract = false): UseAccou const { api } = useApi(); const { isAccount } = useAccounts(); const { isAddress } = useAddresses(); - const accountInfo = useCall(api.derive.accounts.info, [value]); - const accountFlags = useCall(api.derive.accounts.flags, [value]); + const accountInfo = useDeriveAccountInfo(value); + const accountFlags = useDeriveAccountFlags(value); const nominator = useCall(api.query.staking?.nominators, [value]); const validator = useCall(api.query.staking?.validators, [value]); const [accountIndex, setAccountIndex] = useState(undefined); @@ -242,7 +243,7 @@ function useAccountInfoImpl (value: string | null, isContract = false): UseAccou ); const setTags = useCallback( - (tags: string[]): void => setSortedTags(tags.sort()), + (tags: string[]) => setSortedTags(tags.sort()), [] ); diff --git a/packages/react-hooks/src/useDeriveAccountFlags.ts b/packages/react-hooks/src/useDeriveAccountFlags.ts new file mode 100644 index 0000000000..4f161e1499 --- /dev/null +++ b/packages/react-hooks/src/useDeriveAccountFlags.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2021 @polkadot/react-hooks authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { DeriveAccountFlags } from '@polkadot/api-derive/types'; +import type { AccountId, Address } from '@polkadot/types/interfaces'; + +import { createNamedHook } from './createNamedHook'; +import { useCall } from './useCall'; +import { useSystemApi } from './useSystemApi'; + +function useDeriveAccountFlagsImpl (value?: AccountId | Address | Uint8Array | string | null): DeriveAccountFlags | undefined { + const api = useSystemApi(); + + return useCall(api.derive.accounts.flags, [value]); +} + +export const useDeriveAccountFlags = createNamedHook('useDeriveAccountFlags', useDeriveAccountFlagsImpl); diff --git a/packages/react-hooks/src/useDeriveAccountInfo.ts b/packages/react-hooks/src/useDeriveAccountInfo.ts new file mode 100644 index 0000000000..a231464a44 --- /dev/null +++ b/packages/react-hooks/src/useDeriveAccountInfo.ts @@ -0,0 +1,17 @@ +// Copyright 2017-2021 @polkadot/react-hooks authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { DeriveAccountInfo } from '@polkadot/api-derive/types'; +import type { AccountId, AccountIndex, Address } from '@polkadot/types/interfaces'; + +import { createNamedHook } from './createNamedHook'; +import { useCall } from './useCall'; +import { useSystemApi } from './useSystemApi'; + +function useDeriveAccountInfoImpl (value?: AccountId | AccountIndex | Address | Uint8Array | string | null): DeriveAccountInfo | undefined { + const api = useSystemApi(); + + return useCall(api.derive.accounts.info, [value]); +} + +export const useDeriveAccountInfo = createNamedHook('useDeriveAccountInfo', useDeriveAccountInfoImpl); diff --git a/packages/react-hooks/src/useSystemApi.ts b/packages/react-hooks/src/useSystemApi.ts new file mode 100644 index 0000000000..01907931cd --- /dev/null +++ b/packages/react-hooks/src/useSystemApi.ts @@ -0,0 +1,13 @@ +// Copyright 2017-2021 @polkadot/react-hooks authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { ApiPromise } from '@polkadot/api'; + +import { createNamedHook } from './createNamedHook'; +import { useApi } from './useApi'; + +function useSystemApiImpl (): ApiPromise { + return useApi().api; +} + +export const useSystemApi = createNamedHook('useSystemApi', useSystemApiImpl);