DeFi 101. Part 3— Spot Trading, Price Oracles, and Liquidations

A 3-part series on decentralized lending, margin trading, and spot trading

Missed the other parts? Check out Part 1 and Part 2

Recap

Up to this point we’ve discussed decentralized lending, borrowing, and margin trading. Let’s quickly touch on some of the big takeaways from the first two articles before diving into part 3.

Lending: Decentralized loans are secured through the use of collateral, which is deposited by the borrower. Lenders deposit assets into smart contracts seeking interest from borrowers.

Borrowing: Decentralized borrowers deposit collateral into smart contracts in order to borrow assets from lenders. If the value of the borrower’s collateral gets too close to the value of their loan, their collateral can be “liquidated” to pay back the loan.

Margin Trading: The act of using borrowed funds to take leveraged positions on markets in an attempt amplify their gains/losses and to predict price changes. Margin trading is inherently high risk, high reward.

While in the previous two articles we have glossed over some of the infrastructure that is critical to pulling these features off for simplicity, part 3 will dive into these in a bit more detail.

Disclaimer: this series of articles is designed to be educational and informative. It is NOT investment advice. It is not an attempt to sway you into Margin Trading, nor do I assume any liability for your trades :) All trading, especially on margin, is inherently risky: please be careful and do your own research and due diligence!

Part 3 — Decentralized Spot Trading, Price Oracles, and Liquidations

What is Spot Trading?

Spot Trading is the act of exchanging one asset for another. An example of spot trading in the crypto space is trading 1 ETH for its current value in DAI (today it’s ~150 DAI). The word “Spot” essentially denotes that this is effectively just 1x leveraged trading.

While there are a ton of different ways you could do spot trading, the most discussed method in the context of DeFi is to trade your assets on a Decentralized Exchange (or DEX).

Trading (whether margin or spot) is often accompanied with graphical representations of price history. Source: https://ddex.io/margin/ETH-DAI

Spot trading markets are a critical component of the DeFi ecosystem. Lending, borrowing, and margin trading all rely on functioning spot trading markets of some kind. For lending and borrowing, spot trading is often used to maintain automated liquidations. For margin trading itself, in order leverage a position the borrower will have to use some sort of a spot trading market to trade their borrowed assets.

It’s fair to say that without a functioning spot trading markets, DeFi would be extremely narrow in scope.

Lego Blocks vs Native Spot Trading

Decentralized Margin Trading platforms actually have an intriguing decision to make: should I use my own spot trading market, or should I use someone else’s spot trading market? This is a completely new question that does not exist for centralized platforms.

The early versions of decentralized margin trading platforms were essentially lego blocks built on top of existing decentralized exchanges. These platforms provided smart contract based layers for users to lend and borrow, while the actual trading that supported positions (and sometimes liquidations) was performed on a 3rd party exchange. Nuo, the late opyn, and fulcrum (also the early version of dydx) all use 3rd party exchanges to facilitate trades.

There are now two decentralized margin platforms that use a native exchange to facilitate trades: dydx and DDEX. These margin platforms have their own spot trading markets which facilitate opening and closing margin positions. There are pros and cons to doing this.

The big pro is that using a native market allows users to make advanced order types. Since these platforms don’t have to rely on compositing multiple smart contracts together (that may or may not fit perfectly), users are able to see more professional trading features on these non-custodial platforms (eg. limit and stop-limit orders, position adjustment, etc.). Users also benefit from a few QOL benefits, such as lower gas costs, no front running, and instant order matching.

The downside is that these projects now need to focus a large portion of their energy on maintaining a powerful spot trading market. If they can’t offer competitive spreads and depths, then they will likely struggle to stay competitive in this emerging landscape.

Price Oracles

Boromir said it best!

In parts 1 and 2, we discussed the concept of comparing collateral value vs borrowed value as a way to determine whether a loan should be liquidated or not. What we didn’t discuss was how the values of these assets are determined. These values, or prices, are assigned through “price oracles”.

