A Deep Dive into Arbitrage on Decentralized Exchanges

A Deep Dive into Arbitrage on Decentralized Exchanges

Table of Contents

Text Link

Arbitrageurs perform, and profit from, price differences between various marketplaces. In addition to netting these profits, arbitrageurs also serve the broader market ecosystem by bringing prices across different markets into alignment. In the decentralized exchange (DEX) world, arbitrage exists all the same. The unique features of DEXes, however, create a more even playing field for traders large and small to profit. This post aims to shed some light on three mechanisms used in DEX arbitrage smart contracts, and how Nansen’s Wallet Profiler offers a powerful and efficient tool for analyzing and understanding DEX arbitrage.

What is arbitrage?

Let's get started with some definitions. An exchange is any organized marketplace where traders can buy and sell assets according to the rules set by that market’s operator. In a centralized exchange, or CEX, the marketplace is hosted on the operator’s private infrastructure, and it is only available to authorized traders. Orders placed by traders on a CEX are sent to the operator via a direct connection and processed behind the scenes. Traditional institutions like the New York Stock Exchange (NYSE) or Nasdaq, and crypto-institutions like Coinbase or Binance are all forms of centralized exchanges.

A decentralized exchange, or DEX, in contrast, is a marketplace that is hosted on public infrastructure (e.g. the public Ethereum blockchain), and is open to anyone as long as you have the right tools to access them. In contrast to a CEX, orders submitted on a DEX are submitted via transactions to their respective public blockchain, and trades are executed using logic built into smart contracts deployed on that network. Uniswap and 0x Protocol are two of the most used DEXes as of the writing of this article.

Want to track every single trade Smart Money makes on decentralized exchanges? Sign up for Nansen today!

Finally, arbitrage is the process of simultaneously buying and selling assets, usually on different markets, to profit from price differences.

To illustrate this, let’s look at the vintage comic book market. Comic books are sold in a variety of markets — specialty shops, in-person or online auctions, and even garage sales can all be considered different markets.

Let’s say you walk into a garage sale and see a rare comic book priced well below its market value. You know your local comic dealer would be eager to have it in their display case. You ring them up and agree to sell it to the dealer at a specific price, and simultaneously buy it at its lesser price from the garage sale netting yourself a nice little profit. This is essentially how arbitrage works — you buy and sell assets on different markets and take advantage of the price differences.

Arbitrage works very similarly in the exchange world, only that there are multiple buyers and multiple sellers at each marketplace, and trades on each marketplace will also put upward or downward pressure on the price. In fact it is the result of pressure from arbitrageurs that causes markets to gravitate toward the Law of One Price.

Image source: Nymag
Image source: Nymag

To illustrate the Law of One Price, imagine you have two vertical cylinders of water, cylinders A and B. Each cylinder represents a marketplace for identical assets with the height of the water representing the price of that asset on that marketplace. Now imagine you attached a small pipe with a valve to the very bottom of these cylinders so that water can move back and forth when the valve is in the open position. This pipe represents arbitrageurs between A and B.

Let’s say the height of water in cylinder A is a little higher than it is in cylinder B and the valve on the pipe between them is closed. What will happen to the height of the water in cylinders A and B when you open the valve?

When the valve opens, gravity will force the water to flow from cylinder A into cylinder B, lowering the height of A and raising the height of B until they are equal. This is exactly how arbitrage works — it pushes the price down where it is high, and raises it where it is low until they align with one another.

Arbitrage and Risk

So now that we have a good understanding of what arbitrage is, let’s talk a little about a key risk arbitrageurs face when trading on exchanges in this manner — execution risk.

When a trader submits an order to the exchange, it may take time before that order is confirmed or “settled”. During this lag time, the price of that asset may change in an unfavorable manner to the trader. This is what we call execution risk, and it exists today for traders across a wide variety of marketplaces. In the comic book example above, when you buy the comic book at the garage sale, you face the very real possibility that the dealer decides the comic book just isn’t worth your agreed upon price and offers you a lower price when you walk into their store. This is analogous to execution risk. Furthermore, this scenario should illustrate why it’s a good idea for you to complete your trade as quickly as possible and head right to the comic book store after your initial purchase.

