Skip to content

Commit

Permalink
fix(twap): cache fb handler verification for 10min (#5200)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 authored Dec 13, 2024
1 parent afb9fff commit c09f073
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ export async function verifyExtensibleFallback(
try {
const domainVerifier = await signatureVerifierContract.callStatic.domainVerifiers(safeAddress, domainSeparator)

console.log('FALLBACK HANDLER CHECKED, domainVerifier: ', domainVerifier)
if (domainVerifier.toLowerCase() === composableCowContractAddress.toLowerCase()) {
return ExtensibleFallbackVerification.HAS_DOMAIN_VERIFIER
}

return ExtensibleFallbackVerification.HAS_EXTENSIBLE_FALLBACK
} catch {
} catch (e) {
console.log('FALLBACK HANDLER CHECKED, error: ', e)
return ExtensibleFallbackVerification.HAS_NOTHING
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,41 @@ import { useEffect } from 'react'

import { useWalletInfo } from '@cowprotocol/wallet'

import ms from 'ms.macro'
import { useAsyncMemo } from 'use-async-memo'

import { useExtensibleFallbackContext } from '../hooks/useExtensibleFallbackContext'
import { useFallbackHandlerVerification } from '../hooks/useFallbackHandlerVerification'
import { ExtensibleFallbackVerification, verifyExtensibleFallback } from '../services/verifyExtensibleFallback'
import { updateFallbackHandlerVerificationAtom } from '../state/fallbackHandlerVerificationAtom'

const FB_CACHE_TIME = ms`10m`
const FB_UPDATE_TIME_KEY = 'fallbackHandlerUpdateTime'

export function FallbackHandlerVerificationUpdater() {
const { account } = useWalletInfo()
const update = useSetAtom(updateFallbackHandlerVerificationAtom)
const verification = useFallbackHandlerVerification()
const isFallbackHandlerRequired = verification !== ExtensibleFallbackVerification.HAS_DOMAIN_VERIFIER

const fallbackHandlerUpdateTime = localStorage.getItem(FB_UPDATE_TIME_KEY)
const isCacheOutdated = !fallbackHandlerUpdateTime || Date.now() - +fallbackHandlerUpdateTime > FB_CACHE_TIME

const extensibleFallbackContext = useExtensibleFallbackContext()
const fallbackHandlerVerification = useAsyncMemo(
() =>
extensibleFallbackContext && isFallbackHandlerRequired
extensibleFallbackContext && (isFallbackHandlerRequired || isCacheOutdated)
? verifyExtensibleFallback(extensibleFallbackContext)
: null,
[isFallbackHandlerRequired, extensibleFallbackContext],
null
[isFallbackHandlerRequired, isCacheOutdated, extensibleFallbackContext],
null,
)

useEffect(() => {
if (!account || fallbackHandlerVerification === null) return

update({ [account]: fallbackHandlerVerification })
localStorage.setItem(FB_UPDATE_TIME_KEY, Date.now().toString())
}, [fallbackHandlerVerification, update, account])

return null
Expand Down

0 comments on commit c09f073

Please sign in to comment.