Skip to content

Commit

Permalink
feat: shared amm collateral
Browse files Browse the repository at this point in the history
  • Loading branch information
cdummett committed Oct 28, 2024
1 parent 4ac1f4b commit b9be664
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions protocol/0090-VAMM-automated_market_maker.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@ The configuration and resultant lifecycle of an automated market maker is as fol

## Sub-Account Configuration

Each main Vega key will have one associated sub account for a given market, on which an AMM may be set up. The account key should be generated through a hash of the main account key plus the ID of the market to generate a valid Vega address in a predictable manner. Outside of the AMM framework the sub-accounts are treated identically to any other account, they will have the standard associated margin/general accounts and be able to place orders if required as with any other account. The key differentiator is that no external party will have the private key to control these accounts directly. The maintenance of such an account will be performed through a few actions:
Each main Vega key will have a shared collateral sub account and up-to one individual sub account for a given market on which an AMM may be set up. The account keys should be generated through a hash of the main account key plus the ID of the market to generate a valid Vega address in a predictable manner.

An AMM which uses collateral from a shared collateral sub-account is here by called a shared AMM and an AMM which uses **collateral** from an individual sub-account is here by called an **individual** AMM.

Outside of the AMM framework the sub-accounts are treated identically to any other account, they will have the standard associated margin/general accounts and be able to place orders if required as with any other account. The key differentiator is that no external party will have the private key to control these accounts directly. The maintenance of such an account will be performed through a few actions:

- Creation: A sub-account will be funded when a user configures an AMM strategy with a set of criteria and a commitment amount. At this point in time the commitment amount will be transferred to the sub-account's general account and the AMM strategy will commence
- Cancellation: When the vAMM is cancelled the strategy specified will be followed:
- For futures, either any positions associated with the vAMM will be abandoned and given up to the network liquidation engine to close out, along with any associated required collateral, or the vAMM will be set into a mode in which it can only reduce position over time.
- Amendment: Updates the strategy or commitment for a sub-account

Additionally to support shared collateral sub-accounts users will have the additional available actions:

- Deposit: A sub-account can be deposited too with a valid transfer from the main key.
- Withdraw: A sub-account can be withdrawn from with a valid transfer from the sub-account key. Note this will require some additional core implementation to allow transferring from a sub-account.

## Interface

All AMM configurations should implement two key interfaces:
Expand All @@ -57,6 +66,7 @@ Initially there will only be one option for AMM behaviour, that of a constant-fu
leverage_at_upper_bound,
leverage_at_lower_bound,
}
shared_collateral
}
```

Expand All @@ -74,6 +84,7 @@ The concentrated liquidity market maker consists of two liquidity curves of pric
- **Upper Bound Leverage**: `leverage_at_upper_bound`
- **Lower Bound Leverage**: `leverage_at_lower_bound`
- **Spread**: an optional float strictly greater than zero which defines the range around the current fair-price in which the AMM will quote no volume.
- **Shared Collateral** a boolean which specifies whether the network should use the shared collateral sub-account or individual sub-account. When using the shared sub-account the user will have to manually add funds, when using the individual sub-account funds will automatically be deposited.

Note that the independent long and short ranges mean that at `base price` the market maker will be flat with respect to the market with a `0` position. This means that a potential market maker with some inherent exposure elsewhere (likely long in many cases as a token holder) can generate a position which is always either opposite to their position elsewhere (with a capped size), thus offsetting pre-existing exposure, or zero.

Expand Down Expand Up @@ -279,15 +290,51 @@ To calculate this, the interface will need the `starting price` $p_s$, `ending p

If the ending price falls within the spread range simply quote zero volume, otherwise...

First, calculate the implied position at `starting price` and `ending price` and return the difference.
First, calculate the implied position at `starting price` and `ending price` and let the difference be the theoretical volume quoted between two prices $V$.

For a given price $p$ calculate implied position $P_i$ with

$$
P_i = L \cdot \frac{\sqrt{p_u} - \sqrt{p}}{\sqrt{p} \cdotp \sqrt{p_u}} ,
$$

Then simply return the absolute difference between these two prices.
## Cross Market AMMs

To support creation of AMM pools which provide liquidity on multiple markets with a shared pool of collateral, quote volume may require scaling to ensure:

- an AMM has the correct leverage for a given fair price
- an AMM has a neutral position when the fair price is the base price

To achieve the above:

If requesting the volume which would increase the AMMs exposure, the AMM should scale the quoted volume by the ratio of available collateral to commitment amount.

$$
V = V_t\cdot\frac{b_m + b_g}{c}
$$

If requesting the volume which would reduce the AMMs exposure, the AMM should scale the quoted volume by the ratio of the current position against theoretical position.

Where:

- %V% is the scaled volume the AMM would quote between two prices.
- $V_t$ is the theoretical volume the AMM would quote between two prices.
- $b_m$ is the current balance in the markets margin account.
- $b_g$ is the current balance in the AMMs general account.
- $b_g$ is the current balance in the AMMs general account.

$$
V = V_t\cdot\frac{P_a}{P_t}
$$

Where:

- %V% is the scaled volume the AMM would quote between two prices.
- $V_t$ is the theoretical volume the AMM would quote between two prices.
- $P_a$ is the current position of the AMM.
- $P_t$ is the theoretical position of the AMM.

Note, the above implementation ensures after the market trades against an AMM there leverage on that market will always be correct. It does not however ensure the AMMs leverage is correct at all moments in time as the pool will not trigger rebalancing trades itself and instead requires an aggressor to initiate a trade.

## Determining Liquidity Contribution

Expand Down

0 comments on commit b9be664

Please sign in to comment.