Skip to content

Commit

Permalink
fix: do not use infinite approvals in swap when partial approve mode
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Dec 26, 2024
1 parent 98be3cd commit 8e72b4c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { latest } from '@cowprotocol/app-data'
import { CowHookDetails, HookToDappMatch, matchHooksToDappsRegistry } from '@cowprotocol/hook-dapp-lib'
import { InfoTooltip } from '@cowprotocol/ui'
import { useWalletInfo } from '@cowprotocol/wallet'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'

import { ChevronDown, ChevronUp } from 'react-feather'

Expand All @@ -22,9 +23,18 @@ interface OrderHooksDetailsProps {
children: (content: ReactElement) => ReactElement
margin?: string
isTradeConfirmation?: boolean
slippageAdjustedSellAmount?: CurrencyAmount<Currency>
isPartialApprove?: boolean
}

export function OrderHooksDetails({ appData, children, margin, isTradeConfirmation }: OrderHooksDetailsProps) {
export function OrderHooksDetails({
appData,
children,
margin,
isTradeConfirmation,
slippageAdjustedSellAmount,
isPartialApprove,
}: OrderHooksDetailsProps) {
const [isOpen, setOpen] = useState(false)
const { account } = useWalletInfo()
const appDataDoc = useMemo(() => {
Expand Down Expand Up @@ -55,8 +65,13 @@ export function OrderHooksDetails({ appData, children, margin, isTradeConfirmati
? metadata.hooks?.pre?.filter((hook) => {
try {
const permitHookData = parsePermitData(hook.callData)
const isOwnerMatched = permitHookData.owner.toLowerCase() === account.toLowerCase()

return permitHookData.owner.toLowerCase() === account.toLowerCase()
// If the hook is a partial approve, we need to check if the value is equal to the slippageAdjustedSellAmount
// Because there might be a hook with an "infinite" permit from other widget
return isPartialApprove && slippageAdjustedSellAmount
? isOwnerMatched && permitHookData.value.eq(slippageAdjustedSellAmount.quotient.toString())
: isOwnerMatched
} catch {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Percent, TradeType } from '@uniswap/sdk-core'

import { PriceImpact } from 'legacy/hooks/usePriceImpact'
import TradeGp from 'legacy/state/swap/TradeGp'
import { useUserTransactionTTL } from 'legacy/state/user/hooks'
import { usePartialApprove, useUserTransactionTTL } from 'legacy/state/user/hooks'

import { useAppData } from 'modules/appData'
import {
Expand Down Expand Up @@ -70,6 +70,7 @@ export function ConfirmSwapModalSetup(props: ConfirmSwapModalSetupProps) {
const nativeCurrency = useNativeCurrency()
const appData = useAppData()
const [userDeadline] = useUserTransactionTTL()
const [isPartialApprove] = usePartialApprove()

const slippageAdjustedSellAmount = trade?.maximumAmountIn(allowedSlippage)
const isExactIn = trade?.tradeType === TradeType.EXACT_INPUT
Expand Down Expand Up @@ -124,6 +125,8 @@ export function ConfirmSwapModalSetup(props: ConfirmSwapModalSetupProps) {
buttonText={buttonText}
recipient={recipient}
appData={appData || undefined}
slippageAdjustedSellAmount={slippageAdjustedSellAmount}
isPartialApprove={isPartialApprove}
>
{(restContent) => (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CustomRecipientWarningBanner,
LongLoadText,
} from '@cowprotocol/ui'
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'

import { Trans } from '@lingui/macro'
import ms from 'ms.macro'
Expand Down Expand Up @@ -49,6 +50,8 @@ export interface TradeConfirmationProps {
recipient?: string | null
buttonText?: React.ReactNode
children?: (restContent: ReactElement) => ReactElement
slippageAdjustedSellAmount?: CurrencyAmount<Currency>
isPartialApprove?: boolean
}

export function TradeConfirmation(props: TradeConfirmationProps) {
Expand Down Expand Up @@ -76,6 +79,8 @@ export function TradeConfirmation(props: TradeConfirmationProps) {
recipient,
isPriceStatic,
appData,
isPartialApprove,
slippageAdjustedSellAmount,
} = frozenProps || props

/**
Expand Down Expand Up @@ -129,7 +134,12 @@ export function TradeConfirmation(props: TradeConfirmationProps) {
const hookDetailsElement = (
<>
{appData && (
<OrderHooksDetails appData={appData} isTradeConfirmation>
<OrderHooksDetails
appData={appData}
slippageAdjustedSellAmount={slippageAdjustedSellAmount}
isPartialApprove={isPartialApprove}
isTradeConfirmation
>
{(hookChildren) => hookChildren}
</OrderHooksDetails>
)}
Expand Down

0 comments on commit 8e72b4c

Please sign in to comment.