# Posts tagged "defi":

# Reactive Exchanges - SLAMM dunk with Superfluid

## Introduction

*Superfluid protocol* enables money to move between entities continuously in time with no transaction. It is a new
paradigm of how the money payment system works. Using this innovative building block, one can build novel applications
for payroll, subscriptions, vesting, streaming AMMs, gaming, trade finance, rentals, and NFTs. Click here to read more
what is Superfluid.

Though modeling a problem domain explicitly with time is not a novel concept. In fact, functional reactive programming
(FRP) that was formulated in a ICFP 97 paper^{1} demonstrated that we can model animations elegantly and efficiently
using continuous time. More than 20 years later, it was Superfluid, the first one that successfully connected the dots
between FRP and the domain of money payment system, and has made it available on more than 6 EVM (Ethereum Virtual
Machine) chains^{2}.

In this article, we focus on one of its novel applications: different designs of exchanges that can facilitate swaps
continuously in time^{3}. By now, you should understand why I shall name them "Reactive Exchanges." Reactive exchanges
are types of exchanges modeled with functional reactive programming.

The article expects you to be familiar with the trading terminologies and related innovations on decentralized
ledgers. Axo trade whitepaper^{4}, for instance, provides an excellent overview of this domain. Additionally, it would
be best if you had an in-depth overview of what is Superfluid is and how it works^{5}.

## Different Reactiveness

Throughout the article, we separate the "reactiveness" of an exchange into three primary functions:

**Contribution**: does the input asset enter the exchange continuously in time?**Swap**: does swap between the input and assets happen continuously in time?**Distribution**: is output asset distributed continuously in time?

At the end of the article, we will then compare different designs using this classification.

## Pseudo Reactive Exchange

The first attempt at having reactive exchange is making only the contribution continuous in time. The actual swaps are
done through an external DEX (decentralized exchange), and distribution of the swap output is done through Superfluid
IDA^{6}, both happen periodically and are triggered by keepers ("cronjob at scale").

Let's call this "pseudo reactive exchange" since the actual swaps do not continuously in time. Here is a visualization of such exchange:

An example of such exchanges that is in production is Ricochet Exchange, an original and earliest example of how to
build an unique exchange using Superfluid. It has been live for over an year, and provides an efficient way for anyone
to DCA (dollar cost average)^{7} invest into on-chain assets.

## Reactive Constant Function Market Maker (R-CFMM) Exchange

CFMM (Constant Function Market Maker) was the first class of AMM (Automatic Market Maker) applied to real-world
financial markets^{4}. In this article, we look into making one of the earliest and simplest versions of CFMM,
Constant Liquidity Product (CLP) used in Uniswap v1 and v2 ^{8} ^{, }^{9}, reactive.

To solve the equations required to make the CLP reactive, I used the python binding of the SageMath framework, a free open-source mathematics software system licensed under the GPL:

from sage.all import var, assume, function, solve # let's define a handful of symbols L_a, L_b, T, r, r_a, r_b, t_0, t = var("L_a", "L_b", "T", "r", "r_a", "r_b", "t_0", "t") assume(t >= t_0)

First, let's define CLP:

from sage.all import var, assume, function, solve def CLP(x, y, x_prime, y_prime): """Constant Liquidity Product Swap""" return x * y == x_prime * y_prime assume(t >= t_0)

Secondly, let's try to use sage to solve the price function for selling asset A (sell amount \(a_\Delta\)) for asset B (get amount \(b_\Delta\)):

def solve_clp_a4b(): print("# Solve CLP equation for selling A for B instantly\n") v_a = var("a_Δ") v_b = var("b_Δ") clp = CLP( L_a, L_b, L_a + v_a, L_b + v_b ) sols = solve(clp, v_b) assert(len(sols) == 1) print(sols[0]) print("\n")

Sage spits out the well-known equation as expected:

\[L_{b\Delta} = \frac{L_b * a_\Delta}{L_a + a_\Delta}\]

