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

[SWAPS V2]: Add token search logic and ability to select assets #5547

Merged
merged 32 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
52e52cc
initial useSwapAssets work
walmat Mar 22, 2024
b1cf1d6
remove unused chainid ref
walmat Mar 22, 2024
e42e995
comment out favorites until I can match the types
walmat Mar 22, 2024
9e33d17
add token search and user asset search
walmat Mar 22, 2024
59b8ce4
replace a bunch of dummy values with actual color values / balances
walmat Mar 25, 2024
d0932e1
some cleanup
walmat Mar 25, 2024
79de904
undo type change and fix lint
walmat Mar 25, 2024
99feb1a
some cleanup
walmat Mar 26, 2024
dd5ffc0
fix some bugs
walmat Mar 26, 2024
11f51e6
some more optimizations
walmat Mar 27, 2024
f2c4c8a
Merge branch 'develop' into @matthew/APP-1247
walmat Mar 27, 2024
3cce241
touches
walmat Mar 27, 2024
e3b683c
add display prop to animated styles
walmat Mar 27, 2024
dc62a88
merge develop
walmat Mar 28, 2024
761ef47
more prop splitting
walmat Mar 28, 2024
1b71aab
replaced stuff with reanimated stuff
walmat Mar 29, 2024
ea74875
this is fucked
walmat Mar 29, 2024
4277572
moving over some reanimated stuff
walmat Mar 29, 2024
cf9bca0
swap action button colors working
walmat Mar 29, 2024
a610b71
more progress on reanimated conversion
walmat Mar 30, 2024
203c6f9
ya
walmat Apr 2, 2024
f86cc4e
fix token flipping
walmat Apr 2, 2024
459545e
fix https://github.com/rainbow-me/browser-extension/pull/1453/files
walmat Apr 2, 2024
1cb57d7
fix empty acitivity list
walmat Apr 2, 2024
cb2bfae
search + asset lists working
walmat Apr 3, 2024
32c11ab
fix swapbackground linear not using reanimated values
walmat Apr 3, 2024
60732c3
limit buy sections to first 5 assets
walmat Apr 3, 2024
34497fd
perf improvements
walmat Apr 3, 2024
a57ee70
Merge branch 'develop' into @matthew/APP-1247
walmat Apr 3, 2024
7f306d0
fix padding on output asset coin row
walmat Apr 3, 2024
04bb1ae
Merge branch 'develop' into @matthew/APP-1247
walmat Apr 3, 2024
87deb2d
fix lint
walmat Apr 3, 2024
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
47 changes: 40 additions & 7 deletions src/__swaps__/screens/Swap/components/CoinRow.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
import React, { useMemo } from 'react';
import { Address } from 'viem';
import { ButtonPressAnimation } from '@/components/animations';
import { Box, HitSlop, Inline, Stack, Text } from '@/design-system';
import { TextColor } from '@/design-system/color/palettes';
import { Network } from '@/networks/types';
import { useTheme } from '@/theme';
import { SwapCoinIcon } from './SwapCoinIcon';
import { CoinRowButton } from './CoinRowButton';
import { BalancePill } from './BalancePill';
import { ChainId } from '../types/chains';
import { ethereumUtils } from '@/utils';
import { toggleFavorite, useFavorites } from '@/resources/favorites';
import { ETH_ADDRESS } from '@/references';

