---
# System prepended metadata

title: Math

---

# Math

## Low level

The following functions operate on a matrix of values based on the parameters of the system. When assuming the three functions below are continuous, any function built on top of it should also be continious.


### $inj(\sigma, i, v)$

Write value $v$ to index $i$ in space $\sigma$

### $fetch(\sigma, i)$

Read value at index $i$ in space $\sigma$

### $haul(\sigma, i, j, f(i))$

Subtract the output of $f(i)$ from the value at index $i$, and add the same output to the value at index $i$ in space $\sigma$ 

Move output value of $f(i)$, also written as $f(fetch(\sigma, i))$ 

## Helper functions

We assume that the first row of the matrix describes the assets of the ***sub***pools (**not main pool**), the second row describes the liabilities of the subpool and the third row describes the liquidity shares (if we add this to the matrix).
We assume that the first element of the mu vector is the main asset and the second element of the mu vector is the liabilities of the main pool.

Add or subtract value v at index i, while maintaining sigma space properties:
$safe\_add: \Sigma \times \mathbb{N} \times \mathbb{R} \rightarrow \Sigma, \\safe\_add(\sigma, i, v) = inj(\sigma, i, \alpha \cdot fet(\sigma, i))$, where $\alpha = \text{max}\{0, \frac{fet(\sigma, i)}{v} + 1 \}$
Note that if for example you try to remove 150 from a position with value 100, the new value will be 0, not -50!
\
Find index of (n,m) position in state matrix to use for functions with single index paramater:
$ind\_mat: \Sigma_{l,h} \times \mathbb{N} \times \bar h \rightarrow \mathbb{N}\\
ind\_mat(\sigma, n, m) = l + h \cdot (n-1) + m$
For convenience, we can also write $ind\_mat(\sigma, n, m) = \sigma^{(n,m)}$

Fetch element at index (i, j) in matrix. For example, if subpool 2 has an asset of 100, then $mat\_fet(\sigma,1,2) = 100$
$mat\_fet: \Sigma_{l, h} \times \mathbb{N}^2 \rightarrow \mathbb{R}^{+0}$
$mat\_fet(\sigma_, n, m) = fet(\sigma, \sigma^{n,m})$
For conveniene, we can also write $mat\_fet(\sigma, n, m) = \sigma_{(n,m)}$


Return the sum of row $n$ in the matrix:
$sum\_row: \Sigma_{l,h} \times \bar{h} \rightarrow \mathbb{R}^{+0}$ 
$sum\_row(\sigma, n) = \sum_{i=1}^\infty \sigma_{(n,i)}$

Add $v$ amount of assets to subpool $n$:
$add\_asset: \Sigma_{l,h} \times \mathbb{N} \times \mathbb{R} \rightarrow \Sigma_{l,h}$
$add\_asset(\sigma, n, v) = save\_add(\sigma, \sigma^{(1,n)}, v)$

Add $v$ amounts of liabilities to subpool $n$:
$add\_liabilities: \Sigma_{l,h} \times \mathbb{N} \times \mathbb{R} \rightarrow \Sigma_{l,h}$
$add\_liabilities(\sigma, n, v) = save\_add(\sigma, \sigma^{(2,n)}, v)$

Add $v$ amounts of asset to main pool:
$add\_main\_asset: \Sigma_{l,h}  \times \mathbb{R} \rightarrow \Sigma_{l,h}$
$add\_main\_asset(\sigma, v) = safe\_add(\sigma,  1, v)$

Add $v$ amounts of liabilities to main pool:
$add\_main\_liabilities: \Sigma_{l,h} \times \mathbb{R} \rightarrow \Sigma_{l,h}$
$add\_main\_liabilities(\sigma, v) = safe\_add(\sigma, 2, v)$


## High-level 

### Parameters

- Assets
- Liabilities
- Liquidity shares
- Fee Rate
- Hidden assets rate
> Are Fee rates static or are they calculated using the state of the system? If the latter is the case, this should be left out, as we're stuffing the matrix with redundant information.
> What are Liquidity shares?

### Operations

- Deposit
- Withdraw
- WithdrawOther
- Swap 
- Haircut

### Deposit

1. Fetch liabilities for pool, $L_i = fetch(\sigma, {L_i})$ 
2. Fetch assets for pool, $A_i = fetch(\sigma, {A_i})$ 
3. Fetch sum of liquidity shares $\sum_{i=1}^{n} l_i$
4. fetch total liabilitites, $\sum_{i=1}^{n} L_i$
5. fetch total assets, $\sum_{i=1}^{n} A_i$
>Proposal: $l_i = mat\_fet(\sigma, 3, i)$.
>Proposal: $L_i = mat\_fet(\sigma, 2, i)$
>Proposal: $A_i = mat\_fet(\sigma, 1, i)$

For input amount $a$ 

