Skip to content

Commit

Permalink
refactor swap quote
Browse files Browse the repository at this point in the history
  • Loading branch information
abcrane123 committed Jun 13, 2024
1 parent 4eebdfb commit 0c0c4b3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 116 deletions.
71 changes: 46 additions & 25 deletions src/swap/components/Swap.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,67 @@
import { useCallback, useMemo, useState } from 'react';
import { cn } from '../../lib/utils';
import { SwapContext } from '../context';
import { useGetSwapQuote } from '../hooks/useGetSwapQuote';
import type { SwapReact } from '../types';
import type { SwapError, SwapReact } from '../types';
import type { Token } from '../../token';
import { getSwapQuote } from '../core/getSwapQuote';
import { isSwapError } from '../utils';

export function Swap({ account, children, onError }: SwapReact) {
const [fromAmount, setFromAmount] = useState('');
const [fromToken, setFromToken] = useState<Token>();
const [toAmount, setToAmount] = useState('');
const [toToken, setToToken] = useState<Token>();
const [lastTokenAmountUpdated, setLastTokenAmountUpdated] = useState<'to' | 'from' | undefined>();

useGetSwapQuote({
amountReference: lastTokenAmountUpdated,
fromAmount,
fromToken,
onError,
onSuccess: () => setLastTokenAmountUpdated(undefined),
setToAmount,
setFromAmount,
toAmount,
toToken,
});

const handleToAmountChange = useCallback(
(amount: string) => {
if (lastTokenAmountUpdated !== 'to') {
setLastTokenAmountUpdated('to');
}
async (amount: string) => {
setToAmount(amount);
const hasRequiredFields = fromToken && toToken && amount;
if (!hasRequiredFields) {
return;
}
try {
const response = await getSwapQuote({
from: fromToken,
to: toToken,
amount,
amountReference: 'to',
});
if (isSwapError(response)) {
onError?.(response);
return;
}
setFromAmount(response?.fromAmount);
} catch (error) {
onError?.(error as SwapError);
}
},
[lastTokenAmountUpdated, setLastTokenAmountUpdated, setToAmount],
[fromToken, toToken, setFromAmount, setToAmount],
);

const handleFromAmountChange = useCallback(
(amount: string) => {
if (lastTokenAmountUpdated !== 'from') {
setLastTokenAmountUpdated('from');
}
async (amount: string) => {
setFromAmount(amount);
const hasRequiredFields = fromToken && toToken && amount;
if (!hasRequiredFields) {
return;
}
try {
const response = await getSwapQuote({
from: fromToken,
to: toToken,
amount,
amountReference: 'from',
});
if (isSwapError(response)) {
onError?.(response);
return;
}
setToAmount(response?.toAmount);
} catch (error) {
onError?.(error as SwapError);
}
},
[lastTokenAmountUpdated, setFromAmount, setLastTokenAmountUpdated],
[fromToken, toToken, setFromAmount, setToAmount],
);

const value = useMemo(() => {
Expand All @@ -62,6 +82,7 @@ export function Swap({ account, children, onError }: SwapReact) {
fromToken,
handleFromAmountChange,
handleToAmountChange,
setFromToken,
setToAmount,
setToToken,
toAmount,
Expand Down
17 changes: 10 additions & 7 deletions src/swap/components/SwapAmountInputV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,20 @@ export function SwapAmountInputV2({ label, token, type }: SwapAmountInputReact)
return setFromToken;
}, [type, setFromToken, setToToken]);

const handleAmountChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
if (isValidAmount(event.target.value)) {
setAmount?.(event.target.value);
}
},
[setAmount],
);

useEffect(() => {
if (token) {
setToken(token);
}
}, [token]);

const handleAmountChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
if (isValidAmount(event.target.value)) {
setAmount?.(event.target.value);
}
}, []);
}, [token, setToken]);

return (
<div
Expand Down
84 changes: 0 additions & 84 deletions src/swap/hooks/useGetSwapQuote.ts

This file was deleted.

0 comments on commit 0c0c4b3

Please sign in to comment.