Skip to content

Commit

Permalink
Add raw metadata to chain info (#1372)
Browse files Browse the repository at this point in the history
* Add raw metadata to chain info

* Enable users to update metadata once more event if it is up to date
  • Loading branch information
bobo-k2 authored Aug 12, 2024
1 parent 7a1709a commit ab9053a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/config/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion src/hooks/useChainInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ 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';

export interface ChainInfo extends MetadataDef {
color: string | undefined;
tokenSymbol: ASTAR_NATIVE_TOKEN;
rawMetadata: HexString | undefined;
}

const registry = new TypeRegistry();
Expand All @@ -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',
Expand All @@ -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<string, string>,
rawMetadata: undefined,
};
}

Expand All @@ -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()) || '<unknown>').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 {
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useMetaExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
InjectedMetadataKnown,
MetadataDef,
} from '@polkadot/extension-inject/types';
import { LOCAL_STORAGE } from 'src/config/localStorage';

interface ExtensionKnown {
extension: InjectedExtension;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ab9053a

Please sign in to comment.