code.vegaprotocol.io/vega@v0.79.0/core/execution/common/liquidity_provision_snapshot.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 common 17 18 import ( 19 "sort" 20 21 "code.vegaprotocol.io/vega/core/fee" 22 "code.vegaprotocol.io/vega/core/liquidity/v2" 23 "code.vegaprotocol.io/vega/libs/num" 24 "code.vegaprotocol.io/vega/logging" 25 snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1" 26 ) 27 28 func NewMarketLiquidityFromSnapshot( 29 log *logging.Logger, 30 liquidityEngine LiquidityEngine, 31 collateral Collateral, 32 broker Broker, 33 orderBook liquidity.OrderBook, 34 equityShares EquityLikeShares, 35 marketActivityTracker *MarketActivityTracker, 36 fee *fee.Engine, 37 marketType marketType, 38 marketID string, 39 asset string, 40 priceFactor num.Decimal, 41 state *snapshot.MarketLiquidity, 42 amm AMM, 43 ) (*MarketLiquidity, error) { 44 priceRange, _ := num.DecimalFromString(state.PriceRange) 45 46 ml := &MarketLiquidity{ 47 log: log, 48 liquidityEngine: liquidityEngine, 49 collateral: collateral, 50 broker: broker, 51 orderBook: orderBook, 52 equityShares: equityShares, 53 marketActivityTracker: marketActivityTracker, 54 fee: fee, 55 marketType: marketType, 56 marketID: marketID, 57 asset: asset, 58 priceFactor: priceFactor, 59 priceRange: priceRange, 60 amm: amm, 61 tick: state.Tick, 62 } 63 64 ml.ammStats = make(map[string]*AMMState, len(state.Amm)) 65 for _, val := range state.Amm { 66 stake, err := num.DecimalFromString(val.Stake) 67 if err != nil { 68 return nil, err 69 } 70 score, err := num.DecimalFromString(val.Score) 71 if err != nil { 72 return nil, err 73 } 74 ml.ammStats[val.Party] = &AMMState{ 75 stake: stake, 76 score: score, 77 lastTick: val.Tick, 78 ltD: num.DecimalFromInt64(val.Tick), 79 } 80 } 81 82 return ml, nil 83 } 84 85 func (m *MarketLiquidity) GetState() *snapshot.MarketLiquidity { 86 state := &snapshot.MarketLiquidity{ 87 PriceRange: m.priceRange.String(), 88 Tick: m.tick, 89 Amm: make([]*snapshot.AMMValues, 0, len(m.ammStats)), 90 } 91 for id, vals := range m.ammStats { 92 v := &snapshot.AMMValues{ 93 Party: id, 94 Stake: vals.stake.String(), 95 Score: vals.score.String(), 96 Tick: vals.lastTick, 97 } 98 state.Amm = append(state.Amm, v) 99 } 100 sort.SliceStable(state.Amm, func(i, j int) bool { 101 return state.Amm[i].Party < state.Amm[j].Party 102 }) 103 return state 104 } 105 106 func (m *MarketLiquidity) SetState(ml *snapshot.MarketLiquidity) error { 107 if ml == nil { 108 return nil 109 } 110 111 return nil 112 }