flashbots / mev-inspect-py

🔎 an MEV inspector for Ethereum 🔎
MIT License
827 stars 262 forks source link

Sandwich profit amount inflated in some cases #283

Open Dire-0x opened 2 years ago

Dire-0x commented 2 years ago

I have found that the sandwich.profit_amount in some cases is very inflated... I'll give an example (the amounts below are made up but I have seen many cases of this happening).

frontrun: 1,000,000 USDC -> 100 ETH backrun: 300 ETH -> 3,200,000 USDC

mev-inspect calculates the profit as 3,200,000 - 1,000,000 USDC

we should calculate the profit pro rata, like this

profit = 100 * (3,200,000 / 300) - 1,000,000

profit should be $66k but is currently calculated as $2.2m

Here is an example case detected by mev-inspect

frontrun: https://etherscan.io/tx/0x59211262bbc5b8260d4d7ea6311ac81d358a09a99a7422f9a27e3b3fbe92b549 backrun: https://etherscan.io/tx/0x87fda6ee1f2c74e106aab959beca197a8749f988c692aedf10c8499ad5805a81

pmcgoohan commented 2 years ago

This is good. I propose a more conservative version which works for imbalances in both directions (overweight front or back runs).

profitFrontrun = (frontOut (backOut / backIn) - frontIn) profitBackrun = (backOut - (backIn (frontIn / frontOut))) sandwichProfit = min by abs {profitFrontrun, profitBackrun}

tutacrypto commented 2 years ago

Hey @Dire-0x @pmcgoohan, what's your progress on this?

I've had another issue with profit calculation from sandwiches. In this case I get a negative profit (loss), where the transaction looks like it should be positive. Curious to here your thoughts.

Screenshot: Capture d’écran 2022-05-04 à 14 59 32

Frontrun: https://etherscan.io/tx/0x54998a9e51516e5d5979f2a3ea76adb2d609cdeb7c745a01270bf5a03d2cc56a backrun: https://etherscan.io/tx/0x9d7b0757c6b05751d6141464d4ed7f27499a83445fa17c5e25799675a1cbbeab

You can find the sandwich with this query:

select *
from `flashbots`.`sandwiches`
where block_number = 14659109
bryanzk commented 2 years ago

@tutacrypto You can check if our calculation was correct here, which the profit is 0. 215237 WETH plus over 5k BLIB. https://eigenphi.io/ethereum/tx/0xef3b1a72096dd03ccdd7acf8baed8c16e8cc039d1d37fd39a8501d0369f577c0

Hey @Dire-0x @pmcgoohan, what's your progress on this?

I've had another issue with profit calculation from sandwiches. In this case I get a negative profit (loss), where the transaction looks like it should be positive. Curious to here your thoughts.

Screenshot: Capture d’écran 2022-05-04 à 14 59 32

Frontrun: https://etherscan.io/tx/0x54998a9e51516e5d5979f2a3ea76adb2d609cdeb7c745a01270bf5a03d2cc56a backrun: https://etherscan.io/tx/0x9d7b0757c6b05751d6141464d4ed7f27499a83445fa17c5e25799675a1cbbeab

You can find the sandwich with this query:

select *
from `flashbots`.`sandwiches`
where block_number = 14659109
tutacrypto commented 2 years ago

Oh nice tool, thanks for sharing @bryanzk From this link, my computation looks correct ( a ~0.21 wETH profit) and the data on Dune incorrect.

Any news on making the PR with your model @pmcgoohan?