-
Notifications
You must be signed in to change notification settings - Fork 22
/
market_snapshot.go
375 lines (332 loc) · 14.7 KB
/
market_snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package future
import (
"context"
"fmt"
"sort"
"time"
"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/execution/common"
"code.vegaprotocol.io/vega/core/execution/liquidation"
"code.vegaprotocol.io/vega/core/execution/stoporders"
"code.vegaprotocol.io/vega/core/fee"
"code.vegaprotocol.io/vega/core/liquidity/target"
"code.vegaprotocol.io/vega/core/liquidity/v2"
"code.vegaprotocol.io/vega/core/markets"
"code.vegaprotocol.io/vega/core/matching"
"code.vegaprotocol.io/vega/core/monitor"
"code.vegaprotocol.io/vega/core/monitor/price"
"code.vegaprotocol.io/vega/core/positions"
"code.vegaprotocol.io/vega/core/products"
"code.vegaprotocol.io/vega/core/risk"
"code.vegaprotocol.io/vega/core/settlement"
"code.vegaprotocol.io/vega/core/types"
vgcontext "code.vegaprotocol.io/vega/libs/context"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/logging"
snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
"golang.org/x/exp/maps"
)
func NewMarketFromSnapshot(
ctx context.Context,
log *logging.Logger,
em *types.ExecMarket,
riskConfig risk.Config,
positionConfig positions.Config,
settlementConfig settlement.Config,
matchingConfig matching.Config,
feeConfig fee.Config,
liquidityConfig liquidity.Config,
collateralEngine common.Collateral,
oracleEngine products.OracleEngine,
timeService common.TimeService,
broker common.Broker,
stateVarEngine common.StateVarEngine,
assetDetails *assets.Asset,
marketActivityTracker *common.MarketActivityTracker,
peggedOrderNotify func(int64),
referralDiscountRewardService fee.ReferralDiscountRewardService,
volumeDiscountService fee.VolumeDiscountService,
banking common.Banking,
) (*Market, error) {
mkt := em.Market
positionFactor := num.DecimalFromFloat(10).Pow(num.DecimalFromInt64(mkt.PositionDecimalPlaces))
if len(em.Market.ID) == 0 {
return nil, common.ErrEmptyMarketID
}
assetDecimals := assetDetails.DecimalPlaces()
// FIXME: this is to ensure the fundingRateFactors for markets are all set to 0
if vgcontext.InProgressUpgradeFrom(ctx, "v0.73.14") {
if mkt.TradableInstrument.Instrument.GetPerps() != nil {
mkt.TradableInstrument.Instrument.GetPerps().FundingRateScalingFactor = ptr.From(num.MustDecimalFromString("0"))
}
}
tradableInstrument, err := markets.NewTradableInstrumentFromSnapshot(ctx, log, mkt.TradableInstrument, em.Market.ID,
timeService, oracleEngine, broker, em.Product, uint32(assetDecimals))
if err != nil {
return nil, fmt.Errorf("unable to instantiate a new market: %w", err)
}
as := monitor.NewAuctionStateFromSnapshot(mkt, em.AuctionState)
if vgcontext.InProgressUpgradeFrom(ctx, "v0.73.14") {
// protocol upgrade from v0.73.12, lets populate the new liquidity-fee-settings with a default marginal-cost method
log.Info("migrating liquidity fee settings for existing market", logging.String("mid", mkt.ID))
mkt.Fees.LiquidityFeeSettings = &types.LiquidityFeeSettings{
Method: types.LiquidityFeeMethodMarginalCost,
}
// if the market is in a none opening-auction we need to tell the instrument
if as.InAuction() && !as.IsOpeningAuction() {
tradableInstrument.Instrument.Product.UpdateAuctionState(ctx, true)
}
}
// @TODO -> the raw auctionstate shouldn't be something exposed to the matching engine
// as far as matching goes: it's either an auction or not
book := matching.NewCachedOrderBook(
log, matchingConfig, mkt.ID, as.InAuction(), peggedOrderNotify)
asset := tradableInstrument.Instrument.Product.GetAsset()
// this needs to stay
riskEngine := risk.NewEngine(log,
riskConfig,
tradableInstrument.MarginCalculator,
tradableInstrument.RiskModel,
book,
as,
timeService,
broker,
mkt.ID,
asset,
stateVarEngine,
positionFactor,
em.RiskFactorConsensusReached,
&types.RiskFactor{Market: mkt.ID, Short: em.ShortRiskFactor, Long: em.LongRiskFactor},
mkt.LinearSlippageFactor,
mkt.QuadraticSlippageFactor,
)
settleEngine := settlement.NewSnapshotEngine(
log,
settlementConfig,
tradableInstrument.Instrument.Product,
mkt.ID,
timeService,
broker,
positionFactor,
)
positionEngine := positions.NewSnapshotEngine(log, positionConfig, mkt.ID, broker)
var feeEngine *fee.Engine
if em.FeesStats != nil {
feeEngine, err = fee.NewFromState(log, feeConfig, *mkt.Fees, asset, positionFactor, em.FeesStats)
if err != nil {
return nil, fmt.Errorf("unable to instantiate fee engine: %w", err)
}
} else {
feeEngine, err = fee.New(log, feeConfig, *mkt.Fees, asset, positionFactor)
if err != nil {
return nil, fmt.Errorf("unable to instantiate fee engine: %w", err)
}
}
tsCalc := target.NewSnapshotEngine(*mkt.LiquidityMonitoringParameters.TargetStakeParameters, positionEngine, mkt.ID, positionFactor)
pMonitor, err := price.NewMonitorFromSnapshot(mkt.ID, asset, em.PriceMonitor, mkt.PriceMonitoringSettings, tradableInstrument.RiskModel, as, stateVarEngine, log)
if err != nil {
return nil, fmt.Errorf("unable to instantiate price monitoring engine: %w", err)
}
exp := assetDecimals - mkt.DecimalPlaces
priceFactor := num.UintZero().Exp(num.NewUint(10), num.NewUint(exp))
// TODO(jeremy): remove this once the upgrade with the .73 have run on mainnet
// this is required to support the migration to SLA liquidity
if !(mkt.LiquiditySLAParams != nil) {
mkt.LiquiditySLAParams = ptr.From(liquidity.DefaultSLAParameters)
}
liquidityEngine := liquidity.NewSnapshotEngine(
liquidityConfig, log, timeService, broker, tradableInstrument.RiskModel,
pMonitor, book, as, asset, mkt.ID, stateVarEngine, positionFactor, mkt.LiquiditySLAParams)
equityShares := common.NewEquitySharesFromSnapshot(em.EquityShare)
marketLiquidity := common.NewMarketLiquidity(
log, liquidityEngine, collateralEngine, broker, book, equityShares, marketActivityTracker,
feeEngine, common.FutureMarketType, mkt.ID, asset, priceFactor, mkt.LiquiditySLAParams.PriceRange,
)
// backward compatibility check for nil
stopOrders := stoporders.New(log)
if em.StopOrders != nil {
stopOrders = stoporders.NewFromProto(log, em.StopOrders)
} else {
// use the last markPrice for the market to initialise stopOrders price
if em.LastTradedPrice != nil {
stopOrders.PriceUpdated(em.LastTradedPrice.Clone())
}
}
expiringStopOrders := common.NewExpiringOrders()
if em.ExpiringStopOrders != nil {
expiringStopOrders = common.NewExpiringOrdersFromState(em.ExpiringStopOrders)
}
// @TODO same as in the non-snapshot market constructor: default to legacy liquidation strategy for the time being
// this can be removed once this parameter is no longer optional
if mkt.LiquidationStrategy == nil {
mkt.LiquidationStrategy = liquidation.GetLegacyStrat()
}
le := liquidation.New(log, mkt.LiquidationStrategy, mkt.GetID(), broker, book, as, timeService, marketLiquidity, positionEngine, pMonitor)
partyMargin := make(map[string]num.Decimal, len(em.PartyMarginFactors))
for _, pmf := range em.PartyMarginFactors {
partyMargin[pmf.Party], _ = num.DecimalFromString(pmf.MarginFactor)
}
now := timeService.GetTimeNow()
marketType := mkt.MarketType()
markPriceCalculator := common.NewCompositePriceCalculatorFromSnapshot(ctx, em.CurrentMarkPrice, timeService, oracleEngine, em.MarkPriceCalculator)
market := &Market{
log: log,
mkt: mkt,
closingAt: time.Unix(0, mkt.MarketTimestamps.Close),
timeService: timeService,
matching: book,
tradableInstrument: tradableInstrument,
risk: riskEngine,
position: positionEngine,
settlement: settleEngine,
collateral: collateralEngine,
broker: broker,
fee: feeEngine,
referralDiscountRewardService: referralDiscountRewardService,
volumeDiscountService: volumeDiscountService,
liquidityEngine: liquidityEngine,
liquidity: marketLiquidity,
parties: map[string]struct{}{},
tsCalc: tsCalc,
feeSplitter: common.NewFeeSplitterFromSnapshot(em.FeeSplitter, now),
as: as,
pMonitor: pMonitor,
peggedOrders: common.NewPeggedOrdersFromSnapshot(log, timeService, em.PeggedOrders),
expiringOrders: common.NewExpiringOrdersFromState(em.ExpiringOrders),
equityShares: equityShares,
lastBestBidPrice: em.LastBestBid.Clone(),
lastBestAskPrice: em.LastBestAsk.Clone(),
lastMidBuyPrice: em.LastMidBid.Clone(),
lastMidSellPrice: em.LastMidAsk.Clone(),
lastTradedPrice: em.LastTradedPrice,
priceFactor: priceFactor,
lastMarketValueProxy: em.LastMarketValueProxy,
marketActivityTracker: marketActivityTracker,
positionFactor: positionFactor,
stateVarEngine: stateVarEngine,
settlementDataInMarket: em.SettlementData,
settlementAsset: asset,
stopOrders: stopOrders,
expiringStopOrders: expiringStopOrders,
perp: marketType == types.MarketTypePerp,
partyMarginFactor: partyMargin,
liquidation: le,
banking: banking,
markPriceCalculator: markPriceCalculator,
}
markPriceCalculator.SetOraclePriceScalingFunc(market.scaleOracleData)
if em.InternalCompositePriceCalculator != nil {
market.internalCompositePriceCalculator = common.NewCompositePriceCalculatorFromSnapshot(ctx, nil, timeService, oracleEngine, em.InternalCompositePriceCalculator)
market.internalCompositePriceCalculator.SetOraclePriceScalingFunc(market.scaleOracleData)
}
for _, p := range em.Parties {
market.parties[p] = struct{}{}
}
market.assetDP = uint32(assetDecimals)
switch marketType {
case types.MarketTypeFuture:
market.tradableInstrument.Instrument.Product.NotifyOnTradingTerminated(market.tradingTerminated)
market.tradableInstrument.Instrument.Product.NotifyOnSettlementData(market.settlementData)
case types.MarketTypePerp:
market.tradableInstrument.Instrument.Product.NotifyOnSettlementData(market.settlementDataPerp)
case types.MarketTypeSpot:
default:
log.Panic("unexpected market type", logging.Int("type", int(marketType)))
}
if em.SettlementData != nil {
// ensure oracle has the settlement data
market.tradableInstrument.Instrument.Product.RestoreSettlementData(em.SettlementData.Clone())
}
liquidityEngine.SetGetStaticPricesFunc(market.getBestStaticPricesDecimal)
if mkt.State == types.MarketStateTradingTerminated {
market.tradableInstrument.Instrument.UnsubscribeTradingTerminated(ctx)
}
if em.Closed {
market.closed = true
market.tradableInstrument.Instrument.Unsubscribe(ctx)
market.markPriceCalculator.Close(ctx)
if market.internalCompositePriceCalculator != nil {
market.internalCompositePriceCalculator.Close(ctx)
}
stateVarEngine.UnregisterStateVariable(asset, mkt.ID)
}
// FIXME: We need to send the market update to the datanode to me make sure the change to the fundingRateFactor are propagated
if vgcontext.InProgressUpgradeFrom(ctx, "v0.73.14") {
broker.Stage(events.NewMarketUpdatedEvent(ctx, *market.Mkt()))
}
return market, nil
}
func (m *Market) GetNewStateProviders() []types.StateProvider {
return []types.StateProvider{
m.position, m.matching, m.tsCalc,
m.liquidityEngine.V1StateProvider(), m.liquidityEngine.V2StateProvider(),
m.settlement, m.liquidation,
}
}
func (m *Market) GetState() *types.ExecMarket {
rf := m.risk.GetRiskFactors()
var sp *num.Numeric
if m.settlementDataInMarket != nil {
sp = m.settlementDataInMarket.Clone()
}
parties := maps.Keys(m.parties)
sort.Strings(parties)
assetQuantum, _ := m.collateral.GetAssetQuantum(m.settlementAsset)
partyMarginFactors := make([]*snapshot.PartyMarginFactor, 0, len(m.partyMarginFactor))
for k, d := range m.partyMarginFactor {
partyMarginFactors = append(partyMarginFactors, &snapshot.PartyMarginFactor{Party: k, MarginFactor: d.String()})
}
sort.Slice(partyMarginFactors, func(i, j int) bool {
return partyMarginFactors[i].Party < partyMarginFactors[j].Party
})
em := &types.ExecMarket{
Market: m.mkt.DeepClone(),
PriceMonitor: m.pMonitor.GetState(),
AuctionState: m.as.GetState(),
PeggedOrders: m.peggedOrders.GetState(),
ExpiringOrders: m.expiringOrders.GetState(),
LastBestBid: m.lastBestBidPrice.Clone(),
LastBestAsk: m.lastBestAskPrice.Clone(),
LastMidBid: m.lastMidBuyPrice.Clone(),
LastMidAsk: m.lastMidSellPrice.Clone(),
LastMarketValueProxy: m.lastMarketValueProxy,
LastTradedPrice: m.lastTradedPrice,
EquityShare: m.equityShares.GetState(),
RiskFactorConsensusReached: m.risk.IsRiskFactorInitialised(),
ShortRiskFactor: rf.Short,
LongRiskFactor: rf.Long,
FeeSplitter: m.feeSplitter.GetState(),
SettlementData: sp,
NextMTM: m.nextMTM.UnixNano(),
NextInternalCompositePriceCalc: m.nextInternalCompositePriceCalc.UnixNano(),
Parties: parties,
Closed: m.closed,
IsSucceeded: m.succeeded,
StopOrders: m.stopOrders.ToProto(),
ExpiringStopOrders: m.expiringStopOrders.GetState(),
Product: m.tradableInstrument.Instrument.Product.Serialize(),
FeesStats: m.fee.GetState(assetQuantum),
PartyMarginFactors: partyMarginFactors,
MarkPriceCalculator: m.markPriceCalculator.IntoProto(),
}
if m.perp && m.internalCompositePriceCalculator != nil {
em.InternalCompositePriceCalculator = m.internalCompositePriceCalculator.IntoProto()
}
return em
}