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

userAssetsStore refactor #6015

Merged
merged 20 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
5 changes: 3 additions & 2 deletions src/__swaps__/screens/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { useSwapsStore } from '@/state/swaps/swapsStore';
import { SwapWarning } from './components/SwapWarning';
import { clearCustomGasSettings } from './hooks/useCustomGas';
import { SwapProvider, useSwapContext } from './providers/swap-provider';
import { useAccountSettings } from '@/hooks';
import { NavigateToSwapSettingsTrigger } from './components/NavigateToSwapSettingsTrigger';

/** README
Expand Down Expand Up @@ -133,7 +134,7 @@ const useCleanupOnUnmount = () => {
};

const WalletAddressObserver = () => {
const currentWalletAddress = userAssetsStore(state => state.associatedWalletAddress);
const { accountAddress } = useAccountSettings();
const { setAsset } = useSwapContext();

const setNewInputAsset = useCallback(() => {
Expand All @@ -157,7 +158,7 @@ const WalletAddressObserver = () => {
}, [setAsset]);

useAnimatedReaction(
() => currentWalletAddress,
() => accountAddress,
(current, previous) => {
const didWalletAddressChange = previous && current !== previous;

Expand Down
4 changes: 2 additions & 2 deletions src/__swaps__/screens/Swap/components/CoinRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as i18n from '@/languages';
import { RainbowNetworks } from '@/networks';
import { BASE_DEGEN_ADDRESS, DEGEN_CHAIN_DEGEN_ADDRESS, ETH_ADDRESS } from '@/references';
import { toggleFavorite } from '@/resources/favorites';
import { userAssetsStore } from '@/state/assets/userAssets';
import { useUserAssetsStore } from '@/state/assets/userAssets';
import { ethereumUtils, haptics, showActionSheetWithOptions } from '@/utils';
import { startCase } from 'lodash';
import React, { useCallback, useMemo } from 'react';
Expand Down Expand Up @@ -69,7 +69,7 @@ interface OutputCoinRowProps extends PartialAsset {
type CoinRowProps = InputCoinRowProps | OutputCoinRowProps;

export function CoinRow({ isFavorite, onPress, output, uniqueId, testID, ...assetProps }: CoinRowProps) {
const inputAsset = userAssetsStore(state => (output ? undefined : state.getUserAsset(uniqueId)));
const inputAsset = useUserAssetsStore(state => (output ? undefined : state.getUserAsset(uniqueId)));
const outputAsset = output ? (assetProps as PartialAsset) : undefined;

const asset = output ? outputAsset : inputAsset;
Expand Down
4 changes: 2 additions & 2 deletions src/__swaps__/screens/Swap/components/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { opacity } from '@/__swaps__/utils/swaps';
import { Input } from '@/components/inputs';
import { Bleed, Box, Column, Columns, Text, useColorMode, useForegroundColor } from '@/design-system';
import * as i18n from '@/languages';
import { userAssetsStore } from '@/state/assets/userAssets';
import { userAssetsStore, useUserAssetsStore } from '@/state/assets/userAssets';
import { useSwapsStore } from '@/state/swaps/swapsStore';
import React from 'react';
import Animated, {
Expand Down Expand Up @@ -39,7 +39,7 @@ export const SearchInput = ({
const label = useForegroundColor('label');
const labelQuaternary = useForegroundColor('labelQuaternary');

const onInputSearchQueryChange = userAssetsStore(state => state.setSearchQuery);
const onInputSearchQueryChange = useUserAssetsStore(state => state.setSearchQuery);

const onOutputSearchQueryChange = useDebouncedCallback((text: string) => useSwapsStore.setState({ outputSearchQuery: text }), 100, {
leading: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ContextMenuButton } from '@/components/context-menu';
import { AnimatedText, Bleed, Box, Inline, Text, TextIcon, globalColors, useColorMode } from '@/design-system';
import { useAccountAccentColor } from '@/hooks';
import { useSharedValueState } from '@/hooks/reanimated/useSharedValueState';
import { userAssetsStore } from '@/state/assets/userAssets';
import { userAssetsStore, useUserAssetsStore } from '@/state/assets/userAssets';
import { swapsStore } from '@/state/swaps/swapsStore';
import { showActionSheetWithOptions } from '@/utils';
import { OnPressMenuItemEventObject } from 'react-native-ios-context-menu';
Expand All @@ -31,8 +31,11 @@ export const ChainSelection = memo(function ChainSelection({ allText, output }:
const { selectedOutputChainId, setSelectedOutputChainId } = useSwapContext();

// chains sorted by balance on output, chains without balance hidden on input
const balanceSortedChainList = userAssetsStore(state => (output ? state.getBalanceSortedChainList() : state.getChainsWithBalance()));
const inputListFilter = useSharedValue(userAssetsStore.getState().filter);
const { balanceSortedChainList, filter } = useUserAssetsStore(state => ({
balanceSortedChainList: output ? state.getBalanceSortedChainList() : state.getChainsWithBalance(),
filter: state.filter,
}));
const inputListFilter = useSharedValue(filter);

const accentColor = useMemo(() => {
if (c.contrast(accountColor, isDarkMode ? '#191A1C' : globalColors.white100) < (isDarkMode ? 2.125 : 1.5)) {
Expand Down Expand Up @@ -189,7 +192,7 @@ export const ChainSelection = memo(function ChainSelection({ allText, output }:
const ChainButtonIcon = ({ output }: { output: boolean | undefined }) => {
const { selectedOutputChainId: animatedSelectedOutputChainId } = useSwapContext();

const userAssetsFilter = userAssetsStore(state => (output ? undefined : state.filter));
const userAssetsFilter = useUserAssetsStore(state => (output ? undefined : state.filter));
const selectedOutputChainId = useSharedValueState(animatedSelectedOutputChainId, { pauseSync: !output });

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getStandardizedUniqueIdWorklet } from '@/__swaps__/utils/swaps';
import { analyticsV2 } from '@/analytics';
import { useDelayedMount } from '@/hooks/useDelayedMount';
import * as i18n from '@/languages';
import { userAssetsStore } from '@/state/assets/userAssets';
import { userAssetsStore, useUserAssetsStore } from '@/state/assets/userAssets';
import { swapsStore } from '@/state/swaps/swapsStore';
import { DEVICE_WIDTH } from '@/utils/deviceUtils';
import { FlashList } from '@shopify/flash-list';
Expand All @@ -32,7 +32,7 @@ export const TokenToSellList = () => {
const TokenToSellListComponent = () => {
const { inputProgress, internalSelectedInputAsset, internalSelectedOutputAsset, isFetching, isQuoteStale, setAsset } = useSwapContext();

const userAssetIds = userAssetsStore(state => state.getFilteredUserAssetIds());
const userAssetIds = useUserAssetsStore(state => state.getFilteredUserAssetIds());

const handleSelectToken = useCallback(
(token: ParsedSearchAsset | null) => {
Expand Down
13 changes: 7 additions & 6 deletions src/__swaps__/screens/Swap/hooks/useAssetsToSell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMemo } from 'react';
import { Address } from 'viem';

import {
selectUserAssetsList,
Expand All @@ -9,7 +8,7 @@ import {
import { useUserAssets } from '@/__swaps__/screens/Swap/resources/assets';
import { ParsedAssetsDictByChain, ParsedSearchAsset, UserAssetFilter } from '@/__swaps__/types/assets';
import { useAccountSettings, useDebounce } from '@/hooks';
import { userAssetsStore } from '@/state/assets/userAssets';
import { useUserAssetsStore } from '@/state/assets/userAssets';
import { getIsHardhatConnected } from '@/handlers/web3';

const sortBy = (by: UserAssetFilter) => {
Expand All @@ -22,18 +21,20 @@ const sortBy = (by: UserAssetFilter) => {
};

export const useAssetsToSell = () => {
const { accountAddress: currentAddress, nativeCurrency: currentCurrency, network: currentNetwork } = useAccountSettings();
const { accountAddress: currentAddress, nativeCurrency: currentCurrency } = useAccountSettings();

const connectedToHardhat = getIsHardhatConnected();

const filter = userAssetsStore(state => state.filter);
const searchQuery = userAssetsStore(state => state.inputSearchQuery);
const { filter, searchQuery } = useUserAssetsStore(state => ({
filter: state.filter,
searchQuery: state.inputSearchQuery,
}));

const debouncedAssetToSellFilter = useDebounce(searchQuery, 200);

const { data: userAssets = [] } = useUserAssets(
{
address: currentAddress as Address,
address: currentAddress,
currency: currentCurrency,
testnetMode: connectedToHardhat,
},
Expand Down
5 changes: 4 additions & 1 deletion src/__swaps__/screens/Swap/providers/swap-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,10 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {

if (didSelectedAssetChange) {
const assetToSet = insertUserAssetBalance
? { ...asset, balance: (asset && userAssetsStore.getState().getUserAsset(asset.uniqueId)?.balance) || asset?.balance }
? {
...asset,
balance: (asset && userAssetsStore.getState().getUserAsset(asset.uniqueId)?.balance) || asset?.balance,
}
: asset;

if (isSameAsOtherAsset) {
Expand Down
10 changes: 5 additions & 5 deletions src/__swaps__/screens/Swap/resources/assets/userAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ export const USER_ASSETS_STALE_INTERVAL = 30000;
// Query Types

export type UserAssetsArgs = {
address: Address;
address: Address | string;
currency: SupportedCurrencyKey;
testnetMode?: boolean;
};

type SetUserAssetsArgs = {
address: Address;
address: Address | string;
currency: SupportedCurrencyKey;
userAssets?: UserAssetsResult;
testnetMode?: boolean;
};

type SetUserDefaultsArgs = {
address: Address;
address: Address | string;
currency: SupportedCurrencyKey;
staleTime: number;
testnetMode?: boolean;
};

type FetchUserAssetsArgs = {
address: Address;
address: Address | string;
currency: SupportedCurrencyKey;
testnetMode?: boolean;
};
Expand Down Expand Up @@ -161,7 +161,7 @@ async function userAssetsQueryFunctionRetryByChain({
currency,
testnetMode,
}: {
address: Address;
address: Address | string;
chainIds: ChainId[];
currency: SupportedCurrencyKey;
testnetMode?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const addysHttp = new RainbowFetchClient({
// Query Types

export type UserAssetsByChainArgs = {
address: Address;
address: Address | string;
chainId: ChainId;
currency: SupportedCurrencyKey;
};
Expand Down
4 changes: 2 additions & 2 deletions src/components/context-menu-buttons/ChainContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ContextMenuButton } from '@/components/context-menu';
import { Bleed, Box, Inline, Text, TextProps } from '@/design-system';
import * as i18n from '@/languages';
import { ChainId, ChainNameDisplay } from '@/__swaps__/types/chains';
import { useUserAssetsStore } from '@/state/assets/userAssets';
import { showActionSheetWithOptions } from '@/utils';
import { userAssetsStore } from '@/state/assets/userAssets';
import { chainNameForChainIdWithMainnetSubstitution } from '@/__swaps__/utils/chains';

interface DefaultButtonOptions {
Expand Down Expand Up @@ -49,7 +49,7 @@ export const ChainContextMenu = ({
textWeight = 'heavy',
} = defaultButtonOptions;

const balanceSortedChains = userAssetsStore(state =>
const balanceSortedChains = useUserAssetsStore(state =>
// eslint-disable-next-line no-nested-ternary
chainsToDisplay ? chainsToDisplay : excludeChainsWithNoBalance ? state.getChainsWithBalance() : state.getBalanceSortedChainList()
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/expanded-state/AvailableNetworksv2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const AvailableNetworksv2 = ({
screen: Routes.CURRENCY_SELECT_SCREEN,
});
},
[asset, goBack, navigate, networks, swapsV2Enabled, swaps_v2, updateInputCurrency]
[asset, goBack, isReadOnlyWallet, navigate, networks, swapsV2Enabled, swaps_v2, updateInputCurrency]
);

const handlePressContextMenu = useCallback(
Expand Down
Loading
Loading