-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not send permit to quote when enough allowance (#3433)
* fix: do not send permit to quote when enough allowance * fix: remove hook data, and remove unnecessary re-renders * docs: add permit state uml * fix: make sure we log to sentry if we sign with the default signer in permits --------- Co-authored-by: Alfetopito <[email protected]>
- Loading branch information
1 parent
5f45d8e
commit 58b6ade
Showing
9 changed files
with
611 additions
and
21 deletions.
There are no files selected for viewing
62 changes: 52 additions & 10 deletions
62
apps/cowswap-frontend/src/modules/appData/updater/AppDataHooksUpdater.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,67 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useEffect, useMemo, useRef } from 'react' | ||
|
||
import { PermitHookData } from '@cowprotocol/permit-utils' | ||
|
||
import { useAccountAgnosticPermitHookData } from 'modules/permit' | ||
import { useDerivedSwapInfo } from 'modules/swap/hooks/useSwapState' | ||
|
||
import { useLimitHasEnoughAllowance } from '../../limitOrders/hooks/useTradeFlowContext' | ||
import { useSwapEnoughAllowance } from '../../swap/hooks/useSwapFlowContext' | ||
import { useUpdateAppDataHooks } from '../hooks' | ||
import { buildAppDataHooks } from '../utils/buildAppDataHooks' | ||
|
||
// const count = 0 | ||
|
||
function usePermitDataIfNotAllowance(): PermitHookData | undefined { | ||
const permitHookData = useAccountAgnosticPermitHookData() || {} | ||
|
||
// Remove permitData if the user has enough allowance for the current trade | ||
const swapHasEnoughAllowance = useSwapEnoughAllowance() | ||
const limitHasEnoughAllowance = useLimitHasEnoughAllowance() | ||
const shouldUsePermit = swapHasEnoughAllowance === false || limitHasEnoughAllowance === false | ||
|
||
const { target, callData, gasLimit }: Partial<PermitHookData> = permitHookData || {} | ||
|
||
return useMemo(() => { | ||
if (!target || !callData || !gasLimit) { | ||
return undefined | ||
} | ||
|
||
return shouldUsePermit ? { target, callData, gasLimit } : undefined | ||
}, [shouldUsePermit, target, callData, gasLimit]) | ||
} | ||
|
||
export function AppDataHooksUpdater(): null { | ||
const { v2Trade } = useDerivedSwapInfo() | ||
const updateAppDataHooks = useUpdateAppDataHooks() | ||
const permitHookData = useAccountAgnosticPermitHookData() | ||
|
||
// To avoid dumb re-renders | ||
const ref = useRef(permitHookData) | ||
ref.current = permitHookData | ||
const stableRef = JSON.stringify(permitHookData) | ||
const permitData = usePermitDataIfNotAllowance() | ||
const permitDataPrev = useRef<PermitHookData | undefined>(undefined) | ||
const hasTradeInfo = !!v2Trade | ||
|
||
useEffect(() => { | ||
if (stableRef) { | ||
const hooks = buildAppDataHooks(ref.current ? [ref.current] : undefined) | ||
if ( | ||
!hasTradeInfo || // If there's no trade info, wait until we have one to update the hooks (i.e. missing quote) | ||
JSON.stringify(permitDataPrev.current) === JSON.stringify(permitData) // Or if the permit data has not changed | ||
) { | ||
return undefined | ||
} | ||
|
||
const hooks = buildAppDataHooks({ | ||
preInteractionHooks: permitData ? [permitData] : undefined, | ||
}) | ||
|
||
if (hooks) { | ||
// Update the hooks | ||
console.log('[AppDataHooksUpdater]: Set hooks', hooks) | ||
updateAppDataHooks(hooks) | ||
permitDataPrev.current = permitData | ||
} else { | ||
// There was a hook data, but not any more. The hook needs to be removed | ||
console.log('[AppDataHooksUpdater] Clear hooks') | ||
updateAppDataHooks(undefined) | ||
permitDataPrev.current = undefined | ||
} | ||
}, [stableRef, updateAppDataHooks]) | ||
}, [updateAppDataHooks, permitData, hasTradeInfo]) | ||
|
||
return null | ||
} |
9 changes: 6 additions & 3 deletions
9
apps/cowswap-frontend/src/modules/appData/utils/buildAppDataHooks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.