-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add SwapLite
util functions
#1648
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ba1d3b1
add useFundSwapTokens utility
alissacrane-cb 3bfa89c
add useResetFundSwapInputs utility
alissacrane-cb 15f0da5
remove previous functions
alissacrane-cb 3002cc4
add new uti functions
alissacrane-cb ca39272
fix imports
alissacrane-cb dd2d3db
add test coverage
alissacrane-cb df789e9
fix test
alissacrane-cb ac84347
fix lint
alissacrane-cb c727598
fix lint
alissacrane-cb 501a141
fix test
alissacrane-cb 33b694a
move util
alissacrane-cb 6298039
fix test
alissacrane-cb 8cedbe0
move tokens
alissacrane-cb 3873823
fix lint
alissacrane-cb d876180
refactor useSwapLiteTokens hook
alissacrane-cb 46109b4
fix lint and formatting
alissacrane-cb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,187 @@ | ||
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 mockQueryResponse = { | ||
data: undefined, | ||
error: null, | ||
isError: false, | ||
isPending: true, | ||
isSuccess: false, | ||
status: 'pending', | ||
} as const; | ||
|
||
const mockFromTokenResponse = { | ||
...mockQueryResponse, | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockFromETHTokenResponse = { | ||
...mockQueryResponse, | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockFromUSDCTokenResponse = { | ||
...mockQueryResponse, | ||
refetch: vi.fn().mockResolvedValue(undefined), | ||
}; | ||
const mockToTokenResponse = { | ||
...mockQueryResponse, | ||
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, | ||
} as unknown as SwapUnit; | ||
const mockFromETH: SwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromETHTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
} as unknown as SwapUnit; | ||
const mockFromUSDC: SwapUnit = { | ||
balance: '100', | ||
balanceResponse: mockFromUSDCTokenResponse, | ||
amount: '50', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
} as unknown as SwapUnit; | ||
const mockTo: SwapUnit = { | ||
balance: '200', | ||
balanceResponse: mockToTokenResponse, | ||
amount: '75', | ||
setAmount: vi.fn(), | ||
setAmountUSD: vi.fn(), | ||
token: undefined, | ||
loading: false, | ||
setLoading: vi.fn(), | ||
error: undefined, | ||
} as unknown as SwapUnit; | ||
|
||
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) }, | ||
} as unknown as SwapUnit; | ||
const newMockFromUSDC = { | ||
...mockFromUSDC, | ||
balanceResponse: { refetch: vi.fn().mockResolvedValue(undefined) }, | ||
} as unknown as SwapUnit; | ||
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, | ||
} as unknown as SwapUnit; | ||
const mockFromUSDCWithNullResponse = { | ||
...mockFromUSDC, | ||
balanceResponse: null, | ||
} as unknown as SwapUnit; | ||
const mockToWithNullResponse = { | ||
...mockTo, | ||
balanceResponse: null, | ||
} as unknown as SwapUnit; | ||
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]); | ||
}; | ||
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,82 @@ | ||
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 type { Token } from '../../token'; | ||
import { usdcToken } from '../../token/constants'; | ||
import { useSwapBalances } from './useSwapBalances'; | ||
import { useSwapLiteToken } from './useSwapLiteToken'; | ||
|
||
vi.mock('./useSwapBalances', () => ({ | ||
useSwapBalances: vi.fn(), | ||
})); | ||
|
||
vi.mock('../../core-react/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('useSwapLiteToken', () => { | ||
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, | ||
})); | ||
const { result } = renderHook(() => | ||
useSwapLiteToken(toToken, usdcToken, '0x123'), | ||
); | ||
expect(result.current).toEqual({ | ||
amount: '', | ||
amountUSD: '', | ||
balance: '100', | ||
balanceResponse: { refetch: expect.any(Function) }, | ||
error: null, | ||
loading: false, | ||
setAmount: expect.any(Function), | ||
setAmountUSD: expect.any(Function), | ||
setLoading: expect.any(Function), | ||
token: usdcToken, | ||
}); | ||
}); | ||
|
||
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(() => | ||
useSwapLiteToken(toToken, usdcToken, '0x123'), | ||
); | ||
await act(async () => { | ||
await result.current.balanceResponse?.refetch(); | ||
}); | ||
expect(mockFromRefetch).toHaveBeenCalledTimes(1); | ||
expect(mockToRefetch).not.toHaveBeenCalled(); | ||
}); | ||
}); |
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,34 @@ | ||
import { useState } from 'react'; | ||
import type { Address } from 'viem'; | ||
import { useValue } from '../../core-react/internal/hooks/useValue'; | ||
import type { Token } from '../../token'; | ||
import { useSwapBalances } from './useSwapBalances'; | ||
|
||
export const useSwapLiteToken = ( | ||
toToken: Token, | ||
token: Token | undefined, | ||
address: Address | undefined, | ||
) => { | ||
const [amount, setAmount] = useState(''); | ||
const [amountUSD, setAmountUSD] = useState(''); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const { | ||
fromBalanceString: balance, | ||
fromTokenBalanceError: error, | ||
fromTokenResponse: balanceResponse, | ||
} = useSwapBalances({ address, fromToken: token, toToken }); | ||
|
||
return useValue({ | ||
balance, | ||
balanceResponse, | ||
amount, | ||
setAmount, | ||
amountUSD, | ||
setAmountUSD, | ||
token, | ||
loading, | ||
setLoading, | ||
error, | ||
}); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this may be more flexible accepting an array of SwapUnits, it seems like some of this logic could be shared between swap and lite
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
created a ticket to refactor later