-
Notifications
You must be signed in to change notification settings - Fork 176
fix(widget): fire ON_BEFORE_APPROVAL widget hook before permit signing #7697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tenderdeve
wants to merge
13
commits into
cowprotocol:develop
Choose a base branch
from
tenderdeve:fix/7685-widget-on-before-approval
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1567101
fix(widget): fire ON_BEFORE_APPROVAL widget hook before permit signing
tenderdeve f9d6050
fix(widget): skip swap-error analytics for WidgetHookDeclineError
tenderdeve 4b55dfc
Merge branch 'develop' into fix/7685-widget-on-before-approval
elena-zh 97b1ab5
fix(widget): fire ON_BEFORE_APPROVAL hook before the trade approve tx
tenderdeve 6653bb1
fix(widget): fire ON_BEFORE_APPROVAL hook in advance permit signing
tenderdeve 7d49d28
fix(widget): gate advance permit signing on tradeSpenderAddress
tenderdeve b0c24cb
Merge branch 'develop' into fix/7685-widget-on-before-approval
elena-zh 40d5d5f
Merge branch 'develop' into fix/7685-widget-on-before-approval
elena-zh 52717ce
Merge remote-tracking branch 'upstream/develop' into fix/7685-widget-…
tenderdeve c81179d
Merge branch 'develop' into fix/7685-widget-on-before-approval
elena-zh 2db25db
fix(widget): skip ON_BEFORE_APPROVAL when a cached permit is reused
tenderdeve d5dcf3c
Merge remote-tracking branch 'upstream/develop' into fix/7685-widget-…
tenderdeve 0982536
refactor(widget): centralize ON_BEFORE_APPROVAL in permit hook
tenderdeve File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,8 +1,16 @@ | ||
| import { captureError, ERROR_TYPES, normalizeError, reportPermitWithDefaultSigner } from '@cowprotocol/common-utils' | ||
| import { SigningScheme } from '@cowprotocol/cow-sdk' | ||
| import { | ||
| captureError, | ||
| COW_PROTOCOL_VAULT_RELAYER_ADDRESS, | ||
| currencyAmountToTokenAmount, | ||
| ERROR_TYPES, | ||
| normalizeError, | ||
| reportPermitWithDefaultSigner, | ||
| } from '@cowprotocol/common-utils' | ||
| import { SigningScheme, SupportedChainId } from '@cowprotocol/cow-sdk' | ||
| import { Percent } from '@cowprotocol/currency' | ||
| import { isSupportedPermitInfo } from '@cowprotocol/permit-utils' | ||
| import { Command, UiOrderType } from '@cowprotocol/types' | ||
| import { WidgetHookEvents } from '@cowprotocol/widget-lib' | ||
|
|
||
| import { tradingSdk } from 'tradingSdk/tradingSdk' | ||
| import { sendTransaction } from 'wagmi/actions' | ||
|
|
@@ -11,8 +19,9 @@ import { PriceImpact } from 'legacy/hooks/usePriceImpact' | |
| import { partialOrderUpdate } from 'legacy/state/orders/utils' | ||
| import { mapUnsignedOrderToOrder, wrapErrorInOperatorError } from 'legacy/utils/trade' | ||
|
|
||
| import { callWidgetHook } from 'modules/injectedWidget' | ||
| import { LOW_RATE_THRESHOLD_PERCENT } from 'modules/limitOrders/const/trade' | ||
| import { PriceImpactDeclineError, TradeFlowContext } from 'modules/limitOrders/services/types' | ||
| import { PriceImpactDeclineError, TradeFlowContext, WidgetHookDeclineError } from 'modules/limitOrders/services/types' | ||
| import { LimitOrdersSettingsState } from 'modules/limitOrders/state/limitOrdersSettingsAtom' | ||
| import { calculateLimitOrdersDeadline } from 'modules/limitOrders/utils/calculateLimitOrdersDeadline' | ||
| import { emitPostedOrderEvent } from 'modules/orders' | ||
|
|
@@ -27,7 +36,7 @@ import { getSwapErrorMessage } from 'common/utils/getSwapErrorMessage' | |
| import type { Hex } from 'viem' | ||
|
|
||
| // TODO: Break down this large function into smaller functions | ||
| // eslint-disable-next-line max-lines-per-function | ||
| // eslint-disable-next-line max-lines-per-function, complexity | ||
| export async function tradeFlow( | ||
| params: TradeFlowContext, | ||
| priceImpact: PriceImpact, | ||
|
|
@@ -73,7 +82,28 @@ export async function tradeFlow( | |
|
|
||
| try { | ||
| logTradeFlow('LIMIT ORDER FLOW', 'STEP 2: handle permit') | ||
| if (isSupportedPermitInfo(permitInfo)) await beforePermit() | ||
| if (isSupportedPermitInfo(permitInfo)) { | ||
| const cachedPermit = await params.getCachedPermit(sellToken.address) | ||
|
|
||
| if (!cachedPermit) { | ||
| const sellTokenAmount = currencyAmountToTokenAmount(inputAmount) | ||
| const isWidgetHookPassed = await callWidgetHook(WidgetHookEvents.ON_BEFORE_APPROVAL, { | ||
| chainId: sellTokenAmount.currency.chainId, | ||
| sellToken: { | ||
| ...sellTokenAmount.currency, | ||
| name: sellTokenAmount.currency.name || '', | ||
| symbol: sellTokenAmount.currency.symbol || '', | ||
| }, | ||
| sellAmount: (permitAmountToSign ?? 0n).toString(), | ||
| walletAddress: account, | ||
| spenderAddress: COW_PROTOCOL_VAULT_RELAYER_ADDRESS[chainId as SupportedChainId], | ||
| }) | ||
|
|
||
| if (!isWidgetHookPassed) throw new WidgetHookDeclineError() | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not report widget-hook declines as swap errors. At Line 102, Suggested fix } catch (err: unknown) {
const error = normalizeError(err)
+ if (error instanceof WidgetHookDeclineError) {
+ throw error
+ }
logTradeFlow('LIMIT ORDER FLOW', 'STEP 9: ERROR: ', error)
const swapErrorMessage = getSwapErrorMessage(error)🤖 Prompt for AI Agents |
||
|
|
||
| await beforePermit() | ||
| } | ||
|
|
||
| postOrderParams.appData = await handlePermit({ | ||
| permitInfo, | ||
|
|
@@ -196,6 +226,11 @@ export async function tradeFlow( | |
| } catch (err: unknown) { | ||
| const error = normalizeError(err) | ||
|
|
||
| // Expected abort path: skip generic swap-error analytics so widget-hook declines don't pollute telemetry. | ||
| if (error instanceof WidgetHookDeclineError) { | ||
| throw error | ||
| } | ||
|
|
||
| logTradeFlow('LIMIT ORDER FLOW', 'STEP 9: ERROR: ', error) | ||
| const swapErrorMessage = getSwapErrorMessage(error) | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this logic duplicated in three different places.
It looks like it can be implemented only once in
useGeneratePermitHook