From ab9053a6ee7ed633f98636f69ac171467b3e3abb Mon Sep 17 00:00:00 2001 From: Bobo Date: Mon, 12 Aug 2024 15:05:59 +0200 Subject: [PATCH] Add raw metadata to chain info (#1372) * Add raw metadata to chain info * Enable users to update metadata once more event if it is up to date --- src/config/localStorage.ts | 1 + src/hooks/useChainInfo.ts | 11 ++++++++++- src/hooks/useMetaExtensions.ts | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/config/localStorage.ts b/src/config/localStorage.ts index 2d1c07d29..0c9e541ed 100644 --- a/src/config/localStorage.ts +++ b/src/config/localStorage.ts @@ -22,6 +22,7 @@ export enum LOCAL_STORAGE { MULTISIG = 'multisig', CLOSE_DAPP_STAKING_V3_ONBOARDING = 'closeDappStakingV3Onboarding', DECOMMISSION = 'decommission', + HAS_RAW_METADATA_V15 = 'hasRawMetadataV15', } // Memo: A helper function to return the account's history data that is stored in the browser diff --git a/src/hooks/useChainInfo.ts b/src/hooks/useChainInfo.ts index 414742694..0c574d755 100644 --- a/src/hooks/useChainInfo.ts +++ b/src/hooks/useChainInfo.ts @@ -4,6 +4,7 @@ import { getSpecTypes } from '@polkadot/types-known'; import { TypeRegistry } from '@polkadot/types/create'; import { formatBalance, isNumber } from '@polkadot/util'; import { defaults as addressDefaults } from '@polkadot/util-crypto/address/defaults'; +import { HexString } from '@polkadot/util/types'; import { ASTAR_NATIVE_TOKEN } from 'src/config/chain'; import { useChainMetadata } from 'src/hooks/useChainMetadata'; import { ref } from 'vue'; @@ -11,6 +12,7 @@ import { ref } from 'vue'; export interface ChainInfo extends MetadataDef { color: string | undefined; tokenSymbol: ASTAR_NATIVE_TOKEN; + rawMetadata: HexString | undefined; } const registry = new TypeRegistry(); @@ -20,6 +22,7 @@ export const DEFAULT_SS58 = registry.createType('u32', addressDefaults.prefix); function createInfo(api: ApiPromise, systemChain: string, specName: string): ChainInfo { // console.log('chainInfo', `${systemChain} | ${systemName} | ${specName}`); + return { chain: systemChain, color: '#2096F3', @@ -31,12 +34,14 @@ function createInfo(api: ApiPromise, systemChain: string, specName: string): Cha tokenDecimals: (api.registry.chainDecimals || [DEFAULT_DECIMALS.toNumber()])[0], tokenSymbol: (api.registry.chainTokens || formatBalance.getDefaults().unit)[0] as ASTAR_NATIVE_TOKEN, + types: getSpecTypes( api.registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion ) as unknown as Record, + rawMetadata: undefined, }; } @@ -47,7 +52,11 @@ export function useChainInfo(api: ApiPromise) { api.isReady.then(async () => { const specName: string = api.runtimeVersion.specName.toString(); const systemChain: string = ((await api.rpc.system.chain()) || '').toString(); - chainInfo.value = createInfo(api, systemChain, specName); + let info = createInfo(api, systemChain, specName); + const metadata = await api.call.metadata.metadataAtVersion(15); + info.rawMetadata = metadata.toHex(); + + chainInfo.value = info; }); return { diff --git a/src/hooks/useMetaExtensions.ts b/src/hooks/useMetaExtensions.ts index b1515ae3e..5a0e14cfb 100644 --- a/src/hooks/useMetaExtensions.ts +++ b/src/hooks/useMetaExtensions.ts @@ -6,6 +6,7 @@ import type { InjectedMetadataKnown, MetadataDef, } from '@polkadot/extension-inject/types'; +import { LOCAL_STORAGE } from 'src/config/localStorage'; interface ExtensionKnown { extension: InjectedExtension; @@ -75,6 +76,7 @@ function filterAll(api: ApiPromise, all: ExtensionKnown[]): Extensions { // Memo: Talisman and Mathwallet return null const current = info.known.find(({ genesisHash }) => api.genesisHash.eq(genesisHash)) || null; const isUpgradable = + !Boolean(localStorage.getItem(LOCAL_STORAGE.HAS_RAW_METADATA_V15)) || (current && api.runtimeVersion.specVersion.gtn(current.specVersion)) || !hasCurrentProperties(api, info); @@ -112,6 +114,8 @@ async function getExtensionInfo( if (isOk) { saveProperties(api, extension); } + + localStorage.setItem(LOCAL_STORAGE.HAS_RAW_METADATA_V15, 'true'); } catch (error) { // ignore console.error('e', error);