Now it comes to how to make it reactive; two functions of time need to be solved: one for asset A and another for asset B. There are not enough free variables to solve two equations, but we do know that these two equations should be elated to each other. Here was the stroke of insight, and a magic variable "q" was introduced (and please don't ask more):

def solve_rclp_rtb_bidir(): print("# Solve Reactive CLP rtb_bidir equation\n") cf_a = r_a * (t - t_0) cf_b = r_b * (t - t_0) q = var("q") clp = CLP( L_a, L_b, L_a + cf_a + q * cf_b, L_b + cf_b + 1/q * cf_a ) sols = solve(clp, q) print("L_{flowswap_a} =", (1/q * cf_a).subs(sols[0])) print("L_{flowswap_b} =", (q * cf_b).subs(sols[0])) print("\n")

Gratefully, Sage spits out the magic formula we are looking for. Let's call it *flowswap* formula:

- \[L_{flowswap_a} = \frac{-(r_b * t_\Delta - L_b) * r_a * t_\Delta}{r_a * t_\Delta + L_a}\]
- \[L_{flowswap_b} = \frac{-(r_a * t_\Delta - L_a) * r_b * t_\Delta}{r_b * t_\Delta + L_b}\]

Here is an illustration of the global view of a reactive CLP exchange:

Another way of seeing it is from how swaps look after "reactification" on the chart of the CLP formula:

To make the exchange work for more participants, we again need to use IDA from Superfluid money, where the proportion of the flowswap output is determined by the market takers' continuous flow contributions.

Regarding "reactiveness, " market takers always "flowswap" constant flows for another non-linear flow of money, and they are both continuous in time and reactive. However, Superfluid money cannot distribute money through arbitrary formulas; that's why the distribution cannot be reactive unless a bespoke flowswap primitive is a built-in feature of the money.

You can find the work-in-progress prototype of this in the T-REX (Toy Reactive Exchange) Monorepo. If you are interested in making this closer to production, join Superfluid's wave pool program.

## Zero-Intermediate-Liquidity Market Maker (ZILMM) Exchange

Another fascinating and unique variation of AMM enabled by Superfluid money is a market maker that requires zero intermediate liquidity, where liquidity goes from LPs to traders directly as constant flows. As a result, a trade-off (or feature) is that such exchanges may not easily facilitate instant swaps.

I shall call such exchange *ZILMM: Zero-Intermediate-Liquidity Market Makers*. The pioneer of such design is
Superfluid's ecosystem project Aqueduct. We will leave the reader to discover more about the project on their own, and
we will only include an illustration from them to get a taste of it:

At market liquidity "equilibrium"

Traders tilting the "equilibrium"

Since all flows involved are constant flows, it should not be a surprise that this design of the reactive exchange ticks all the reactiveness boxes!

## Conclusion

Let us review the "reactiveness" of all three Superfluid money enabled market maker designs:

Exchange Type | Contribution | Swap | Distribution | Known |
---|---|---|---|---|

/ Reactiveness | Reactiveness | Reactiveness | Reactiveness | Projects |

Pseudo | Yes | No | No | Ricochet |

R-CFMM | Yes | Yes | Maybe(a) | T-Rex |

ZILMM | Yes | Yes | Yes | Aqueduct |

Note:

a) Distribution with the complex formula required by R-CFMM is viable but not necessarily desirable.

Each design has its pros and cons, and it is beyond the purpose of this article to dive into them.

However, Superfluid money demonstrably enlarged the design surface of AMM. I believe it should be a safe bet that innovations enabled by Superfluid money are not restricted to AMM and other types of financial contracts such as options, futures, interest rate swaps, etc., should have unique Superfluid money enabled designs too.

While reactive exchanges accurately reflects the underlying technology, a more playful term could be *SLAMM (Streaming
Liquidity Automatic Market Makers)*. I will end the article with a slogan:

**Let's SLAMM dunk it with Superfluid.**

## Footnotes:

^{1}

Elliott, Conal; Hudak, Paul. "Functional Reactive Animation". Functional Reactive Animation. ICFP ’97. Retrieve 14 July 2018.

^{2}

All deployments of Superfluid protocol can be accessed through Superfluid app.

^{3}

It should not to be confused with the existing financial term continuous trading.

^{8}

Uniswap v1 Protocol Hayden Adams et al. Novembrer 2018. URL: https://docs.uniswap.org/protocol/V1/introduction. Accessed on 15/10/2021.

^{9}

Uniswap v2 Core Hayden Adams et al. March 2020. URL: https://uniswap.org/ whitepaper.pdf. Accessed on 15/10/2021.