Unless you have been living under a crypto rock for past few months, you most likely have heard of #DeFi (Decentralized Finance) and all the ways people seem to be getting incredibly rich from it.
Instead of making yet another post on how to “farm all the yields” and earn over 9000% APR, we will try to explore what truly powers the decentralized finance and where the real technical complexity of it lies in.
Surface
At a surface level of DeFi, we have various protocols and clever ideas that have taken existing financial instruments and ported them over to the smart contract land of Ethereum.
We have lending protocols like Compound and Aave that allow users to borrow assets in one token while providing collateral with another. Protocols like Maker that act somewhat like a decentralized central bank and allow users to issue fiat pegged assets collateralized by volatile crypto assets. We also have decentralized exchanges like Uniswap, Curve and 0x protocol. Even decentralized hedge funds or investment firms that will put users money to work to earn returns like Yearn Finance. And many more forks of all these projects with various modifications.
While none of these protocols are by any means easy to build, the software part of them is still relatively simple. The ideas themselves are quite clever, but once the idea is known, building them is not that difficult. And since most of these protocols are fully open source, extending them and building slightly modified versions of them is even simpler.
Big challenge with most of these DeFi protocols, much like traditional finance, is the risk management. There are of course technical parts of the risk management, like bugs or mistakes in the smart contracts, but in the long run, much bigger risks are economic and financial in nature. For example, if a mistake is made with the choice of collaterals or lending/borrowing limits in a lending platform, a market crash type of event for the collateral can cause large losses for all the users of that lending platform.
Since we don’t know all that much about risk management, let’s go back to discussing the technical parts of what makes DeFi go around. As mentioned, protocols themselves aren’t that complex to implement. However, that is only scratching the surface of what truly powers DeFi. Underneath it all, there is huge amounts of complexity and automation that keeps things running and efficient.
DeFi Engine
Beneath the surface, we enter the land of automation. Bots that do market making, arbitrage, liquidations and some. All of the DeFi protocols depend on this background automation to function properly. Lending protocols require efficient liquidators to minimize their risk, decentralized exchanges depend on complex market makers and arbitrageurs to stabilize exchange rates and so on.
This space is much more complex on a technical level, primarily due to the competition. Early bird does get the worm when it comes to arbitrage. It also doesn’t help that Ethereum is an incredibly complex platform to operate such systems on. Working with Ethereum is certainly much more complex than interacting with a Centralized Exchange through their REST and WebSocket apis.
Let’s look at some basic ideas of what is needed to keep this DeFi engine running and their underlying complexities.
Simple Arbitrage
The idea behind arbitrage is fairly intuitive. If you can buy asset A with another asset B cheaper in system one, compared to selling asset A for asset B in system two, you can buy in system one, sell it in system two and keep the difference as either asset A or asset B as profit.
Arbitrage opportunities can exist between DEX-es like Uniswap, Curve and 0x, or between a DEX and a CEX, like Uniswap and Coinbase. Arbitrage opportunity can also exist between an Exchange and a Lending platform for liquidating under collateralized loans.
In DeFi land, we can split arbitrage trades in two distinct categories:
Fully on-chain
Half on-chain, half off-chain
Full on-chain arbitrage trades perform full arbitrage cycle in a single transaction since everything happens on the chain. Where as half on-chain, half off-chain trades perform one part of the arbitrage on-chain and another off-chain (e.x. with a centralized exchange). Both of these options have their pros, cons and complexities.
Fully on-chain arbitrage is unique to DeFi and sounds like a dream at first. Hypothetically, you could even do these type of trades with no initial capital by using flash loans. Risk should be low, since in worst case scenario, transaction fails and you only loss some gas fees. This is until you find out that some clever people have written front running arbitrage bots that can detect any “profitable” transaction that is queued in Ethereum mempool and run that transaction themselves first. Never letting you get in a profitable arbitrage trade. So your system does all the work to find profitable arbitrage trades and at the last second, someone else swoops in and takes all the profits. Not quite the dream as it may seem at a first glance.
Dangers and complexities of half off-chain trades are more intuitive. Since full arbitrage cycle can’t be done in a single transaction, there is always a risk for second half of the arbitrage to fail if someone else gets there first and manages to take the profits before your program gets there. It is a dog eat dog kind of world out there with arbitrageurs.
Built-in complexities aside, things are doubly complex due to how Ethereum network works. Complexities of gas pricing, transaction ordering and long (sometimes very long) block finalization times create even more risk for arbitrageurs. Not even mentioning hypothetical dangers of miners who have unfair advantage and control over transaction ordering and placement.
Market Making
Uniswap and its forks are called AMMs (automated market making) protocols which seems to give a vibe that these protocols don’t require any active market making or liquidity provisioning. Users just put in funds to provide liquidity once and wait for the profits to flow their way.
While currently there really is such free lunch for these protocols (mainly due to bubbly incentives through governance token distributions), in long term, that most likely won’t remain the case. Uniswap and other AMM LPs (liquidity providers) will require active market making to earn positive returns in the long term.
Fundamentally, there are two ways to arbitrage with Uniswap when price of the swap differs from price on another exchange:
You can do simple arbitrage and make a trade on Uniswap and counter trade with the other exchange.
You can be an LP in Uniswap, remove portion of liquidity, perform a trade on another exchange and reinsert liquidity in Uniswap again, increasing your overall LP share. There are multiple other versions of this, that includes adding/removing liquidity and making trades on other exchanges to continuously growing share of the pool. Never actually trading on Uniswap itself.
Version 1 is easier and doesn’t require maintaining long term exposure to either tokens or to the Uniswap protocol itself. However, it is fundamentally less efficient since you have to pay the trading fees to perform the arbitrage.
Version 2 is more similar to traditional market making where you maintain long term exposure to both tokens in a trading pair and to the protocol itself, but pay less trading fees, making it more efficient when done well.
This type of market making still has similar complexities to arbitrage. Being first is an advantage to get best rates when performing changes in provided liquidity. Much like arbitrage, it is also a race to the bottom to run the system with thinnest of margins.
Future DeFi Platforms
Considering that the protocols themselves are only the tip of the iceberg for DeFi, their implementations are less likely to be the biggest deterministic factor for deciding which network will become most dominant host of DeFi activity in the long term.
Networks that provide best environment for automated arbitrageurs and market makers have higher chance to capture most of the DeFi traffic in the future. Safer arbitrage means more efficient arbitrage, which leads to lower fees, efficient prices and safer DeFi products.
So what helps to make arbitrage safer?
Lower transaction latency and confirmation times.
Lower transaction fees. This is also correlated with higher transaction throughput.
Simple to understand transaction ordering rules that are “fair”. Defining “fair” is certainly difficult, but as a rule of thumb, more fair is better.
Less transparency for queued transactions. No arbitrageur wants other people to know what they are up to until their transactions are final and committed.
More atomicity the better. Being able to perform more operations as a single atomic transaction makes arbitraging safer. Things like sharding, or having to perform operations across different chains will generally lead to less atomicity, hence more risk and less efficiency.
Of course, before all of those, the most important part still remains overall volume and money that exists in the network. Ethereum has a distinct advantage since it natively has 10s of billions of dollars worth of value already available on the platform. Other networks have to depend on cross-chain interoperability to make at least portion of that value available and all cross-chain solutions come with extra associated costs and complexities with them.
It’s exciting to watch and see how DeFi will continue to develop. Will Ethereum make necessary improvements to become better platform for DeFi? Or will another network that already has better fundamentals as a DeFi engine, manage to amass sufficient amount of capital on it to properly rival Ethereum’s dominance?