Skip to content

Commit

Permalink
[Update] Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
tunghp2002 committed Dec 24, 2024
1 parent 592f8ea commit 070518e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
8 changes: 2 additions & 6 deletions packages/extension-base/src/koni/background/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function getSuitableRegistry (registries: RegistrySource[], payload: Sign
return a.distance - b.distance;
}

return a.specVersion - b.specVersion;
return b.specVersion - a.specVersion;
});

return sortedRegistries[0].registry;
Expand Down Expand Up @@ -68,11 +68,7 @@ export function setupDatabaseRegistry (metadata: MetadataItem, chainInfo: _Chain
const _metadata = new Metadata(registry, metadata.hexValue);

registry.register(metadata.types);
registry.setChainProperties(registry.createType('ChainProperties', {
ss58Format: metadata.tokenInfo?.ss58Format,
tokenDecimals: metadata.tokenInfo?.tokenDecimals,
tokenSymbol: metadata.tokenInfo?.tokenSymbol
}) as unknown as ChainProperties);
registry.setChainProperties(registry.createType('ChainProperties', metadata.tokenInfo) as unknown as ChainProperties);
registry.setMetadata(_metadata, payload.signedExtensions, metadata.userExtensions);

return {
Expand Down
26 changes: 14 additions & 12 deletions packages/extension-base/src/services/chain-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { AssetLogoMap, AssetRefMap, ChainAssetMap, ChainInfoMap, ChainLogoMap, MultiChainAssetMap } from '@subwallet/chain-list';
import { _AssetRef, _AssetRefPath, _AssetType, _ChainAsset, _ChainInfo, _ChainStatus, _EvmInfo, _MultiChainAsset, _SubstrateChainType, _SubstrateInfo, _TonInfo } from '@subwallet/chain-list/types';
import { AssetSetting, ValidateNetworkResponse } from '@subwallet/extension-base/background/KoniTypes';
import { AssetSetting, MetadataItem, ValidateNetworkResponse } from '@subwallet/extension-base/background/KoniTypes';
import { _DEFAULT_ACTIVE_CHAINS, _ZK_ASSET_PREFIX, LATEST_CHAIN_DATA_FETCHING_INTERVAL } from '@subwallet/extension-base/services/chain-service/constants';
import { EvmChainHandler } from '@subwallet/extension-base/services/chain-service/handler/EvmChainHandler';
import { MantaPrivateHandler } from '@subwallet/extension-base/services/chain-service/handler/manta/MantaPrivateHandler';
Expand Down Expand Up @@ -2082,42 +2082,44 @@ export class ChainService {
return this.dbService.stores.metadata.getMetadataByGenesisHash(hash);
}

getExtraInfo (chain: string): Omit<ExtraInfo, 'specVersion' | 'specName'> {
const chainInfo = this.getChainInfoByKey(chain);
getExtraInfo (metadata: MetadataItem): Omit<ExtraInfo, 'specVersion' | 'specName'> {
const tokenInfo = metadata.tokenInfo;

return {
decimals: chainInfo.substrateInfo?.decimals ?? 0,
tokenSymbol: chainInfo.substrateInfo?.symbol ?? 'Unit',
base58Prefix: chainInfo.substrateInfo?.addressPrefix ?? 42
decimals: tokenInfo?.tokenDecimals ?? 0,
tokenSymbol: tokenInfo?.tokenSymbol ?? 'Unit',
base58Prefix: tokenInfo?.ss58Format ?? 42
};
}

async calculateMetadataHash (chain: string): Promise<string | undefined> {
const metadata = await this.getMetadata(chain);
const metadataV15 = await this.getMetadataV15(chain);

if (!metadata || !metadata.hexV15) {
if (!metadata || !metadataV15 || !metadataV15.hexV15) {
return undefined;
}

const extraInfo = this.getExtraInfo(chain);
const extraInfo = this.getExtraInfo(metadata);
const specVersion = parseInt(metadata.specVersion);
const specName = metadata.specName;
const hexV15 = metadata.hexV15;
const hexV15 = metadataV15.hexV15;

return calculateMetadataHash({ ...extraInfo, specVersion, specName }, hexV15);
}

async shortenMetadata (chain: string, txBlob: string): Promise<string | undefined> {
const metadata = await this.getMetadata(chain);
const metadataV15 = await this.getMetadataV15(chain);

if (!metadata || !metadata.hexV15) {
if (!metadata || !metadataV15 || !metadataV15.hexV15) {
return undefined;
}

const extraInfo = this.getExtraInfo(chain);
const extraInfo = this.getExtraInfo(metadata);
const specVersion = parseInt(metadata.specVersion);
const specName = metadata.specName;
const hexV15 = metadata.hexV15;
const hexV15 = metadataV15.hexV15;

return getShortMetadata(txBlob as HexString, { ...extraInfo, specVersion, specName }, hexV15);
}
Expand Down
11 changes: 5 additions & 6 deletions packages/extension-base/src/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const getShortMetadata = (blob: HexString, extraInfo: ExtraInfo, metadata
return u8aToHex(_merkleizeMetadata.getProofForExtrinsicPayload(blob));
};

const getMetadataV15 = async (chain: string, api: ApiPromise, chainService?: ChainService): Promise<void> => {
const updateMetadataV15 = async (chain: string, api: ApiPromise, chainService?: ChainService): Promise<void> => {
try {
const currentSpecVersion = api.runtimeVersion.specVersion.toString();
const genesisHash = api.genesisHash.toHex();
Expand All @@ -62,7 +62,6 @@ const getMetadataV15 = async (chain: string, api: ApiPromise, chainService?: Cha
genesisHash: genesisHash,
specVersion: currentSpecVersion,
hexV15

};

chainService?.upsertMetadataV15(chain, { ...updateMetadata }).catch(console.error);
Expand All @@ -73,7 +72,7 @@ const getMetadataV15 = async (chain: string, api: ApiPromise, chainService?: Cha
}
};

const getMetadata = async (
const updateMetadata = async (
chain: string,
api: ApiPromise,
chainService?: ChainService
Expand Down Expand Up @@ -120,8 +119,8 @@ export const cacheMetadata = (
chainService?: ChainService
): void => {
// Update metadata to database with async methods
substrateApi.api.isReady.then(async (api) => {
await getMetadata(chain, api, chainService);
await getMetadataV15(chain, api, chainService);
substrateApi.api.isReady.then((api) => {
return updateMetadata(chain, api, chainService)
.then(() => updateMetadataV15(chain, api, chainService));
}).catch(console.error);
};

0 comments on commit 070518e

Please sign in to comment.