To counteract execution risk, traditional finance firms invest money in speed — and huge sums of it at that. Michael Lewis’s book Flash Boys details one such investment on a $300mm USD fiber-optic cable running from Chicago to New Jersey just to save a few milliseconds on data transmission.

Image source: Bloomberg

To them, speed is everything, and the biggest budgets help create the fastest trading systems. As such, profiting from arbitrage is all but impossible except for the largest firms.

While it’s possible for a small shop or individual trader to make money off of arbitrage, it gets harder and harder for them every day as the market gets more and more crowded and the big players get bigger. It’s not far-fetched to say that the firms with the deepest pockets are already finding ways to tilt the scales in their favor. For instance, it’s likely that sophisticated CEX arbitrageurs run their bots on the same cloud providers used by the exchanges they interact with.

How DEXes are different

In a decentralized exchange context, the ground rules are different, and even small traders can still turn a profit. In contrast to their CEX counterparts, DEX arbitrageurs can operate without execution risk (although not all do, as will be explained below).

A specially crafted arbitrage smart contract can execute multiple trades on different markets with a single transaction. These atomic transactions guarantee that either all of the trades will go through, or none of them will. This limits the trader’s downside to just the gas fees they pay for a dud of a transaction that doesn’t go through.

Similarly, speed is no longer a major advantage when trading on a DEX. In Ethereum block times are roughly 15 seconds. Period. Every trader big and small must wait for the next block of transactions for their orders to be confirmed. As such, shaving a few milliseconds off your transaction processing time has little if any impact on your profitability. This greatly levels the playing field for traders of all sizes in contrast to traditional CEX markets.

That being said, arbitrageurs still face risks when trading on a DEX.

Miners play a significant role in DEX trading environments. Transaction ordering — the way miners decide whose transactions get processed first — is a function of the gas price paid for those transactions. The more you pay in gas, the more likely a miner is to confirm your transaction first. DEX traders are also susceptible to frontrunning from miners or other third parties. When traders submit transactions to the network, other traders or the miners themselves may take advantage of the knowledge they obtain from these pending transactions and submit their own trades to be processed first.

Both transaction ordering and front running can be influenced by personal relationships or outside business dealings. It’s very possible, for instance, that mining pools may offer front running as a service.

Even with these risks, however, there is still a vibrant trading ecosystem on DEXes. With the rapid pace of innovation and asset introduction, as well as the overall nascency of these markets, there is still a lot of opportunity for players big and small to get a slice of the pie, arbitrageurs included.

Real-world DEX arbitrage contracts

So we have an idea in mind about the pros and cons of DEX arbitrage, let’s look at some real-world examples of how traders are arbitraging on DEXes today.

We analyzed data from Uniswap and identified 3 types of DEX arbitrage smart contracts exhibiting different mechanisms and levels of sophistication:

  • Type 1 arbitrage contracts are the most sophisticated of the three. They take advantage of gas tokens, flash loans, mempool transaction replacement, and trade batching (wrapping multiple trades across one or more DEXes into a single transaction).
  • Type 2 arbitrage contracts only use trade batching to wrap multiple trades across one or more DEXes into a single transaction, but do not show any of the other sophisticated features
  • Type 3 arbitrage contracts do not batch trades. Instead, an operator performs two-point arbitrage using separate transactions. Nevertheless, some of these contracts may use other techniques like gas tokens.

Mechanisms

Trade batching ensures that either all trades go through, or none of them do. In the comic book example, you can think of this like having the comic dealer coming to the garage sale to meet you where you simultaneously buy and sell the comic book in one place with one swift action. On the flip side if the comic dealer doesn’t show up, you don’t buy the comic book from the garage sale.

Flash loans are another sophisticated technique, and go hand-in-hand with trade batching. They allow anyone to borrow funds they need to complete a trade and then pay them back in the same transaction. Let’s continue with the same scenario in trade batching where the comic dealer meets you at the garage sale. You can think of using a flash loan like having a cash-carrying friend at the same garage sale lend you the money you need to buy the comic book and then get their loan repaid right after you sell the comic to the dealer. Using this mechanism arbitrageurs do not require a great deal of funds to make money — they can just borrow and return funds via flash loans without having to risk any collateral.

