Account for subtraction underflow in order creation #14
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.
This PR adds an explicit underflow check for the subtractions in the
getTradeableOrder
function.This is done because a revert due to underflow during a watchtower call to
getTradeableOrder
would cause the watchtower to stop indexing AMM order, stopping the automated order creation on the orderbook until the order is deleted and then recreated.Note that we're mainly concerned about subtraction overflows. We're leaving all multiplication and addition overflow there because they are only likely to occur when the amounts are so large that they are close to the amount the CoW AMM isn't able to handle by design. In the case of subtraction, an underflow could happen when the AMM reserves are extremely close to the reference price. This may occur by chance for pairs with tokens that have low number of decimals, like USDC.
How to test
New unit test. Note that I changed
setUpDefaultData
togetDefaultData
when we set the reserves by hand because otherwise there'd be two mocks set for the same oracle/pair.