# rDEX Architecture

## Target

Provide liquidity for rToken and maintain the value of rToken.

1.Completely decentralized and independent&#x20;

2.Support to provide liquidity asymmetrically, but encourage to provide liquidity symmetrically

3.Make up for LP’s impermanent losses through shared trading fees and mining incentives

## &#x20;Architecture&#x20;

The overall structure is shown in the following figure:

![](https://lh4.googleusercontent.com/RFkW6kPMmolHEhCjnhPaH-bJnqRzWbWGnUEWQZjGS0Q1sVAA2Ka8u_blTEyONt1xVECt4XJd38awuMw-ITCIFnJMz7XwXfmqBVhHMdSv18CEj_QEheeFv6U5UL8t-22DmeuStbiU)

When the user swaps tokens on rDEX, the trading fee based on slippage will be charged. If the slippage is too high, the corresponding trading fee will also increase hugely, limiting some large transactions and also compensating the liquidity provider.

## Mining Module&#x20;

The module mainly provides mining program incentives for Liquidity Provider:&#x20;

1.Deposit Function: Liquidity provider stakes the LP token to participate in mining program

2.Withdraw Function: The user withdraws LP token and stops mining, and the corresponding mining reward will be issued at this time&#x20;

3.Claim Function: Withdraw the mining rewards

## Lpbalance Module&#x20;

This module deals with the functions like minting, burning and transferring LP tokens, etc.

## Formula Derivation&#x20;

Product formula in Uniswap:    &#x20;

`X*Y=K`

When the number of inputs is `x` and the number of outputs is `y`:

`(X+x)*(Y-y)=K`

`X*Y-X*y+x*Y-x*y=X*Y`

`X*y+x*y=x*Y`

`y=(x*Y)/(X+x)`

If the exchange is made at the exchange price, the output is `y'`:&#x20;

`y'/Y = x/X`&#x20;

`y'=(x*Y)/X`

We can further get slippage slip:&#x20;

&#x20;`slip=(y'-y)/y'=1-y/y'=1-X/(X+x)=x/(x+X)`

We can see that when `x` is closer to `X`, the slippage is greater.

The trading fee rate in Uniswap is fixed at 0.3%, and rDEX will use a slippage based fee model instead.

If we take slip as the transaction fee rate here:&#x20;

`fee=slip*y=(x/(x+X))*(x*Y/(X+x))=(x^2*Y)/(X+x)^2`

The output `y''` in the hands of the end user after deducting the handling fee:&#x20;

`y'=y-fee=(x*Y)/(X+x)-(x^2*Y)/(X+x)^2=(x*X*Y)/(X+x)^2`

Current price: `y1=(x*X1*Y1)/(X1+x)^2`&#x20;

Pool after exchange: `X2=X1+xY2=Y1-y1`&#x20;

Price after exchange: `y2=(x*X2*Y2)/(X2+x)^2`&#x20;

## Formula summary&#x20;

Calculation formulas for transaction fee, exchange amount, etc.:

`Swap Fee=(x^2*Y)/(x+X)^2`

`Swap Result=(x*X*Y)/(x+X)^2`

`x`=Sent Asset Amount&#x20;

`X`=Sent Asset Pool Balance&#x20;

`Y`=Received Asset Pool Balance

Liquidity provider liquidity pool share calculation formula:

`slipAdjustment=(1-ABS((F*r-f*R)/((f+F)(r+R))))`

`units=((P*(r*F+R*f))/(2*R*F))*slipAdjustment`

`f`=Native amount added&#x20;

`r`=rToken amount added&#x20;

`F`=Native Balance (before)&#x20;

`R`=rToken Balance (before)&#x20;

`P`=existing Pool Units&#x20;

## rDEX-bot Service&#x20;

It is an independent service whose main responsibility is to maintain the balance between the rToken trading price on rDEX and the rToken exchange rate on the StaFi chai&#x6E;**.**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rdex.finance/welcome-to-rdex/rdex-the-amm-dex-for-rtokens/rdex-architecture.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
