> For the complete documentation index, see [llms.txt](https://docs.mayaprotocol.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mayaprotocol.com/mayachain-dev-docs/introduction/swapping-guide/streaming-swaps.md).

# Streaming Swaps

Streaming Swaps is a means for a swapper to get better price execution if they are patient. This ensures Capital Efficiency while still keeping with the philosophy "impatient people pay more".

There are two important parts to streaming swaps:

1. The **interval** part of the stream allows arbs enough time to rebalance intra-swap - this means the capital demands of swaps are met throughout, instead of *after*.
2. The **quantity** part of the stream allows the swapper to reduce the size of their sub-swap so each is executed with less slip (so the total swap will be executed with less slip) *without* losing capital to on-chain L1 fees.

If a swapper is willing to be patient, they can execute the swap with a better price, by allowing arbs to rebalance the pool between the streaming swaps.

Once all swaps are executed and the streaming swap is completed, the target token is sent to the user (minus outbound fees).

Streaming Swaps is similar to a Time Weighted Average Price (TWAP) trade however it is restricted to 24 hours (Mimir `STREAMINGSWAPMAXLENGTH = 14400` blocks).

### Using Streaming Swaps <a href="#using-streaming-swaps" id="using-streaming-swaps"></a>

To utilise a streaming swap, use the following within a [Memo](/mayachain-dev-docs/concepts/transaction-memos.md#swap):

Trade Target or Limit / Swap Interval / Swap Quantity.

* **Limit** or Trade Target: Uses the trade limit to set the maximum asset ratio at which a mini-swap can occur; otherwise, a refund is issued.
* **Interval**: Block separation of each swap. For example, a value of 10 means a mini-swap is performed every 10 blocks.
* **Quantity**: The number of swaps to be conducted. If set to 0, the network will determine the appropriate quantity.

Using the values Limit/10/5 would conduct five mini-swaps with a block separation of 10. Only swaps that achieve the specified asset ratio (defined by Limit) will be performed, while others will result in a refund.

On each swap attempt, the network will track how much (in funds) failed to swap and how much was successful. After all swap attempts are made (specified by "swap quantity"), the network will send out all successfully swapped value, and the remaining source asset via refund (that failed to swap for some reason, most likely due to the trade target).

If the first swap attempt fails for some reason, the entire streaming swap is refunded and no further attempts will be made. If the `swap quantity` is set to zero, the network will determine the number of swaps on its own with a focus on the lowest fees and maximize the number of trades.

### Minimum Swap Size <a href="#minimum-swap-size" id="minimum-swap-size"></a>

A min swap size is placed on the network for streaming swaps (Mimir `StreamingSwapMinBPFee = 5`Basis Points). This is the minimum slip for each individual swap within a streaming swap allowed. This also puts a cap on the number of swaps in a streaming swap. This allows the network to be more friendly to large trades, while also keeping revenues up for small or medium-sized trades.

#### Calculate Optimal Swap <a href="#calculate-optimal-swap" id="calculate-optimal-swap"></a>

The network works out the optimal streaming swap solution based on the Mimumn Swap Size and the swapAmount.

**Single Swap**: To calculate the minimum swap size for a single swap, you take 2.5 basis points (bps) of the depth of the pool. The formula is as follows:

$$MinimumSwapSize=MinBPStreamingSwap∗CacaoPoolDepth$$

Example using BTC Pool:

* BTC Cacao Depth = 20,007,476 CACAO
* StreamingSwapMinBPFee = 5 bp

MinimumSwapSize = 0.0005 \* 20,007,476 = 10,003. CACAO

**Double Swap**: When dealing with two pools of arbitrary depths and aiming for a precise 5 bps swap fee (set by `StreamingSwapMinBPFee`), you need to create a virtual pool size called `cacaoDepth` using the following formula:

$$
virtualCacaoDepth=(2∗r1∗r2)/(r1+r2)
$$

`r1` represents the cacao depth of pool1, and `r2` represents the cacao depth of pool2.

The cacao`Depth` is then used with 1.25 bps (half of 2.5 bps since there are two swaps), which gives you the minimum swap size that results in a 5 bps swap fee.

$$
MinimumSwapSize=(MinBPStreamingSwap/2)∗virtualCacaoDepth
$$

{% hint style="success" %}
The larger the difference between the pools, the more the virtual pool skews towards the smaller pool. This results in less rewards given to the larger pool, and more rewards given to the smaller pool.
{% endhint %}

Example using BTC and ETH Pool

* BTC Cacao Depth = 20,007,476 CACAO
* ETH Cacao Depth = 8,870,648 CACAO
* StreamingSwapMinBPFee = 5 bp

virtualCacaoDepth = (2\*20,007,476\*8,870,648) / (20,007,476 + 8,870,648) = 12,291,607 CACAO

MinimumSwapSize = (0.0005/4) \* 12,291,607 = 1536.45 CACAO

#### Swap Count <a href="#swap-count" id="swap-count"></a>

The number of swaps required is determined by dividing the `swap Amount` by the minimum swap size calculated in the previous step.

$$
swapCount=swapAmount/MinimumSwapSize
$$

The `swapAmount` represents the total amount to be swapped.

Example: swap 20,000 CACAO worth of BTC to ETH. (approx 0.653 BTC).

20,000 / 3,072.90 = 6.5 = 7 Swaps.

#### Comparing Price Execution <a href="#comparing-price-execution" id="comparing-price-execution"></a>

The difference between streaming swaps and non-streaming swaps can be calculated using the swap count with the following formula:

$$
difference=(swapCount−1)/swapCount
$$

The `difference`value represents the percentage of the swap fee saved compared to doing the same swap with a regular fee structure. There higher the swapCount, the bigger the difference.

Example:

* (7-1)/7 = 6/7 = 85% better price execution by being patient.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.mayaprotocol.com/mayachain-dev-docs/introduction/swapping-guide/streaming-swaps.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.
