-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15f0da5
commit 3002cc4
Showing
8 changed files
with
513 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import type { SwapError, SwapUnit } from '../../swap/types'; | ||
import { isSwapError } from '../../swap/utils/isSwapError'; | ||
import type { Token } from '../../token'; | ||
import { formatTokenAmount } from '../utils/formatTokenAmount'; | ||
import { getSwapQuote } from './getSwapQuote'; | ||
import type { | ||
APIError, | ||
GetSwapQuoteParams, | ||
GetSwapQuoteResponse, | ||
} from './types'; | ||
|
||
type GetSwapLiteQuoteResponse = { | ||
response?: GetSwapQuoteResponse; | ||
error?: APIError; | ||
formattedFromAmount?: string; | ||
}; | ||
|
||
type GetSwapLiteQuoteParams = Omit<GetSwapQuoteParams, 'from'> & { | ||
fromSwapUnit: SwapUnit; | ||
from?: Token; | ||
}; | ||
|
||
export async function getSwapLiteQuote({ | ||
amount, | ||
amountReference, | ||
from, | ||
maxSlippage, | ||
to, | ||
useAggregator, | ||
fromSwapUnit, | ||
}: GetSwapLiteQuoteParams): Promise<GetSwapLiteQuoteResponse> { | ||
// only fetch quote if the from token is provided | ||
if (!from) { | ||
return { response: undefined, formattedFromAmount: '', error: undefined }; | ||
} | ||
|
||
let response: GetSwapQuoteResponse | undefined; | ||
// only fetch quote if the from and to tokens are different | ||
if (to?.symbol !== from?.symbol) { | ||
response = await getSwapQuote({ | ||
amount, | ||
amountReference, | ||
from, | ||
maxSlippage, | ||
to, | ||
useAggregator, | ||
}); | ||
} | ||
|
||
let formattedFromAmount = ''; | ||
if (response && !isSwapError(response)) { | ||
formattedFromAmount = formatTokenAmount( | ||
response.fromAmount, | ||
response.from.decimals, | ||
); | ||
|
||
fromSwapUnit.setAmountUSD(response?.fromAmountUSD || ''); | ||
fromSwapUnit.setAmount(formattedFromAmount || ''); | ||
} | ||
|
||
let error: SwapError | undefined; | ||
if (isSwapError(response)) { | ||
error = response; | ||
response = undefined; | ||
} | ||
|
||
return { response, formattedFromAmount, error }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import type { SwapUnit } from '../types'; | ||
import { useResetSwapLiteInputs } from './useResetSwapLiteInputs'; | ||
|
||
describe('useResetSwapLiteInputs', () => { | ||
const mockFromTokenResponse = { | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockFromETHTokenResponse = { | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockFromUSDCTokenResponse = { | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockToTokenResponse = { refetch: vi.fn().mockResolvedValue(undefined) }; | ||
const mockFrom: SwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
const mockFromETH: SwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromETHTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
const mockFromUSDC: SwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromUSDCTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
}; | ||
const mockTo: SwapUnit = { | ||
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(() => | ||
useResetSwapLiteInputs({ | ||
fromETH: mockFromETH, | ||
fromUSDC: mockFromUSDC, | ||
from: mockFrom, | ||
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(() => | ||
useResetSwapLiteInputs({ | ||
fromETH: mockFromETH, | ||
fromUSDC: mockFromUSDC, | ||
from: mockFrom, | ||
to: mockTo, | ||
}), | ||
); | ||
await act(async () => { | ||
await result.current(); | ||
}); | ||
expect(mockFromETHTokenResponse.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(() => | ||
useResetSwapLiteInputs({ | ||
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 }) => | ||
useResetSwapLiteInputs({ | ||
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(() => | ||
useResetSwapLiteInputs({ | ||
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(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useCallback } from 'react'; | ||
import type { SwapLiteTokens } from '../types'; | ||
|
||
// Refreshes balances and inputs post-swap | ||
export const useResetSwapLiteInputs = ({ | ||
fromETH, | ||
fromUSDC, | ||
from, | ||
to, | ||
}: SwapLiteTokens) => { | ||
return useCallback(async () => { | ||
await Promise.all([ | ||
from?.balanceResponse?.refetch(), | ||
from?.setAmount(''), | ||
from?.setAmountUSD(''), | ||
fromETH.balanceResponse?.refetch(), | ||
fromETH.setAmount(''), | ||
fromETH.setAmountUSD(''), | ||
fromUSDC.balanceResponse?.refetch(), | ||
fromUSDC.setAmount(''), | ||
fromUSDC.setAmountUSD(''), | ||
to.balanceResponse?.refetch(), | ||
to.setAmount(''), | ||
to.setAmountUSD(''), | ||
]); | ||
}, [from, fromETH, fromUSDC, to]); | ||
}; |
Oops, something went wrong.