export const CoinRow = ({
address,
mainnetAddress,
chainId,
balance,
isTrending,
name,
nativeBalance,
color,
iconUrl,
onPress,
output,
symbol,
}: {
address: Address | 'eth';
address: string;
mainnetAddress: string;
chainId: ChainId;
balance: string;
isTrending?: boolean;
name: string;
nativeBalance: string;
color: string | undefined;
iconUrl: string | undefined;
onPress?: () => void;
output?: boolean;
symbol: string;
}) => {
const theme = useTheme();
const { favoritesMetadata } = useFavorites();

const favorites = Object.values(favoritesMetadata);

const isFavorite = (address: string) => {
return favorites.find(fav =>
fav.address === ETH_ADDRESS ? '0x0000000000000000000000000000000000000000' === address : fav.address === address
);
};

const percentChange = useMemo(() => {
if (isTrending) {
Expand All @@ -43,18 +62,27 @@ export const CoinRow = ({
}, [isTrending]);

return (
<ButtonPressAnimation onPress={onPress} scaleTo={0.95}>
<ButtonPressAnimation disallowInterruption onPress={onPress} scaleTo={0.95}>
<HitSlop vertical="10px">
<Box alignItems="center" flexDirection="row" justifyContent="space-between" width="full">
<Inline alignVertical="center" space="10px">
<SwapCoinIcon address={address} large network={Network.mainnet} symbol={symbol} theme={theme} />
<SwapCoinIcon
iconUrl={iconUrl}
address={address}
mainnetAddress={mainnetAddress}
large
network={ethereumUtils.getNetworkFromChainId(chainId)}
symbol={symbol}
theme={theme}
color={color}
/>
<Stack space="10px">
<Text color="label" size="17pt" weight="semibold">
{name}
</Text>
<Inline alignVertical="center" space={{ custom: 5 }}>
<Text color="labelTertiary" size="13pt" weight="semibold">
{output ? symbol : `${balance} ${symbol}`}
{output ? symbol : `${balance}`}
</Text>
{isTrending && percentChange && (
<Inline alignVertical="center" space={{ custom: 1 }}>
Expand All @@ -72,7 +100,12 @@ export const CoinRow = ({
{output ? (
<Inline space="8px">
<CoinRowButton icon="􀅳" outline size="icon 14px" />
<CoinRowButton icon="􀋃" weight="black" />
<CoinRowButton
color={isFavorite(address) ? '#FFCB0F' : undefined}
onPress={() => toggleFavorite(address)}
icon="􀋃"
weight="black"
/>
</Inline>
) : (
<BalancePill balance={nativeBalance} />
Expand Down
18 changes: 13 additions & 5 deletions src/__swaps__/screens/Swap/components/CoinRowButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { LIGHT_SEPARATOR_COLOR, SEPARATOR_COLOR, THICK_BORDER_WIDTH } from '../c
import { opacity } from '../utils/swaps';

export const CoinRowButton = ({
color,
icon,
onPress,
outline,
size,
weight,
}: {
color?: string;
icon: string;
onPress?: () => void;
outline?: boolean;
Expand All @@ -26,23 +28,29 @@ export const CoinRowButton = ({
const separatorTertiary = useForegroundColor('separatorTertiary');

return (
<ButtonPressAnimation onPress={onPress} scaleTo={0.8}>
<ButtonPressAnimation disallowInterruption onPress={onPress} scaleTo={0.8}>
<Box
alignItems="center"
borderRadius={14}
height={{ custom: 28 }}
justifyContent="center"
style={{
backgroundColor: outline ? 'transparent' : isDarkMode ? fillQuaternary : opacity(fillTertiary, 0.04),
borderColor: outline ? (isDarkMode ? SEPARATOR_COLOR : LIGHT_SEPARATOR_COLOR) : separatorTertiary,
backgroundColor: outline
? 'transparent'
: color
? opacity(color, 0.25)
: isDarkMode
? fillQuaternary
: opacity(fillTertiary, 0.04),
borderColor: outline ? (isDarkMode ? SEPARATOR_COLOR : LIGHT_SEPARATOR_COLOR) : color ? opacity(color, 0.1) : separatorTertiary,
borderWidth: THICK_BORDER_WIDTH,
}}
width={{ custom: 28 }}
>
<TextIcon
color="labelQuaternary"
color={color ? { custom: color } : 'labelQuaternary'}
containerSize={28}
opacity={isDarkMode ? 0.6 : 0.75}
opacity={isDarkMode ? 1 : 0.75}
size={size || 'icon 12px'}
weight={weight || 'heavy'}
>
Expand Down
10 changes: 8 additions & 2 deletions src/__swaps__/screens/Swap/components/ExchangeRateBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import { opacity } from '../utils/swaps';
import { ButtonPressAnimation } from '@/components/animations';
import Animated from 'react-native-reanimated';
import { useSwapContext } from '../providers/swap-provider';
import { useSwapAssetStore } from '../state/assets';

export const ExchangeRateBubble = () => {
const { isDarkMode } = useColorMode();
const { AnimatedSwapStyles } = useSwapContext();
const { assetToBuy, assetToSell } = useSwapAssetStore();

const fillTertiary = useForegroundColor('fillTertiary');

// TODO: Do proper exchange rate calculation once we receive the quote

if (!assetToSell || !assetToBuy) return null;

return (
<ButtonPressAnimation scaleTo={0.925} style={{ marginTop: 4 }}>
<Box
Expand All @@ -32,7 +38,7 @@ export const ExchangeRateBubble = () => {
>
<Inline alignHorizontal="center" alignVertical="center" space="6px" wrap={false}>
<Text align="center" color="labelQuaternary" size="13pt" style={{ opacity: isDarkMode ? 0.6 : 0.75 }} weight="heavy">
1 ETH
1 {assetToSell?.symbol}
</Text>
<Box
borderRadius={10}
Expand All @@ -46,7 +52,7 @@ export const ExchangeRateBubble = () => {
</TextIcon>
</Box>
<Text align="center" color="labelQuaternary" size="13pt" style={{ opacity: isDarkMode ? 0.6 : 0.75 }} weight="heavy">
1,624.04 USDC
1,624.04 {assetToBuy?.symbol}
</Text>
</Inline>
</Box>
Expand Down
37 changes: 31 additions & 6 deletions src/__swaps__/screens/Swap/components/FlipButton.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
/* eslint-disable no-nested-ternary */
import c from 'chroma-js';
import React from 'react';
import React, { useCallback } from 'react';
import Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated';
import SwapSpinner from '@/__swaps__/assets/swapSpinner.png';
import { ButtonPressAnimation } from '@/components/animations';
import { AnimatedSpinner, spinnerExitConfig } from '@/__swaps__/components/animations/AnimatedSpinner';
import { Bleed, Box, IconContainer, Text, globalColors, useColorMode } from '@/design-system';
import { colors } from '@/styles';
import { SEPARATOR_COLOR, THICK_BORDER_WIDTH } from '../constants';
import { ETH_COLOR, ETH_COLOR_DARK, SEPARATOR_COLOR, THICK_BORDER_WIDTH } from '../constants';
import { opacity } from '../utils/swaps';
import { IS_ANDROID, IS_IOS } from '@/env';
import { AnimatedBlurView } from './AnimatedBlurView';
import { useSwapContext } from '../providers/swap-provider';
import { StyleSheet } from 'react-native';
import { useSwapAssetStore } from '../state/assets';

export const FlipButton = () => {
const { isDarkMode } = useColorMode();
const { bottomColor, isFetching, AnimatedSwapStyles } = useSwapContext();

const { isFetching, AnimatedSwapStyles } = useSwapContext();
const { assetToBuy, assetToSell, setAssetToBuy, setAssetToSell } = useSwapAssetStore();

const bottomColor = (assetToBuy?.colors?.primary || assetToBuy?.colors?.fallback) ?? (isDarkMode ? ETH_COLOR_DARK : ETH_COLOR);

const fetchingStyle = useAnimatedStyle(() => {
return {
borderWidth: isFetching ? withTiming(2, { duration: 300 }) : withTiming(THICK_BORDER_WIDTH, spinnerExitConfig),
};
});

const handleSwapAssets = useCallback(() => {
const prevAssetToSell = assetToSell;
const prevAssetToBuy = assetToBuy;

if (prevAssetToBuy) {
setAssetToSell(prevAssetToBuy);
}

if (prevAssetToSell) {
setAssetToBuy(prevAssetToSell);
}

// TODO: Fetch current prices of each asset and update the input and output values on the native thread
// runOnUI(() => {
// SwapInputController.inputValues.modify(prev => ({
// ...prev,
// // inputNativeValue: prevAssetToBuy.native.price.amount,
// outputNativeValue: prevAssetToSell.native.price.amount,
// }));
// })();
}, [assetToBuy, assetToSell, setAssetToBuy, setAssetToSell]);

return (
<Box
alignItems="center"
Expand All @@ -43,9 +70,7 @@ export const FlipButton = () => {
shadowRadius: isDarkMode ? 6 : 8,
}}
>
<ButtonPressAnimation scaleTo={0.8} style={{ paddingHorizontal: 20, paddingVertical: 8 }}>
{/* TODO: Temp fix - rewrite to actually avoid type errors */}
{/* @ts-expect-error The conditional as={} is causing type errors */}
<ButtonPressAnimation onPress={handleSwapAssets} scaleTo={0.8} style={{ paddingHorizontal: 20, paddingVertical: 8 }}>
<Box
alignItems="center"
as={IS_IOS ? AnimatedBlurView : Animated.View}
Expand Down
16 changes: 10 additions & 6 deletions src/__swaps__/screens/Swap/components/SearchInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Input } from '@/components/inputs';
import { Bleed, Box, Column, Columns, Text, useColorMode, useForegroundColor } from '@/design-system';
import { LIGHT_SEPARATOR_COLOR, SEPARATOR_COLOR, THICK_BORDER_WIDTH } from '../constants';
import { opacity } from '../utils/swaps';
import { useSwapAssetStore } from '../state/assets';
import { useAssetColors } from '../hooks/useAssetColors';

export const SearchInput = ({
color,
Expand All @@ -22,9 +24,10 @@ export const SearchInput = ({
setIsFocused: React.Dispatch<React.SetStateAction<boolean>>;
}) => {
const { isDarkMode } = useColorMode();
const { topColor, bottomColor } = useAssetColors();
const { searchFilter, setSearchFilter } = useSwapAssetStore();

const inputRef = React.useRef<TextInput>(null);
const [query, setQuery] = React.useState('');

const fillTertiary = useForegroundColor('fillTertiary');
const label = useForegroundColor('label');
Expand Down Expand Up @@ -58,9 +61,10 @@ export const SearchInput = ({
<Input
onBlur={() => {
handleExitSearch();
setSearchFilter('');
setIsFocused(false);
}}
onChange={(value: string) => setQuery(value)}
onChangeText={setSearchFilter}
onFocus={() => {
handleFocusSearch();
setIsFocused(true);
Expand All @@ -77,7 +81,7 @@ export const SearchInput = ({
height: 44,
zIndex: 10,
}}
value={query}
value={searchFilter}
/>
</Columns>
</Box>
Expand All @@ -102,12 +106,12 @@ export const SearchInput = ({
justifyContent="center"
paddingHorizontal={{ custom: 12 - THICK_BORDER_WIDTH }}
style={{
backgroundColor: opacity(color, isDarkMode ? 0.1 : 0.08),
borderColor: opacity(color, isDarkMode ? 0.06 : 0.01),
backgroundColor: opacity(output ? bottomColor : topColor, isDarkMode ? 0.1 : 0.08),
borderColor: opacity(output ? bottomColor : topColor, isDarkMode ? 0.06 : 0.01),
borderWidth: THICK_BORDER_WIDTH,
}}
>
<Text align="center" color={{ custom: color }} size="17pt" weight="heavy">
<Text align="center" color={{ custom: output ? bottomColor : topColor }} size="17pt" weight="heavy">
{isFocused ? 'Cancel' : 'Close'}
</Text>
</Box>
Expand Down
46 changes: 24 additions & 22 deletions src/__swaps__/screens/Swap/components/SwapActionButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,30 @@ export const SwapActionButton = ({
)}
</Column>
)}
<Column width="content">
{typeof label === 'string' ? (
<Text
align="center"
color={{ custom: outline ? color || fallbackColor : textColor }}
numberOfLines={1}
size={small ? '17pt' : '20pt'}
weight="heavy"
>
{label}
</Text>
) : (
<AnimatedText
align="center"
color={{ custom: outline ? color || fallbackColor : textColor }}
numberOfLines={1}
size={small ? '17pt' : '20pt'}
text={label}
weight="heavy"
/>
)}
</Column>
{typeof label !== 'undefined' && (
<Column width="content">
{typeof label === 'string' ? (
<Text
align="center"
color={{ custom: outline ? color || fallbackColor : textColor }}
numberOfLines={1}
size={small ? '17pt' : '20pt'}
weight="heavy"
>
{label}
</Text>
) : (
<AnimatedText
align="center"
color={{ custom: outline ? color || fallbackColor : textColor }}
numberOfLines={1}
size={small ? '17pt' : '20pt'}
text={label}
weight="heavy"
/>
)}
</Column>
)}
{rightIcon && (
<Column width="content">
<Text
Expand Down
4 changes: 2 additions & 2 deletions src/__swaps__/screens/Swap/components/SwapBackground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { IS_ANDROID } from '@/env';
import { ScreenCornerRadius } from 'react-native-screen-corner-radius';
import { navbarHeight } from '@/components/navbar/Navbar';
import { safeAreaInsetValues } from '@/utils';
import { useSwapContext } from '../providers/swap-provider';
import { useAssetColors } from '../hooks/useAssetColors';

export const SwapBackground = ({ children }: { children: ReactNode }) => {
const { bottomColor, topColor } = useSwapContext();
const { height: deviceHeight, width: deviceWidth } = useDimensions();
const { isDarkMode } = useColorMode();
const { topColor, bottomColor } = useAssetColors();

const fallbackColor = isDarkMode ? ETH_COLOR_DARK : ETH_COLOR;

Expand Down
Loading
Loading