From 3ad4fb6846baed2e129aa2894d1d12942de2758d Mon Sep 17 00:00:00 2001 From: Kristie Huang Date: Mon, 2 Oct 2023 13:04:30 -0400 Subject: [PATCH] feat: change address screening service to Uniswap /screen API (#7339) * change fetch url to uniswap api * clean code + write tests * nit updates * nit-rename test --------- Co-authored-by: Kristie Huang --- src/hooks/useAccountRiskCheck.test.ts | 56 +++++++++++++++++++++++++++ src/hooks/useAccountRiskCheck.ts | 8 +++- 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/hooks/useAccountRiskCheck.test.ts diff --git a/src/hooks/useAccountRiskCheck.test.ts b/src/hooks/useAccountRiskCheck.test.ts new file mode 100644 index 0000000000..ecd8246e19 --- /dev/null +++ b/src/hooks/useAccountRiskCheck.test.ts @@ -0,0 +1,56 @@ +import { ApplicationModal, setOpenModal } from 'state/application/reducer' +import { renderHook, waitFor } from 'test-utils/render' + +import useAccountRiskCheck from './useAccountRiskCheck' + +// Mock the useAppDispatch hook +const dispatchMock = jest.fn() +jest.mock('state/hooks', () => ({ + useAppDispatch: () => dispatchMock, +})) + +describe('useAccountRiskCheck', () => { + it('should handle blocked account', async () => { + const account = 'blocked-account' + const mockResponse = { block: true } + const fetchMock = jest.spyOn(window, 'fetch').mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce(mockResponse), + } as any) + + renderHook(() => useAccountRiskCheck(account)) + + await waitFor(() => { + expect(fetchMock).toHaveBeenCalledWith('https://api.uniswap.org/v1/screen', { + method: 'POST', + headers: new Headers({ + 'Content-Type': 'application/json', + }), + body: JSON.stringify({ address: account }), + }) + + expect(dispatchMock).toHaveBeenCalledWith(setOpenModal(ApplicationModal.BLOCKED_ACCOUNT)) + }) + }) + + it('should handle non-blocked account', async () => { + const account = 'non-blocked-account' + const mockResponse = { block: false } + const fetchMock = jest.spyOn(window, 'fetch').mockResolvedValueOnce({ + json: jest.fn().mockResolvedValueOnce(mockResponse), + } as any) + + renderHook(() => useAccountRiskCheck(account)) + + await waitFor(() => { + expect(fetchMock).toHaveBeenCalledWith('https://api.uniswap.org/v1/screen', { + method: 'POST', + headers: new Headers({ + 'Content-Type': 'application/json', + }), + body: JSON.stringify({ address: account }), + }) + + expect(dispatchMock).not.toHaveBeenCalledWith(setOpenModal(ApplicationModal.BLOCKED_ACCOUNT)) + }) + }) +}) diff --git a/src/hooks/useAccountRiskCheck.ts b/src/hooks/useAccountRiskCheck.ts index 6692b8b7e3..a01b51b644 100644 --- a/src/hooks/useAccountRiskCheck.ts +++ b/src/hooks/useAccountRiskCheck.ts @@ -11,11 +11,12 @@ export default function useAccountRiskCheck(account: string | null | undefined) const riskCheckLocalStorageKey = `risk-check-${account}` const now = Date.now() try { + // Check local browser cache const storedTime = localStorage.getItem(riskCheckLocalStorageKey) const checkExpirationTime = storedTime ? parseInt(storedTime) : now - 1 if (checkExpirationTime < Date.now()) { const headers = new Headers({ 'Content-Type': 'application/json' }) - fetch('https://screening-worker.uniswap.workers.dev', { + fetch('https://api.uniswap.org/v1/screen', { method: 'POST', headers, body: JSON.stringify({ address: account }), @@ -26,9 +27,12 @@ export default function useAccountRiskCheck(account: string | null | undefined) dispatch(setOpenModal(ApplicationModal.BLOCKED_ACCOUNT)) } }) - .catch(() => dispatch(setOpenModal(null))) + .catch(() => { + dispatch(setOpenModal(null)) + }) } } finally { + // Set item to have 1 day local cache storage localStorage.setItem(riskCheckLocalStorageKey, (now + ms(`1d`)).toString()) } }