Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Adjust sandwiches calculation #307

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Adjust sandwiches calculation #307

wants to merge 4 commits into from

Conversation

gheise
Copy link
Contributor

@gheise gheise commented Aug 4, 2022

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

  • Read the contributing guide
  • Installed and ran pre-commit hooks
  • All tests pass with ./mev test

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

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

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?

Copy link
Contributor Author

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")

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.

Copy link
Contributor Author

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

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