-
Notifications
You must be signed in to change notification settings - Fork 19
/
engine.go
537 lines (496 loc) · 18.3 KB
/
engine.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package settlement
import (
"context"
"sort"
"sync"
"time"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/metrics"
"code.vegaprotocol.io/vega/core/products"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
)
// MarketPosition ...
//
//go:generate go run github.com/golang/mock/mockgen -destination mocks/market_position_mock.go -package mocks code.vegaprotocol.io/vega/core/settlement MarketPosition
type MarketPosition interface {
Party() string
Size() int64
Buy() int64
Sell() int64
Price() *num.Uint
VWBuy() *num.Uint
VWSell() *num.Uint
ClearPotentials()
}
// Product ...
//
//go:generate go run github.com/golang/mock/mockgen -destination mocks/settlement_product_mock.go -package mocks code.vegaprotocol.io/vega/core/settlement Product
type Product interface {
Settle(*num.Uint, uint32, num.Decimal) (*types.FinancialAmount, bool, error)
GetAsset() string
}
// TimeService.
//
//go:generate go run github.com/golang/mock/mockgen -destination mocks/time_service_mock.go -package mocks code.vegaprotocol.io/vega/core/settlement TimeService
type TimeService interface {
GetTimeNow() time.Time
}
// Broker - the event bus broker, send events here.
type Broker interface {
SendBatch(events []events.Event)
}
// Engine - the main type (of course).
type Engine struct {
Config
log *logging.Logger
market string
product Product
settledPosition map[string]int64 // party -> last mark-to-market position
mu *sync.Mutex
trades map[string][]*settlementTrade
timeService TimeService
broker Broker
positionFactor num.Decimal
lastMarkPrice *num.Uint // price at last mark to market
}
// New instantiates a new instance of the settlement engine.
func New(log *logging.Logger, conf Config, product Product, market string, timeService TimeService, broker Broker, positionFactor num.Decimal) *Engine {
// setup logger
log = log.Named(namedLogger)
log.SetLevel(conf.Level.Get())
return &Engine{
Config: conf,
log: log,
market: market,
product: product,
settledPosition: map[string]int64{},
mu: &sync.Mutex{},
trades: map[string][]*settlementTrade{},
timeService: timeService,
broker: broker,
positionFactor: positionFactor,
}
}
func (e *Engine) UpdateProduct(product products.Product) {
e.product = product
}
// ReloadConf update the internal configuration of the settlement engined.
func (e *Engine) ReloadConf(cfg Config) {
e.log.Info("reloading configuration")
if e.log.GetLevel() != cfg.Level.Get() {
e.log.Info("updating log level",
logging.String("old", e.log.GetLevel().String()),
logging.String("new", cfg.Level.String()),
)
e.log.SetLevel(cfg.Level.Get())
}
e.Config = cfg
}
// Update merely adds positions to the settlement engine, and won't be useful for a MTM settlement
// this function is mainly used for testing, and should be used with extreme caution as a result
// perhaps the tests should be refactored to use the Settle call to create positions first.
func (e *Engine) Update(positions []events.MarketPosition) {
e.mu.Lock()
for _, p := range positions {
party := p.Party()
e.settledPosition[party] = p.Size()
e.lastMarkPrice = p.Price()
}
e.mu.Unlock()
}
// Settle run settlement over all the positions.
func (e *Engine) Settle(t time.Time, assetDecimals uint32) ([]*types.Transfer, error) {
e.log.Debugf("Settling market, closed at %s", t.Format(time.RFC3339))
positions, err := e.settleAll(assetDecimals)
if err != nil {
e.log.Error(
"Something went wrong trying to settle positions",
logging.Error(err),
)
return nil, err
}
return positions, nil
}
// AddTrade - this call is required to get the correct MTM settlement values
// each change in position has to be calculated using the exact price of the trade.
func (e *Engine) AddTrade(trade *types.Trade) {
e.mu.Lock()
var buyerSize, sellerSize int64
// checking the len of cd shouldn't be required here, but it is needed in the second if
// in case the buyer and seller are one and the same...
if cd, ok := e.trades[trade.Buyer]; !ok || len(cd) == 0 {
e.trades[trade.Buyer] = []*settlementTrade{}
// check if the buyer already has a known position
if pos, ok := e.settledPosition[trade.Buyer]; ok {
buyerSize = pos
}
} else {
buyerSize = cd[len(cd)-1].newSize
}
if cd, ok := e.trades[trade.Seller]; !ok || len(cd) == 0 {
e.trades[trade.Seller] = []*settlementTrade{}
// check if seller has a known position
if pos, ok := e.settledPosition[trade.Seller]; ok {
sellerSize = pos
}
} else {
sellerSize = cd[len(cd)-1].newSize
}
size := int64(trade.Size)
// the parties both need to get a MTM settlement on the traded volume
// and this MTM part has to be based on the _actual_ trade value
price := trade.Price.Clone()
e.trades[trade.Buyer] = append(e.trades[trade.Buyer], &settlementTrade{
price: price,
marketPrice: trade.MarketPrice,
size: size,
newSize: buyerSize + size,
})
e.trades[trade.Seller] = append(e.trades[trade.Seller], &settlementTrade{
price: price.Clone(),
marketPrice: trade.MarketPrice,
size: -size,
newSize: sellerSize - size,
})
e.mu.Unlock()
}
func (e *Engine) HasTraded() bool {
return len(e.trades) > 0
}
func (e *Engine) getMtmTransfer(mtmShare *num.Uint, neg bool, mpos events.MarketPosition, owner string) *mtmTransfer {
if mtmShare.IsZero() {
return &mtmTransfer{
MarketPosition: mpos,
transfer: nil,
}
}
typ := types.TransferTypeMTMWin
if neg {
typ = types.TransferTypeMTMLoss
}
return &mtmTransfer{
MarketPosition: mpos,
transfer: &types.Transfer{
Type: typ,
Owner: owner,
Amount: &types.FinancialAmount{
Amount: mtmShare,
Asset: e.product.GetAsset(),
},
},
}
}
func (e *Engine) winSocialisationUpdate(transfer *mtmTransfer, amt *num.Uint) {
if amt.IsZero() {
return
}
if transfer.transfer == nil {
transfer.transfer = &types.Transfer{
Type: types.TransferTypeMTMWin,
Owner: transfer.Party(),
Amount: &types.FinancialAmount{
Amount: num.UintZero(),
Asset: e.product.GetAsset(),
},
}
}
transfer.transfer.Amount.Amount.AddSum(amt)
}
func (e *Engine) SettleMTM(ctx context.Context, markPrice *num.Uint, positions []events.MarketPosition) []events.Transfer {
timer := metrics.NewTimeCounter("-", "settlement", "SettleOrder")
defer func() { e.lastMarkPrice = markPrice.Clone() }()
e.mu.Lock()
tCap := e.transferCap(positions)
transfers := make([]events.Transfer, 0, tCap)
// roughly half of the transfers should be wins, half losses
wins := make([]events.Transfer, 0, tCap/2)
trades := e.trades
e.trades = map[string][]*settlementTrade{} // remove here, once we've processed it all here, we're done
evts := make([]events.Event, 0, len(positions))
var (
largestShare *mtmTransfer // pointer to whomever gets the last remaining amount from the loss
zeroShares = []*mtmTransfer{} // all zero shares for equal distribution if possible
zeroAmts = false
mtmDec = num.NewDecimalFromFloat(0)
lossTotal = num.UintZero()
winTotal = num.UintZero()
lossTotalDec = num.NewDecimalFromFloat(0)
winTotalDec = num.NewDecimalFromFloat(0)
)
// Process any network trades first
traded, hasTraded := trades[types.NetworkParty]
if hasTraded {
// don't create an event for the network. Its position is irrelevant
mtmShare, mtmDShare, neg := calcMTM(markPrice, markPrice, 0, traded, e.positionFactor)
// MarketPosition stub for network
netMPos := &npos{
price: markPrice.Clone(),
}
mtmTransfer := e.getMtmTransfer(mtmShare.Clone(), neg, netMPos, types.NetworkParty)
if !neg {
wins = append(wins, mtmTransfer)
winTotal.AddSum(mtmShare)
winTotalDec = winTotalDec.Add(mtmDShare)
mtmDec = mtmDShare
largestShare = mtmTransfer
// mtmDec is zero at this point, this will always be the largest winning party at this point
if mtmShare.IsZero() {
zeroShares = append(zeroShares, mtmTransfer)
zeroAmts = true
}
} else if mtmShare.IsZero() {
// This would be a zero-value loss, so not sure why this would be at the end of the slice
// shouldn't really matter if this is in the wins or losses part of the slice, but
// this was the previous behaviour, so let's keep it
wins = append(wins, mtmTransfer)
lossTotalDec = lossTotalDec.Add(mtmDShare)
} else {
transfers = append(transfers, mtmTransfer)
lossTotal.AddSum(mtmShare)
lossTotalDec = lossTotalDec.Add(mtmDShare)
}
}
for _, evt := range positions {
party := evt.Party()
current, lastSettledPrice := e.getOrCreateCurrentPosition(party, evt.Size(), evt.Price())
traded, hasTraded = trades[party]
tradeset := make([]events.TradeSettlement, 0, len(traded))
for _, t := range traded {
tradeset = append(tradeset, t)
}
// create (and add position to buffer)
evts = append(evts, events.NewSettlePositionEvent(ctx, party, e.market, evt.Price(), tradeset, e.timeService.GetTimeNow().UnixNano(), e.positionFactor))
// no changes in position, and the MTM price hasn't changed, we don't need to do anything
if !hasTraded && lastSettledPrice.EQ(markPrice) {
// no changes in position and markPrice hasn't changed -> nothing needs to be marked
continue
}
// calculate MTM value, we need the signed mark-price, the OLD open position/volume
// the new position is either the same, or accounted for by the traded var (added trades)
// and the old mark price at which the party held the position
// the trades slice contains all trade positions (position changes for the party)
// at their exact trade price, so we can MTM that volume correctly, too
mtmShare, mtmDShare, neg := calcMTM(markPrice, lastSettledPrice, current, traded, e.positionFactor)
// we've marked this party to market, their position can now reflect this
e.settledPosition[party] = evt.Size()
// we don't want to accidentally MTM a party who closed out completely when they open
// a new position at a later point, so remove if size == 0
if evt.Size() == 0 && evt.Buy() == 0 && evt.Sell() == 0 {
// broke this up into its own func for symmetry
e.rmPosition(party)
}
posEvent := newPos(evt, markPrice)
mtmTransfer := e.getMtmTransfer(mtmShare.Clone(), neg, posEvent, party)
if !neg {
wins = append(wins, mtmTransfer)
winTotal.AddSum(mtmShare)
winTotalDec = winTotalDec.Add(mtmDShare)
if mtmShare.IsZero() {
zeroShares = append(zeroShares, mtmTransfer)
zeroAmts = true
}
if mtmDShare.GreaterThan(mtmDec) {
mtmDec = mtmDShare
largestShare = mtmTransfer
}
} else if mtmShare.IsZero() {
// zero value loss
wins = append(wins, mtmTransfer)
lossTotalDec = lossTotalDec.Add(mtmDShare)
} else {
transfers = append(transfers, mtmTransfer)
lossTotal.AddSum(mtmShare)
lossTotalDec = lossTotalDec.Add(mtmDShare)
}
}
// no need for this lock anymore
e.mu.Unlock()
delta := num.UintZero().Sub(lossTotal, winTotal)
if !delta.IsZero() {
if zeroAmts {
// there are more transfers from losses than we pay out to wins, but some winning parties have zero transfers
// this delta should == combined win decimals, let's sanity check this!
if winTotalDec.LessThan(lossTotalDec) {
e.log.Panic("There's less MTM wins than losses, even accounting for decimals",
logging.Decimal("total loss", lossTotalDec),
logging.Decimal("total wins", winTotalDec),
)
}
// parties with a zero win transfer should get AT MOST a transfer of value 1
// any remainder after that should go to the largest win share, unless we only have parties
// with a win share of 0. that shouldn't be possible however, and so we can ignore that case
// should this happen at any point, the collateral engine will panic on settlement balance > 0
// which is the correct behaviour
// start distributing the delta
one := num.NewUint(1)
for _, transfer := range zeroShares {
e.winSocialisationUpdate(transfer, one)
delta.Sub(delta, one)
if delta.IsZero() {
break // all done
}
}
}
// delta is whatever amount the largest share win party gets, this shouldn't be too much
// delta can be zero at this stage, which is fine
e.winSocialisationUpdate(largestShare, delta)
}
// append wins after loss transfers
transfers = append(transfers, wins...)
e.broker.SendBatch(evts)
timer.EngineTimeCounterAdd()
return transfers
}
// RemoveDistressed - remove whatever settlement data we have for distressed parties
// they are being closed out, and shouldn't be part of any MTM settlement or closing settlement.
func (e *Engine) RemoveDistressed(ctx context.Context, evts []events.Margin) {
devts := make([]events.Event, 0, len(evts))
e.mu.Lock()
for _, v := range evts {
key := v.Party()
margin := num.Sum(v.MarginBalance(), v.GeneralBalance())
devts = append(devts, events.NewSettleDistressed(ctx, key, e.market, v.Price(), margin, e.timeService.GetTimeNow().UnixNano()))
delete(e.settledPosition, key)
delete(e.trades, key)
}
e.mu.Unlock()
e.broker.SendBatch(devts)
}
// simplified settle call.
func (e *Engine) settleAll(assetDecimals uint32) ([]*types.Transfer, error) {
e.mu.Lock()
// there should be as many positions as there are parties (obviously)
aggregated := make([]*types.Transfer, 0, len(e.settledPosition))
// parties who are in profit should be appended (collect first).
// The split won't always be 50-50, but it's a reasonable approximation
owed := make([]*types.Transfer, 0, len(e.settledPosition)/2)
// ensure we iterate over the positions in the same way by getting all the parties (keys)
// and sort them
keys := make([]string, 0, len(e.settledPosition))
for p := range e.settledPosition {
keys = append(keys, p)
}
sort.Strings(keys)
for _, party := range keys {
pos := e.settledPosition[party]
// this is possible now, with the Mark to Market stuff, it's possible we've settled any and all positions for a given party
if pos == 0 {
continue
}
e.log.Debug("Settling position for party", logging.String("party-id", party))
// @TODO - there was something here... the final amount had to be oracle - market or something
amt, neg, err := e.product.Settle(e.lastMarkPrice, assetDecimals, num.DecimalFromInt64(pos).Div(e.positionFactor))
// for now, product.Settle returns the total value, we need to only settle the delta between a parties current position
// and the final price coming from the oracle, so oracle_price - mark_price * volume (check with Tamlyn whether this should be absolute or not)
if err != nil {
e.log.Error(
"Failed to settle position for party",
logging.String("party-id", party),
logging.Error(err),
)
e.mu.Unlock()
return nil, err
}
settlePos := &types.Transfer{
Owner: party,
Amount: amt,
}
e.log.Debug(
"Settled position for party",
logging.String("party-id", party),
logging.String("amount", amt.Amount.String()),
)
if neg { // this is a loss transfer
settlePos.Type = types.TransferTypeLoss
aggregated = append(aggregated, settlePos)
} else { // this is a win transfer
settlePos.Type = types.TransferTypeWin
owed = append(owed, settlePos)
}
}
// append the parties in profit to the end
aggregated = append(aggregated, owed...)
e.mu.Unlock()
return aggregated, nil
}
func (e *Engine) getOrCreateCurrentPosition(party string, size int64, price *num.Uint) (int64, *num.Uint) {
p, ok := e.settledPosition[party]
if !ok {
e.settledPosition[party] = size
return 0, price
}
return p, e.lastMarkPrice
}
func (e *Engine) HasPosition(party string) bool {
_, okPos := e.settledPosition[party]
_, okTrades := e.trades[party]
return okPos && okTrades
}
func (e *Engine) rmPosition(party string) {
delete(e.settledPosition, party)
}
// just get the max len as cap.
func (e *Engine) transferCap(evts []events.MarketPosition) int {
curLen, evtLen := len(e.settledPosition), len(evts)
if curLen >= evtLen {
return curLen
}
return evtLen
}
// party.PREV_OPEN_VOLUME * (product.value(current_price) - product.value(prev_mark_price)) + SUM(from i=1 to new_trades.length)( new_trade(i).volume(party) * (product.value(current_price) - new_trade(i).price ) )
// the sum bit is a worry, we do not have all the trades available at this point...
// calcMTM only handles futures ATM. The formula is simple:
// amount = prev_vol * (current_price - prev_mark_price) + SUM(new_trade := range trades)( new_trade(i).volume(party)*(current_price - new_trade(i).price )
// given that the new trades price will equal new mark price, the sum(trades) bit will probably == 0 for nicenet
// the size here is the _new_ position size, the price is the OLD price!!
func calcMTM(markPrice, price *num.Uint, size int64, trades []*settlementTrade, positionFactor num.Decimal) (*num.Uint, num.Decimal, bool) {
delta, sign := num.UintZero().Delta(markPrice, price)
// this shouldn't be possible I don't think, but just in case
if size < 0 {
size = -size
// swap sign
sign = !sign
}
mtmShare := delta.Mul(delta, num.NewUint(uint64(size)))
for _, c := range trades {
delta, neg := num.UintZero().Delta(markPrice, c.price)
size := num.NewUint(uint64(c.size))
if c.size < 0 {
size = size.SetUint64(uint64(-c.size))
neg = !neg
}
add := delta.Mul(delta, size)
if mtmShare.IsZero() {
mtmShare.Set(add)
sign = neg
} else if neg == sign {
// both mtmShare and add are the same sign
mtmShare = mtmShare.Add(mtmShare, add)
} else if mtmShare.GTE(add) {
// regardless of sign, we just have to subtract
mtmShare = mtmShare.Sub(mtmShare, add)
} else {
// add > mtmShare, we don't care about signs here
// just subtract mtmShare and switch signs
mtmShare = add.Sub(add, mtmShare)
sign = neg
}
}
// as mtmShare was calculated with the volumes as integers (not decimals in pdp space) we need to divide by position factor
decShare := mtmShare.ToDecimal().Div(positionFactor)
res, _ := num.UintFromDecimal(decShare)
return res, decShare, sign
}