ghost-portal/packages/page-settings/src/useChainInfo.ts
Antoine Estienne e55a18689b
Add chainType to chainInfo (#5766)
* add chainType

* Update packages/page-settings/src/types.ts

Co-authored-by: Jaco <jacogr@gmail.com>

Co-authored-by: Jaco <jacogr@gmail.com>
2021-07-05 15:02:48 +02:00

41 lines
1.8 KiB
TypeScript

// Copyright 2017-2021 @polkadot/app-settings authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiPromise } from '@polkadot/api';
import type { ChainInfo } from './types';
import { useMemo } from 'react';
import { getSystemColor, getSystemIcon } from '@polkadot/apps-config';
import { DEFAULT_DECIMALS, DEFAULT_SS58 } from '@polkadot/react-api';
import { useApi } from '@polkadot/react-hooks';
import { getSpecTypes } from '@polkadot/types-known';
import { formatBalance, isNumber } from '@polkadot/util';
function createInfo (api: ApiPromise, systemChain: string, systemName: string, specName: string, isEthereum: boolean): ChainInfo {
return {
chain: systemChain,
chainType: isEthereum ? 'ethereum' : 'substrate',
color: getSystemColor(systemChain, systemName, specName),
genesisHash: api.genesisHash.toHex(),
icon: getSystemIcon(systemName, specName),
metaCalls: Buffer.from(api.runtimeMetadata.asCallsOnly.toU8a()).toString('base64'),
specVersion: api.runtimeVersion.specVersion.toNumber(),
ss58Format: isNumber(api.registry.chainSS58) ? api.registry.chainSS58 : DEFAULT_SS58.toNumber(),
tokenDecimals: (api.registry.chainDecimals || [DEFAULT_DECIMALS.toNumber()])[0],
tokenSymbol: (api.registry.chainTokens || formatBalance.getDefaults().unit)[0],
types: getSpecTypes(api.registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as unknown as Record<string, string>
};
}
export default function useChainInfo (): ChainInfo | null {
const { api, isApiReady, isEthereum, specName, systemChain, systemName } = useApi();
return useMemo(
() => isApiReady
? createInfo(api, systemChain, systemName, specName, isEthereum)
: null,
[api, isApiReady, specName, systemChain, systemName, isEthereum]
);
}