Browse Source

Pioneer - fix tokenDecimals

Leszek Wiesner 4 years ago
parent
commit
9d1e36874b

+ 1 - 1
pioneer/packages/page-settings/src/useChainInfo.ts

@@ -25,7 +25,7 @@ export default function useChainInfo (): ChainInfo | null {
       metaCalls: Buffer.from(api.runtimeMetadata.asCallsOnly.toU8a()).toString('base64'),
       specVersion: api.runtimeVersion.specVersion.toNumber(),
       ss58Format: isNumber(api.registry.chainSS58) ? api.registry.chainSS58 : 42,
-      tokenDecimals: isNumber(api.registry.chainDecimals) ? api.registry.chainDecimals : 12,
+      tokenDecimals: isNumber(api.registry.chainDecimals[0]) ? api.registry.chainDecimals[0] : 12,
       tokenSymbol: api.registry.chainTokens[0] || 'Unit',
       types: getSpecTypes(registry, systemChain, api.runtimeVersion.specName, api.runtimeVersion.specVersion) as unknown as Record<string, string>
     });

+ 8 - 8
pioneer/packages/react-api/src/Api.tsx

@@ -2,6 +2,7 @@
 // This software may be modified and distributed under the terms
 // of the Apache-2.0 license. See the LICENSE file for details.
 
+import type BN from 'bn.js';
 import { InjectedExtension } from '@polkadot/extension-inject/types';
 import { ChainProperties, ChainType } from '@polkadot/types/interfaces';
 import { ApiProps, ApiState } from './types';
@@ -104,29 +105,28 @@ async function retrieve (api: ApiPromise): Promise<ChainData> {
 
 async function loadOnReady (api: ApiPromise, store: KeyringStore | undefined, types: Record<string, Record<string, string>>): Promise<ApiState> {
   registry.register(types);
-
   const { injectedAccounts, properties, systemChain, systemChainType, systemName, systemVersion } = await retrieve(api);
   const ss58Format = uiSettings.prefix === -1
     ? properties.ss58Format.unwrapOr(DEFAULT_SS58).toNumber()
     : uiSettings.prefix;
-  const tokenSymbol = properties.tokenSymbol.unwrap()[0].toString();
-  const tokenDecimals = properties.tokenDecimals.unwrap()[0].toNumber();
-  const isDevelopment = systemChainType.isDevelopment || systemChainType.isLocal || isTestChain(systemChain);
+  const tokenSymbol = properties.tokenSymbol.unwrap();
+  const tokenDecimals = properties.tokenDecimals.unwrap();
+  const isDevelopment = (systemChainType.isDevelopment || systemChainType.isLocal || isTestChain(systemChain));
 
   console.log(`chain: ${systemChain} (${systemChainType.toString()}), ${JSON.stringify(properties)}`);
 
   // explicitly override the ss58Format as specified
-  registry.setChainProperties(registry.createType('ChainProperties', { ...properties, ss58Format }));
+  registry.setChainProperties(registry.createType('ChainProperties', { ss58Format, tokenDecimals, tokenSymbol }));
 
   // FIXME This should be removed (however we have some hanging bits, e.g. vanity)
   setSS58Format(ss58Format);
 
   // first setup the UI helpers
   formatBalance.setDefaults({
-    decimals: tokenDecimals,
-    unit: tokenSymbol
+    decimals: (tokenDecimals as BN[]).map((b) => b.toNumber()),
+    unit: tokenSymbol[0].toString()
   });
-  TokenUnit.setAbbr(tokenSymbol);
+  TokenUnit.setAbbr(tokenSymbol[0].toString());
 
   // finally load the keyring
   keyring.loadAll({