-
Notifications
You must be signed in to change notification settings - Fork 102
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
fix: do not send permit to quote when enough allowance #3433
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Hi @anxolin , in great job! In most of cases it works as you described!
|
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.
see my comments above
I will investigate
This is normal. Its only when we detect the change in the allowance. We are working to make this faster, but is independent of this fix/pr. I added a disclaimer that this could happen.
I will investigate. I would think is not related to this PR, but I still want to know what happens... THANKS!!! |
@elena-zh , i can't reproduce. In principle, it will show it if it needs to be shown (approval needed), but you also need a quote. Could it be that the sell token or buy token was not selected? Do you have a sequence I can try to reproduce? |
@anxolin , I will try finding a dependency and will let you know. |
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.
Hey @anxolin , here is what I found: even if I don't see sometimes a hook log in the console, it appears in the appdata log in the order details.
I'm approving this PR.
Yes, for limit orders i believe this is fine. Here is ok the quote doesn't have it. The price in limit orders is an indication, and should not include the hook. The important thing is that the SWAP includes the hook, so its approved if it happens. |
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.
Nice, thanks for following up and fixing my mess :)
Summary
This PR is a continuation of Leandro's #3411
I went through multiple rabbit holes to debug this one.
Basically, making the story short, there was 3 issues:
Ciclic dependency
sellAmount
after slippagesellAmount
during the quote re-calculation, we... BECOME UNSURE if our allowance is enoughPart of the solution is to only add the permit if we are sure that the user don't have enough allowance. Before was treating
undefined
as a reason to add it.https://github.com/cowprotocol/cowswap/pull/3433/files#diff-eb9b3098bb927275324420456886b1e05659b33bca5eca07e0af1a672bcccf7bR21
However, the key thing to solve the issue was to use
v2Trade
in the hook that updates the hooks.https://github.com/cowprotocol/cowswap/pull/3433/files#diff-eb9b3098bb927275324420456886b1e05659b33bca5eca07e0af1a672bcccf7bR43
Basically, only when we have the quote and all the other data we consider update.
Too many re-renders! (even if you remove the infinite loop)
It's still not amazing but this PR reduces the re-renders quite a bit.
Part of the reduction is the creation of
usePermitDataIfNotAllowance
which will return a memoized object that depends on the hook data onlyI did some improvements so it only updates the hook (which triggers re-renders) if the hook really changes
hasTradeInfo
(to avoid multiple intermediate states)Not clearing the hook
After solving the issues above became easy
https://github.com/cowprotocol/cowswap/pull/3433/files#diff-eb9b3098bb927275324420456886b1e05659b33bca5eca07e0af1a672bcccf7bR61
Bonus
Added a diagram with the state of the permit
permit-state.drawio
It's very basic, but since i did it to understand better the atoms, I just pushed it.
Also changed the signature of
useSwapAmountsWithSlippage
to make it easier to read. The positional arguments are error prone:https://github.com/cowprotocol/cowswap/pull/3433/files#diff-45b0bcb8baef0bafb447c88c917e750a718f7c7bdae6dadfd6f73a6d54fa9ac6R85
Bonus 2
Added a notification for Sentry if we leak the fake signer account used in permit in the
appData
.https://github.com/cowprotocol/cowswap/pull/3433/files#diff-808338a054c7b81dd64d500d781a534b581f4b4012a1927bbef2f6bd2a0696e6R73
Test
Test for Limit Orders and Swaps
Basically we need to assert that:
quote request
, and theappData
How to remove allowances, to test the permit
https://revoke.cash, click on top right, my approvals
How to check when the app clears or sets a hook
While testing, you can filter to see when hooks are added and removed, as the image shows:
How to check if there's a hook in the posted order
Go to the explorer, and check the appData in the bottom:
https://explorer.cow.fi/orders/0xe9d079fa47fdfd47c483a282e805620361cea3860adefbb374c69f43c2a9fd81020ca66c30bec2c4fe3861a94e4db4a498a35872655f04eb?tab=overview
How to check when a quote request has hooks
Check in the Chrome extension tools for the appData we send