# Replicating Solver

{% hint style="info" %}
**Solver Monorepo**: [https://github.com/haiko-xyz/solver](https://github.com/haiko-xyz/solver)
{% endhint %}

The Replicating Solver is the first [Solver]() released by the Haiko team. It is designed for maximal gas efficiency, return market-neutral yield to depositors, and offer principal protection against market volatility using dynamic quoting and impermanent loss caps.

It is based on the original [Replicating Strategy](../strategies-v1/replicating-strategy), improved for our new [Solver]() architecture.

## Architecture

The Replicating Solver runs a simple market making strategy for each market by placing two positions (bid and ask) positions around the imputed oracle price, plus a dynamically adjusted spread to collect fees and optimise for portfolio skew / execution quality.

Quotes and liquidity positions are calculated on the fly whenever a swap is conducted, meaning positions are never stored in state. This allows Solver markets to be constantly rebalanced at zero cost to both swapper and LP.

Like the core AMM and prior Strategies, the Replicating Solver is built with a singleton design. This means all markets can be served from a single smart contract.

## Market parameters

Each market has a set of configurable parameters that can be set by the **admin / owner** or by **pool governance** amongst pool depositors, if this feature is enabled.&#x20;

The parameters are as follows, and are mostly denominated in `limits` (1/10 basis point).

<table><thead><tr><th width="155">Parameter</th><th width="294">Description</th><th>Example</th></tr></thead><tbody><tr><td><code>min_spread</code></td><td>Spread applied to oracle price to calculate bid and ask quotes, before other adjustments</td><td><p><code>min_spread = 50</code><br></p><p>Bids start at oracle_p - 0.5%<br>Asks start at oracle_p + 0.5%</p></td></tr><tr><td><code>range</code></td><td>The range of the virtual liquidity position used to calculate swap amounts, related to its slippage</td><td><p><code>range = 5000</code></p><p><code>min_spread = 50</code><br><br>Bid: </p><p>[oracle_p - 5.5%, oracle_p - 0.5%]<br>Ask: </p><p>[oracle_p + 0.5%, oracle_p + 5.5%]</p></td></tr><tr><td><code>max_skew</code></td><td>The maximum portfolio skew of the pool, above which swaps are rejected if they worsen the skew</td><td><p><code>base_assets = 2000</code><br><code>quote_assets = 500</code><br><code>max_skew = 5000</code> (50%)<br><br>Skew = (2000 - 500) / (2000 + 500) = 60% > 50%<br></p><p>Buys are allowed, sells are rejected</p></td></tr><tr><td><code>max_delta</code></td><td>The amount by which bid and ask quotes are skewed to incentivise an improvement in inventory skew</td><td><p><code>max_delta = 500</code></p><p><code>skew = 50%</code><br><br>Delta = 50% * 500 = 250<br>Bid = Bid - 2.5% (incentivise buys)<br>Ask = Ask - 2.5% (discourage sells)</p></td></tr></tbody></table>

The `max_skew` parameter can be used to set **Impermanent Loss Caps**, rejecting swaps that bring the pool above its maximum allowed portfolio skew.

## Public and Private Vaults

Vaults are public by default, meaning they are open to third party depositors on a permissionless basis. Vault ownership is tracked using an ERC20 token, which is also used to enable **pool-level governance** of market parameters amongst pool depositors.

A second vault type, Private Vaults, are closed to third party depositors while offering greater flexibility over deposits and withdrawals, as well as other admin functions. Private Vaults and perfect for use cases such as protocol-owned or liquidity bootstrapping pools.

They can also be used to permissionlessly issue assets on Starknet using just an oracle price feed in the absence of other arbitrage venues.

## Rebalancing P\&L

AMM liquidity positions are subject to Impermanent Loss (IL). This refers to the phenomenon whereby price changes cause the position to be increasingly weighted toward the asset that is falling in value.

With the Replicating Solver, Impermanent Loss is replaced by a Rebalancing P\&L:

* If rebalancing P\&L is outweighed by losses from IL, a net rebalancing loss is reported
* If rebalancing profits outperform losses from IL, a net rebalancing gain is reported

Either way, constant rebalancing typically improves IL outcomes as compared to regular positions.

## Safeguards

The Solver's execution is highly dependent on the quality of the oracle price feed, which it uses to generate swap quotes.&#x20;

The oracle price feed is provided by [Pragma](https://docs.pragmaoracle.com/), a Starkware-backed company that provides oracle infrastructure for Starknet. In addition, we introduce safeguards by internalising a number of safeguards within the Solver logic itself.&#x20;

In particular, Solver markets will be paused, preventing swaps and new deposits, whenever the oracle feed deteriorates in quality, either when:

1. A stale price quote is returned (e.g. older than 10 minutes)
2. A non-robust price quote is returned (e.g. it achieves consensus from <3 publishers)
