Commit 566da074 authored by Jordan Frankfurt's avatar Jordan Frankfurt Committed by GitHub

feat(risk): cache risk check with ttl (#3965)

parent 31a3840b
import { sendEvent } from 'components/analytics' import { sendEvent } from 'components/analytics'
import ms from 'ms.macro'
import { useEffect } from 'react' import { useEffect } from 'react'
import { ApplicationModal, setOpenModal } from 'state/application/reducer' import { ApplicationModal, setOpenModal } from 'state/application/reducer'
import { useAppDispatch } from 'state/hooks' import { useAppDispatch } from 'state/hooks'
...@@ -8,26 +9,34 @@ export default function useAccountRiskCheck(account: string | null | undefined) ...@@ -8,26 +9,34 @@ export default function useAccountRiskCheck(account: string | null | undefined)
useEffect(() => { useEffect(() => {
if (account) { if (account) {
const headers = new Headers({ 'Content-Type': 'application/json' }) const riskCheckLocalStorageKey = `risk-check-${account}`
fetch('https://screening-worker.uniswap.workers.dev', { const now = Date.now()
method: 'POST', try {
headers, const storedTime = localStorage.getItem(riskCheckLocalStorageKey)
body: JSON.stringify({ address: account }), const checkExpirationTime = storedTime ? parseInt(storedTime) : now - 1
}) if (checkExpirationTime < Date.now()) {
.then((res) => res.json()) const headers = new Headers({ 'Content-Type': 'application/json' })
.then((data) => { fetch('https://screening-worker.uniswap.workers.dev', {
if (data.block) { method: 'POST',
dispatch(setOpenModal(ApplicationModal.BLOCKED_ACCOUNT)) headers,
sendEvent({ body: JSON.stringify({ address: account }),
category: 'Address Screening', })
action: 'blocked', .then((res) => res.json())
label: account, .then((data) => {
if (data.block) {
dispatch(setOpenModal(ApplicationModal.BLOCKED_ACCOUNT))
sendEvent({
category: 'Address Screening',
action: 'blocked',
label: account,
})
}
}) })
} .catch(() => dispatch(setOpenModal(null)))
}) }
.catch(() => { } finally {
dispatch(setOpenModal(null)) localStorage.setItem(riskCheckLocalStorageKey, (now + ms`7 days`).toString())
}) }
} }
}, [account, dispatch]) }, [account, dispatch])
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment