Flash Loan Arbitrage on PancakeSwap: Yummy Cake or Pie in the Sky? Part II

PVK
Coinmonks
Published in
11 min readMar 29, 2023

--

Flash loan based arbitrage opportunities have been explored and exploitet for the good part of 2 years now. But, as of 2013, is it still a lucrative proposition, or just pie in the sky, wishful thinking? More specifically, do the PancakeSwap liquiditiy pools and traiding pairs still offer flash loan triangular arbitrage opportunities, as they did at the inception of flash loans?

In the previous installment of this article “Are there still Flash Loan Triangular Arbitrage Opportunities on PancakeSwap? Part I: Static Analysis” I showed how out of the more than 1.2 million PancakeSwap pools available at the beginning of 2023, practically only 10'221 cycles of length 3 can be derived, which have the potential to give rise to arbitrage.

Recall, that for an arbitrage opportunity to arise, a market event must occur, which sufficiently dislocates the price relationships of the currency pairs in a triangular cycle, so that the circular trade becomes profitable. The relevant, actionable market events for our purposes are caused by trades in the PancakeSwap pools. Lukily for us, the respective PancakeSwap pool / pair smart contract emits a Sync event for each trade, and such Sync events can be monitored on the BSC blockchain (for details, see for instance the Pancake Pair Protocol)

Insights from Sync Event Analysis

To gain an initial insight into the type and size of arbitrage opportunies across the 10'221 triangular cycles, we start by looking a the frequency of trades and the pools they occur in. This we do through the Sync events the trades generate.

1'171'114

Taking 23892902 as the starting block number on the BSC chain, the present analysis covered a total of 1'171'114 blocks. The first block, was minted on Dec. 14th, 2022, the last block with number 25064016, on Jan. 24, 2023. Thus, this event analysis covers a time span of a little more than 1 month. As a reminder, on the BSC chain, a block is generated approximately every 3 seconds.

20'693'930

The total number of possibly relevant Sync events (i.e. trades) during this time period was exactly 20'693'930 . Thus, at least theoretically speaking, there were over 20 million opportunities for arbitrage in the 5 week observation period. These are, however, not all Sync events that occured, but only those that could potentially be relevant for arbitrage. This is a subtle, but important difference, particularly when it comes to any practical implementation, and thus it’s worthwhile to spend a little time on it.

A block typically includes several transactions, and any number of those transactions come from trades on PancakeSwap. Furthermore, within any one block, it is possible and indeed quite common, that two or more trades within the same PancakeSwap pool are recorded. This then results in several Sync events within the same block. To identify arbitrage opportunities, only the very last trade and thus the very last Sync event for a pool is of relevance, as only that last event determines the state we can act upon. Take for instance the WBNB-BUSD pool (0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16). Trades in this pool happen all the time, and there is a good chance to find more than one of these trades in any block. But only the very last trade sets the new “price” for the currency pair that is useful as an input to an arbitrage trade. This is btw the same “price” one gets when using the respective smart contract function of the pool (getReserves). These last trades and their Sync events are what I’ve been calling “relevant” in here. And, although obvious it’s still good to be clear: as a consequence of the definition, all relevant Sync events within one single block necessarily belong to trades in different pools.

So, after this little detour, back to the analysis. The chart below, shows the number of relevant events per block over time.

The chart reveals that the average number, 17, of relevant events per block (and thus impacted pools / currency pairs) is pretty stable during the observation period. The maximum number of relevant events in a single block was 362 in block number 24898410 on 18 Jan 2023.

Unsurprisingly, the top 5 pools with the most number of Sync events in the total 1'171'114 blocks were are follows (number next to the pool shows the number of relevant Sync events identified)

  1. USDT-WBNB 937'596
  2. WBNB-BUSD 724'588
  3. USDT-BUSD 224'411
  4. Cake-WBNB 161'804
  5. USDC-WBNB 102'165

Note, that as these pools / currency pairs are also those most frequently occuring in the viable triangular cycles (see Part I for more details), each such event, if big enough, could potentially result in an arbitrage opportunity among the majority of the more than 10'000 cycles.

85'906

