diff --git a/src/swap/hooks/useSwapLiteToken.test.ts b/src/buy/hooks/useBuyToken.test.ts similarity index 89% rename from src/swap/hooks/useSwapLiteToken.test.ts rename to src/buy/hooks/useBuyToken.test.ts index 4d4d6dc9fd..f229d41a4c 100644 --- a/src/swap/hooks/useSwapLiteToken.test.ts +++ b/src/buy/hooks/useBuyToken.test.ts @@ -2,12 +2,12 @@ import { act, renderHook } from '@testing-library/react'; import { base } from 'viem/chains'; import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; import { useValue } from '../../core-react/internal/hooks/useValue'; +import { useSwapBalances } from '../../swap/hooks/useSwapBalances'; import type { Token } from '../../token'; import { usdcToken } from '../../token/constants'; -import { useSwapBalances } from './useSwapBalances'; -import { useSwapLiteToken } from './useSwapLiteToken'; +import { useBuyToken } from './useBuyToken'; -vi.mock('./useSwapBalances', () => ({ +vi.mock('../../swap/hooks/useSwapBalances', () => ({ useSwapBalances: vi.fn(), })); @@ -25,7 +25,7 @@ const toToken: Token = { chainId: base.id, }; -describe('useSwapLiteToken', () => { +describe('useBuyToken', () => { beforeEach(() => { vi.clearAllMocks(); }); @@ -43,7 +43,7 @@ describe('useSwapLiteToken', () => { ...props, })); const { result } = renderHook(() => - useSwapLiteToken(toToken, usdcToken, '0x123'), + useBuyToken(toToken, usdcToken, '0x123'), ); expect(result.current).toEqual({ amount: '', @@ -71,7 +71,7 @@ describe('useSwapLiteToken', () => { response: props.response, })); const { result } = renderHook(() => - useSwapLiteToken(toToken, usdcToken, '0x123'), + useBuyToken(toToken, usdcToken, '0x123'), ); await act(async () => { await result.current.balanceResponse?.refetch(); diff --git a/src/swap/hooks/useSwapLiteToken.ts b/src/buy/hooks/useBuyToken.ts similarity index 88% rename from src/swap/hooks/useSwapLiteToken.ts rename to src/buy/hooks/useBuyToken.ts index 8c3f620517..7be9f07f28 100644 --- a/src/swap/hooks/useSwapLiteToken.ts +++ b/src/buy/hooks/useBuyToken.ts @@ -1,10 +1,10 @@ import { useState } from 'react'; import type { Address } from 'viem'; import { useValue } from '../../core-react/internal/hooks/useValue'; +import { useSwapBalances } from '../../swap/hooks/useSwapBalances'; import type { Token } from '../../token'; -import { useSwapBalances } from './useSwapBalances'; -export const useSwapLiteToken = ( +export const useBuyToken = ( toToken: Token, token: Token | undefined, address: Address | undefined, diff --git a/src/swap/hooks/useSwapLiteTokens.test.ts b/src/buy/hooks/useBuyTokens.test.ts similarity index 83% rename from src/swap/hooks/useSwapLiteTokens.test.ts rename to src/buy/hooks/useBuyTokens.test.ts index 7b8280f6f8..5da700332f 100644 --- a/src/swap/hooks/useSwapLiteTokens.test.ts +++ b/src/buy/hooks/useBuyTokens.test.ts @@ -2,6 +2,7 @@ import { act, renderHook } from '@testing-library/react'; import { base } from 'viem/chains'; import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; import { useValue } from '../../core-react/internal/hooks/useValue'; +import { useSwapBalances } from '../../swap/hooks/useSwapBalances'; import type { Token } from '../../token'; import { daiToken, @@ -9,12 +10,11 @@ import { ethToken, usdcToken, } from '../../token/constants'; -import { useSwapBalances } from './useSwapBalances'; -import { useSwapLiteToken } from './useSwapLiteToken'; -import { useSwapLiteTokens } from './useSwapLiteTokens'; +import { useBuyToken } from './useBuyToken'; +import { useBuyTokens } from './useBuyTokens'; -vi.mock('./useSwapLiteToken'); -vi.mock('./useSwapBalances'); +vi.mock('./useBuyToken'); +vi.mock('../../swap/hooks/useSwapBalances'); vi.mock('../../core-react/internal/hooks/useValue'); const toToken: Token = { @@ -73,10 +73,10 @@ const mockTo = { const address = '0x123'; -describe('useSwapLiteTokens', () => { +describe('useBuyTokens', () => { beforeEach(() => { vi.clearAllMocks(); - (useSwapLiteToken as Mock).mockImplementation((_toToken, fromToken) => { + (useBuyToken as Mock).mockImplementation((_toToken, fromToken) => { if (fromToken === ethToken) { return mockFromETH; } @@ -97,12 +97,12 @@ describe('useSwapLiteTokens', () => { it('should return expected swap tokens', () => { const { result } = renderHook(() => - useSwapLiteTokens(toToken, daiToken, address), + useBuyTokens(toToken, daiToken, address), ); - expect(useSwapLiteToken).toHaveBeenCalledWith(toToken, ethToken, address); - expect(useSwapLiteToken).toHaveBeenCalledWith(toToken, usdcToken, address); - expect(useSwapLiteToken).toHaveBeenCalledWith(toToken, daiToken, address); + expect(useBuyToken).toHaveBeenCalledWith(toToken, ethToken, address); + expect(useBuyToken).toHaveBeenCalledWith(toToken, usdcToken, address); + expect(useBuyToken).toHaveBeenCalledWith(toToken, daiToken, address); expect(useSwapBalances).toHaveBeenCalledWith({ address, fromToken: ethToken, @@ -130,7 +130,7 @@ describe('useSwapLiteTokens', () => { }); it('should handle toToken.symbol === ETH', () => { - renderHook(() => useSwapLiteTokens(ethToken, degenToken, address)); + renderHook(() => useBuyTokens(ethToken, degenToken, address)); expect(useSwapBalances).toHaveBeenCalledWith({ address, @@ -151,7 +151,7 @@ describe('useSwapLiteTokens', () => { response: props.response, })); const { result } = renderHook(() => - useSwapLiteTokens(toToken, undefined, '0x123'), + useBuyTokens(toToken, undefined, '0x123'), ); await act(async () => { await result.current.to.balanceResponse?.refetch(); diff --git a/src/swap/hooks/useSwapLiteTokens.ts b/src/buy/hooks/useBuyTokens.ts similarity index 72% rename from src/swap/hooks/useSwapLiteTokens.ts rename to src/buy/hooks/useBuyTokens.ts index e364df6faf..9a09159877 100644 --- a/src/swap/hooks/useSwapLiteTokens.ts +++ b/src/buy/hooks/useBuyTokens.ts @@ -1,20 +1,20 @@ import { useState } from 'react'; import type { Address } from 'viem'; import { useValue } from '../../core-react/internal/hooks/useValue'; +import { useSwapBalances } from '../../swap/hooks/useSwapBalances'; import type { Token } from '../../token'; import { ethToken, usdcToken } from '../../token/constants'; -import type { SwapLiteTokens } from '../types'; -import { useSwapBalances } from './useSwapBalances'; -import { useSwapLiteToken } from './useSwapLiteToken'; +import type { BuyTokens } from '../types'; +import { useBuyToken } from './useBuyToken'; -export const useSwapLiteTokens = ( +export const useBuyTokens = ( toToken: Token, fromToken?: Token, address?: Address, -): SwapLiteTokens => { - const fromETH = useSwapLiteToken(toToken, ethToken, address); - const fromUSDC = useSwapLiteToken(toToken, usdcToken, address); - const from = useSwapLiteToken(toToken, fromToken, address); +): BuyTokens => { + const fromETH = useBuyToken(toToken, ethToken, address); + const fromUSDC = useBuyToken(toToken, usdcToken, address); + const from = useBuyToken(toToken, fromToken, address); const [toAmount, setToAmount] = useState(''); const [toAmountUSD, setToAmountUSD] = useState(''); diff --git a/src/swap/hooks/useResetSwapLiteInputs.test.ts b/src/buy/hooks/useResetBuyInputs.test.ts similarity index 94% rename from src/swap/hooks/useResetSwapLiteInputs.test.ts rename to src/buy/hooks/useResetBuyInputs.test.ts index c1232384c5..452cb757ae 100644 --- a/src/swap/hooks/useResetSwapLiteInputs.test.ts +++ b/src/buy/hooks/useResetBuyInputs.test.ts @@ -1,9 +1,9 @@ import { act, renderHook } from '@testing-library/react'; import { beforeEach, describe, expect, it, vi } from 'vitest'; -import type { SwapUnit } from '../types'; -import { useResetSwapLiteInputs } from './useResetSwapLiteInputs'; +import type { SwapUnit } from '../../swap/types'; +import { useResetBuyInputs } from './useResetBuyInputs'; -describe('useResetSwapLiteInputs', () => { +describe('useResetBuyInputs', () => { const mockQueryResponse = { data: undefined, error: null, @@ -80,7 +80,7 @@ describe('useResetSwapLiteInputs', () => { it('should return a function', () => { const { result } = renderHook(() => - useResetSwapLiteInputs({ + useResetBuyInputs({ fromETH: mockFromETH, fromUSDC: mockFromUSDC, from: mockFrom, @@ -92,7 +92,7 @@ describe('useResetSwapLiteInputs', () => { it('should call refetch on responses and set amounts to empty strings when executed', async () => { const { result } = renderHook(() => - useResetSwapLiteInputs({ + useResetBuyInputs({ fromETH: mockFromETH, fromUSDC: mockFromUSDC, from: mockFrom, @@ -112,7 +112,7 @@ describe('useResetSwapLiteInputs', () => { it("should not create a new function reference if from and to haven't changed", () => { const { result, rerender } = renderHook(() => - useResetSwapLiteInputs({ + useResetBuyInputs({ fromETH: mockFromETH, fromUSDC: mockFromUSDC, to: mockTo, @@ -126,7 +126,7 @@ describe('useResetSwapLiteInputs', () => { it('should create a new function reference if from or to change', () => { const { result, rerender } = renderHook( ({ fromETH, fromUSDC, to }) => - useResetSwapLiteInputs({ + useResetBuyInputs({ fromETH, fromUSDC, to, @@ -170,7 +170,7 @@ describe('useResetSwapLiteInputs', () => { balanceResponse: null, } as unknown as SwapUnit; const { result } = renderHook(() => - useResetSwapLiteInputs({ + useResetBuyInputs({ fromETH: mockFromWithNullResponse, fromUSDC: mockFromUSDCWithNullResponse, to: mockToWithNullResponse, diff --git a/src/swap/hooks/useResetSwapLiteInputs.ts b/src/buy/hooks/useResetBuyInputs.ts similarity index 84% rename from src/swap/hooks/useResetSwapLiteInputs.ts rename to src/buy/hooks/useResetBuyInputs.ts index af62ad64ce..137ef44f9d 100644 --- a/src/swap/hooks/useResetSwapLiteInputs.ts +++ b/src/buy/hooks/useResetBuyInputs.ts @@ -1,13 +1,13 @@ import { useCallback } from 'react'; -import type { SwapLiteTokens } from '../types'; +import type { BuyTokens } from '../types'; // Refreshes balances and inputs post-swap -export const useResetSwapLiteInputs = ({ +export const useResetBuyInputs = ({ fromETH, fromUSDC, from, to, -}: SwapLiteTokens) => { +}: BuyTokens) => { return useCallback(async () => { await Promise.all([ from?.balanceResponse?.refetch(), diff --git a/src/buy/types.ts b/src/buy/types.ts new file mode 100644 index 0000000000..ac853e565e --- /dev/null +++ b/src/buy/types.ts @@ -0,0 +1,8 @@ +import type { SwapUnit } from '@/swap/types'; + +export type BuyTokens = { + fromETH: SwapUnit; + fromUSDC: SwapUnit; + to: SwapUnit; + from?: SwapUnit; +}; diff --git a/src/swap/utils/getSwapLiteQuote.test.ts b/src/buy/utils/getBuyQuote.test.ts similarity index 94% rename from src/swap/utils/getSwapLiteQuote.test.ts rename to src/buy/utils/getBuyQuote.test.ts index b31ec306e2..45aeec94e5 100644 --- a/src/swap/utils/getSwapLiteQuote.test.ts +++ b/src/buy/utils/getBuyQuote.test.ts @@ -4,7 +4,7 @@ import type { Token } from '@/token/types'; import { base } from 'viem/chains'; import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest'; import { isSwapError } from '../../swap/utils/isSwapError'; -import { getSwapLiteQuote } from './getSwapLiteQuote'; +import { getBuyQuote } from './getBuyQuote'; vi.mock('@/core/api/getSwapQuote'); vi.mock('@/core/utils/formatTokenAmount'); @@ -66,13 +66,13 @@ const mockFromSwapUnit = { token: fromToken, }; -describe('getSwapLiteQuote', () => { +describe('getBuyQuote', () => { beforeEach(() => { vi.clearAllMocks(); }); it('should return default values if `from` token is not provided', async () => { - const result = await getSwapLiteQuote({ + const result = await getBuyQuote({ amount: '1', amountReference: 'exactIn', maxSlippage: '0.5', @@ -93,7 +93,7 @@ describe('getSwapLiteQuote', () => { (isSwapError as unknown as Mock).mockReturnValue(false); (formatTokenAmount as Mock).mockReturnValue('1.0'); - const result = await getSwapLiteQuote({ + const result = await getBuyQuote({ amount: '1', amountReference: 'exactIn', from: fromToken, @@ -128,7 +128,7 @@ describe('getSwapLiteQuote', () => { (isSwapError as unknown as Mock).mockReturnValue(false); (formatTokenAmount as Mock).mockReturnValue('1.0'); - const result = await getSwapLiteQuote({ + const result = await getBuyQuote({ amount: '1', amountReference: 'exactIn', from: fromToken, @@ -168,7 +168,7 @@ describe('getSwapLiteQuote', () => { (getSwapQuote as Mock).mockResolvedValue(mockError); (isSwapError as unknown as Mock).mockReturnValue(true); - const result = await getSwapLiteQuote({ + const result = await getBuyQuote({ amount: '1', amountReference: 'exactIn', from: fromToken, @@ -187,7 +187,7 @@ describe('getSwapLiteQuote', () => { }); it('should not call `getSwapQuote` if `from` and `to` tokens are the same', async () => { - const result = await getSwapLiteQuote({ + const result = await getBuyQuote({ amount: '1', amountReference: 'exactIn', from: fromToken, diff --git a/src/swap/utils/getSwapLiteQuote.ts b/src/buy/utils/getBuyQuote.ts similarity index 81% rename from src/swap/utils/getSwapLiteQuote.ts rename to src/buy/utils/getBuyQuote.ts index cd1009a76d..0234c3dec7 100644 --- a/src/swap/utils/getSwapLiteQuote.ts +++ b/src/buy/utils/getBuyQuote.ts @@ -9,14 +9,14 @@ import type { SwapError, SwapUnit } from '../../swap/types'; import { isSwapError } from '../../swap/utils/isSwapError'; import type { Token } from '../../token'; -type GetSwapLiteQuoteResponse = { +type GetBuyQuoteResponse = { response?: GetSwapQuoteResponse; error?: APIError; formattedFromAmount?: string; }; -type GetSwapLiteQuoteParams = Omit & { - fromSwapUnit: SwapUnit; +type GetBuyQuoteParams = Omit & { + fromSwapUnit?: SwapUnit; from?: Token; }; @@ -24,7 +24,7 @@ type GetSwapLiteQuoteParams = Omit & { * Fetches a quote for a swap, but only if the from and to tokens are different. */ -export async function getSwapLiteQuote({ +export async function getBuyQuote({ amount, amountReference, from, @@ -32,7 +32,7 @@ export async function getSwapLiteQuote({ to, useAggregator, fromSwapUnit, -}: GetSwapLiteQuoteParams): Promise { +}: GetBuyQuoteParams): Promise { // only fetch quote if the from token is provided if (!from) { return { response: undefined, formattedFromAmount: '', error: undefined }; @@ -57,8 +57,8 @@ export async function getSwapLiteQuote({ ? formatTokenAmount(response.fromAmount, response.from.decimals) : ''; - fromSwapUnit.setAmountUSD(response?.fromAmountUSD || ''); - fromSwapUnit.setAmount(formattedFromAmount || ''); + fromSwapUnit?.setAmountUSD(response?.fromAmountUSD || ''); + fromSwapUnit?.setAmount(formattedFromAmount || ''); } let error: SwapError | undefined; diff --git a/src/swap/types.ts b/src/swap/types.ts index 44436dbcfc..cc9dd656bc 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -173,13 +173,6 @@ export type ProcessSwapTransactionParams = { walletCapabilities: WalletCapabilities; // EIP-5792 wallet capabilities }; -export type SwapLiteTokens = { - fromETH: SwapUnit; - fromUSDC: SwapUnit; - to: SwapUnit; - from?: SwapUnit; -}; - /** * Note: exported as public Type */