Skip to content

Commit

Permalink
@skylar/clean account asset (#5350)
Browse files Browse the repository at this point in the history
* chore: remove generic assets from account assets

* clean

* clean up
  • Loading branch information
skylarbarrera committed Jan 26, 2024
1 parent 272df66 commit 3b2c7c6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
16 changes: 11 additions & 5 deletions src/components/exchange/ExchangeTokenRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import isEqual from 'react-fast-compare';
import { Box, Column, Columns, Inline, Stack, Text } from '@/design-system';
import { isNativeAsset } from '@/handlers/assets';
import { Network } from '@/networks/types';
import { useAccountAsset, useDimensions } from '@/hooks';
import { useAsset, useDimensions } from '@/hooks';
import { ethereumUtils } from '@/utils';
import FastCoinIcon from '../asset-list/RecyclerAssetList2/FastComponents/FastCoinIcon';
import { ButtonPressAnimation } from '../animations';
Expand All @@ -25,7 +25,6 @@ export default React.memo(function ExchangeTokenRow({
showFavoriteButton,
onPress,
theme,
nativeCurrency,
nativeCurrencySymbol,
favorite,
toggleFavorite,
Expand All @@ -37,12 +36,19 @@ export default React.memo(function ExchangeTokenRow({
testID,
type,
disabled,
decimals,
},
}: ExchangeTokenRowProps) {
const { width: deviceWidth } = useDimensions();

// TODO https://github.com/rainbow-me/rainbow/pull/3313/files#r876259954
const item = useAccountAsset(uniqueId, nativeCurrency);
const item = useAsset({
uniqueId,
mainnet_address,
symbol,
type,
address,
name,
decimals,
});
const network = ethereumUtils.getNetworkFromType(type) ?? Network.mainnet;
const rowTestID = `${testID}-exchange-coin-row-${
symbol ?? item?.symbol ?? ''
Expand Down
39 changes: 1 addition & 38 deletions src/hooks/useAccountAsset.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
import useAccountSettings from './useAccountSettings';
import useGenericAsset from './useGenericAsset';
import { AssetType } from '@/entities';
import { parseAssetNative } from '@/parsers';
import { ETH_ADDRESS, ETH_ICON_URL } from '@/references';
import { useUserAsset } from '@/resources/assets/useUserAsset';

const getZeroEth = () => {
return {
address: ETH_ADDRESS,
balance: {
amount: '0',
display: '0 ETH',
},
color: '#29292E',
decimals: 18,
icon_url: ETH_ICON_URL,
isCoin: true,
isPlaceholder: true,
isSmall: false,
name: 'Ethereum',
symbol: 'ETH',
type: AssetType.token,
uniqueId: ETH_ADDRESS,
};
};

// this is meant to be used for assets under balances
// with a fallback for generic assets
// and an ETH placeholder
// NFTs are not included in this hook
// this is meant to be used for assets contained in the current wallet
export default function useAccountAsset(
uniqueId: string,
nativeCurrency: string | undefined = undefined
) {
const { data: accountAsset } = useUserAsset(uniqueId);

const genericAssetBackup = useGenericAsset(uniqueId);

// this is temporary for FastBalanceCoinRow to make a tiny bit faster
// we pass nativeCurrency only in that case
// for all the other cases it will work as expected
Expand All @@ -46,14 +18,5 @@ export default function useAccountAsset(

if (accountAsset) {
return parseAssetNative(accountAsset, nativeCurrencyToUse);
} else if (uniqueId === ETH_ADDRESS) {
const result = parseAssetNative(genericAssetBackup, nativeCurrencyToUse);
const placeholderEth = {
...getZeroEth(),
...result,
};
return placeholderEth;
} else {
return genericAssetBackup;
}
}
14 changes: 10 additions & 4 deletions src/hooks/useAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,30 @@ import { useMemo } from 'react';
import useAccountAsset from './useAccountAsset';
import useCollectible from './useCollectible';
import { AssetTypes, ParsedAddressAsset } from '@/entities';
import useGenericAsset from './useGenericAsset';

// To fetch an asset from account assets,
// generic assets, and uniqueTokens
export default function useAsset(asset: ParsedAddressAsset) {
const accountAsset = useAccountAsset(
asset?.uniqueId || asset?.mainnet_address || asset?.address
);
const genericAsset = useGenericAsset(
asset?.uniqueId || asset?.mainnet_address || asset?.address
);
const uniqueToken = useCollectible(asset);
return useMemo(() => {
if (!asset) return null;

let matched = null;
if (asset.type === AssetTypes.token) {
matched = accountAsset;
} else if (asset.type === AssetTypes.nft) {
if (asset.type === AssetTypes.nft) {
matched = uniqueToken;
} else if (accountAsset) {
matched = accountAsset;
} else if (genericAsset) {
matched = genericAsset;
}

return matched || asset;
}, [accountAsset, asset, uniqueToken]);
}, [accountAsset, asset, genericAsset, uniqueToken]);
}

0 comments on commit 3b2c7c6

Please sign in to comment.