Price oracles come in a variety of shapes and sizes, some more centralized, some more decentralized, but the general concept is that price oracles provide a price for a given asset. While this sounds obvious, doing this 24/7 with no false values is more complex than you might think.

Centralized margin exchanges have used various shapes of price oracles for a while now, but even these have their own issues. Trying to decentralize the process makes some aspects easier (downtime), but some aspects much more complex (trustlessness).

Why are price oracles important in DeFi?

We mentioned before that borrowers have to deposit collateral in order to take out loans. So imagine that you’re a borrower who wants to borrow $100 of DAI using ETH as your collateral. If the platform requires a collateral rate of 150%, you need to deposit $150 worth of ETH. How does a platform determine what the current value of ETH is?

This is where price oracles come in. An ETH-DAI price oracle could say “the current price of ETH is $150”, thus the user would need to deposit 1 ETH for their loan. How does the price oracle come up with this value though?

You might say: why not just grab the mid-market price off of a big exchange? Not a bad idea, but what if the exchange’s servers go down? What if their market liquidity falls off? Or what if someone purposely manipulates the mid-market price of a market?

Price oracles achieve this 24/7 reporting in a several different ways. Generally they are based on some sort of price feed or aggregation of several price feeds. The data for the current value of an asset could come from a centralized price feed, a blockchain based price feed, or a variety of complex algorithms.

If you lend/borrow in DeFi right now: do you know where the price oracles are getting their data on the platforms you use?

Incentive To Fail

As a borrower, I would love it if the price oracle were to screw up and say that 1 ETH is worth 10 million dollars. With only 1 ETH as collateral, I could then borrow and withdraw millions of dollars worth of other assets. Talk about incentive! If someone could manipulate the price oracle to spit out a bad value, there is potentially quite a bit of profit to be made.

Because there are those who could profit off of price oracle failure, securing price oracles is an area of great debate in the DeFi community. An effective price oracle must be robust — any failure could be catastrophic.

Liquidations

So we learned that a borrowers collateral can be liquidated if the value of their collateral gets too close to the value of their loan. But what does this liquidation do exactly? Why is it needed and how does it work?

There’s actually not a perfect standard for this right now — it varies by platform. I’ll go over the main methods though.

The Goal of Liquidations

The goal of a liquidation is to repay the borrower’s loan.

Liquidations are designed to protect lenders from having their borrowers default on their loan. This is achieved by taking the borrower’s collateral and converting it into the loan they took out.

How Liquidations Work

Liquidations need to find a way to convert the borrower’s collateral back into their loan. So if a borrower used ETH as collateral to borrow DAI, the liquidation would need to SELL the ETH for DAI to repay the loan.

Simple then, right? Just sell the collateral off on the spot market and repay the loan — easy! Alas, only a few platforms actually use this method explicitly, and with good reason. If several large positions get liquidated simultaneously during periods of high market volatility, the spot market could potentially get drained of all liquidity. If the spread gets really large, then simply selling the collateral back on the spot market won’t completely cover the loan!

Instead, many platforms use 3rd party liquidator bots to perform these liquidations. These bots search for positions that can be liquidated — hoping to make a profit off of the ill-fated borrowers.

Incentivizing Liquidations

The 3rd party liquidators are incentivized to do these liquidations because they are hoping to achieve a better price than the current market rate. Different platforms provide incentives to liquidators in a variety of ways:

Maker and DDEX both use a sort of dutch auction, where greater portions of the borrower’s collateral are incrementally offered to liquidators.

Other platforms like dydx offer an instant reward for the liquidators, like an automatic 5% off of midmarket price.

Some platforms just try to market buy/sell collateral on their respective spot markets to repay as much of the loan as possible

These methods have different pros and cons. The dutch auction may be gentler to the borrowers, but potentially less profitable for liquidators and slower. The automatic reward structure is sweet for the liquidators, but can be pretty brutal for the borrower who is already licking his liquidated wounds.

Regardless of the method, a successful liquidation is one that always pays loan back!