Skip to content

Commit

Permalink
Merge pull request #3437 from cowprotocol/hotfix/1.49.7
Browse files Browse the repository at this point in the history
fix: hotfix 1.49.7
  • Loading branch information
anxolin authored Nov 24, 2023
2 parents 86743fe + 58b6ade commit f22c3cf
Show file tree
Hide file tree
Showing 10 changed files with 612 additions and 22 deletions.
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
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { AppDataHooks, PostHooks, PreHooks } from '../types'

export function buildAppDataHooks(
preInteractionHooks?: PreHooks,
export function buildAppDataHooks({
preInteractionHooks,
postInteractionHooks,
}: {
preInteractionHooks?: PreHooks
postInteractionHooks?: PostHooks
): AppDataHooks | undefined {
}): AppDataHooks | undefined {
if (!preInteractionHooks && !postInteractionHooks) {
return undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ import { useTradeQuote } from 'modules/tradeQuote'

import { useLimitOrdersDerivedState } from './useLimitOrdersDerivedState'

export function useLimitHasEnoughAllowance(): boolean | undefined {
const state = useLimitOrdersDerivedState()
const { chainId, account } = useWalletInfo()

const checkAllowanceAddress = GP_VAULT_RELAYER[chainId]
const { enoughAllowance } = useEnoughBalanceAndAllowance({
account,
amount: state.slippageAdjustedSellAmount || undefined,
checkAllowanceAddress,
})
return enoughAllowance
}

export function useTradeFlowContext(): TradeFlowContext | null {
const { provider } = useWeb3React()
const { chainId, account } = useWalletInfo()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { OrderClass } from '@cowprotocol/cow-sdk'
import { PERMIT_SIGNER } from '@cowprotocol/permit-utils'
import { Percent } from '@uniswap/sdk-core'

import * as Sentry from '@sentry/browser'

import { PriceImpact } from 'legacy/hooks/usePriceImpact'
import { partialOrderUpdate } from 'legacy/state/orders/utils'
import { signAndPostOrder } from 'legacy/utils/trade'
Expand Down Expand Up @@ -67,6 +70,14 @@ export async function tradeFlow(
generatePermitHook,
})

if (postOrderParams.appData.fullAppData.includes(PERMIT_SIGNER.address)) {
// report this to sentry if we ever use the default signer in the permit
Sentry.captureException('User signed the permit using PERMIT_SIGNER instead of their account', {
tags: { errorType: 'permitWithDefaultSigner' },
contexts: { params: { account } },
})
}

logTradeFlow('LIMIT ORDER FLOW', 'STEP 3: send transaction')
tradeFlowAnalytics.trade(swapFlowAnalyticsContext)

Expand Down
Loading

2 comments on commit f22c3cf

@vercel
Copy link

@vercel vercel bot commented on f22c3cf Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on f22c3cf Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

cosmos – ./

cowswap-seven.vercel.app
cosmos-cowswap.vercel.app
cosmos-git-main-cowswap.vercel.app
cosmos.cow.fi

Please sign in to comment.