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:
parent
1c76277c46
commit
3ad4fb6846
56
src/hooks/useAccountRiskCheck.test.ts
Normal file
56
src/hooks/useAccountRiskCheck.test.ts
Normal file
@ -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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user