-
Notifications
You must be signed in to change notification settings - Fork 266
Adjust sandwiches calculation #307
base: main
Are you sure you want to change the base?
Conversation
and other_swap.from_address == sandwicher_address | ||
back_swap.token_out_address == front_swap.token_in_address | ||
and back_swap.token_in_address == front_swap.token_out_address | ||
and back_swap.from_address == sandwicher_address |
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.
⭐ for the rename.
): | ||
if len(sandwiched_swaps) > 0: | ||
profit_amount: float |
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 think this formula needs some documentation in the code. Explain a little what is happening in here, and if you can put a math formula in clearer syntax than python code that would be nice.
@gheise how do we know if the formula is correct?
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.
thanks for pointing that out, the reasons for this are mathematically expressible. I will add further detail on the PR about why i chose this formula and what it means, but i think we should leave this out of the code
): | ||
if len(sandwiched_swaps) > 0: | ||
profit_amount: float | ||
if back_swap.token_in_amount == 0 and back_swap.error is None: | ||
raise ValueError("Backrun cannot swap 0 tokens") |
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.
This error doesn't have a test case.
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.
we don't expect this to ever happen on chain; i added this here to be explicit about the assumption that the back swap must be ruled out in the case this value is 0 as to avoid a DivisionByZero error, so test coverage here would not be meaningful
What does this PR do?
Adjust the calculation of sandwiches to address incorrect profit amounts. This corrects the naive sandwich profit formula by including an exchange rate to compute only the realized profit for sandwiches with an imbalance between the frontrun and backrun.
Related issue
Testing
Two test cases included, one for each imbalance. All previously implemented tests for sandwiches pass.
Checklist before merging
./mev test