# Risk Marketplace

The Risk Marketplace is a weighted constant product automated market maker (AMM) that enables decentralized token trading. It is a fork of Balancer V1, significantly modified for the specific use case of The Risk Protocol. The DEX supports two main types of swaps: standard `swaps` and `split-and-swap` operations.

## 1.  Types of Swaps <a href="#types-of-swaps" id="types-of-swaps"></a>

* **Standard Swap**: Users can trade between underlying assets and the SMART tokens within customized elastic supply pools.
* **Split and Swap**: This function allows users to deposit the underlying token and simultaneously swap one of the resulting SMART tokens for the other in a single, atomic transaction. This is particularly useful for traders who prefer to hold only one type of SMART token.

## 2.  Core Mechanism

The Risk Marketplace uses a weighted constant product invariant to determine token prices and swap outputs. Unlike traditional constant product AMMs (x × y = k), it extends the formula to support multiple tokens with customizable weights:

$$\prod\_{i=1}^{n} B\_i^{W\_i} = k$$

Where:

* $$B\_i$$ = Balance of token (i) in the pool
* $$W\_i$$ = Normalized weight of token (i)
* $$k$$ = Invariant (remains constant during swaps)

This design allows pools to hold 2-8 tokens with arbitrary weight distributions (e.g., 80/20, 60/20/20), enabling more capital-efficient exposure to specific assets.

***

#### Spot Price

The spot price between any two tokens in the pool is derived from their balance and weight ratios:

$$SP\_{i}^{o} = \frac{B\_i / W\_i}{B\_o / W\_o} \cdot \frac{1}{1 - LPFee}$$

Wher&#x65;**:**

* $$B\_i$$: Balance of token *i*, the token being sold by the trader (going into the pool).
* $$B\_o$$: Balance of token *o*, the token being bought by the trader (coming out of the pool).
* $$W\_i$$: Weight of token *i*.
* $$W\_o$$: Weight of token *o*.

***

## 3.  Swap Calculations

#### 3. a)  Output Given Input (Exact In)

When a user specifies an exact input amount, the output is calculated as:

$$
A\_o = B\_o \cdot \left(1 - \left(\frac{B\_i}{B\_i + A\_i \cdot (1 - \text{LPFee})}\right)^{\frac{W\_i}{W\_o}}\right)
$$

Where:

* $$A\_o$$: Output token amount
* $$A\_i$$: Input token amount
* $$B\_i, B\_o$$: Pool balances for tokens (i) and (o), respectively
* $$W\_i, W\_o$$: Weights of tokens (i) and (o)

#### 3. b)  Input Given Output (Exact Out)

When a user specifies an exact output amount, the required input is:

$$
A\_i = \frac{B\_i \cdot \left(\left(\frac{B\_o}{B\_o - A\_o}\right)^{\frac{W\_o}{W\_i}} - 1\right)}{1 - \text{LPFee}}
$$

Where:

* $$A\_i$$: Input token amount needed
* $$A\_o$$: Desired output token amount
* $$B\_i, B\_o$$: Pool balances for tokens (i) and (o), respectively
* $$W\_i, W\_o$$: Weights of tokens (i) and (o)
