Are there still Flash Loan Triangular Arbitrage Opportunities on PancakeSwap? Part I: Static Analysis

PVK
8 min readMar 7, 2023

As 2023 began, I couldn’t help but wonder whether flash loan-based triangular arbitrage opportunities still existed on PancakeSwap. With over 1.2 million pools, the belief was that with the right approach and analysis, there had to be profitable deals to be found. However, reality turned out to be different than expected, and for some surprising reasons. If you’re interested in pursuing flash loans and cyclical arbitrage in general and particularly on PancakeSwap, read on … it might help you avoid dead-ends and save you valueable time.

This first part focuses on a static analysis of the PancakeSwap pools and the triangular arbitrage cycles that can be created. The second forthcoming part covers issues of practical implementation and market dynamics, and integrates both, static and dynamic, views to come to a conclusion.

Flash Loans and Triangular Arbitrage

I assume here that the reader has some familiarity with flash loans and their use in triangular arbitrage, and is more interested in finding suitable opportunities on PancakeSwap. For a good and solid introduction to flash loans with a lot of practical utility I can whole heartedly recommend CryptoWizards’ Shaun McDonogh Udemy course “Crypto Trading Execution with Flash Loans, Web3 and Hardhat” (no affiliation).

But just to keep this text somewhat self-contained here a short “descriptions” of flash loans and triangular arbitrage.

A flash loan is a type of uncollateralized loan implemented in a smar contract for instance on Solidity, that allows users to borrow cryptocurrency without any collateral, as long as the loan is repaid within the same transaction. Triangular arbitrage, on the other hand, is a trading strategy that takes advantage of price differences between three different assets to generate profits. Triangular arbitrage are part of the larger cyclical arbitrage familiy, in which any number of assets could be involved. The atomicity of blockchain transactions, makes the combination of flash loans and triangular (or more generally cyclical) arbitrage very appealing.

Fig 1: Triangular Arbitrage with a Flash Loan

For instance, in step (0) borrow WBNB from the WBNB-USDT pool, then exchange WBNB for CAKE (1), CAKE for BUSD (2), BUSD back to WBNB. Repay the flash loan in step (3), and pocket the remainder as profit (4). Given the magic of blockchain, all of this can be done in the same transaction, with zero risk and zero upfront capital [this is not quite correct, as there as still quite a number of risks, just not “price risk”. It is however common, albeit unfortunate, to still talk about having “zero risk”. We will have to look into some of these additional, non-price risks in the second part of this article when considering implementation issues].

PancakeSwap

Given the high gas prices on Ethereum , instead of considering Uniswap, the analysis focuses on PancakeSwap which uses the UniswapV2 protocol on the BSC chain.

At the time of the analysis (January 2023), there were 1'241'705 pools on PancakeSwap involving 372'921 unique tokens. Each pool allows exchange of one token for another. [The term “pool” and “pair” are used interchangably here. Both can be regarded as referring to a specific instance of the PanakePair.sol contract]

With a little bit of math, one can easily derive that there are 25'499'342'116'760 ways of selecting a tuple of 3 elements out of a set of 372'921 tokens, and thus there are just as many theoretical cycles of length 3 for triangular arbitrage. As there are only 1'241'705 pools, it means, that not all theoretical cycles of length 3 are possible and thus we need to identify which cycles are available to us in practice.

Another aspect to consider is that any one can easily create a pool on PancakeSwap. Thus there are quite a number of pools that are either abandoned or honeytraps of sorts, which also need to be filtered out.

Pools

To identify which pools can form a feasible triangular arbitrage cycle, a first point to realize is that the tokens involved in a feasible cycle have to be part of at least 2 pools. For instance, the token AgaveInu only appears in one single pool together with WBNB [AgaveInu/WBNB]. Thus, once we swap WBNB into AgaveInu, the only other possible swap is back to WBNB. Pools with such “dead-end” tokens can therefore be ignored. As it turns out, of the 372'921 tokens, there are merely 7'796 tokens, which are included in 2 or more pools. All other tokens, a whopping 365'125 of them, are “dead-end”.

Filtering all the PancakeSwap pairs for these 7'796 tokens, there remain 17'510 pools which are of potential relevance to arbitrage cycles.

These pools still contain pairs with no supply or very low liquidity. But we want a lot of liquidity, as low liquidity means high price impact when trading, and thus minimal chances for the triangular trade being profitable. Putting some further light, but justifiable contrains on available token supply and pool liquidity, we end up with a set of 16'757 pools for the next step of analysis.

Network

Now, 16'757 of pools is still a pretty big number. Visualizing the network of these pools, does not only have curiosity value, but can further hint at some interesting hidden structures.

Fig 2: PancakeSwap token network. Nodes / vertices = tokens, edges = pools

