ghost-portal/packages/react-hooks/src/useProxies.ts

33 lines
1.3 KiB
TypeScript

// Copyright 2017-2021 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import BN from 'bn.js';
import { AccountId } from '@polkadot/types/interfaces';
import { NodeRuntimeProxyType, PalletProxyProxyDefinition } from '@polkadot/types/lookup';
import { createNamedHook } from './createNamedHook';
import { useAccounts } from './useAccounts';
import { useApi } from './useApi';
import { useCall } from './useCall';
function useProxiesImpl (): [PalletProxyProxyDefinition[], BN][] | undefined {
const { api } = useApi();
const { allAccounts } = useAccounts();
return useCall<[PalletProxyProxyDefinition[], BN][]>(api.query.proxy?.proxies.multi, [allAccounts], {
transform: (result: [([AccountId, NodeRuntimeProxyType] | PalletProxyProxyDefinition)[], BN][]): [PalletProxyProxyDefinition[], BN][] =>
api.tx.proxy.addProxy.meta.args.length === 3
? result as [PalletProxyProxyDefinition[], BN][]
: (result as [[AccountId, NodeRuntimeProxyType][], BN][]).map(([arr, bn]): [PalletProxyProxyDefinition[], BN] =>
[arr.map(([delegate, proxyType]): PalletProxyProxyDefinition =>
api.createType('ProxyDefinition', {
delegate,
proxyType
})), bn]
)
});
}
export const useProxies = createNamedHook('useProxies', useProxiesImpl);