---
# System prepended metadata

title: Formal Model

---

# Formal Model

Before we can delve into how the Shell Protocol model works, we first need to define some key terms.

## 1. Definitions

Liquidity Pool
: A smart contract that holds and manages several different kinds of fungible assets, such as ERC-20 tokens of ether.  A liquidity pool allows users to deposit, withdraw, and swap these assets.

Liquidity Pool Arity
: The number of different asset kinds being managed by the liquidity pool.

Liquidity Pool Reserves
: A vector $[x_1, x_2, \dots, x_n]$, where $n$ is the liquidity pool arity and $x_i$ is the amount of $i$-th asset currently being held by the liquidity pool.

Liquidity Pool Shells
: ERC-20 tokens representing fungible shares in the liquidity pool and the assets being held by it.

Liquidity Pool State
: A pair $\langle [x_1, x_2, \dots, x_n], T \rangle$, where $[x_1, x_2, \dots, x_n]$ is liquidity pool reserves and $T$ is the total amount of the liquidity pool shells in circulation.

Deposit
: An act of putting additional assets into a liquidity pool in exchange for newly minted shells.

Withdrawal
: An act of taking assets from a liquidity pool in exchange for shells being destroyed.

Swap
: An act of putting certain amount of one asset into a liquidity pool in exchange for certain amount of another asset.


# Behaviors of the model
With these terms defined, we now need to define the key behaviors. Namely, we have deposits, swaps and withdrawals. In a deposit, the user adds tokens to the pool. In return, she receives shells. In a withdrawal, the user withdraws tokens by redeeming her shells. In a swap, the user exchanges one reserve token for another.

## 2. Model

Operations of a liquidity pool are controlled by the following three functions:

### 2.1. Deposit Function

We define the deposit function as:

