From 7b48127e09135d0ca86e9422a42ef458e7844d8f Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 10 Sep 2024 11:14:27 +0100 Subject: [PATCH 1/5] chore: correct spec so curve is fixed --- protocol/0090-VAMM-automated_market_maker.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocol/0090-VAMM-automated_market_maker.md b/protocol/0090-VAMM-automated_market_maker.md index d44604a8b..1128098c2 100644 --- a/protocol/0090-VAMM-automated_market_maker.md +++ b/protocol/0090-VAMM-automated_market_maker.md @@ -167,7 +167,7 @@ $$ P_{v_u} = \frac{r_f b}{p_u (1 + r_f) - r_f p_a} , $$ -where $r_f$ is the `short` factor for the upper range and the `long` factor for the lower range, `b` is the current total balance of the vAMM across all accounts, $P_{v_l}$ is the theoretical volume and the bottom of the lower bound and $P_{v_u}$ is the (absolute value of the) theoretical volume at the top of the upper bound. The final $L$ scores can then be reached with the equation +where $r_f$ is the `short` factor for the upper range and the `long` factor for the lower range, `b` is the commitment amount, $P_{v_l}$ is the theoretical volume and the bottom of the lower bound and $P_{v_u}$ is the (absolute value of the) theoretical volume at the top of the upper bound. The final $L$ scores can then be reached with the equation $$ L = P_v \cdot \frac{\sqrt{p_u} \sqrt{p_l}}{\sqrt{p_u} - \sqrt{p_l}} = P_v L_u, From ad4570195d29b018652ec91f9689baa3000395b7 Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 10 Sep 2024 11:15:08 +0100 Subject: [PATCH 2/5] feat: add best bid ask calculations --- protocol/0090-VAMM-automated_market_maker.md | 27 ++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/protocol/0090-VAMM-automated_market_maker.md b/protocol/0090-VAMM-automated_market_maker.md index 1128098c2..e3f30608a 100644 --- a/protocol/0090-VAMM-automated_market_maker.md +++ b/protocol/0090-VAMM-automated_market_maker.md @@ -235,6 +235,33 @@ $$ Then simply return the absolute difference between these two prices. +## Best bid / best ask + +As the volume provided between two ticks can theoretically be less than the smallest unit of volume supported by the market's position decimals, the best-bid and ask will not always simply be one tick greater or less than the AMMs current fair price. + +Instead the best-bid and best-ask of an AMM curve is defined as the price levels at which the AMM will quote at least one unit of volume between those prices and the current fair price. Re-arranging the formulas defined in the prior [section](#volume-between-two-prices) yields the following: + +$$ +p_{bb} = \frac{L\cdot\sqrt{p_f}}{L + \Delta{P}\cdot \sqrt{p_f}} +$$ + +$$ +p_{ba} = \frac{L\cdot\sqrt{p_f}}{L - \Delta{P}\cdot \sqrt{p_f}} +$$ + +Where: + +- $P_{bb}$ is the calculated best bid +- $P_{ba}$ is the calculated best ask +- $p_{f}$ is the current fair price as calculated [here](#fair-price) +- $L$ is the liquidity score for the current curve as calculated [here](#determining-volumes-and-prices) +- $\Delta{P}$ is the smallest possible position supported by the markets position decimals, i.e. 1 unit of volume. + +Note, there is no need to handle the complexity where the fair price is currently on the lower curve but the best ask exists on the upper curve (or visa-versa) as by definition if the party has the smallest possible position $\Delta{P}$, their fair price should be such that between that price and the base price they quote at least $\Delta{P}$. In short: + +- if the fair price is currently on the lower curve, the best-ask will also be on the lower curve +- if the fair price is currently on the upper curve, the best-bid will also be on the upper curve + ## Determining Liquidity Contribution The provided liquidity from an AMM commitment must be determined for two reasons. Firstly to decide instantaneous distribution of liquidity fees between the various liquidity types and secondly to calculate a virtual liquidity commitment amount for assigning AMM users with an ELS value. This will be used for determining the distribution of ELS-eligible fees on a market along with voting weight in market change proposals. From 705a1189525f1ba632c6803d572dd319fad53cd4 Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 10 Sep 2024 11:15:31 +0100 Subject: [PATCH 3/5] feat: allowed empty amm levels --- protocol/0001-MKTF-market_framework.md | 1 + protocol/0090-VAMM-automated_market_maker.md | 25 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/protocol/0001-MKTF-market_framework.md b/protocol/0001-MKTF-market_framework.md index 3d32d838a..ad93df93e 100644 --- a/protocol/0001-MKTF-market_framework.md +++ b/protocol/0001-MKTF-market_framework.md @@ -35,6 +35,7 @@ Data: - **Tick size**: the minimum change in quote price for the market. Order prices and offsets for pegged orders must be given as an exact multiple of the tick size. For example if the tick size is 0.02 USD. then a price of 100.02 USD is acceptable and a price of 100.03 USD is not. The tick size of a market can be updated through governance. Note, the tick size should be specified in terms of the market decimals, e.g. for a scaled tick size of `0.02` (USDT) in a market using `5` decimal places, the tick size would be set to `2000`. - **Liquidation strategy**: A field specifying the liquidation strategy for the market. Please refer to [0012-POSR-position_resolution](./0012-POSR-position_resolution.md#managing-networks-position) for supported strategies. - **Transaction Prioritisation**: A boolean, whether to enable [transaction prioritisation](./0092-TRTO-trading_transaction_ordering.md). +- **Empty AMM Price Levels**: An integer greater than or equal to zero which defines the maximum number of price levels permitted in an AMM range which would quote zero volume. This value should default to 100. Note: it is agreed that initially the integer representation of the full precision of both order and positions can be required to fit into an int64, so this means that the largest position/order size possible reduces by a factor of ten for every extra decimal place used. This also means that, for instance, it would not be possible to create a `BTCUSD` market that allows order/position sizes equivalent to 1 sat. diff --git a/protocol/0090-VAMM-automated_market_maker.md b/protocol/0090-VAMM-automated_market_maker.md index e3f30608a..8c9f3f9ea 100644 --- a/protocol/0090-VAMM-automated_market_maker.md +++ b/protocol/0090-VAMM-automated_market_maker.md @@ -262,6 +262,31 @@ Note, there is no need to handle the complexity where the fair price is currentl - if the fair price is currently on the lower curve, the best-ask will also be on the lower curve - if the fair price is currently on the upper curve, the best-bid will also be on the upper curve +## Curve validity + +To prevent the protocol evaluating AMMs providing little liquidity to the network, the network will reject AMM submissions or amendments which cause an AMMs liquidity to be spread too thinly across it's entire range. + +If the number of ticks in the largest price range in which the AMM quotes zero volume is more than the parameter $allowedEmptyAmmLevels$, then the submission or amendment will be rejected. Note this parameter is defined on each market to allow different markets with varying volatility to be configured appropriately. + +- for the lower curve, this range is between the fair price at which the AMMs long position is $P=1$ and the base price. +- for the upper curve, this range is between the fair price at which the AMMs short position is $(P_v-1)$ and the upper price bound. + +The above definitions yield the following inequality which must be satisfied for both the upper and lower curve (providing they are defined) for the AMM to be valid. + +$$ +n\cdot\Delta{p} \geq \frac{L\cdot\sqrt{p_u}}{L + \Delta{P}\cdot \sqrt{p_u}} +$$ + +Where: + +- $\Delta{p}$ is the smallest price movement supported by the market's price decimals and tick size +- $\Delta{P}$ is the smallest position size supported by the market's position decimals +- $n$ is the market parameter $allowedEmptyAmmLevels$ +- $L$ is the liquidity score for the relevant curve +- $p_u$ is the upper price (the base price for the lower curve and the upper bound for the upper curve) + +Note, if the market parameter $allowedEmptyAmmLevels$ is updated via governance all existing pools that would no longer satisfy the inequality **must not** be cancelled. Amends to these pools should be evaluated and rejected if necessary and any cancellations should be accepted. + ## Determining Liquidity Contribution The provided liquidity from an AMM commitment must be determined for two reasons. Firstly to decide instantaneous distribution of liquidity fees between the various liquidity types and secondly to calculate a virtual liquidity commitment amount for assigning AMM users with an ELS value. This will be used for determining the distribution of ELS-eligible fees on a market along with voting weight in market change proposals. From 3b477bd9223b43ed9baea9f55654275cfd53bf30 Mon Sep 17 00:00:00 2001 From: Charlie Date: Tue, 10 Sep 2024 14:51:24 +0100 Subject: [PATCH 4/5] fix: update formula and inequalities --- protocol/0090-VAMM-automated_market_maker.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/protocol/0090-VAMM-automated_market_maker.md b/protocol/0090-VAMM-automated_market_maker.md index 8c9f3f9ea..dff750741 100644 --- a/protocol/0090-VAMM-automated_market_maker.md +++ b/protocol/0090-VAMM-automated_market_maker.md @@ -242,11 +242,11 @@ As the volume provided between two ticks can theoretically be less than the smal Instead the best-bid and best-ask of an AMM curve is defined as the price levels at which the AMM will quote at least one unit of volume between those prices and the current fair price. Re-arranging the formulas defined in the prior [section](#volume-between-two-prices) yields the following: $$ -p_{bb} = \frac{L\cdot\sqrt{p_f}}{L + \Delta{P}\cdot \sqrt{p_f}} +p_{bb} = (\frac{L\cdot\sqrt{p_f}}{L + \Delta{P}\cdot \sqrt{p_f}})^2 $$ $$ -p_{ba} = \frac{L\cdot\sqrt{p_f}}{L - \Delta{P}\cdot \sqrt{p_f}} +p_{ba} = (\frac{L\cdot\sqrt{p_f}}{L - \Delta{P}\cdot \sqrt{p_f}})^2 $$ Where: @@ -268,13 +268,17 @@ To prevent the protocol evaluating AMMs providing little liquidity to the networ If the number of ticks in the largest price range in which the AMM quotes zero volume is more than the parameter $allowedEmptyAmmLevels$, then the submission or amendment will be rejected. Note this parameter is defined on each market to allow different markets with varying volatility to be configured appropriately. -- for the lower curve, this range is between the fair price at which the AMMs long position is $P=1$ and the base price. -- for the upper curve, this range is between the fair price at which the AMMs short position is $(P_v-1)$ and the upper price bound. +- for the lower curve, this range is between the base price and the best bid price when the fair price is the base price. +- for the upper curve, this range is between the upper bound and the best bid price when the fair price is the upper bound. The above definitions yield the following inequality which must be satisfied for both the upper and lower curve (providing they are defined) for the AMM to be valid. $$ -n\cdot\Delta{p} \geq \frac{L\cdot\sqrt{p_u}}{L + \Delta{P}\cdot \sqrt{p_u}} +n\cdot\Delta{p} \geq (p_u - p_{bb}) +$$ + +$$ +n\cdot\Delta{p} \geq {p_u} - (\frac{L\cdot\sqrt{p_u}}{L + \Delta{P}\cdot \sqrt{p_u}})^2 $$ Where: From ddfb9f8a11323a12cf381123e6257d35bacbdfdc Mon Sep 17 00:00:00 2001 From: Charlie <99198652+cdummett@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:01:48 +0100 Subject: [PATCH 5/5] Apply suggestions from code review feat: apply refactor feedback Co-authored-by: David Siska <62546419+davidsiska-vega@users.noreply.github.com> --- protocol/0090-VAMM-automated_market_maker.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocol/0090-VAMM-automated_market_maker.md b/protocol/0090-VAMM-automated_market_maker.md index dff750741..63267ebc0 100644 --- a/protocol/0090-VAMM-automated_market_maker.md +++ b/protocol/0090-VAMM-automated_market_maker.md @@ -242,11 +242,11 @@ As the volume provided between two ticks can theoretically be less than the smal Instead the best-bid and best-ask of an AMM curve is defined as the price levels at which the AMM will quote at least one unit of volume between those prices and the current fair price. Re-arranging the formulas defined in the prior [section](#volume-between-two-prices) yields the following: $$ -p_{bb} = (\frac{L\cdot\sqrt{p_f}}{L + \Delta{P}\cdot \sqrt{p_f}})^2 +p_{bb} = \bigg(\frac{L\cdot\sqrt{p_f}}{L + \Delta{P}\cdot \sqrt{p_f}}\bigg)^2 $$ $$ -p_{ba} = (\frac{L\cdot\sqrt{p_f}}{L - \Delta{P}\cdot \sqrt{p_f}})^2 +p_{ba} = \bigg(\frac{L\cdot\sqrt{p_f}}{L - \Delta{P}\cdot \sqrt{p_f}}\bigg)^2 $$ Where: