-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Auto update metadata while fetching accounts balances (#1517)
Co-authored-by: Nick <ekbatanifard@gmail.com>
- Loading branch information
1 parent
4c9e2ff
commit 17367eb
Showing
10 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
...polkagate/src/util/workers/utils/index.js → ...polkagate/src/util/workers/utils/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// @ts-nocheck | ||
|
||
// @ts-nocheck | ||
|
||
export * from './balancify.js'; | ||
export * from './closeWebsockets.js'; | ||
export * from './fastestEndpoint.js'; | ||
export * from './getChainEndpoints.js'; | ||
export * from './balancify.js'; | ||
export * from './metadataFromApi'; | ||
export * from './toGetNativeToken.js'; |
39 changes: 39 additions & 0 deletions
39
packages/extension-polkagate/src/util/workers/utils/metadataFromApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import type { ApiPromise } from '@polkadot/api'; | ||
import type { MetadataDef } from '@polkadot/extension-inject/types'; | ||
|
||
import { createWsEndpoints } from '@polkagate/apps-config'; | ||
|
||
import { getSpecTypes } from '@polkadot/types-known'; | ||
import { formatBalance, isNumber } from '@polkadot/util'; | ||
import { base64Encode } from '@polkadot/util-crypto'; | ||
|
||
const endpoints = createWsEndpoints(); | ||
|
||
export function metadataFromApi (api: ApiPromise): {metadata: MetadataDef} { | ||
const DEFAULT_DECIMALS = api.registry.createType('u32', 12); | ||
const DEFAULT_SS58 = api.registry.createType('u32', 42); | ||
const chainName = api.runtimeChain.toHuman(); | ||
const apiGenesisHash = api.genesisHash.toHex(); | ||
const color = endpoints.find(({ genesisHash, ui }) => genesisHash === apiGenesisHash && ui.color)?.ui?.color; | ||
|
||
const metadata = { | ||
chain: chainName, | ||
chainType: 'substrate' as 'ethereum' | 'substrate', | ||
color, | ||
genesisHash: apiGenesisHash, | ||
icon: 'substrate', | ||
metaCalls: base64Encode(api.runtimeMetadata.asCallsOnly.toU8a()), | ||
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, chainName, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as Record<string, string | Record<string, string>> | ||
}; | ||
|
||
return { metadata }; | ||
} |