Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom networks assets #1186

Merged
merged 6 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/core/resources/assets/customNetworkAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from '~/core/utils/chains';
import { isZero } from '~/core/utils/numbers';
import { RainbowError, logger } from '~/logger';
import { ETH_MAINNET_ASSET } from '~/test/utils';

import { ASSETS_TIMEOUT_DURATION } from './assets';

Expand Down Expand Up @@ -133,7 +134,7 @@ export const getCustomChainIconUrl = (
const baseUrl =
'https://raw.githubusercontent.com/rainbow-me/assets/master/blockchains/';

if (address === AddressZero) {
if (address === AddressZero || address === ETH_MAINNET_ASSET.address) {
return `${baseUrl}${customChainIdsToAssetNames[chainId]}/info/logo.png`;
} else {
return `${baseUrl}${customChainIdsToAssetNames[chainId]}/assets/${address}/logo.png`;
Expand Down Expand Up @@ -161,24 +162,26 @@ async function customNetworkAssetsFunction({
try {
const assetsPromises = customChains.map(async (chain) => {
const provider = getProvider({ chainId: chain.id });
const nativeAssetAddress =
chain.id === ChainId.mainnet ? ETH_MAINNET_ASSET.address : AddressZero;
const nativeAssetBalance = await provider.getBalance(address);
const customNetworkNativeAssetParsed =
nativeAssetBalance && !isZero(nativeAssetBalance.toString())
? parseUserAssetBalances({
asset: {
address: AddressZero,
address: nativeAssetAddress,
chainId: chain.id,
chainName: chain.name as ChainName,
isNativeAsset: true,
name: chain.nativeCurrency.symbol,
symbol: chain.nativeCurrency.symbol,
uniqueId: `${AddressZero}_${chain.id}`,
uniqueId: `${nativeAssetAddress}_${chain.id}`,
decimals: 18,
native: { price: undefined },
price: { value: 0 },
bridging: { isBridgeable: false, networks: [] },
mainnetAddress: AddressZero as AddressOrEth,
icon_url: getCustomChainIconUrl(chain.id, AddressZero),
mainnetAddress: nativeAssetAddress as AddressOrEth,
icon_url: getCustomChainIconUrl(chain.id, nativeAssetAddress),
},
currency,
balance: nativeAssetBalance.toString(),
Expand Down Expand Up @@ -246,6 +249,15 @@ async function customNetworkAssetsFunction({
allCustomNetworkAssets[i].native.price = parsedAsset.native.price;
allCustomNetworkAssets[i].price =
parsedAsset?.price as ZerionAssetPrice;
// Now we have the price, we have to calculate the native balance
if (allCustomNetworkAssets[i].isNativeAsset) {
const assetWithPriceAndNativeBalance = parseUserAssetBalances({
asset: allCustomNetworkAssets[i],
currency,
balance: nativeAssetBalance.toString(),
});
allCustomNetworkAssets[i] = assetWithPriceAndNativeBalance;
}
}
});

Expand All @@ -271,7 +283,6 @@ async function customNetworkAssetsFunction({
},
{} as Record<ChainId | number, ParsedAssetsDict>,
);

return parsedAssetsDict;
} catch (e) {
logger.error(new RainbowError('customNetworkAssetsFunction: '), {
Expand Down
27 changes: 24 additions & 3 deletions src/entries/popup/hooks/useUserAssetsBalance.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { useAccount } from 'wagmi';
import { Address, useAccount } from 'wagmi';

import {
selectUserAssetsBalance,
selectorFilterByUserChains,
} from '~/core/resources/_selectors/assets';
import { useUserAssets } from '~/core/resources/assets';
import { useCustomNetworkAssets } from '~/core/resources/assets/customNetworkAssets';
import { useCurrentCurrencyStore } from '~/core/state';
import { convertAmountToNativeDisplay } from '~/core/utils/numbers';
import { add, convertAmountToNativeDisplay } from '~/core/utils/numbers';

export function useUserAssetsBalance() {
const { address } = useAccount();
const { currentCurrency: currency } = useCurrentCurrencyStore();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wanna make this useCurrentAddressStore since where here?

const { data: totalAssetsBalance } = useUserAssets(
const { data: totalAssetsBalanceKnownNetworks } = useUserAssets(
{
address,
currency,
Expand All @@ -22,6 +23,26 @@ export function useUserAssetsBalance() {
},
);

const { data: totalAssetsBalanceCustomNetworks = [] } =
useCustomNetworkAssets(
{
address: address as Address,
currency,
},
{
select: (data) =>
selectorFilterByUserChains({
data,
selector: selectUserAssetsBalance,
}),
},
);

const totalAssetsBalance = add(
totalAssetsBalanceKnownNetworks as string,
totalAssetsBalanceCustomNetworks as string,
);
Comment on lines +42 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't add break if one of these are undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The selector returns '0' by default so we're good


return {
amount: totalAssetsBalance,
display: convertAmountToNativeDisplay(totalAssetsBalance || 0, currency),
Expand Down
27 changes: 26 additions & 1 deletion src/entries/popup/hooks/useVisibleTokenCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
selectorFilterByUserChains,
} from '~/core/resources/_selectors/assets';
import { useUserAssets } from '~/core/resources/assets';
import { useCustomNetworkAssets } from '~/core/resources/assets/customNetworkAssets';
import { useCurrentAddressStore, useCurrentCurrencyStore } from '~/core/state';
import { useHideSmallBalancesStore } from '~/core/state/currentSettings/hideSmallBalances';

Expand All @@ -30,7 +31,31 @@ export const useVisibleTokenCount = () => {
},
);

const visibleTokenCount = useMemo(() => assets.length || 0, [assets.length]);
const { data: customNetworkAssets = [] } = useCustomNetworkAssets(
{
address: address,
currency: currentCurrency,
},
{
select: (data) =>
selectorFilterByUserChains({
data,
selector: hideSmallBalances
? selectUserAssetsFilteringSmallBalancesList
: selectUserAssetsList,
}),
},
);

const allAssets = useMemo(
() => [...assets, ...customNetworkAssets],
[assets, customNetworkAssets],
);

const visibleTokenCount = useMemo(
() => allAssets.length || 0,
[allAssets.length],
);

return { visibleTokenCount };
};
7 changes: 6 additions & 1 deletion src/entries/popup/pages/home/Tokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ export function Tokens() {
);

const allAssets = useMemo(
() => [...assets, ...customNetworkAssets],
() =>
[...assets, ...customNetworkAssets].sort(
(a: ParsedUserAsset, b: ParsedUserAsset) =>
parseFloat(b?.native?.balance?.amount) -
parseFloat(a?.native?.balance?.amount),
),
[assets, customNetworkAssets],
);

Expand Down
Loading