code.vegaprotocol.io/vega@v0.79.0/core/execution/future/liquidation.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package future 17 18 import ( 19 "context" 20 "time" 21 22 "code.vegaprotocol.io/vega/core/events" 23 "code.vegaprotocol.io/vega/core/types" 24 "code.vegaprotocol.io/vega/libs/num" 25 "code.vegaprotocol.io/vega/logging" 26 ) 27 28 func (m *Market) checkNetwork(ctx context.Context, now time.Time) error { 29 if m.as.InAuction() { 30 return nil 31 } 32 // this only returns an error if we couldn't get the price range, incidating no orders on book 33 order, _ := m.liquidation.OnTick(ctx, now, m.midPrice()) 34 if order == nil { 35 return nil 36 } 37 // register the network order on the positions engine 38 _ = m.position.RegisterOrder(ctx, order) 39 order.OriginalPrice, _ = num.UintFromDecimal(order.Price.ToDecimal().Div(m.priceFactor)) 40 m.broker.Send(events.NewOrderEvent(ctx, order)) 41 conf, err := m.matching.SubmitOrder(order) 42 if err != nil { 43 // order failed to uncross, reject and done 44 return m.unregisterAndReject(ctx, order, err) 45 } 46 order.ClearUpExtraRemaining() 47 // this should not be possible (network position can't really flip) 48 if order.ReduceOnly && order.Remaining > 0 { 49 order.Status = types.OrderStatusStopped 50 } 51 52 // if the order is not staying in the book, then we remove it 53 // from the potential positions 54 if order.IsFinished() && order.Remaining > 0 { 55 _ = m.position.UnregisterOrder(ctx, order) 56 } 57 // send the event with the order in its final state 58 m.broker.Send(events.NewOrderEvent(ctx, order)) 59 60 // no trades... 61 if len(conf.Trades) == 0 { 62 return nil 63 } 64 // transfer fees to the good party -> fees are now taken from the insurance pool 65 fees, _ := m.fee.GetFeeForPositionResolution(conf.Trades, m.referralDiscountRewardService, m.volumeDiscountService, m.volumeRebateService) 66 tresps, err := m.collateral.TransferFees(ctx, m.GetID(), m.settlementAsset, fees) 67 if err != nil { 68 // we probably should reject the order, although if we end up here we have a massive problem. 69 _ = m.position.UnregisterOrder(ctx, order) 70 // if we get an eror transfer fees, we are missing accounts, and something is terribly wrong. 71 // the fees we get from the fee engine result in transfers with the minimum amount set to 0, 72 // so the only thing that could go wrong is missing accounts. 73 m.log.Panic("unable to transfer fees for positions resolution", 74 logging.Error(err), 75 logging.String("market-id", m.GetID())) 76 return err 77 } 78 if len(tresps) > 0 { 79 m.broker.Send(events.NewLedgerMovements(ctx, tresps)) 80 } 81 // Now that the fees have been taken care of, get the current last traded price: 82 lastTraded := m.getLastTradedPrice() 83 tradeType := types.TradeTypeNetworkCloseOutGood 84 // now handle the confirmation like you would any other order/trade confirmation 85 m.handleConfirmation(ctx, conf, &tradeType) 86 // restore the last traded price, the network trades do not count towards the mark price 87 // nor do they factor in to the price monitoring logic. 88 m.lastTradedPrice = lastTraded 89 // update the liquidation engine to reflect the trades have happened 90 m.liquidation.UpdateNetworkPosition(conf.Trades) 91 92 // check for reference moves again? We should've already done this 93 // This can probably be removed 94 m.checkForReferenceMoves(ctx, conf.PassiveOrdersAffected, false) 95 return nil 96 }