diff --git a/src/swap/hooks/useFundSwapTokens.test.ts b/src/swap/hooks/useFundSwapTokens.test.ts deleted file mode 100644 index 83b5cb8b19..0000000000 --- a/src/swap/hooks/useFundSwapTokens.test.ts +++ /dev/null @@ -1,116 +0,0 @@ -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 '../../internal/hooks/useValue'; -import type { Token } from '../../token'; -import { USDC_TOKEN } from '../mocks'; -import { useFundSwapTokens } from './useFundSwapTokens'; -import { useSwapBalances } from './useSwapBalances'; - -vi.mock('./useSwapBalances', () => ({ - useSwapBalances: vi.fn(), -})); - -vi.mock('../../internal/hooks/useValue', () => ({ - useValue: vi.fn(), -})); - -const toToken: Token = { - name: 'DEGEN', - address: '0x4ed4e862860bed51a9570b96d89af5e1b0efefed', - symbol: 'DEGEN', - decimals: 18, - image: - 'https://d3r81g40ycuhqg.cloudfront.net/wallet/wais/3b/bf/3bbf118b5e6dc2f9e7fc607a6e7526647b4ba8f0bea87125f971446d57b296d2-MDNmNjY0MmEtNGFiZi00N2I0LWIwMTItMDUyMzg2ZDZhMWNm', - chainId: base.id, -}; - -describe('useFundSwapTokens', () => { - beforeEach(() => { - vi.clearAllMocks(); - }); - - it('should return correct values', () => { - (useSwapBalances as Mock).mockReturnValue({ - fromBalanceString: '100', - fromTokenBalanceError: null, - fromTokenResponse: { refetch: vi.fn() }, - toBalanceString: '200', - toTokenBalanceError: null, - toTokenResponse: { refetch: vi.fn() }, - }); - (useValue as Mock).mockImplementation((props) => ({ - ...props, - amount: '100', - amountUSD: '150', - response: props.response, - setAmount: vi.fn(), - setAmountUSD: vi.fn(), - setLoading: vi.fn(), - token: USDC_TOKEN, - })); - const { result } = renderHook(() => useFundSwapTokens(toToken, '0x123')); - expect(result.current.fromETH).toEqual({ - amount: '100', - amountUSD: '150', - balance: '100', - balanceResponse: { refetch: expect.any(Function) }, - error: null, - loading: false, - setAmount: expect.any(Function), - setAmountUSD: expect.any(Function), - setLoading: expect.any(Function), - token: USDC_TOKEN, - }); - expect(result.current.to).toEqual({ - amount: '100', - amountUSD: '150', - balance: '200', - balanceResponse: { refetch: expect.any(Function) }, - error: null, - loading: false, - setAmount: expect.any(Function), - setAmountUSD: expect.any(Function), - setLoading: expect.any(Function), - token: USDC_TOKEN, - }); - }); - - it('should call fromTokenResponse.refetch when fromETH.response.refetch is called', async () => { - const mockFromRefetch = vi.fn().mockResolvedValue(undefined); - const mockToRefetch = vi.fn().mockResolvedValue(undefined); - (useSwapBalances as Mock).mockReturnValue({ - fromTokenResponse: { refetch: mockFromRefetch }, - toTokenResponse: { refetch: mockToRefetch }, - }); - (useValue as Mock).mockImplementation((props) => ({ - ...props, - response: props.response, - })); - const { result } = renderHook(() => useFundSwapTokens(toToken, '0x123')); - await act(async () => { - await result.current.fromETH.balanceResponse?.refetch(); - }); - expect(mockFromRefetch).toHaveBeenCalledTimes(1); - expect(mockToRefetch).not.toHaveBeenCalled(); - }); - - it('should call toTokenResponse.refetch when to.response.refetch is called', async () => { - const mockFromRefetch = vi.fn().mockResolvedValue(undefined); - const mockToRefetch = vi.fn().mockResolvedValue(undefined); - (useSwapBalances as Mock).mockReturnValue({ - fromTokenResponse: { refetch: mockFromRefetch }, - toTokenResponse: { refetch: mockToRefetch }, - }); - (useValue as Mock).mockImplementation((props) => ({ - ...props, - response: props.response, - })); - const { result } = renderHook(() => useFundSwapTokens(toToken, '0x123')); - await act(async () => { - await result.current.to.balanceResponse?.refetch(); - }); - expect(mockToRefetch).toHaveBeenCalledTimes(1); - expect(mockFromRefetch).not.toHaveBeenCalled(); - }); -}); diff --git a/src/swap/hooks/useFundSwapTokens.ts b/src/swap/hooks/useFundSwapTokens.ts deleted file mode 100644 index 547f934cfb..0000000000 --- a/src/swap/hooks/useFundSwapTokens.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { useState } from 'react'; -import type { Address } from 'viem'; -import { ethToken, usdcToken } from '../constants'; -import { useValue } from '../../internal/hooks/useValue'; -import type { Token } from '../../token'; -import type { FundSwapTokens } from '../types'; -import { useSwapBalances } from './useSwapBalances'; - -export const useFundSwapTokens = ( - toToken: Token, - address?: Address, -): FundSwapTokens => { - const [toAmount, setToAmount] = useState(''); - const [toAmountUSD, setToAmountUSD] = useState(''); - const [toLoading, setToLoading] = useState(false); - const [fromETHAmount, setFromETHAmount] = useState(''); - const [fromETHAmountUSD, setFromETHAmountUSD] = useState(''); - const [fromETHLoading, setFromETHLoading] = useState(false); - const [fromUSDCAmount, setFromUSDCAmount] = useState(''); - const [fromUSDCAmountUSD, setFromUSDCAmountUSD] = useState(''); - const [fromUSDCLoading, setFromUSDCLoading] = useState(false); - - const { - fromBalanceString: fromETHBalanceString, - fromTokenBalanceError: fromEthBalanceError, - toBalanceString, - toTokenBalanceError, - fromTokenResponse: fromETHResponse, - toTokenResponse, - } = useSwapBalances({ address, fromToken: ethToken, toToken }); - - const { - fromBalanceString: fromUSDCBalanceString, - fromTokenBalanceError: fromUSDCBalanceError, - fromTokenResponse: fromUSDCResponse, - } = useSwapBalances({ address, fromToken: usdcToken, toToken }); - - const fromETH = useValue({ - balance: fromETHBalanceString, - balanceResponse: fromETHResponse, - amount: fromETHAmount, - setAmount: setFromETHAmount, - amountUSD: fromETHAmountUSD, - setAmountUSD: setFromETHAmountUSD, - token: ethToken, - loading: fromETHLoading, - setLoading: setFromETHLoading, - error: fromEthBalanceError, - }); - - const fromUSDC = useValue({ - balance: fromUSDCBalanceString, - balanceResponse: fromUSDCResponse, - amount: fromUSDCAmount, - setAmount: setFromUSDCAmount, - amountUSD: fromUSDCAmountUSD, - setAmountUSD: setFromUSDCAmountUSD, - token: usdcToken, - loading: fromUSDCLoading, - setLoading: setFromUSDCLoading, - error: fromUSDCBalanceError, - }); - - const to = useValue({ - balance: toBalanceString, - balanceResponse: toTokenResponse, - amount: toAmount, - amountUSD: toAmountUSD, - setAmountUSD: setToAmountUSD, - setAmount: setToAmount, - token: toToken, - loading: toLoading, - setLoading: setToLoading, - error: toTokenBalanceError, - }); - - return { fromETH, fromUSDC, to }; -}; diff --git a/src/swap/hooks/useResetFundSwapInputs.test.ts b/src/swap/hooks/useResetFundSwapInputs.test.ts deleted file mode 100644 index 2fc25bccb3..0000000000 --- a/src/swap/hooks/useResetFundSwapInputs.test.ts +++ /dev/null @@ -1,150 +0,0 @@ -import { act, renderHook } from '@testing-library/react'; -import { beforeEach, describe, expect, it, vi } from 'vitest'; -import type { FundSwapUnit } from '../types'; -import { useResetFundSwapInputs } from './useResetFundSwapInputs'; - -describe('useResetFundSwapInputs', () => { - const mockFromTokenResponse = { - refetch: vi.fn().mockResolvedValue(undefined), - }; - const mockFromUSDCTokenResponse = { - refetch: vi.fn().mockResolvedValue(undefined), - }; - const mockToTokenResponse = { refetch: vi.fn().mockResolvedValue(undefined) }; - const mockFromETH: FundSwapUnit = { - balance: '100', - balanceResponse: mockFromTokenResponse, - amount: '50', - setAmount: vi.fn(), - setAmountUSD: vi.fn(), - token: undefined, - loading: false, - setLoading: vi.fn(), - error: undefined, - }; - const mockFromUSDC: FundSwapUnit = { - balance: '100', - balanceResponse: mockFromUSDCTokenResponse, - amount: '50', - setAmount: vi.fn(), - setAmountUSD: vi.fn(), - token: undefined, - loading: false, - setLoading: vi.fn(), - error: undefined, - }; - const mockTo: FundSwapUnit = { - balance: '200', - balanceResponse: mockToTokenResponse, - amount: '75', - setAmount: vi.fn(), - setAmountUSD: vi.fn(), - token: undefined, - loading: false, - setLoading: vi.fn(), - error: undefined, - }; - - beforeEach(() => { - vi.clearAllMocks(); - }); - - it('should return a function', () => { - const { result } = renderHook(() => - useResetFundSwapInputs({ - fromETH: mockFromETH, - fromUSDC: mockFromUSDC, - to: mockTo, - }), - ); - expect(typeof result.current).toBe('function'); - }); - - it('should call refetch on responses and set amounts to empty strings when executed', async () => { - const { result } = renderHook(() => - useResetFundSwapInputs({ - fromETH: mockFromETH, - fromUSDC: mockFromUSDC, - to: mockTo, - }), - ); - await act(async () => { - await result.current(); - }); - expect(mockFromTokenResponse.refetch).toHaveBeenCalledTimes(1); - expect(mockToTokenResponse.refetch).toHaveBeenCalledTimes(1); - expect(mockFromETH.setAmount).toHaveBeenCalledWith(''); - expect(mockFromETH.setAmountUSD).toHaveBeenCalledWith(''); - expect(mockTo.setAmount).toHaveBeenCalledWith(''); - expect(mockTo.setAmountUSD).toHaveBeenCalledWith(''); - }); - - it("should not create a new function reference if from and to haven't changed", () => { - const { result, rerender } = renderHook(() => - useResetFundSwapInputs({ - fromETH: mockFromETH, - fromUSDC: mockFromUSDC, - to: mockTo, - }), - ); - const firstRender = result.current; - rerender(); - expect(result.current).toBe(firstRender); - }); - - it('should create a new function reference if from or to change', () => { - const { result, rerender } = renderHook( - ({ fromETH, fromUSDC, to }) => - useResetFundSwapInputs({ - fromETH, - fromUSDC, - to, - }), - { - initialProps: { - fromETH: mockFromETH, - fromUSDC: mockFromUSDC, - to: mockTo, - }, - }, - ); - const firstRender = result.current; - const newMockFromETH = { - ...mockFromETH, - balanceResponse: { refetch: vi.fn().mockResolvedValue(undefined) }, - }; - const newMockFromUSDC = { - ...mockFromUSDC, - balanceResponse: { refetch: vi.fn().mockResolvedValue(undefined) }, - }; - rerender({ - fromETH: newMockFromETH, - fromUSDC: newMockFromUSDC, - to: mockTo, - }); - expect(result.current).not.toBe(firstRender); - }); - - it('should handle null responses gracefully', async () => { - const mockFromWithNullResponse = { ...mockFromETH, balanceResponse: null }; - const mockFromUSDCWithNullResponse = { - ...mockFromUSDC, - balanceResponse: null, - }; - const mockToWithNullResponse = { ...mockTo, balanceResponse: null }; - const { result } = renderHook(() => - useResetFundSwapInputs({ - fromETH: mockFromWithNullResponse, - fromUSDC: mockFromUSDCWithNullResponse, - to: mockToWithNullResponse, - }), - ); - await act(async () => { - await result.current(); - }); - expect(mockFromWithNullResponse.setAmount).toHaveBeenCalledWith(''); - expect(mockFromWithNullResponse.setAmountUSD).toHaveBeenCalledWith(''); - expect(mockToWithNullResponse.setAmount).toHaveBeenCalledWith(''); - expect(mockToWithNullResponse.setAmountUSD).toHaveBeenCalledWith(''); - }); -}); diff --git a/src/swap/hooks/useResetFundSwapInputs.ts b/src/swap/hooks/useResetFundSwapInputs.ts deleted file mode 100644 index 26f7af2935..0000000000 --- a/src/swap/hooks/useResetFundSwapInputs.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { useCallback } from 'react'; -import type { FundSwapTokens } from '../types'; - -// Refreshes balances and inputs post-swap -export const useResetFundSwapInputs = ({ - fromETH, - fromUSDC, - to, -}: FundSwapTokens) => { - return useCallback(async () => { - await Promise.all([ - fromETH.balanceResponse?.refetch(), - fromUSDC.balanceResponse?.refetch(), - to.balanceResponse?.refetch(), - fromETH.setAmount(''), - fromUSDC.setAmount(''), - fromETH.setAmountUSD(''), - fromUSDC.setAmountUSD(''), - to.setAmount(''), - to.setAmountUSD(''), - ]); - }, [fromETH, fromUSDC, to]); -};