As might be remembered (Part I), the 10'000+ triangular cycles are built out of 16'458 pools, from a total of over 1.2 million available on PancakeSwap. Contrast this now, to the 85'906 pools, which is the total number of pools for which at least one relevant Sync event occured during the observation period. This means in particular, that trades in more than 70'000 pools have occured which are not part of any triangular cycle and thus are irrelevant from an arbitrage point of view. What it also means, is that more that 1.1 million of the approx. 1.2 million PancakeSwap pools in existence as of Jan 2023 did not have a single trade in the observed 5 weeks, and thus can safely be labled inactive.

10'002

Mapping the pools of the observed Sync events to the triangular cycles, we see that 10'002 cycles out of the total viable 10'221 have been impacted and could thus hold arbitrage opportunities. In turn, this also means only 219 triangular cycles can be excluded all together from further analysis as no trades occured in any of their pools.

On Arbitrage Profits

To get any further, we need to start looking at the hypothetical revenues from trading in the various triangular cycles. The revenues are hypothetical as we are just playing back past events for now, without taking any action.

In what follows, I assume that the reader has a general understanding of UniswapV2-Type AMM’s and in particular how pool reserves are used to calculate relevant prices and potential trade revenues in triangular arbitrage cycles. As will be recalled, any trade in a pool involves some slippage, i.e. an adverse price movement, which is dependent on the size of the trade and the pool reserves. This slippage limits how much we can put in a triangular arbitrage trade and still be profitable. The diagram below depicts schematically how the revenue in a triangular arbitrage changes with the amount traded. The range within which the revenue is positive is bounded, and in some cases or reserve relationships, the revenue can be negative over the entire range.

This means in particular, fixing an amount to trade upfront, as it is done in many of the tutorials introducing flash loans and triangular arbitrage, is not an option in the real world. Instead, for each triangular cycle, and for each set of pool reserves, we need to find the optimal amount that maximizes the revenue from trading the cylce. If this maximum covers all the costs, in particular the transaction’s gas fees (as we will see in the forthcoming third part, there are many other costs to consider as well), we have a profitable arbitrage opportunity and extract the maxium possible value.

Using some easy (although messy) calculus and algebra the following formula can be derived giving the amount to trade for getting the optimum revenue out of a triangular cycle with flash loan. Any amount less or more, will lead to a reduction in revenue.

In this formula the r’s refer to the reserves of the pools involved, i.e. r0, r1 are the reserves of pool one, r2, r3 the reserves of pool two and r4, r5 the reserves of pool three. f is the fee rate charged by the pools, which in the case of PancakeSwap is 0.25%. The formula also assumes that a flash loan is used to get the amount of currency to trade, and that the same fee applies for loan as well. This is certainly the case if the flash loan and the triangular cycle exclusively use PancakeSwap pools, as in this analysis.

Here is a short example:

Let’s consider the triangular arbitrage cycle WBNB > SonM > BUSD and back to WBNB at block number 23892902. This involves the following three pools and their respective reserves expressed in the basic unit of the token:

WBNB-SonM
r0 = 3286737067090
r1 = 7854654854

SonM-BUSD
r2 = 18656996
r3 = 18039659217741649083

BUSD-WBNB
r4 = 84142804193278841528263584
r5 = 305313076263510807823690

As for the flash-loan, any liquid pool which includes WBNB and is not part of the triangular cycle can be chosen, e.g. WBNB-USDT. With the PancakeSwap fee rate of 0.25% we can calulate the optimal amount which maximizes the revenue to be

451032257445159 wei or 0.000451032 WBNB

Using this as the amount to trade, the arbitrage revenue comes to 0.0651361 WBNB after repayment of the flash loan, but without any gas costs.

Now to the fun part: starting at the first block in the observation period, i.e. block number 23892902, we first collect the reserves of all the 16k+ pools, and then for each block forward, we use the relevant Sync events to update the correponding pool reserves and check whether any of the impacted triangular cycles present an arbitrage opportuniy using the optimum trade amount as determined above. To be closer to reality, a gas cost of 0.0025 WBNB is deducted from any revenue optained. As will be explained in the forthcoming Part III, the 0.0025 WBNB is a fair representation of the transaction cost involved using a somewhat optimized smart contract, abeit with some caveats.

Due to practical constraints, the abitrage reveneu analysis only includes blocks up to number 24170796, i.e. a total of 277'894 blocks, covering 10 days between Dec 14 and Dec 24. Despite this limitation, the insights are representative of other observation periods.

