From fe9f4ef0a2f8d041cfb895c15c184df50590ce6f Mon Sep 17 00:00:00 2001 From: wwestgarth Date: Fri, 25 Oct 2024 09:23:25 +0100 Subject: [PATCH] fix: an AMM waiting for a data source cannot be set to reduce only --- core/execution/amm/engine.go | 6 +++ .../amm/0090-VAMM-oracle-base.feature | 43 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/core/execution/amm/engine.go b/core/execution/amm/engine.go index 22c85002ac..b2f2695fbe 100644 --- a/core/execution/amm/engine.go +++ b/core/execution/amm/engine.go @@ -45,6 +45,7 @@ var ( ErrCommitmentTooLow = errors.New("commitment amount too low") ErrRebaseOrderDidNotTrade = errors.New("rebase-order did not trade") ErrRebaseTargetOutsideBounds = errors.New("rebase target outside bounds") + ErrCannotCancelPendingAMM = errors.New("pending AMM with a position cannot be cancelled with reduce-only method") ) const ( @@ -800,6 +801,11 @@ func (e *Engine) CancelAMM( } if cancel.Method == types.AMMCancellationMethodReduceOnly { + // a pending pool has no curves until a data-source sets its base, so cannot be set in reduce-only mode + if pool.IsPending() { + return nil, ErrCannotCancelPendingAMM + } + // pool will now only accept trades that will reduce its position pool.status = types.AMMPoolStatusReduceOnly e.sendUpdate(ctx, pool) diff --git a/core/integration/features/amm/0090-VAMM-oracle-base.feature b/core/integration/features/amm/0090-VAMM-oracle-base.feature index 6cefe97a66..69bde0c989 100644 --- a/core/integration/features/amm/0090-VAMM-oracle-base.feature +++ b/core/integration/features/amm/0090-VAMM-oracle-base.feature @@ -345,4 +345,45 @@ Feature: vAMM with oracle driven base price # base price update only happened because the slippage was increased in the amend that set the new oracle value Then the AMM pool status should be: | party | market id | amount | status | base | lower bound | upper bound | - | vamm1 | ETH/MAR22 | 100000 | STATUS_ACTIVE | 115 | 80 | 120 | \ No newline at end of file + | vamm1 | ETH/MAR22 | 100000 | STATUS_ACTIVE | 115 | 80 | 120 | + + + @VAMM3 + Scenario: 0090-VAMM-043 AMM amended into pending AMM cannot be set into reduce only + Then the parties submit the following AMM: + | party | market id | amount | slippage | base | lower bound | upper bound | proposed fee | + | vamm1 | ETH/MAR22 | 100000 | 0.01 | 100 | 80 | 120 | 0.03 | + Then the AMM pool status should be: + | party | market id | amount | status | base | lower bound | upper bound | + | vamm1 | ETH/MAR22 | 100000 | STATUS_ACTIVE | 100 | 80 | 120 | + + + # give it a position + When the parties place the following orders: + | party | market id | side | volume | price | resulting trades | type | tif | reference | + | party1 | ETH/MAR22 | sell | 10 | 50 | 1 | TYPE_LIMIT | TIF_GTC | | + + # the base price is + Then the parties amend the following AMM: + | party | market id | amount | slippage | base | lower bound | upper bound | proposed fee | data source id | + | vamm1 | ETH/MAR22 | 100000 | 0.05 | 0 | 200 | 300 | 0.03 | 1234 | + Then the AMM pool status should be: + | party | market id | amount | status | base | lower bound | upper bound | + | vamm1 | ETH/MAR22 | 100000 | STATUS_PENDING | 0 | 200 | 300 | + + + + # try to cancel with reduce only + Then the parties cancel the following AMM: + | party | market id | method | error | + | vamm1 | ETH/MAR22 | METHOD_REDUCE_ONLY | pending AMM with a position cannot be cancelled with reduce-only method | + + # try to cancel with immediate + Then the parties cancel the following AMM: + | party | market id | method | + | vamm1 | ETH/MAR22 | METHOD_IMMEDIATE | + + # base price update only happened because the slippage was increased in the amend that set the new oracle value + Then the AMM pool status should be: + | party | market id | amount | status | base | lower bound | upper bound | + | vamm1 | ETH/MAR22 | 100000 | STATUS_CANCELLED | 0 | 200 | 300 | \ No newline at end of file