Skip to content
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

Relax trade verification checks for pre-interactions #3081

Merged
merged 6 commits into from
Oct 28, 2024

Conversation

MartinquaXD
Copy link
Contributor

@MartinquaXD MartinquaXD commented Oct 23, 2024

Description

Currently the trade verification does a few checks and actions on behalf of the user that requested the quote to make the verification possible.
For example if a user has ETH and no WETH but requests a quote for WETH we'd try wrap the necessary WETH before doing the fake settlement in the simulation.
We also do a check before the settlement to verify that the user has the required sell token balance. That way we can return a more helpful error message than when the settlement contract reverts due to the missing funds.

Both of these things could fail if the user provides pre-hooks that will only set up the necessary trade pre-conditions for the from address.

Changes

Adjusted the simulation code such that we can control whether some reasonable pre-conditions will be set up as part of the simulation or not.
If a user provides pre-conditions we now assume that they are necessary to set things up and we don't do our set up to not interfere with that.

How to test

Added an e2e test that transfers the needed sell tokens from a safe to the trader address in a pre-hook. This test fails without the change introduced in this PR.

Edit: The test works locally but appears to cause issues in CI for whatever reason. :/
Edit2: It's actually an existing test that fails exactly because of the reason described 👇

While working on the test I learned about a niche edge case that currently can't be verified: If you try to get a quote for a from address that is a contract that will only be deployed in the pre-interaction.
The reason is that before we do the simulation we check whether the from address is a contract. If it is a contract we use some state override magic to make our fake user during the simulation behave like the contract that was deployed at that address. But we can't know that the from address will contain contract code if the contract will only be deployed in the pre-interaction.

@MartinquaXD MartinquaXD requested a review from a team as a code owner October 23, 2024 17:49
Comment on lines +73 to +77
// Warm the storage for sending ETH to smart contract addresses.
// We allow this call to revert becaues it was either unnecessary in the first place
// or failing to send `ETH` to the `receiver` will cause a revert in the settlement
// contract.
receiver.call{value: 0}("");
Copy link
Contributor

Choose a reason for hiding this comment

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

Was it moved here just as a part of refactoring, or is it essential for the changes to work correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having to warm the storage for sending ETH to a smart contract wallet is a very annoying implementation detail the user shouldn't have to worry about. This part should always happen and should not lead to reverts for any reasonable smart contract receiver.
And since the prepareSwap() step is now optional depending on the existence of pre-interactions I moved it out of there. Also the context of this doesn't matter (i.e. whether the trader or the solver sends it).

Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

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

LGTM. Can't think of any pitfalls.

@MartinquaXD MartinquaXD enabled auto-merge (squash) October 28, 2024 10:59
@MartinquaXD MartinquaXD merged commit c1b102a into main Oct 28, 2024
11 checks passed
@MartinquaXD MartinquaXD deleted the trade-verification-workaround branch October 28, 2024 11:04
@github-actions github-actions bot locked and limited conversation to collaborators Oct 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants