The trickiest part of a two-pool arbitrage is determining the optimal borrow amount.
To understand how the problem is solved, consider the the hypothetical equation:
P(x) = y_b(x) - y_a(x)
where y_b(x)
is the output of token x swapped for token y through Pool B, y_a(x)
is the repayment required in token y for a flash borrow of token x through Pool A, and …