$$
\mathcal{D}\,(\langle [x_1, x_2, \dots, x_n], T \rangle, [x'_1, x'_2, \dots, x'_n]) = y
$$

Here $\langle [x_1, x_2, \dots, x_n], T \rangle$ is the liquidity pool state before the deposit, $[x'_1, x'_2, \dots, x'_n]$ is the vector of asset amounts being put into the pool, and $y$ is the amount of shells being minted and given to the depositor in exchange for the assets.

The state after the deposit could be calculated like this:

$$
\langle [x_1 + x'_1, x_2 + x'_2, \dots, x_n + x'_n], T + y \rangle
$$

### 2.2. Withdrawal Function

We define the withdrawal function as:

$$
\mathcal{W}\,(\langle [x_1, x_2, \dots, x_n], T \rangle, [x'_1, x'_2, \dots, x'_n]) = y
$$

Here $\langle [x_1, x_2, \dots, x_n], T \rangle$ is the liquidity pool state before the withdrawal, $[x'_1, x'_2, \dots, x'_n]$ is the vector of asset amounts being taken from the pool, and $y$ is the amount of shells being taken from the withdrawer in changed for assets, and destroyed.

The state after the withdrawal could be calculated like this:

$$
\langle [x_1 - x'_1, x_2 - x'_2, \dots, x_n - x'_n], T - y \rangle
$$

### 2.3. Swap Function

For every $i \in \{1 \dots n\}$ and $j \in \{1 \dots n\} \setminus \{i\}$ we define swap function for exchanging $i$-th asset to $j$-th asset like this:

$$
\mathcal{S}_{i,j}\,(\langle [x_1, x_2, \dots, x_n], T \rangle, x) = y
$$

Here $\langle [x_1, x_2, \dots, x_n], T \rangle$ is the liquidity pool state before the swap, $x$ is the amount of $i$ asset being taken from the swap initiator, $y$ is the amount of $j$-th asset being given to the swap initiator in exchange.

The state after the swap could be calculated as $\langle [x'_1, x'_2, \dots, x'_n], T \rangle$, where
$$
x'_k =
\begin{cases}
x_k + x,\ \textrm{if}\ k = i \\
x_k - y,\ \textrm{if}\ k = j \\
x_k,\ \textrm{otherwise}
\end{cases}
$$

# This isn't a section but here is some Util Fn:
This model can be described equivalently in terms of the *utility function* $U(x_1,x_2,\ldots,x_n)$. Let us define
$$
U(x_1,x_2,\ldots,x_n)  = \prod x_i^{\alpha_i}
$$
for some  weights $\{\alpha_i\}$.


# This section will describe the model
Now that we have defined the relevant terms and behaviors of the model, we can discuss the model itself. What we have described so far is applicable to any liquidity pool, not just Shell. First, we will describe the macro utility function. Second, we will describe the micro functions. We will conclude by showing how this construct can efficiently generate a highly adaptable liquidity brain.

Here is where I will talk about the utility function, U = G - Psi. Then I will transition to focusing on the fee function, Psi. Because G is just the sum, if we want to understand the micro functions, we nee do focus on Psi.

I need to add things like U = G - Psi. Actually, I also need to add things like having a liquidity neuron. Hmm, I must admit that the easiest thing to do here is to ditch the liquidity brain/neuron altogether and just bang this out. Maybe that's what I should do? Assuming I don't, how do I go about this? I think the first step is to introduce the utility function. I should probably explain at a high level how/why having a utility function is worthwhile. Indeed, I should probably do that before I talk about the model per se. You know what, here's what I'm thinking. We take section 3 and put it into an appendix. And the proofs validating the model properties will also go into an appendix. That way, the paper will flow like this: liq pool story -> utility function -> review of Uni-Bal-Crv -> liq brain -> formal model: definitions/terms -> macro-level util function -> micro-level fee functions -> deposits-withdrawals -> swaps. I think the formal model section can actually be quite compact. The appendix will be longer which is kind of the point. Most people won't be able to understand it anyway, and it'll distract from the flow. Plus, I can make minimal edits to ABDK's work since the appendix doesn't need to read as nicely as the rest of the paper. Anyone reading this section won't be reading it for the story but for the nitty-gritty details.

### 4.4.1. Fee Function

Let $G$ be the gross liquidity of the protocol:

$$
G = \sum_i x_i
$$

Let $w_1, w_2, \dots, w_n$ be the ideal weights of the assets in the portfolio.

Fee function:

$$
\Psi ([x_1, x_2, \dots, x_n]) = \Delta\cdot G \sum_i w_i \Psi_i\!\left(\left|\frac{x_i}{w_i G} - 1\right|\right)
$$

where

$$
\Psi_i (\xi_i) =
  \begin{cases}
    0, & \textrm{if}\ \xi_i \leqslant \beta \\
    \left(\xi_i - \beta\right)^2, & \textrm{otherwise}
  \end{cases}
$$

Here $\xi_i = \left|\frac{x_i}{w_i G} - 1\right|$ is the imbalance of $i$-th asset in the portfolio.

#### 4.4.1.1. Fee Formula with Limited Derivative

Ideal balance $I_i = G \cdot w_i$.
Lower threshold $L_i = I_i \cdot (1 - \beta)$.
Upper threshold $U_i = I_i \cdot (1 + \beta)$.
Margin $M_i = \max (L_i - x_i, x_i - U_i, 0)$.
Fee intermediate $m_i = \Delta \cdot \frac{M_i}{I_i}$.
Fee $F_i = M_i \cdot \min \left(m_i, \frac{1}{4}\right)$.

Lets do some reductions:

$$
M_i =
\max (L_i - x_i, x_i - U_i, 0) =\\
= \max \left(I_i \cdot (1 - \beta) - x_i, x_i - I_I \cdot (1 + \beta), 0\right) =\\
= I_i \cdot \max \left(1 - \beta - \frac{x_i}{I_i}, \frac{x_i}{I_i} - 1 - \beta, 0\right) =\\
= I_i \cdot \max \left(\left(1 - \frac{x_i}{I_i}\right) - \beta, \left(\frac{x_i}{I_i} - 1\right) - \beta, 0\right) =\\
= I_i \cdot \max \left(\max\left(1 - \frac{x_i}{I_i}, \frac{x_i}{I_i} - 1\right) - \beta, 0\right) =\\
= I_i \cdot \max \left(\left|\frac{x_i}{I_i} - 1\right| - \beta, 0\right)
$$

And some more reductions:

$$
F_i = M_i \cdot \min \left(m_i, \frac{1}{4}\right) =\\
= M_i \cdot \min \left(\Delta \cdot \frac{M_i}{I_i}, \frac{1}{4}\right) =\\
= I_i \cdot \max \left(\left|\frac{x_i}{I_i} - 1\right| - \beta, 0\right) \cdot \min \left(\Delta \cdot \frac{I_i \cdot \max \left(\left|\frac{x_i}{I_i} - 1\right| - \beta, 0\right)}{I_i}, \frac{1}{4}\right) =\\
= I_i \cdot \max \left(\left|\frac{x_i}{I_i} - 1\right| - \beta, 0\right) \cdot \min \left(\Delta \cdot \max \left(\left|\frac{x_i}{I_i} - 1\right| - \beta, 0\right), \frac{1}{4}\right) =\\
= \begin{cases}
0, & \mathrm{if}\ \left|\frac{x_i}{I_i} - 1\right| \leqslant \beta\\
\Delta \cdot I_i \cdot \left(\left|\frac{x_i}{I_i} - 1\right| - \beta\right)^2, & \mathrm{if}\ \beta < \left|\frac{x_i}{I_i} - 1\right| \leqslant \beta + \frac{1}{4\Delta}\\
\frac{I_i}{4} \cdot \left(\left|\frac{x_i}{I_i} - 1\right| - \beta\right), & \mathrm{if}\ \beta + \frac{1}{4\Delta} < \left|\frac{x_i}{I_i} - 1\right|
\end{cases}
$$

### 4.4.2. Deposit, Withdrawal Functions

Let $\mathbf{x} = [x_1, x_2, \dots, x_n]$ be initial reserves, and $\mathbf{x}' = [x'_1, x'_2, \dots, x'_n]$ be the amounts of asserts being deposited or withdrawn.  Then, deposit and withdrawal functions look like this:

$$
\mathcal{D}(\langle\mathbf{x}, T\rangle, \mathbf{x}') = 
\frac{T}{G( \mathbf{x})-\Psi( \mathbf{x})}\left(G(\mathbf{x}') -
\rho\,\left(
  \Psi(\mathbf{x} + \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right)
$$

$$
\mathcal{W}(\langle\mathbf{x}, T\rangle, \mathbf{x}') = 
\frac{T}{G(\mathbf{x})-\Psi( \mathbf{x})}\left(G(\mathbf{x}') +
\rho\,\left(
  \Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right)
$$

where

$$
\rho\,(x) =
\begin{cases}
  x, & \textrm{if}\ x > 0 \\
  \lambda x, & \textrm{otherwise}
\end{cases}
$$

### 4.4.3. Swap Functions

Note that the deposit function applied to negative vector $\mathbf{x}'$ gives the withdrawal function:
\begin{multline}
\mathcal{D}(\langle\mathbf{x}, T\rangle, -\mathbf{x}')  =\frac{T}{G( \mathbf{x})-\Psi( \mathbf{x})}\left(G(-\mathbf{x}') -
\rho\,\left(
  \Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right) =\\=-\frac{T}{G( \mathbf{x})-\Psi( \mathbf{x})}\left(G(\mathbf{x}') +
\rho\,\left(
  \Psi(\mathbf{x} + \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right) = \mathcal{W}(\langle\mathbf{x}, T\rangle, \mathbf{x}')
\end{multline}
i.e. withdrawal is just a deposit of a negative amount. 

Therefore, a swap operation that does not modify the shell supply must be equivalent to the deposit operation that results in zero minting. Denote the swap output by  $y$:
$$
\mathcal{S}_{i,j}\,(\langle [x_1, x_2, \dots, x_n], T \rangle, x) = y
$$
Then
$$
\mathcal{D}(\langle\mathbf{x}, T\rangle, \mathbf{x}') =0
$$
where $\mathbf{x}' =[0,\ldots,0,x'_i=x,0,\ldots,0,x'_j=-y,0,\ldots,0]$.

This is equivalent to
$$
G(\mathbf{x}') -
\rho\,(
  \Psi(\mathbf{x} + \mathbf{x}') -
  \Psi(\mathbf{x}))=0
$$
which reduces to
$$
y = x - \rho\,\left(
  \Psi([x''_1, x''_2, \dots, x''_n]) - \Psi([x_1, x_2, \dots, x_n])
\right)
$$

where

$$
x''_k =
\begin{cases}
x_k + x,\ & \textrm{if}\ k = i \\
x_k - y,\ & \textrm{if}\ k = j \\
x_k,\ & \textrm{otherwise}
\end{cases}
$$

Unfortunately, we have not been able to find a closed and compact formula for $y$.

### 4.4.4. Invariant

If we put $\lambda$ fee aside, then all operations preserve the following invariant:

$$
c \cdot T = \sum_i x_i - \Psi ([x_1, x_2, \dots, x_n]) = G( \mathbf{x})-\Psi( \mathbf{x}).
$$

Indeed, for the **deposit** we have

\begin{multline}
c \cdot T_{new} = \\
c \cdot T + c \cdot \mathcal{D}\,\left(\langle\mathbf{x}, T\rangle, \mathbf{x}'\right) = \\
c \cdot T + c  \cdot \frac{T \left(G(\mathbf{x}') -
\rho\,\left(
  \Psi(\mathbf{x} + \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right)}{G(\mathbf{x})-\Psi( \mathbf{x})} = \\
с \cdot T\left(1+\frac{G(\mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}') + \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right) = \\
(G(\mathbf{x})-\Psi(\mathbf{x})) \left(1+\frac{G(\mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}') +
  \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right) = \\
G( \mathbf{x})-\Psi( \mathbf{x})+G( \mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}')+\Psi( \mathbf{x}) = \\
G(\mathbf{x} + \mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}')
\end{multline}

The same holds for **withdrawal**:
\begin{multline}
c \cdot T_{new} = \\
c \cdot T- c \cdot \mathcal{W}\left(\langle\mathbf{x}, T\rangle, \mathbf{x}'\right) = \\
c \cdot T - c \cdot \frac{T \left(G(\mathbf{x}') +
\rho\,\left(
  \Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right)}{G( \mathbf{x})-\Psi( \mathbf{x})} = \\
с \cdot T\left(1-\frac{G(\mathbf{x}')+\Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right) = \\
(G(\mathbf{x})-\Psi(\mathbf{x}))
  \left(1-\frac{G(\mathbf{x}')+\Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right)= \\
G( \mathbf{x})-\Psi( \mathbf{x})-G( \mathbf{x}')-\Psi(\mathbf{x} - \mathbf{x}')+\Psi( \mathbf{x}) = \\
G(\mathbf{x} - \mathbf{x}')-\Psi(\mathbf{x} - \mathbf{x}').
\end{multline}

For the $\lambda$-enhanced versions we have the following inequations:
\begin{align}
\mathcal{D}_{\lambda\leq 1}(\langle\mathbf{x}, T\rangle, \mathbf{x}') \leq \mathcal{D}_{\lambda=1}(\langle\mathbf{x}, T\rangle, \mathbf{x}');\\
\mathcal{W}_{\lambda\leq 1}(\langle\mathbf{x}, T\rangle, \mathbf{x}') \geq \mathcal{W}_{\lambda=1}(\langle\mathbf{x}, T\rangle, \mathbf{x}').
\end{align}
Therefore $\lambda<1$ mints fewer shells at deposit and burns more at withdrawal, effectively increasing the shell price.


# We will define model properties and prove these properties in the Appendix
Monotonicity ensures that depositing tokens results in a positive amount of shells created. Withdrawing tokens results in a negative amount of shells created. And that for swaps, the user always puts in a positive amount of tokens and also receives a positive amount of tokens. Limitedness ensures that you can always take out what you put into the pool(??). Exhaustibility ensures that..?? Optimality means that a user cannot compose deposits, swaps and withdrawals in such a way that they can avoid fees or mitigate slippage(??). And also limitedness...?? 

## 3. Model Properties

The Model may have the following properties:

### 3.1. Monotonicity

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$, and any two vectors $[x'_1, x'_2, \dots, x'_n]$ and $[x''_1, x''_2, \dots, x''_n]$, such that $\forall i : x'_i \leqslant x''_i$, the following condition holds:

$$
\mathcal{D}\,(\sigma, [x'_1, x'_2, \dots, x'_n]) \leqslant \mathcal{D}\,(\sigma, [x''_1, x''_2, \dots, x''_n])
$$

In case $\forall i : x''_i \leqslant x_i$, the following condition holds:

$$
\mathcal{W}\,(\sigma, [x'_1, x'_2, \dots, x'_n]) \leqslant \mathcal{W}\,(\sigma, [x''_1, x''_2, \dots, x''_n])
$$

For any $i$ and $j$, such that $i \neq j$, and for any $x'$ and $x''$, such that $x' \leqslant x''$, the following condition holds:

$$
\mathcal{S}_{i,j}\,(\sigma, x') \leqslant \mathcal{S}_{i,j}\,(\sigma, x'')
$$

### 3.2. Limitedness

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$, and any vector $[x'_1, x'_2, \dots, x'_n]$, such that $\forall i : x'_i \leqslant x_i$, the following condition holds:

$$
\mathcal{W}\,(\sigma, [x'_1, x'_2, \dots, x'_n]) \leqslant T
$$

For any $i$ and $j$, such that $i \neq j$, and for any $x$, the following condition is met:

$$
\mathcal{S}_{i,j}\,(\sigma, x) \leqslant x_j
$$

### 3.3. Exhaustibility

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$ the following condition is met:

$$
\mathcal{W}\,(\sigma, [x_1, x_2, \dots, x_n]) = T
$$

### 3.4. Optimality

In general, optimality means that when a single operation, such as deposit, withdrawal, or swap, could be replaced by a chain of smaller operation, then the finalcial result of the chain of operation for the user should not be better than the finalcial result of the single operation.  Here are some examples:

#### 3.4.1. Deposit Optimality

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$ and any two vectors $\mathbb{x}' = [x'_1, x'_2, \dots, x'_n]$ and $\mathbb{x}'' = [x''_1, x''_2, \dots, x''_n]$, the following condition is met:

$$
\mathcal{D}\,(\sigma, \mathbb{x}') + \mathcal{D}\,(\sigma', \mathbb{x}'') \leqslant \mathcal{D}\,(\sigma, \mathbb{x}' + \mathbb{x}'')
$$

Here $\sigma' = \langle [x_1, x_2, \dots, x_n] + \mathbb{x}', T + \mathcal{D}\,(\sigma, \mathbb{x}') \rangle$ is the liquidity pool state after the first deposit.

#### 3.4.2. Withdrawal Optimality

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$ and any two vectors $\mathbb{x}' = [x'_1, x'_2, \dots, x'_n]$ and $\mathbb{x}'' = [x''_1, x''_2, \dots, x''_n]$, such that $\forall i : x'_i + x''_i \leqslant x_i$ the following condition is met:

$$
\mathcal{W}\,(\sigma, \mathbb{x}') + \mathcal{W}\,(\sigma', \mathbb{x}'') \geqslant \mathcal{W}\,(\sigma, \mathbb{x}' + \mathbb{x}'')
$$

Here $\sigma' = \langle [x_1, x_2, \dots, x_n] - \mathbb{x}', T - \mathcal{W}\,(\sigma, \mathbb{x}') \rangle$ is the liquidity pool state after the first withdrawal.

#### 3.4.3. Swap Partition Optimality

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$, any $i$ and $j$, such that $i \neq j$, and any two values $x$ and $y$, the following condition is met:

$$
\mathcal{S}_{i,j}\,(\sigma, x) + \mathcal{S}_{i,j}\,(\sigma', y) \leqslant \mathcal{S}_{i,j}\,(\sigma, x + y)
$$

Here $\sigma' = \langle [x'_1, x'_2, \dots, x'_n], T \rangle$ is the liquidity pool state after the first swap:

$$
x'_k =
\begin{cases}
x_k + x,\ & \textrm{if}\ k = i \\
x_k - \mathcal{S}_{i,j}\,(\sigma, x),\ & \textrm{if}\ k = j \\
x_k,\ & \textrm{otherwise}
\end{cases}
$$

#### 3.4.4. Swap Chain Optimality

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$, any $i$, $j$, and $k$, such that $i \neq j$, $i \neq k$, and $j \neq k$, and any value $x$, the following condition is met:

$$
\mathcal{S}_{j,k}\,(\sigma', \mathcal{S}_{i,j}\,(\sigma, x)) \leqslant \mathcal{S}_{i,k}\,(\sigma, x)
$$

Here $\sigma' = \langle [x'_1, x'_2, \dots, x'_n], T$ is the liquidity pool state after the first swap:

$$
x'_l =
\begin{cases}
x_l + x,\ & \textrm{if}\ l = i \\
x_l - \mathcal{S}_{i,j}\,(\sigma, x),\ & \textrm{if}\ l = j \\
x_l,\ & \textrm{otherwise}
\end{cases}
$$

#### 3.4.4. Swap Decomposition Optimality

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$, any $i$ and $j$, such that $i \neq j$, and any value $x$, the following condition is met:

$$
\mathcal{D}\,(\sigma, [\underbrace{0, \dots, 0}_{i-1}, x, \underbrace{0, \dots, 0}_{n - i}]) \leqslant \mathcal{W}\,(\sigma', [\underbrace{0, \dots, 0}_{i-1}, \mathcal{S}_{i,j}\,(\sigma, x), \underbrace{0, \dots, 0}_{n-i}])
$$

Here

$$
\sigma' = \langle [x_1, \dots, x_{i-1}, x_i + x, x_{i+1}, \dots, x_n], T + \mathcal{D}\,(\sigma, [\underbrace{0, \dots, 0}_{i-1}, x, \underbrace{0, \dots, 0}_{n - i}]) \rangle
$$

is the liquidity pool state after the deposit.

### 3.5. Losslessness

In general, losslessness means, that a chain of operations that returns the user into initial state may not have positive financial result for the user. Here are some examples:

#### 3.5.1. Deposit-Widthdraw Losslessness

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$ and any vector $\mathbb{x}' = [x'_1, x'_2, \dots, x'_n]$, the following condition holds:

$$
\mathcal{W}\,(\sigma', \mathbb{x}') \leqslant \mathcal{D}\,(\sigma, \mathbb{x}')
$$

Here $\sigma' = \langle [x_1, x_2, \dots, x_n] + \mathbb{x}', T + \mathcal{D}\,(\sigma, \mathbb{x}') \rangle$ is the liquidity pool state after the deposit.

#### 3.5.2. Swap Losslessness

For any liquidity pool state $\sigma = \langle [x_1, x_2, \dots, x_n], T \rangle$, any $i$ and $j$, such that $i \neq j$, and any value $x$, the following condition holds:

$$
\mathcal{S}_{j,i}\,(\sigma', \mathcal{S}_{i,j}\,(\sigma, x)) \leqslant x
$$

Here $\sigma' = \langle [x'_1, x'_2, \dots, x'_n], T \rangle$ is the liquidity pool state after the first swap:

$$
x'_k =
\begin{cases}
x_k + x,\ & \textrm{if}\ k = i \\
x_k - \mathcal{S}_{i,j}\,(\sigma, x),\ & \textrm{if}\ k = j \\
x_k,\ & \textrm{otherwise}
\end{cases}
$$




### 4.4.4. Invariant

If we put $\lambda$ fee aside, then all operations preserve the following invariant:

$$
c \cdot T = \sum_i x_i - \Psi ([x_1, x_2, \dots, x_n]) = G( \mathbf{x})-\Psi( \mathbf{x}).
$$

Indeed, for the **deposit** we have

\begin{multline}
c \cdot T_{new} = \\
c \cdot T + c \cdot \mathcal{D}\,\left(\langle\mathbf{x}, T\rangle, \mathbf{x}'\right) = \\
c \cdot T + c  \cdot \frac{T \left(G(\mathbf{x}') -
\rho\,\left(
  \Psi(\mathbf{x} + \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right)}{G(\mathbf{x})-\Psi( \mathbf{x})} = \\
с \cdot T\left(1+\frac{G(\mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}') + \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right) = \\
(G(\mathbf{x})-\Psi(\mathbf{x})) \left(1+\frac{G(\mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}') +
  \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right) = \\
G( \mathbf{x})-\Psi( \mathbf{x})+G( \mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}')+\Psi( \mathbf{x}) = \\
G(\mathbf{x} + \mathbf{x}')-\Psi(\mathbf{x} + \mathbf{x}')
\end{multline}

The same holds for **withdrawal**:
\begin{multline}
c \cdot T_{new} = \\
c \cdot T- c \cdot \mathcal{W}\left(\langle\mathbf{x}, T\rangle, \mathbf{x}'\right) = \\
c \cdot T - c \cdot \frac{T \left(G(\mathbf{x}') +
\rho\,\left(
  \Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right)}{G( \mathbf{x})-\Psi( \mathbf{x})} = \\
с \cdot T\left(1-\frac{G(\mathbf{x}')+\Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right) = \\
(G(\mathbf{x})-\Psi(\mathbf{x}))
  \left(1-\frac{G(\mathbf{x}')+\Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})}{G( \mathbf{x})-\Psi( \mathbf{x})}\right)= \\
G( \mathbf{x})-\Psi( \mathbf{x})-G( \mathbf{x}')-\Psi(\mathbf{x} - \mathbf{x}')+\Psi( \mathbf{x}) = \\
G(\mathbf{x} - \mathbf{x}')-\Psi(\mathbf{x} - \mathbf{x}').
\end{multline}

For the $\lambda$-enhanced versions we have the following inequations:
\begin{align}
\mathcal{D}_{\lambda\leq 1}(\langle\mathbf{x}, T\rangle, \mathbf{x}') \leq \mathcal{D}_{\lambda=1}(\langle\mathbf{x}, T\rangle, \mathbf{x}');\\
\mathcal{W}_{\lambda\leq 1}(\langle\mathbf{x}, T\rangle, \mathbf{x}') \geq \mathcal{W}_{\lambda=1}(\langle\mathbf{x}, T\rangle, \mathbf{x}').
\end{align}
Therefore $\lambda<1$ mints fewer shells at deposit and burns more at withdrawal, effectively increasing the shell price.

### 4.4.5 Other Properties
It is easy to see that this model is
* Monotonous as long as  $\nabla (G-\Psi)$ is an all-positive vector, i.e. all partial derivatives   of  $G(\mathbf{x})- \Psi(\mathbf{x})$ are positive.
* Limited as long as
$$
\left(G(\mathbf{x}') +
\rho\,\left(
  \Psi(\mathbf{x} - \mathbf{x}') -
  \Psi(\mathbf{x})
\right)\right) \leq G( \mathbf{x})-\Psi( \mathbf{x}),
$$
which for  $\rho>0$ is equivalent to  $G(\mathbf{x}-\mathbf{x}')\geq \Psi(\mathbf{x}-\mathbf{x}')$. However, for  $\rho<0$ the property may not hold.
* For $\lambda<1$ not exhaustible.
* Deposit- and withdrawal-optimal for trades that increase imbalance (i.e. where $\rho$ is positive).

### 4.4.6 Swap Properties

**Swap partition optimality** property holds as equation as long as $\nabla (G-\Psi)>0$:
$$
\mathcal{S}_{i,j}\,(\sigma, x) + \mathcal{S}_{i,j}\,(\sigma', y) = \mathcal{S}_{i,j}\,(\sigma, x + y)
$$
Indeed, if after first two operations we got $z$ coins, but after the third one we get $z'\neq z$, then this means that there exist two states $\sigma_1,\sigma_2$ differing by $z-z'$ coins of a single asset only, which have the same supply. However, this contradicts the condition that the gradient is an all-positive vector:
$$
T(\sigma_2)-T(\sigma_1) = \partial(G-\Psi)/\partial x_j (z')(z-z')\neq 0.
$$


**Swap chain optimality** property also holds as equation under the same condition:
$$
\mathcal{S}_{j,k}\,(\sigma', \mathcal{S}_{i,j}\,(\sigma, x))= \mathcal{S}_{i,k}\,(\sigma, x)
$$
Again, if the two sequences of operations lead to the same supply but the state differs by a single asset, this would contradict the gradient positivity.

**Swap decomposition optimality** and **swap losslessness** holds as equations by the swap definition.

### 4.4.7. Iterative Swap Calculation

As shown in the previous sections, the value $y$ of swap function

$$
\mathcal{S}_{i,j}\,(\langle [x_1, x_2, \dots, x_n], T \rangle, x) = y
$$

Could be found by solving the following equation:

$$
y = x - \rho\,\left(
  \Psi([x'_1, x'_2, \dots, x'_n]) - \Psi([x_1, x_2, \dots, x_n])
\right)
$$

where

$$
x'_k =
\begin{cases}
x_k + x,\ & \textrm{if}\ k = i \\
x_k - y,\ & \textrm{if}\ k = j \\
x_k,\ & \textrm{otherwise}
\end{cases}
$$

So the right part of the equation if a function of $y$:

$$
f(y) = x - \rho\,\left(
  \Psi([x'_1, x'_2, \dots, x'_n]) - \Psi([x_1, x_2, \dots, x_n])
\right)
$$

and the equation itself could be rewritten as:

$$
y = f(y)
$$

Cowri solves this equation iteratively like this:

$$
\begin{array}{l}
y_0 = x\\
y_k = f(y_{k-1})
\end{array}
$$

In case $\forall z \in [x; y] \cup [y; x] : \left| \frac{\mathrm{d} f(z)}{\mathrm{d} z} \right| \leqslant \varepsilon$, then $|y_k - y| \leqslant \varepsilon |y_{k-1} - y|$, so $|y_k - y| \leqslant \varepsilon^k |x - y|$.  Thus, in order to guarantee, that iterative method to converges, we need $\varepsilon < 1$.

$$
\left|\frac{\mathrm{d} f(z)}{\mathrm{d} z} \right| =\\
\left|\frac{\mathrm{d} \left(x - \rho\,\left(
  \Psi([x'_1, x'_2, \dots, x'_n]) - \Psi([x_1, x_2, \dots, x_n])\right)\right)}{\mathrm{d} z} \right| \leqslant\\
\left|\frac{\mathrm{d}
  \Psi([x'_1, x'_2, \dots, x'_n])}{\mathrm{d} z} \right| =\\
\left|\frac{\partial
\Psi([x'_1, x'_2, \dots, x'_n])}{\partial x'_j} \right| =\\
\left|\frac{\partial\Delta\cdot G \sum_k w_k \Psi_k\!\left(\left|\frac{x_k}{w_k G} - 1\right|\right)}{\partial x'_j} \right| =\\
\Delta\cdot\left|\frac{\partial\,G}{\partial x'_j} \cdot \sum_k w_k \Psi_k\!\left(\left|\frac{x_k}{w_k G} - 1\right|\right) + G \cdot \frac{\partial \sum_k w_k \Psi_k\!\left(\left|\frac{x_k}{w_k G} - 1\right|\right)}{\partial x'_j}\right| =\\
\Delta\cdot\left|\sum_k \left( w_k \Psi_k\!\left(\left|\frac{x_k}{w_k G} - 1\right|\right) + G w_k \frac{\partial \, \Psi_k\!\left(\left|\frac{x_k}{w_k G} - 1\right|\right)}{\partial x'_j}\right)\right| =\\