Simulated Token Balances for Verified Quotes #1
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.
Description
This PR is a follow up to cowprotocol#3125 and uses the component introduced in the aforementioned PR for setting up a simulated token balance using state overrides in order for quote verification to work even when the trader does not have sufficient balance.
Changes
The way it works is by configuring known mapping slots for the
mapping(address => uint256) balances
in ERC20 token contract implementations and using this to compute the slot for overriding the solver's token balance for the simulation. The solver can then use this balance to top up the trader's account if needed (in the case were we allow mocking preconditions in the simulation - currently this is determined by whether or not the quote has hooks). We intentionally do not override the trader's balance in order to not interfere with hook execution in verified quotes.Note that the type of the state override changed slightly. This is because it was wrong to begin with. Node implementations I tested with (Geth and Anvil) expect both the slot and the value for state overrides to be exactly 32-bytes long (so
H256
). I guess this feature of the state override in theethrpc
crate was not used in the past and therefore no one noticed 🤷.How to test
Added an E2E test that uses the new token balance override feature in order to produce a verified quote for a trader with no balances. Note that commenting out the API arguments causes the test to fail as expected.