when i monitor a profitable trade. The difference in profit calculated by my own method and the result of getAmoutsOut is not significant. Before sending the transaction, I would use the getAmoutsOut function to double check if the output amount is greater than input amount. If the output amount is greater than input amount I would send the transaction. But I encountered a problem that if the arbitrage is profitable, let's say greater than or equal to 0.1 WBNB, then after flash loan arbitrage the output amount is less than input amount, can not repay the loan and the transaction reverted. In testing, if the profit is in the range of 10 to the power of -4 or even less, the transaction usually succeed. I reality, even all the profitable arbitrage chance would fail. Anyone can tell me why this happend?
Every swap() that occurs on the LP contract must fee of around 0.3%. Different AMM's have different fee rates, and even some LPs have different fee rates among themselves within an AMM.
When swapping between AMMs to take advantage of an arbitrage opportunity you must take into account the swap fees on LP in the AMM where the swap() occurs. This, along with gas prices, can quickly remove any potential profit to be made and should be accounted for as best as possible.
One potential solution is to to do real human due diligence on each market you plan on scraping for arbs. Get a list of AMMs, comb their docs, and start to construct a object/json with each AMMs details including fee rates, contract addresses, etc. Using this object as a constant within your arb bot can add necessary context for your code to calculate fees while also reducing unnecessary calls to the chain; i.e. making individual calls to the LP contract to determine fees.