[Ethereum] Understanding front running. Why did the transaction fail? (PancakeSwap V2)

decentralized-exchangepancakeswappending-transactionstransactionstxpool

I am doing some research on front running transactions. My goal is to purchase a token in PancakeSwap in the same block that liquidity is added.

Lets take this token as an example: https://bscscan.com/token/0x195d3576bbf6504d8eac5b614dcb756e32b911d5

It was launched on block https://bscscan.com/block/7699663, which a single transaction adding 20 BNB LP on the PancakeSwap V2 Router (https://bscscan.com/tx/0x48b3b3b9dafcaff0f77e4c40abd80afcc47a20b19db23d9dc96a9ff326446a8c).

I used that transaction as a trigger for my swap order (only 0.01 BNB, 10 gwei) https://bscscan.com/tx/0xa66fbba13749cbd3005957d257da22e5afb07cfc007fe790976cda284a64c1f0 which failed. I want to understand why.

Maybe I placed my order too soon, but that is the goal, right? How should I estimate when to place then order then?

I can also think the problem was the gas price. My gas price was 10 gwei and the tx that added the liquidity had a price of 5 gwei. Does this mean that my tx will get processed first, and therefor will fail because there is not enough liquidity yet?

If this explanation is correct, would doing this transaction with the same gas price as the liquidity (or perhaps 1 less gwei?) guarantee that it will get processed? What would be the best way to achieve a good compromise between speed and success rate?

From this little experiment I can imagine that detecting a pending transaction (with a low or average gas price) and then submitting an equivalent swap with x2-3 gas price will likely result in my transaction being processed first, right?

Isn't this a very important flaw of DEX? how can one solve this problem?

Thanks!

Best Answer

There's no predefined order for transactions in a block. The only requirement is for transactions from the same address to be sorted by the transaction's nonce but transactions from different addresses can be in any order.

In Ethereum mainnet miners will sort by gas price because they will get a larger reward. But it is not always the case, search for "Miner Extractable Value" and the Flashbot project, some times miners will get a bigger reward by including a group of transactions.

Unfortunately I don't know a solution that will work in a PoA network like BSC.

Related Topic