Skip to content

Commit 0db1976

Browse files
authored
fix: user added chains price id (#1571)
1 parent 929b519 commit 0db1976

File tree

6 files changed

+23
-31
lines changed

6 files changed

+23
-31
lines changed

packages/extension-polkagate/src/fullscreen/accountDetails/index.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ import { useParams } from 'react-router';
1515
import { BN } from '@polkadot/util';
1616

1717
import { AccountContext, ActionContext, Warning } from '../../components';
18-
import { useAccountAssets, useBalances, useCurrency, useFullscreen, useInfo, usePrices, useTranslation } from '../../hooks';
18+
import { useAccountAssets, useBalances, useCurrency, useFullscreen, useInfo, usePrices, useTokenPrice, useTranslation } from '../../hooks';
1919
import { getValue } from '../../popup/account/util';
2020
import HistoryModal from '../../popup/history/modal/HistoryModal';
2121
import { AccountLabel } from '../../popup/home/AccountLabel';
2222
import ReceiveModal from '../../popup/receive/ReceiveModal';
2323
import { ASSET_HUBS, GOVERNANCE_CHAINS, STAKING_CHAINS } from '../../util/constants';
2424
import getParentNameSuri from '../../util/getParentNameSuri';
25-
import { getPriceIdByChainName } from '../../util/utils';
2625
import FullScreenHeader from '../governance/FullScreenHeader';
2726
import Bread from '../partials/Bread';
2827
import DeriveAccountModal from '../partials/DeriveAccountModal';
@@ -70,6 +69,7 @@ export default function AccountDetails (): React.ReactElement {
7069
const [unlockInformation, setUnlockInformation] = useState<UnlockInformationType | undefined>();
7170

7271
const assetId = useMemo(() => assetIdOnAssetHub !== undefined ? assetIdOnAssetHub : selectedAsset?.assetId, [assetIdOnAssetHub, selectedAsset?.assetId]);
72+
const { price: currentPrice } = useTokenPrice(address, assetId);
7373

7474
const balances = useBalances(address, refreshNeeded, setRefreshNeeded, undefined, assetId || undefined);
7575

@@ -106,21 +106,6 @@ export default function AccountDetails (): React.ReactElement {
106106
balancesToShow?.soloTotal && balancesToShow?.pooledBalance && !balancesToShow.soloTotal.isZero() && !balancesToShow.pooledBalance.isZero()
107107
, [balancesToShow?.pooledBalance, balancesToShow?.soloTotal]);
108108

109-
const currentPrice = useMemo((): number | undefined => {
110-
const selectedAssetPriceId = selectedAsset?.priceId;
111-
112-
if (selectedAsset && !selectedAssetPriceId) {
113-
// price is 0 for assets with no priceId
114-
return 0;
115-
}
116-
117-
const _priceId = getPriceIdByChainName(chainName);
118-
const currentAssetPrices = pricesInCurrency?.prices?.[(selectedAssetPriceId || _priceId)];
119-
const mayBeTestNetPrice = pricesInCurrency?.prices && !currentAssetPrices ? 0 : undefined;
120-
121-
return currentAssetPrices?.value || mayBeTestNetPrice;
122-
}, [selectedAsset, chainName, pricesInCurrency?.prices]);
123-
124109
useEffect(() => {
125110
// reset assetId on chain switch
126111
assetIdOnAssetHub && setAssetIdOnAssetHub(undefined);

packages/extension-polkagate/src/hooks/useBalances.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export default function useBalances (address: string | undefined, refresh?: bool
100100
return;
101101
}
102102

103-
const ED = api.consts['balances']['existentialDeposit'] as unknown as BN;
103+
const ED = api.consts['balances'] ? api.consts['balances']['existentialDeposit'] as unknown as BN : BN_ZERO;
104104

105105
formatted && api.derive.balances?.all(formatted).then((allBalances) => {
106106
//@ts-ignore

packages/extension-polkagate/src/hooks/useTokenPrice.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import type { Price } from '../util/types';
66
import { createAssets } from '@polkagate/apps-config/assets';
77
import { useMemo } from 'react';
88

9+
import { useUserAddedPriceId } from '../fullscreen/addNewChain/utils';
910
import { toCamelCase } from '../fullscreen/governance/utils/util';
1011
import { ASSET_HUBS, NATIVE_TOKEN_ASSET_ID } from '../util/constants';
1112
import { getPriceIdByChainName } from '../util/utils';
12-
import { useChain, useChainName, usePrices } from '.';
13+
import { useInfo, usePrices } from '.';
1314

1415
const DEFAULT_PRICE = {
1516
price: undefined,
@@ -25,13 +26,13 @@ const assetsChains = createAssets();
2526
* @returns price : price of the token which the address is already switched to
2627
*/
2728
export default function useTokenPrice (address: string | undefined, assetId?: number): Price | typeof DEFAULT_PRICE {
28-
const chainName = useChainName(address);
29-
const chain = useChain(address);
30-
const isAssetHub = ASSET_HUBS.includes(chain?.genesisHash || '');
31-
29+
const { chainName, genesisHash } = useInfo(address);
30+
const userAddedPriceId = useUserAddedPriceId(genesisHash);
3231
const pricesInCurrencies = usePrices();
3332
const mayBeAssetsOnMultiAssetChains = assetsChains[toCamelCase(chainName || '')];
3433

34+
const isAssetHub = ASSET_HUBS.includes(genesisHash || '');
35+
3536
const _assetId = assetId !== undefined
3637
? assetId
3738
: isAssetHub
@@ -46,7 +47,7 @@ export default function useTokenPrice (address: string | undefined, assetId?: nu
4647
// FixMe, on second fetch of asset id its type will get string which is weird!!
4748
const priceId = _assetId !== undefined && _assetId > NATIVE_TOKEN_ASSET_ID
4849
? mayBeAssetsOnMultiAssetChains?.find(({ id }) => id === Number(_assetId))?.priceId
49-
: getPriceIdByChainName(chainName);
50+
: userAddedPriceId || getPriceIdByChainName(chainName);
5051

5152
const mayBePriceValue = priceId ? pricesInCurrencies.prices?.[priceId]?.value || 0 : 0;
5253

@@ -55,5 +56,5 @@ export default function useTokenPrice (address: string | undefined, assetId?: nu
5556
priceChainName: chainName?.toLocaleLowerCase(),
5657
priceDate: pricesInCurrencies.date
5758
};
58-
}, [_assetId, chainName, mayBeAssetsOnMultiAssetChains, pricesInCurrencies]);
59+
}, [_assetId, chainName, mayBeAssetsOnMultiAssetChains, pricesInCurrencies, userAddedPriceId]);
5960
}

packages/extension-polkagate/src/util/utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { Chain } from '@polkadot/extension-chains/types';
88
import type { Text } from '@polkadot/types';
99
import type { AccountId } from '@polkadot/types/interfaces';
1010
import type { Compact, u128 } from '@polkadot/types-codec';
11-
import type { DropdownOption, FastestConnectionType, RecentChainsType, TransactionDetail } from './types';
11+
import type { DropdownOption, FastestConnectionType, RecentChainsType, TransactionDetail, UserAddedChains } from './types';
1212

1313
import { ApiPromise, WsProvider } from '@polkadot/api';
1414
import { BN, BN_TEN, BN_ZERO, hexToBn, hexToU8a, isHex } from '@polkadot/util';
@@ -371,11 +371,19 @@ export const getProfileColor = (index: number, theme: Theme): string => {
371371
return PROFILE_COLORS[0][theme.palette.mode];
372372
};
373373

374-
export const getPriceIdByChainName = (chainName?: string) => {
374+
export const getPriceIdByChainName = (chainName?: string, useAddedChains?: UserAddedChains) => {
375375
if (!chainName) {
376376
return '';
377377
}
378378

379+
if (useAddedChains) {
380+
const maybeUserAddedPriceId = Object.entries(useAddedChains).find(([_, { chain }]) => chain?.replace(/\s/g, '')?.toLowerCase() === chainName.toLowerCase())?.[1]?.priceId;
381+
382+
if (maybeUserAddedPriceId) {
383+
return maybeUserAddedPriceId;
384+
}
385+
}
386+
379387
const _chainName = (sanitizeChainName(chainName) as unknown as string).toLocaleLowerCase();
380388

381389
return EXTRA_PRICE_IDS[_chainName] ||

packages/extension-polkagate/src/util/workers/getAssetOnRelayChain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async function getAssetOnRelayChain (addresses, chainName, userAddedEndpoints) {
9595
const genesisHash = api.genesisHash.toString();
9696
const priceId = TEST_NETS.includes(genesisHash)
9797
? undefined
98-
: getPriceIdByChainName(chainName);
98+
: getPriceIdByChainName(chainName, userAddedEndpoints);
9999

100100
results[address] = [{ // since some chains may have more than one asset hence we use an array here! even thought its not needed for relay chains but just to be as a general rule.
101101
assetId: NATIVE_TOKEN_ASSET_ID, // Rule: we set asset id 0 for native tokens

packages/extension-polkagate/src/util/workers/utils/getChainEndpoints.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
import { createWsEndpoints } from '@polkagate/apps-config';
55

6-
import { sanitizeChainName } from '../../utils';
7-
86
/**
97
* to get all available chain endpoints of a chain except light client
108
* @param {string} chainName
@@ -18,7 +16,7 @@ export function getChainEndpoints (chainName, userAddedEndpoints) {
1816
.filter((endpoint) => endpoint.info && endpoint.info.toLowerCase() === chainName.toLowerCase() && !endpoint.isDisabled && !endpoint?.isLightClient);
1917

2018
if (!endpoints.length && userAddedEndpoints) {
21-
const maybeEndpoint = Object.entries(userAddedEndpoints).find(([_, { chain }]) => sanitizeChainName(chain)?.toLowerCase() === chainName.toLowerCase());
19+
const maybeEndpoint = Object.entries(userAddedEndpoints).find(([_, { chain }]) => chain?.replace(/\s/g, '')?.toLowerCase() === chainName.toLowerCase());
2220

2321
// @ts-ignore
2422
endpoints = maybeEndpoint ? [{ text: 'endpoint', value: maybeEndpoint[1].endpoint }] : [];

0 commit comments

Comments
 (0)