In the network diagram of figure 2, each line, or edge, corresponds to one of the 16'757 pools and the vertices or nodes at the ends of each line, represent the tokens of the pool.

The layout of the network has been drawn based on the Force Atlas algorithm in Gephi. This algorithm pushes nodes that have many connections (so called hubs) away from each other, while pulling nodes that are connected to these hubs closer together and thereby forming “communities”. We can thus immediately see, that PancakePools are dominated by 4 tokens, which are, unsurprisingly, WBNB, USDT, BUSD and CAKE. There are a few tokens, such as ETH, USDC or, perhaps somewhat surprisingly, SAFEMOON, which are also central to the PancakeSwap pools, although to a much lesser extent than the four “big” ones. It is thus fair to assume, and indeed the case, that the large majority of the possible triangular arbitrage cycles will include at least one of the above mentioned coins.

I will leave the interpretation of the fine structures of the diagram to perhaps another article, and include here for now only a blow up of one of the intriguing areas as a teaser.

Fig 3: Token communities (edges removed)

Cycles

So, we have now identified 16'757 pools, out of which we need to find cycles of length 3 that would allow triangular arbitrage. This includes basically taking a pool, identify it’s tokens, e.g. WBNB and CAKE, finding a second pool that involves one of the tokens, e.g. the CAKE-BUSD pair, and checking whether the cycle can be closed, i.e. whether there is a pool with BUSD and WBNB as it’s tokens. If so, we have found a cycle suitable for triangular arbitrage.

There are many great graph-algorithm libraries out there that implement very efficient methods to find such a set of cycles. Using the excellent NetworkX Python package, a total of 10'279 unique cycles can be constructed with the 16'757 pools of PancakeSwap. This includes 261 cycles of length 4 or longer, including for instance the cycle ULC -> ZIL -> GodCoin -> LINK -> CCA -> POOCOIN -> DAI and back to ULC.

To close of this first part of the story, let’s look briefly at some statistics. The numbers confirm, what the network diagram introduced above indicates: WBNB, BUSD, USDT and Cake are central to most triangular arbitrage cycles. All of them feature in more than 1000 cycles each, with BUSD taking the top spot at 7'334 cycles.

There are 10 additional tokens, which are part of at least 100 and up to 1000 cycles. These are: ETH, BTCB, USDC, SAFEMOON, DOGE, XBN, DAI BabyDoge, ADA, B.

Then there are 1719 tokens, that appear in anywhere between 2 and 10 cycles, but the lion share of 4417 tokens come up in one and only one cycle. Remember though, that to be able to be part of a cycle, a token needs to be contained in at least 2 pools. Thus, we have 4417 tokens, that are part of exactly two pools, and no more. Take as an example FPUPPY. This token shows up together with WBNB and BUSD, and forms a single triangular cycle as follows: WBNB -> FPUPPY -> BUSD and back to WBNB.

Lastly, there are 147 cycles which don’t contain any of the main coins. Among those we find some fairly odd looking ones, like the “Bot”-Familiy, BOT9->BOT5->BOT12, BOT11->BOT7->BOT12, or cycles such as YHJB->ZHXY->HZTB

Conclusion and Outlook

We are at the end of what a statical analysis can reveal about the possible PancakeSwap arbitrage cycles. Strictly speaking, one could continue with a contract analysis of each token and add that to the statical analysis, but we leave that aside for now. Nontheless, some interesting conclusions can be drawn:

Out of a total of over 1.2m pools and more than a trillion theoretically possible triangular cycles, we end up with slightly less than 10'000 feasible cycles (excluding cycles of length larger than 3) where triangular arbitrage opportunities might be found. These cycles are dominated by WBNB, BUSD, USDT and Cake, which means that any large move in any of these coins could present an arbitrage opportunity across thousands of cycles. However, more than half of these cycles contain tokens that are not “diversified” across pools, but only pop-up in a handful of them. This could imply low liquidity and stale markets. We will review this in the next installment of this story.

Also in the second part, we will have to look at the following, among others:

  • Infrastructure considerations: what are the system requirements needed to have reasonable expectations of success with triangular arbitrage on PancakeSwap
  • Event Monitoring: how can we make sure that we always have the latest market information to be able to recognize and take advantage of arbitrage opportunities ahead of competitors
  • Artbitrage amount: Once an arbitrage opportunity has been identified was is the optimal trade amount, considering price slippage?
  • Gas cost: what’s the implication of gas prices on realizable profits combined with considerations for transaction ordering and MEV

If you found this information of some use, or have any other comments and feedback please leave a line in the comments section.

Donations are most welcome to : 0xE17083697160dAA6c9a80Ae558cdE2D323F783Dc

--

--