Skip to content

Commit

Permalink
refactor some types, add queries for withdrawals
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredvu committed Jan 8, 2025
1 parent bb4aa80 commit 39c59f6
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 69 deletions.
5 changes: 2 additions & 3 deletions src/views/dialogs/DepositDialog2/AmountInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { formatUnits, parseUnits } from 'viem';

import { STRING_KEYS } from '@/constants/localization';
import { TOKEN_DECIMALS } from '@/constants/numbers';
import { ETH_DECIMALS } from '@/constants/tokens';
import { ETH_DECIMALS, TokenForTransfer } from '@/constants/tokens';
import { WalletNetworkType } from '@/constants/wallets';

import { useAccounts } from '@/hooks/useAccounts';
Expand All @@ -17,13 +17,12 @@ import { Icon, IconName } from '@/components/Icon';
import { Output, OutputType } from '@/components/Output';

import { useBalance } from './queries';
import { DepositToken } from './types';
import { getTokenSymbol, isNativeTokenDenom } from './utils';

export type AmountInputProps = {
value: string;
onChange: (newValue: string) => void;
token: DepositToken;
token: TokenForTransfer;
onTokenClick: () => void;
};

Expand Down
7 changes: 3 additions & 4 deletions src/views/dialogs/DepositDialog2/DepositDialog2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DepositDialog2Props, DialogProps } from '@/constants/dialogs';
import { CosmosChainId } from '@/constants/graz';
import { STRING_KEYS } from '@/constants/localization';
import { SOLANA_MAINNET_ID } from '@/constants/solana';
import { USDC_ADDRESSES, USDC_DECIMALS } from '@/constants/tokens';
import { TokenForTransfer, USDC_ADDRESSES, USDC_DECIMALS } from '@/constants/tokens';
import { WalletNetworkType } from '@/constants/wallets';

import { useAccounts } from '@/hooks/useAccounts';
Expand All @@ -20,9 +20,8 @@ import { SourceAccount } from '@/state/wallet';

import { DepositForm } from './DepositForm';
import { TokenSelect } from './TokenSelect';
import { DepositToken } from './types';