6. Update assets with amount $inj(\sigma, {A_i}, A_i + a)$
7. Update liabilities with amount $inj(\sigma, {L_i}, L_i + a)$
8. Update liquidity shares for user $inj(\sigma, l_i, f(l_i))$
where $f(a, \sigma) = a * \frac{l_i}{L_i} * \frac{\sum_{i=1}^{n} A_i}{\sum_{i=1}^{n} L_i}$


> if $A_i < -a$, then this is a violation. Proposal, for $inj(\sigma, x, y)$, use $safe\_add$
 

### Withdraw

1. Fetch liabilities for pool, $L_i = fetch(\sigma, {L_i})$ (Proposal: $L_i = mat\_fet(\sigma, 2, i)$)
2. fetch assets for pool, $A_i = fetch(\sigma, {A_i})$ (Proposal: $A_i = mat\_fet(\sigma, 1, i)$)
3. Fetch sum of liquidity shares $\sum_{i=1}^{n} l_i$
4. fetch total liabilitites, $\sum_{i=1}^{n} L_i$
5. fetch total assets, $\sum_{i=1}^{n} A_i$

When burning an amount of liquidity shares $l$ 

6. Update liquidity shares for user $l * l_i / L_i$

### WithdrawOther

### Swap

1. Fetch liabilities and assets for Token $i$
    a. $fetch(\sigma, L_i)$
    b. $fetch(\sigma, A_i)$
2. Fetch liabilities and assets for Token $j$
    a. $fetch(\sigma, L_j)$
    b. $fetch(\sigma, A_j)$
    
>[Proposal: 1. Fetch liabilities and assets for Token $i$
a. $mat\_fet(\sigma, 2, i)$
b. $mat\_fet(\sigma, 1, i)$
>[Proposal: 2. Fetch liabilities and assets for Token $j$
    a. $mat\_fet(\sigma, 2, j)$
    b. $mat\_fet(\sigma, 1, i)$

For input amount $a_{i->j}$
#### Calcuations
1. Calculate slippage for Token $i$ for amounr $a_i$ 
    - Coverage before swap $R_i = \frac{A_i}{L_i}$
    - Coverage after swap $R'_i = \frac{A_i + a_{i}}{L_i}$
    - Slippage $S_{i} = \frac{S(R_i) - S(R'_i)}{R'_i - R_i}$ 
2. Similarly calculate slippage for Token $j$ ie. $S_j$
3. Slippage = $\Delta S = S_i - S_j$
4. toAmount  $a_j = a_{i} * \Delta S$
5. TODO: Apply haricut?

Slippage functions
Given $R_i^n$ is the coverage ratio
- $S(R_i) = \frac{k}{R_i^n}$   if $R_i > T$ 
- $S(R_i) = C - R_i$  if $R_i < T$ 

#### State Chainges
1. Update cash of asset i $inj(\sigma, i_{A_i}, A_i + a_i)$
2. Update cash of asset j $inj(\sigma, i_{A_j}, A_j - a_j)$
3. Update liablities of asset j $inj(\sigma, i_{L_j}, L_t + X)$ TODO: Where X = (amount in - amount out) * Retention ratio
> 1. Use $add\_asset(\sigma, i, a_i)$
> 2. Use $add\_asset(\sigma, j, -a_j)$
> 3. Use $add\_liabilities(\sigma, j, X)$



### Rebase

## Theory 

### Continuity

What is continuity 

e.g. $y=x$ 

interval [0,1] = 'dense set' we can only create a function on a dense set.

What is a dense ? 

Consider set A 
$A = { .... }$

For epsilon > 0, there is an a in A for which thre is also a b in A for which the distance between a and b is smaller than epilon and a!=b
$for e>0: a \in A: b \in A for_which distance(a, b) < e and b!: a$


For each value x there is a value y


So how do we definine continuity ? 
d=delta

f(1-d, 1+d) = (1 -d, 1+d) 


let e>0: (1-e, 1+e)

Can we find delta for which f(1-d, 1+d) is a subset of (1-e, 1+e)

let d=e , then f(1-d, 1+d) is subset of (1-e, 1+e)


bal B(x, e) = (x-e, x+e)

f: R -> R 

f is continious 

IF  e>0 we can find a delta >0 so that f(B(x, e)) is a subset of B(f(x), e);

Haul(sigma, n , m, [0,1]) -> sigma 2

Is this continuous ? 

f(B(x, d)) is subset of B(f(x), e)

(x are parameters of the haul)



What is a metric space ? 
(Set(x), d) 

d= how do we measure (e.g. Manhatten distance)

E.g. Houses d(h1, d2) = |h1-h2|


Metric spaces, density, continuity, limits


## Math 16-1 


Appels , a
Bananen, b
Cheeries, c

f(B_a(a, delta)) is subset van B_b(f(a), epsilon)

alle appels met lengteverschil kleiner dan delta

![](https://i.imgur.com/lD9OgQe.png)


# Math 23-1

$\sigma -> \sigma_1 -> \sigma_2 -> \sigma_n$ 

$i: \Sigma -> R$ 