Gas tokens allow arbitrageurs to reduce the transaction fees of their smart contracts. Typically, when the Ethereum network is less busy and gas prices are low, such arbitrageurs mint gas tokens cheaply. If they perform arbitrage when gas prices are higher, they burn these tokens instead of paying the current prices. In the comic book example, you can think of using gas tokens like filling your car’s fuel tank up when the price of fuel is low, and then driving your car a week later to complete your trades regardless of the current price at the pump.

Mempool transaction replacement is a sophisticated frontrunning technique. If an arbitrageur sees that their pending transaction may be frontrun and they don’t want to fight back (they could frontrun the frontrunner), they may create a simple transaction with a higher gas price that when mined would cancel out their initial transaction. This reduces the arbitrageur’s potential losses. (Note that some but not all arbitrageurs are frontrunners, and not all frontrunning arbitrageurs use this technique.)

Turning back to the comic example, let’s say the comic book store puts up a comic for an open auction on their webpage. You bid a high price for the comic, and thinking you’ve won, you start driving to the store to complete the transaction. On your way there you see that someone else has outbid you for the same comic (they have replaced your transaction). While you could raise your bid again (and replace their transaction), you decide it’s no longer worth your time and money, and instead turn around and head back home. This cuts your losses and saves you the time and gas you’d spend on going all the way to the store for nothing. This is more or less how mempool transaction replacement works.

Finally, two-point arbitrage using separate transactions is the most simplistic mechanism. Such an arbitrageur buys and sells an asset in two transactions in quick succession, and hopes that they net a profit. Often, they do not, as more sophisticated arbitrageurs take away this opportunity first, causing them to lose money overall.

Diving into DEX arbitrage data

Nansen provides labeled addresses of contracts involved in arbitrage, categorized by the above types. Its dashboards provide further information about each address. The Wallet Profiler dashboard, for instance, allows users to view at a glance information about a Type 1 arbitrage contract 0x0000...67e5:

Nansen wallet profiler dashboard

The Wallet Profiler shows that this contract is extremely active as it did thousands of transactions over roughly two months. Moreover, it seems to have been active at all times of the day, which indicates that it could be operated by a bot. The dashboard further provides a helpful overview of the addresses which it interacts with:

Top interacting wallets dashboard

This screenshot only shows one page out of many, but we can already see that this contract interacted with Externally Owned Accounts, or EOAs, that also performed flash loans, and has also made many transactions to various DEXes like Uniswap and Sushiswap.

The Wallet Profiler makes quick work of another sophisticated arbitrage contract, 0x0000...9f56. At a single glance, we can see that this contract operates on Uniswap, Kyber, 0x, Bancor, Balancer, and Sushiswap. Most interestingly, the dashboard also shows a large number of self-destruct transactions, which indicates that it burns a kind of gas token to reduce gas costs.

Conclusion

Through this post we hope you have gained a better understanding of what arbitrage is, how sophisticated arbitrageurs are trading across DEXes, and how you can examine the data yourself via Nansen’s Wallet Profiler (which you can check out at https://nansen.ai/).

Thanks for reading, and sign up for a Nansen plan today!

Disclaimer

The authors of this content and members of Nansen may be participating or invested in some of the protocols or tokens mentioned herein. The foregoing statement acts as a disclosure of potential conflicts of interest and is not a recommendation to purchase or invest in any token or participate in any protocol. Nansen does not recommend any particular course of action in relation to any token or protocol. The content herein is meant purely for educational and informational purposes only and should not be relied upon as financial, investment, legal, tax or any other professional or other advice. None of the content and information herein is presented to induce or to attempt to induce any reader or other person to buy, sell or hold any token or participate in any protocol or enter into, or offer to enter into, any agreement for or with a view to buying or selling any token or participating in any protocol. Statements made herein (including statements of opinion, if any) are wholly generic and not tailored to take into account the personal needs and unique circumstances of any reader or any other person. Readers are strongly urged to exercise caution and have regard to their own personal needs and circumstances before making any decision to buy or sell any token or participate in any protocol. Observations and views expressed herein may be changed by Nansen at any time without notice. Nansen accepts no liability whatsoever for any losses or liabilities arising from the use of or reliance on any of this content.

Related Articles