function getDefaultToken(sourceAccount: SourceAccount): DepositToken {
function getDefaultToken(sourceAccount: SourceAccount): TokenForTransfer {
if (!sourceAccount.chain) throw new Error('No user chain detected');

// TODO(deposit2.0): Use user's biggest balance as the default token
Expand Down Expand Up @@ -53,7 +52,7 @@ export const DepositDialog2 = ({ setIsOpen }: DialogProps<DepositDialog2Props>)
const { sourceAccount } = useAccounts();

const [amount, setAmount] = useState('');
const [token, setToken] = useState<DepositToken>(getDefaultToken(sourceAccount));
const [token, setToken] = useState<TokenForTransfer>(getDefaultToken(sourceAccount));

const { isMobile } = useBreakpoints();
const stringGetter = useStringGetter();
Expand Down
15 changes: 7 additions & 8 deletions src/views/dialogs/DepositDialog2/DepositForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { formatUnits, parseUnits } from 'viem';

import { ButtonAction, ButtonState, ButtonType } from '@/constants/buttons';
import { STRING_KEYS } from '@/constants/localization';
import { USDC_DECIMALS } from '@/constants/tokens';
import { TokenForTransfer, USDC_DECIMALS } from '@/constants/tokens';

import { SkipRouteSpeed } from '@/hooks/transfers/skipClient';
import { useDebounce } from '@/hooks/useDebounce';
import { useStringGetter } from '@/hooks/useStringGetter';

Expand All @@ -14,8 +15,7 @@ import { Output, OutputType } from '@/components/Output';

import { AmountInput } from './AmountInput';
import { RouteOptions } from './RouteOptions';
import { useRoutes } from './queries';
import { DepositSpeed, DepositToken } from './types';
import { useDepositRoutes } from './queries';

export const DepositForm = ({
onTokenSelect,
Expand All @@ -26,13 +26,13 @@ export const DepositForm = ({
onTokenSelect: () => void;
amount: string;
setAmount: Dispatch<SetStateAction<string>>;
token: DepositToken;
token: TokenForTransfer;
}) => {
const stringGetter = useStringGetter();
const [selectedSpeed, setSelectedSpeed] = useState<DepositSpeed>('fast');
const [selectedSpeed, setSelectedSpeed] = useState<SkipRouteSpeed>('fast');

const debouncedAmount = useDebounce(amount);
const { data: routes, isFetching } = useRoutes(token, debouncedAmount);
const { data: routes, isFetching } = useDepositRoutes(token, debouncedAmount);

useEffect(() => {
if (debouncedAmount && !isFetching && !routes?.fast) setSelectedSpeed('slow');
Expand Down Expand Up @@ -64,8 +64,7 @@ export const DepositForm = ({
{/* TODO(deposit2.0): Show difference between current and new balance here */}
{selectedRoute && (
<div tw="flex justify-between text-small">
{/* TODO(deposit2.0): localization */}
<div tw="text-color-text-0">Available balance</div>
<div tw="text-color-text-0">{stringGetter({ key: STRING_KEYS.AVAILABLE_BALANCE })}</div>
<div>
+
<Output
Expand Down
8 changes: 4 additions & 4 deletions src/views/dialogs/DepositDialog2/RouteOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { formatUnits } from 'viem';

import { USD_DECIMALS } from '@/constants/numbers';

import { SkipRouteSpeed } from '@/hooks/transfers/skipClient';

import { LightningIcon, ShieldIcon } from '@/icons';

import { Output, OutputType } from '@/components/Output';

import { DepositSpeed } from './types';

type Props = {
routes: { slow?: RouteResponse; fast?: RouteResponse };
isLoading: boolean;
disabled: boolean;
selectedSpeed: DepositSpeed;
onSelectSpeed: (route: DepositSpeed) => void;
selectedSpeed: SkipRouteSpeed;
onSelectSpeed: (route: SkipRouteSpeed) => void;
};

export const RouteOptions = ({
Expand Down
8 changes: 4 additions & 4 deletions src/views/dialogs/DepositDialog2/TokenSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { parseUnits } from 'viem';

import { CHAIN_INFO } from '@/constants/chains';
import { TOKEN_DECIMALS, USD_DECIMALS } from '@/constants/numbers';
import { TokenForTransfer } from '@/constants/tokens';

import { AssetIcon } from '@/components/AssetIcon';
import { LoadingSpinner } from '@/components/Loading/LoadingSpinner';
import { Output, OutputType } from '@/components/Output';

import { useBalances } from './queries';
import { DepositToken } from './types';
import { getTokenSymbol } from './utils';

export const TokenSelect = ({
Expand All @@ -21,8 +21,8 @@ export const TokenSelect = ({
setToken,
}: {
onBack: () => void;
token: DepositToken;
setToken: Dispatch<SetStateAction<DepositToken>>;
token: TokenForTransfer;
setToken: Dispatch<SetStateAction<TokenForTransfer>>;
}) => {
const { isLoading, data } = useBalances();

Expand Down Expand Up @@ -50,7 +50,7 @@ export const TokenSelect = ({
return partition(allBalances, (balance) => parseUnits(balance.amount, balance.decimals!) > 0);
}, [data]);

const onTokenClick = (newToken: DepositToken) => () => {
const onTokenClick = (newToken: TokenForTransfer) => () => {
setToken(newToken);
onBack();
};
Expand Down
14 changes: 8 additions & 6 deletions src/views/dialogs/DepositDialog2/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import { DYDX_DEPOSIT_CHAIN, EVM_DEPOSIT_CHAINS } from '@/constants/chains';
import { CosmosChainId } from '@/constants/graz';
import { SOLANA_MAINNET_ID } from '@/constants/solana';
import { timeUnits } from '@/constants/time';
import { DYDX_CHAIN_USDC_DENOM, USDC_ADDRESSES } from '@/constants/tokens';
import { DYDX_CHAIN_USDC_DENOM, TokenForTransfer, USDC_ADDRESSES } from '@/constants/tokens';
import { WalletNetworkType } from '@/constants/wallets';

import { useSkipClient } from '@/hooks/transfers/skipClient';
import { useAccounts } from '@/hooks/useAccounts';

import { SourceAccount } from '@/state/wallet';

import { DepositToken } from './types';

export function useBalances() {
const { sourceAccount } = useAccounts();
const { skipClient } = useSkipClient();
Expand Down Expand Up @@ -112,7 +110,11 @@ function networkTypeToBalances(sourceAccount: SourceAccount): BalanceRequest {
throw new Error('Fetching balances for unknown chain');
}

async function getSkipRoutes(skipClient: SkipClient, token: DepositToken, amount: string) {
async function getSkipDepositRoutes(
skipClient: SkipClient,
token: TokenForTransfer,
amount: string
) {
const routeOptions: RouteRequest = {
sourceAssetDenom: token.denom,
sourceAssetChainID: token.chainId,
Expand All @@ -133,12 +135,12 @@ async function getSkipRoutes(skipClient: SkipClient, token: DepositToken, amount
return { slow, fast: isFastRouteAvailable ? fast : undefined };
}

export function useRoutes(token: DepositToken, amount: string) {
export function useDepositRoutes(token: TokenForTransfer, amount: string) {
const { skipClient } = useSkipClient();
const rawAmount = amount && parseUnits(amount, token.decimals);
return useQuery({
queryKey: ['routes', token.chainId, token.denom, amount],
queryFn: () => getSkipRoutes(skipClient, token, amount),
queryFn: () => getSkipDepositRoutes(skipClient, token, amount),
enabled: Boolean(rawAmount && rawAmount > 0),
staleTime: 1 * timeUnits.minute,
refetchOnMount: 'always',
Expand Down
7 changes: 0 additions & 7 deletions src/views/dialogs/DepositDialog2/types.ts

This file was deleted.

9 changes: 5 additions & 4 deletions src/views/dialogs/WithdrawDialog2/AddressInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EventHandler } from 'react';
import { SyntheticInputEvent } from 'react-number-format/types/types';
import styled from 'styled-components';

import { CHAIN_INFO } from '@/constants/chains';
import { STRING_KEYS } from '@/constants/localization';
import { WalletNetworkType } from '@/constants/wallets';

Expand All @@ -15,14 +16,14 @@ import { Icon, IconName } from '@/components/Icon';
type AddressInputProps = {
value: string;
onChange: (newValue: string) => void;
_destinationChain: string;
destinationChain: string;
onDestinationClicked: () => void;
};

export const AddressInput = ({
value,
onChange,
_destinationChain,
destinationChain,
onDestinationClicked,
}: AddressInputProps) => {
const stringGetter = useStringGetter();
Expand Down Expand Up @@ -50,8 +51,8 @@ export const AddressInput = ({
onClick={onDestinationClicked}
>
<div tw="flex items-center gap-0.5">
<AssetIcon tw="h-[2rem] w-[2rem]" symbol="ETH" />
<div>Ethereum</div>
<AssetIcon tw="h-[2rem] w-[2rem]" logoUrl={CHAIN_INFO[destinationChain]?.icon} />
<div>{CHAIN_INFO[destinationChain]?.name}</div>
</div>
{sourceAccount.chain !== WalletNetworkType.Solana && (
<$CaretIcon size="10px" iconName={IconName.Caret} />
Expand Down
45 changes: 20 additions & 25 deletions src/views/dialogs/WithdrawDialog2/ChainSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Dispatch, Fragment, SetStateAction } from 'react';

import { mainnet } from 'viem/chains';

import { CHAIN_INFO } from '@/constants/chains';
import { WITHDRAWABLE_ASSETS } from '@/constants/tokens';

import { AssetIcon } from '@/components/AssetIcon';

Expand All @@ -23,30 +22,26 @@ export const ChainSelect = ({
return (
<div tw="flex flex-col gap-0.5 py-1">
<div tw="flex flex-col">
{[mainnet.id].map((chain) => {
const chainId = chain.toString();

return (
<Fragment key={chainId}>
<button
onClick={onChainClick(chainId)}
type="button"
style={{
backgroundColor: chainId === selectedChain ? 'var(--color-layer-4)' : undefined,
}}
tw="flex w-full justify-between px-1.25 py-1 hover:bg-color-layer-4"
key={chainId}
>
<div tw="flex items-center gap-0.75">
<AssetIcon tw="h-[2rem] w-[2rem]" symbol="ETH" />
<div tw="flex flex-col items-start gap-0.125">
<div tw="text-medium font-medium">{CHAIN_INFO[chainId]?.name}</div>
</div>
{Object.values(WITHDRAWABLE_ASSETS).map(({ chainId }) => (
<Fragment key={chainId}>
<button
onClick={onChainClick(chainId)}
type="button"
style={{
backgroundColor: chainId === selectedChain ? 'var(--color-layer-4)' : undefined,
}}
tw="flex w-full justify-between px-1.25 py-1 hover:bg-color-layer-4"
key={chainId}
>
<div tw="flex items-center gap-0.75">
<AssetIcon tw="h-[2rem] w-[2rem]" logoUrl={CHAIN_INFO[chainId]?.icon} />
<div tw="flex flex-col items-start gap-0.125">
<div tw="text-medium font-medium">{CHAIN_INFO[chainId]?.name}</div>
</div>
</button>
</Fragment>
);
})}
</div>
</button>
</Fragment>
))}
</div>
</div>
);
Expand Down
12 changes: 10 additions & 2 deletions src/views/dialogs/WithdrawDialog2/WithdrawDialog2.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { useRef, useState } from 'react';

import styled from 'styled-components';
import { mainnet } from 'viem/chains';

import { DepositDialog2Props, DialogProps } from '@/constants/dialogs';
import { CosmosChainId } from '@/constants/graz';
import { STRING_KEYS } from '@/constants/localization';
import { WalletNetworkType } from '@/constants/wallets';

import { useAccounts } from '@/hooks/useAccounts';
import { useBreakpoints } from '@/hooks/useBreakpoints';
import { useStringGetter } from '@/hooks/useStringGetter';

Expand All @@ -14,8 +18,12 @@ import { ChainSelect } from './ChainSelect';
import { WithdrawForm } from './WithdrawForm';

export const WithdrawDialog2 = ({ setIsOpen }: DialogProps<DepositDialog2Props>) => {
const [destinationAddress, setDestinationAddress] = useState('');
const [destinationChain, setDestinationChain] = useState('');
const { sourceAccount } = useAccounts();
const [destinationAddress, setDestinationAddress] = useState(sourceAccount.address ?? '');
const [destinationChain, setDestinationChain] = useState(
sourceAccount.chain === WalletNetworkType.Evm ? mainnet.id.toString() : CosmosChainId.Noble
);

const [amount, setAmount] = useState('');

const { isMobile } = useBreakpoints();
Expand Down
Loading

0 comments on commit 39c59f6

Please sign in to comment.