1'589'709

During this time, 4'838'044 Sync events occured, for which a total of 1'589'709 arbitrage opportunities were identified. Summing together all the optimal arbitrage revenues after covering gas costs, the extratable profit is over 10'000 WBNB. And this for only 10 days. Sounds very promissing, right? …. But not so fast!

The first issue is, that we are just passive observes in the market, playing back past events and considering the what-if’s. In particular, we have not executed any trades yet. And just like any observation of quantum phenomena changes that what is observed according to the uncertainty principle of physics, so does a trade change the market in which it is traded. In other words, it’s very possible that if we would have taken advantage of some of the 1.5 million opportunity, it would have removed further opportunities, which are currently still are on the records.

25

One way to get closer to reality is to see how many of the 10'000+ arbitrage cycles have been triggered as having some profit and how often. Think about this for a moment: the more distributed the arbitrage opportunties are among the cycles (i.e. the more cycles have been involved) and the lower the number of arbitrage opportunities per cycle is, the more likely it is that those arbitrage opportunities represent real opportunties which might have been snapped up by fellow arbitrageurs.

The table below shows what has been going on:

Now, this is rather surprising, I say.

Firstly, out of the 10'000+ possible cycles, only the 25 showed up with some profit taking opportunities. So, despite the fact that out of the 10'022 total cycles as many as 10'002 cycles were impacted by the over 1.5 million events, only 25 cycles presented arbitrage opportunities withing the 10 day window. That, I must say, and I think you will agree, is rather disappointing!

Secondly, for some of those cycles, for istance WBNB > USDT > IDAC, there appears to be rather large number of opportunities, i.e. 225'792. This is significant given that the total number of blocks in the analysis was only 277'894. This would imply that either this is an untapped money-well (highly unlikely), or that nobody identified the arbitrage (unlikely) or that there is something else going on here (very likely). To let the cat out of the bag: there is something else going on, but what that could be, will have to wait until Part III.

Thirdly, average profit per potential arbitrage is, again rather surprisingly, only in the 1 / 100th of a WBNB. And, although not apparent from the table able, the individual arbitrage profits do not vary much either. Although it is known that arbitrage profits tend to diminish with market maturity, this insight is another surprise … and another disappointment.

2

If there is any hope to be had at this point in time, maybe some of these 25 triangular cycles truly represented some arbitrage opportunities on several occasions. Without doing “in-the-field” exploration, or, in another words, executing real, live trades, we can at least check whether other arbitrageurs, of which they are plenty, managed to get in on the game and performed the arbitrage. As flash-loan based triangular arbitrages leverage atomicity of smart contract transactions, we first want to find all the blocks which include events corresponding to all the pools belonging to at least one of the 25 cycles. Doing this, we arrive at another shattering insight: only complete events to two (2!) cycles can be found in any of the observed blocks, and these are:

  • WBNB > SGPro > BUSD in block 24847713, 24847747
  • WBNB > RMES > ShinjaV3 in block 24786919

And to put the nail in the coffin, a deeper look at the transaction logs reveals that there was no triangular arbitrage at play and it was just coincidence that the relevant pool Sync events all appeared together in a single block.

0 (?)

What does all of this leave us with? The conclusion must be that despite all the possible scenarios under which arbitrage opportunities could occure, at least for flash-loan based triangular arbitrage on PancakeSwap the market is already too efficient and no arbitrage opportunities are available under normal circumstance. It would need a major market dislocation for profitable arbitrage opportunities of this kind to occur. So, that’s it. Building flash-loan triangular arbitrage bots for PancakeSwap might be an interesting lab experiment, but if you plan to make a living of it, look elsewhere instead!

But is that really it? Is that the end for arbitrageurs on PancakeSwap? Well, perhaps not. Perhaps there are other opportunities that can be explored on PancakeSwap, if we go beyond flash-loan based triangular cycles. That and other issues will have to wait to be explored in Part III.

If you found this information useful, or have any questions or feedback please leave a line in the comments section.

Donations are most welcome to : 0xE17083697160dAA6c9a80Ae558cdE2D323F783Dc

New to trading? Try crypto trading bots or copy trading on best crypto exchanges

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--