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 <kristie.huang@uniswap.org>
This commit is contained in:
Kristie Huang 2023-10-02 13:04:30 -04:00 committed by GitHub
parent 1c76277c46
commit 3ad4fb6846
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 2 deletions

@ -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))
})
})
})

@ -11,11 +11,12 @@ export default function useAccountRiskCheck(account: string | null | undefined)
const riskCheckLocalStorageKey = `risk-check-${account}` const riskCheckLocalStorageKey = `risk-check-${account}`
const now = Date.now() const now = Date.now()
try { try {
// Check local browser cache
const storedTime = localStorage.getItem(riskCheckLocalStorageKey) const storedTime = localStorage.getItem(riskCheckLocalStorageKey)
const checkExpirationTime = storedTime ? parseInt(storedTime) : now - 1 const checkExpirationTime = storedTime ? parseInt(storedTime) : now - 1
if (checkExpirationTime < Date.now()) { if (checkExpirationTime < Date.now()) {
const headers = new Headers({ 'Content-Type': 'application/json' }) const headers = new Headers({ 'Content-Type': 'application/json' })
fetch('https://screening-worker.uniswap.workers.dev', { fetch('https://api.uniswap.org/v1/screen', {
method: 'POST', method: 'POST',
headers, headers,
body: JSON.stringify({ address: account }), body: JSON.stringify({ address: account }),
@ -26,9 +27,12 @@ export default function useAccountRiskCheck(account: string | null | undefined)
dispatch(setOpenModal(ApplicationModal.BLOCKED_ACCOUNT)) dispatch(setOpenModal(ApplicationModal.BLOCKED_ACCOUNT))
} }
}) })
.catch(() => dispatch(setOpenModal(null))) .catch(() => {
dispatch(setOpenModal(null))
})
} }
} finally { } finally {
// Set item to have 1 day local cache storage
localStorage.setItem(riskCheckLocalStorageKey, (now + ms(`1d`)).toString()) localStorage.setItem(riskCheckLocalStorageKey, (now + ms(`1d`)).toString())
} }
} }