From 7ab741865f4dc7eaec5d24df851ff1555eb6e93f Mon Sep 17 00:00:00 2001 From: ze97286 Date: Wed, 31 Jul 2024 17:47:32 +0100 Subject: [PATCH] fix: order spam check for amends fixed --- CHANGELOG.md | 2 +- core/execution/future/market.go | 77 ++++++++++++++------------------- core/execution/spot/market.go | 67 +++++++++++++--------------- 3 files changed, 63 insertions(+), 83 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c7ee588a1..5ffc83fb51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,7 @@ ### 🐛 Fixes - [11513](https://github.com/vegaprotocol/vega/issues/11513) - Rollback CometBFT to version `v0.38.8`. - +- [11516](https://github.com/vegaprotocol/vega/issues/11516) - Fix order spam check for amends. ## 0.77.4 diff --git a/core/execution/future/market.go b/core/execution/future/market.go index 663b89b106..c487c71a53 100644 --- a/core/execution/future/market.go +++ b/core/execution/future/market.go @@ -5281,81 +5281,68 @@ func (m *Market) emitPartyMarginModeUpdated(ctx context.Context, party string, m m.broker.Send(events.NewPartyMarginModeUpdatedEvent(ctx, e)) } -func (m *Market) checkOrderAmendForSpam(order *types.Order) error { - rf := num.DecimalOne() - - factor := m.mkt.LinearSlippageFactor - if m.risk.IsRiskFactorInitialised() { - if order.Side == types.SideBuy { - rf = m.risk.GetRiskFactors().Long - } else { - rf = m.risk.GetRiskFactors().Short - } - } - var price *num.Uint - if order.PeggedOrder == nil { - price, _ = num.UintFromDecimal(order.Price.ToDecimal().Mul(m.priceFactor)) - } else { - priceInMarket, _ := num.UintFromDecimal(m.getCurrentMarkPrice().ToDecimal().Div(m.priceFactor)) - if order.Side == types.SideBuy { - priceInMarket.AddSum(order.PeggedOrder.Offset) - } else { - priceInMarket = priceInMarket.Sub(priceInMarket, order.PeggedOrder.Offset) - } - price, _ = num.UintFromDecimal(priceInMarket.ToDecimal().Mul(m.priceFactor)) - } - margins := num.UintZero().Mul(price, num.NewUint(order.TrueRemaining())).ToDecimal().Div(m.positionFactor) - assetQuantum, err := m.collateral.GetAssetQuantum(m.settlementAsset) - if err != nil { - return err - } - if margins.Mul(rf.Add(factor)).Div(assetQuantum).LessThan(m.minMaintenanceMarginQuantumMultiplier.Mul(assetQuantum)) { - return fmt.Errorf("order value is less than minimum maintenance margin for spam") - } - return nil -} - -func (m *Market) CheckOrderSubmissionForSpam(orderSubmission *types.OrderSubmission, party string, quantumMultiplier num.Decimal) error { +func (m *Market) checkOrderForSpam(side types.Side, orderPrice *num.Uint, orderSize uint64, peggedOrder *types.PeggedOrder, orderType vegapb.Order_Type, quantumMultiplier num.Decimal) error { rf := num.DecimalOne() factor := m.mkt.LinearSlippageFactor if m.risk.IsRiskFactorInitialised() { - if orderSubmission.Side == types.SideBuy { + if side == types.SideBuy { rf = m.risk.GetRiskFactors().Long } else { rf = m.risk.GetRiskFactors().Short } } - var price *num.Uint - if orderSubmission.PeggedOrder != nil || orderSubmission.Type == vega.Order_TYPE_MARKET { + if peggedOrder != nil || orderType == vega.Order_TYPE_MARKET { priceInMarket, _ := num.UintFromDecimal(m.getCurrentMarkPrice().ToDecimal().Div(m.priceFactor)) offset := num.UintZero() - if orderSubmission.PeggedOrder != nil { - offset = orderSubmission.PeggedOrder.Offset + if peggedOrder != nil { + offset = peggedOrder.Offset } - if orderSubmission.Side == types.SideBuy { + if side == types.SideBuy { priceInMarket.AddSum(offset) } else { priceInMarket = priceInMarket.Sub(priceInMarket, offset) } price, _ = num.UintFromDecimal(priceInMarket.ToDecimal().Mul(m.priceFactor)) } else { - price, _ = num.UintFromDecimal(orderSubmission.Price.ToDecimal().Mul(m.priceFactor)) + price, _ = num.UintFromDecimal(orderPrice.ToDecimal().Mul(m.priceFactor)) } - margins := num.UintZero().Mul(price, num.NewUint(orderSubmission.Size)).ToDecimal().Div(m.positionFactor) + margins := num.UintZero().Mul(price, num.NewUint(orderSize)).ToDecimal().Div(m.positionFactor) assetQuantum, err := m.collateral.GetAssetQuantum(m.settlementAsset) if err != nil { return err } - if margins.Mul(rf.Add(factor)).LessThan(quantumMultiplier.Mul(assetQuantum)) { - return fmt.Errorf("order value is less than minimum maintenance margin for spam") + value := margins.Mul(rf.Add(factor)) + required := quantumMultiplier.Mul(assetQuantum) + if value.LessThan(required) { + return fmt.Errorf(fmt.Sprintf("order value (%s) is less than minimum maintenance margin for spam (%s)", value.String(), required.String())) } return nil } +func (m *Market) checkOrderAmendForSpam(order *types.Order) error { + return m.checkOrderForSpam( + order.Side, + order.Price, + order.Size, + order.PeggedOrder, + order.Type, + m.minMaintenanceMarginQuantumMultiplier) +} + +func (m *Market) CheckOrderSubmissionForSpam(orderSubmission *types.OrderSubmission, party string, quantumMultiplier num.Decimal) error { + return m.checkOrderForSpam( + orderSubmission.Side, + orderSubmission.Price, + orderSubmission.Size, + orderSubmission.PeggedOrder, + orderSubmission.Type, + quantumMultiplier) +} + func (m *Market) GetFillPrice(volume uint64, side types.Side) (*num.Uint, error) { return m.matching.GetFillPrice(volume, side) } diff --git a/core/execution/spot/market.go b/core/execution/spot/market.go index 8fdcb2a820..a2e40ab43d 100644 --- a/core/execution/spot/market.go +++ b/core/execution/spot/market.go @@ -3323,62 +3323,55 @@ type IDGen interface { NextID() string } -func (m *Market) checkOrderAmendForSpam(order *types.Order) error { - assetQuantum, err := m.collateral.GetAssetQuantum(m.quoteAsset) - if err != nil { - return err - } - - var price *num.Uint - if order.PeggedOrder == nil { - price, _ = num.UintFromDecimal(order.Price.ToDecimal().Mul(m.priceFactor)) - } else { - priceInMarket, _ := num.UintFromDecimal(m.getCurrentMarkPrice().ToDecimal().Div(m.priceFactor)) - if order.Side == types.SideBuy { - priceInMarket.AddSum(order.PeggedOrder.Offset) - } else { - priceInMarket = priceInMarket.Sub(priceInMarket, order.PeggedOrder.Offset) - } - price, _ = num.UintFromDecimal(priceInMarket.ToDecimal().Mul(m.priceFactor)) - } - - minQuantum := assetQuantum.Mul(m.minHoldingQuantumMultiplier) - value := num.UintZero().Mul(num.NewUint(order.Size), price).ToDecimal() - value = value.Div(m.positionFactor).Div(assetQuantum) - if value.LessThan(minQuantum.Mul(assetQuantum)) { - return fmt.Errorf("order value is less than minimum holding requirement for spam") - } - return nil -} - -func (m *Market) CheckOrderSubmissionForSpam(orderSubmission *types.OrderSubmission, party string, quantumMultiplier num.Decimal) error { +func (m *Market) checkOrderForSpam(side types.Side, orderPrice *num.Uint, orderSize uint64, peggedOrder *types.PeggedOrder, orderType vega.Order_Type, quantumMultiplier num.Decimal) error { assetQuantum, err := m.collateral.GetAssetQuantum(m.quoteAsset) if err != nil { return err } var price *num.Uint - if orderSubmission.PeggedOrder != nil || orderSubmission.Type == vega.Order_TYPE_MARKET { + if peggedOrder != nil || orderType == vega.Order_TYPE_MARKET { priceInMarket, _ := num.UintFromDecimal(m.getCurrentMarkPrice().ToDecimal().Div(m.priceFactor)) offset := num.UintZero() - if orderSubmission.PeggedOrder != nil { - offset = orderSubmission.PeggedOrder.Offset + if peggedOrder != nil { + offset = peggedOrder.Offset } - if orderSubmission.Side == types.SideBuy { + if side == types.SideBuy { priceInMarket.AddSum(offset) } else { priceInMarket = priceInMarket.Sub(priceInMarket, offset) } price, _ = num.UintFromDecimal(priceInMarket.ToDecimal().Mul(m.priceFactor)) } else { - price, _ = num.UintFromDecimal(orderSubmission.Price.ToDecimal().Mul(m.priceFactor)) + price, _ = num.UintFromDecimal(orderPrice.ToDecimal().Mul(m.priceFactor)) } minQuantum := assetQuantum.Mul(quantumMultiplier) - value := num.UintZero().Mul(num.NewUint(orderSubmission.Size), price).ToDecimal() + value := num.UintZero().Mul(num.NewUint(orderSize), price).ToDecimal() value = value.Div(m.positionFactor) - if value.LessThan(minQuantum.Mul(assetQuantum)) { - return fmt.Errorf("order value is less than minimum holding requirement for spam") + required := minQuantum.Mul(assetQuantum) + if value.LessThan(required) { + return fmt.Errorf(fmt.Sprintf("order value (%s) is less than minimum holding requirement for spam (%s)", value.String(), required.String())) } return nil } + +func (m *Market) checkOrderAmendForSpam(order *types.Order) error { + return m.checkOrderForSpam( + order.Side, + order.Price, + order.Size, + order.PeggedOrder, + order.Type, + m.minHoldingQuantumMultiplier) +} + +func (m *Market) CheckOrderSubmissionForSpam(orderSubmission *types.OrderSubmission, party string, quantumMultiplier num.Decimal) error { + return m.checkOrderForSpam( + orderSubmission.Side, + orderSubmission.Price, + orderSubmission.Size, + orderSubmission.PeggedOrder, + orderSubmission.Type, + quantumMultiplier) +}