---
title: 3.2 DEX / AMM / Aggregators / Perps 
description: Specific examples of the DeFi product stack and its layers of complexity
---

# 3.2 On DEXs / AMMs / Aggregators / Orderbooks and Perps

**“Decentralized finance is, in some views, a category that started off honorable but limited, turned into somewhat of an overcapitalized monster that relied on unsustainable forms of yield farming, and is now in the early stages of setting down into a stable medium, improving security and refocusing on a few applications that are particularly valuable.” ([Vitalik](https://vitalik.eth.limo/general/2022/12/05/excited.html))**

Getting into the nitty-gritty of Decentralized Finance (DeFi), centers upon a number of first principles and underlying benefits as compared to existing finance. We will then move into looking at specific examples of the DeFi product stack, in increasing layers of complexity. 

## Principles of DeFi: Benefits and Legacy Shortcomings

As we explore the following DeFi legos, keep in mind the table of benefits and shortcomings as DeFi compares to the legacy financial system: 


| DeFi Benefit | Legacy System Shortcoming |
| --- | --- |
| Efficiency | Slow Settlement Cycles |
| Transparency | Liquidity Challenges |
| Financial Inclusion | Lack of Assurance Around Underlying Assets |


## _Service Categories of DeFi_

While we will not be zooming in on each of these verticals, to date, these are the primary foci of DeFi: 

* _Stablecoins. Maintains stable value most usually pegged to a real-world currency or asset such as the US Dollar. (to be discussed below)_
* _Exchanges. Allows for the trading of one digital asset into or for another. (to be discussed below)_
* _Credit / Lending Markets. Creates loans and payment conditions between digital assets utilizing smart contracts to manage the loan and repayment of the assets. (to be discussed in lecture 4 of this module)._
* _Derivatives. Complex financial instruments built on top of existing digital instruments._ (to be discussed below). 
* _Insurance. Covers a future scenario as programmed in smart contract or agreed upon on-chain, such that if the event happens the protocol pays out to the wronged party_  Dominated by [Nexus Mutual.](https://nexusmutual.io/) And specifically focused on covering smart contract vulnerabilities and exchange.  
* _Asset Management. On-chain management of funds at scale, including risk mitigation, and time-horizon customization of assets._ Decentralized hedge-fund management, as evident from Enzyme.finance. 

## _Incentive Mechanisms Used Across Protocols in DeFi_

As we dive into the different protocols, it is useful to keep in mind the underlying incentives that tend to govern most of the actions of the different network participants: 

* **Lock Up Yields:** Whereby interest is paid out to users, willing to lock up their value into the protocol, to be used primarily as liquidity or collateral that other traders can make use of on the protocol. 
* **Liquidation Fees:** When liquidity providers or market makers, are rewarded when undercollateralized loans are liquidated and a proportion of the liquidation value is paid out to the liquidity providers. 
* **Liquidity Mining:** Users are rewarded from the protocol in question for locking up their value in a liquidity pool, and paid out in the protocol token as an incentive mechanism. In these scenarios, users are rewarded at a fixed rate in tokens from the mother protocol in proportion to the reward fee set for the pool the user is participating in. 

## The Network Participants Tend To Revolve Around The Follow Characters

* **Programmable Financial Functions:** When the smart contract is designed to execute a specific function, when certain preconditions are fulfilled: Swapping one asset for another, lending out at a certain rate for a collateral deposit, paying out or liquidating on certain market dynamics.  
* **Liquidity Providers:** Participants that lock up and provide value for the protocol, for certain incentives and time-conditions. 
* **Users:** Participants who engage with the protocol, by placing their value (assets) in the protocol, and thereby interacting with the smart contract and the liquidity pool. 

The different use-cases outlined below, all revolve around these central stakeholders. 

## Decentralized Exchanges and AMMs

An Automated Market Maker (AMM) is a type of algorithm used to enable the trading of assets without the need for a traditional order book. Instead of matching buyers and sellers based on pre-existing orders (as traditional order books do), AMMs use a mathematical formula to determine the price of an asset based on the amount of liquidity in the market.

In this sense, a user is not trading against another user, but rather against a protocol’s liquidity pool - that is to say, the algorithm itself. When a user wants to trade an asset, they can do so through the AMM by providing the desired amount and the asset they want to trade for. The AMM will then use its formula to determine the price of the trade and execute it automatically.


![](@site/static/img/bootcamp/mod-em-3.2.1.png)


AMM’s invert traditional order books in a number of ways: 



* They can facilitate trading without the need for a central authority or exchange. This allows for more decentralized and efficient trading of assets.
* AMMs can enable the trading of assets that may not have a pre-existing market or liquidity, as the AMM can create a market for them on-the-fly.
* AMM’s provide a ‘constant’ liquidity event, for users or investors to reliably sell assets at any point in time - regardless of if there is a counterparty willing to purchase their assets. 
* NEAR specifically, allows AMM’s to access liquidity across shards, in order to execute trades at optimal prices (for example, a trade on Ref, can pull liquidity from Trisolaris if need be). 

_The underlying incentive governing AMM’s is known as ‘Yield Farming’ - it specifically refers to the first of the three incentive mechanisms discussed above._

Yield farming refers to the incentive mechanism by which a user is incentivized to participate in a liquidity pool. Users are rewarded, for providing liquidity - usually both pairs of it - into a specific pool. The APY (yield) is set either using the protocols native tokens, or tokens from the projects involved in the pool. In some cases, protocols can make agreements with a suite of tokens to offer a basket of yield to a given user. Importantly, the rates and returns from yield farming vary based upon the total amount of liquidity provided in the pool. In this sense, those who provide liquidity earlier on, stand to earn more.


![](@site/static/img/bootcamp/mod-em-3.2.2.png)


The _farming_ element, refers to a user's capacity to harvest rewards, and then promptly move over into another liquidity pool, so as to benefit from a higher APY. Many users will not ‘park’ their liquidity in a single pool for too long - and as a result there is sometimes a challenge in keeping liquidity on a certain protocol (known as sticky liquidity). 

## Uniswap and Sushi Swap

The first AMM to launch at scale (not counting 0x) was [Uniswap](https://defillama.com/protocol/uniswap). It remains the largest and most successful with 3.5 billion locked as of December 2022 (followed by Pancake Swap on BSC with 2.9 billion). Shortly after Uniswap launched, an anonymous team forked the open-source code of Uniswap, and named it Sushiswap, from which they launched and airdropped the $SUSHI token, which could be locked up to receive fees from the trades of users. This was one of the first times ever, that a massively successful protocol was forked. Shortly after, Uniswap launched its own governance token that did not harvest protocol revenue. The V3 of Uniswap also included a commercial license, which is set to expire in Quarter 2 of 2023. 


![](@site/static/img/bootcamp/mod-em-3.2.3.png)


While Sushiswap was well positioned to capitalize on the lack of fee sharing on Uniswap, poor management of the protocol by the team, coupled with a lack of committed ‘sticky’ liquidity, ultimately led to its demise. 

## _1inch and the Power of Aggregators:_

While many different AMM’s may compete on a single L1, it remains up to the user to decide which AMM may offer them the best deal for their swap. To simplify optionality for users, _aggregators_ are commonly utilized on top of a specific L1 ecosystem to offer users all available options for a specific trading pair - from which a small fee is taken on top. [1inch remains ](https://1inch.io/)the leading aggregator on Ethereum. 

## Orderbook DEXs and Perps / Futures

Orderbook DEXs refer to decentralized exchanges - that are structured to pair trades, as done normally through a centralized exchange. While orderbook DEX’s still operate off of user-pooled liquidity, the difference between an orderbook and an AMM is as follows: 

_In an AMM, the user is trading against the contract - and the algorithm programming the swap rates. With an Orderbook, the user is trading against other users - and has the freedom to sell their tokens at higher or lower prices, or with stop loss._

AMM’s predominantly rely upon passive market making - meaning the algorithm is changing, after a trade, and based on the current state of liquidity. Orderbook DEXs rely upon proactive market making - meaning a pair can be set at a higher or lower price, beyond what other users are trading for. 

Orderbooks in crypto are relatively new innovations due to the high-throughput and low cost requirements that traditional L1s like Ethereum were unable to accommodate for. As Consensys explains: 

_“Which lets users transparently match their orders with one another on a 'price-time-priority-basis.' In short, the highest bid and the lowest ask converge to represent the current market price, and users have the option to cross this bid-ask spread to immediately execute the order. Given the transparency of this system, users can see market depth in real-time. While these features of a CLOB (central limit Orderbook) are quite powerful, they are difficult to implement within an on-chain system. This difficulty stems primarily from high throughput and low execution cost requirements of an order book. That is, in order for a CLOB to effectively operate on-chain, gas fees must be cheap and transactions must be very fast.”_ ([Consensys](https://consensys.net/blog/cryptoeconomic-research/serum-a-decentralized-on-chain-central-limit-order-book/))

And with a robust orderbook structure - on-chain - and therefore a decentralized, open-source infrastructure, other builders can create further financial primitives on top of any Orderbook, including perpetual futures and token bond markets. 

Notably orderbook DEXs in crypto include:

**Serum on Solana:** [https://docs.projectserum.com/](https://docs.projectserum.com/)

**Tonic on NEAR:** [https://tonic.foundation/](https://tonic.foundation/)

## _Perpetuals_

Perpetuals or perps, refers to leveraged positions taken by a user. Unlike traditional futures contracts, which have a fixed expiration date, perpetual futures contracts do not expire. Rather, they are designed to mimic the underlying spot market and allow traders to hold their positions indefinitely. Positions only expire, when a user sells, or they are forceable liquidated due to the price dropping. 

While usually deemed risky for users, Orderbooks or liquidity providers stand to gain much from poor trading positions. Certain perpetual dApps such as [GMX ](https://gmx.io/#/)or [GNS ](https://gains-network.gitbook.io/docs-home/)allow users to take out up to 20x (sometimes more) leverage on their trades. 

From a protocol design perspective, the more value that is transacted or leveraged, means that the fees LP’s and stakers stand to gain are also higher - assuming the protocol has been designed in a decentralized manner. On GMX,[ GNP is the single sided liquidity pool](https://itsa-global.medium.com/itsa-defi-insight-gmx-on-chain-perpetuals-and-glp-935bb3168f0a#:~:text=GLP%20is%20a%20single%20sided,protocol%20lose%20and%20vice%20versa.) from which users are able to access fees, and which harvests the upside of liquidations. 

_The most important takeaway from these advanced financial protocols is twofold: (1) First, they require price matching from orderbook DEXs in order to operate, and (2) They have yet to be stress tested, in ways other crypto products (Terra/Luna) have shown serious vulnerabilities only at a later time._

## The Common Suspects of Token Utility in DeFi

In this initial overview, we are essentially circling between theory and reality, with the intent of driving home the core conceptual logic of decentralized finance: Self-executing code, used as a foundation for liquidity providers, and users to collaboratively service themselves. When it comes to token design, there are typically three common token-economics used. 

_Note_: Anyone working with such projects, must be attentive to the design of the token and specifically where the revenue is distributed in the protocol. 


* **Liquidity incentives:** As discussed above, this refers to yields, liquidation fees, and liquidity providing fees. Note that liquidity incentives from a project perspective centers on _emission rates _which must be sustainable in order for the token to maintain its value. 
* **Governance:** The capacity to vote in new rates, token emissions, revenue sharing designs, or even legal (as for example with Uniswap V3). Best examples to date include Uniswap and 1inch. Neither protocol token does anything more than governance. 
* **Yield from Fees:** This, as said above in reference to GMX or Sushiswap, is the basis for decentralizing the revenue sharing that a protocol may earn from fees. Token holders ‘stake’ or ‘lock-up’ their tokens, and in return yield fees from the active users of the network. 
* **Aidrops**: While not an actually token utility, it is very common for DeFi protocols to attract a committed and active userbase in the protocols early days, by guaranteeing a _snapshot_ and future airdrop for the most active users. As it sounds, the protocol team drops tokens to the addresses most actively using the platform. Usually this is done randomly, and it is no different than simply giving away money for free to early users. Suspects of this include 1inch and DYDX. 

## _Where is there room for innovation in DeFi?_

As we come to the end of this overview, there are a number of interesting areas whereby we can imagine DeFi to innovate in the coming years. At the top of the list includes the need to better align incentives _sustainably_ between the different stakeholders: 

* _Long term focused participants and token incentives_ How can we better incentivize sticky liquidity? How can this be done, without over-inflating the token via LP payouts? 
* _L1 - DeFi Integration for additional incentives._ Is there a way that the block inflation of the L1 ecosystem can be harnessed with a DeFi protocol to offer users rewards such that the DeFi native token can strengthen over time? Are there certain liquidity mechanics that an L1 ecosystem can provide to a DeFi protocol, in order to ensure further liquidity is attracted into the ecosystem? 
* _[Just in-time liquidity.](https://twitter.com/Crypto_McKenna/status/1599593678976454657?s=20&t=p1N1zudbRDGHZ1Vn2nF3Mw)_ Attempts to strengthen AMMs such that they can offer users quick trade execution at specific prices. However, as demonstrated in [Uniswaps blog](https://uniswap.org/blog/jit-liquidity), this remains a very small amount of trades on AMMs to date. 


![](@site/static/img/bootcamp/mod-em-3.2.4.png)


* _Risk Management Innovations:_ Through tranching protocols such as [BarnBridge ](https://barnbridge.com/)or [Saffron Finance](https://saffron.finance/), that are able to create delta neutral positions for users or through institutional pools, with improved KYC controls, we are likely to see increasingly complex infrastructures built on top of base-layer DeFi protocols in order to offer improved risk management and legal accommodation.