-
Notifications
You must be signed in to change notification settings - Fork 22
/
engine.go
580 lines (517 loc) · 17 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// 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 fee
import (
"errors"
"sort"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
)
var (
ErrEmptyTrades = errors.New("empty trades slice sent to fees")
ErrInvalidFeeFactor = errors.New("fee factors must be positive")
)
type Engine struct {
log *logging.Logger
cfg Config
asset string
feeCfg types.Fees
f factors
positionFactor num.Decimal
}
type factors struct {
makerFee num.Decimal
infrastructureFee num.Decimal
liquidityFee num.Decimal
}
func New(log *logging.Logger, cfg Config, feeCfg types.Fees, asset string, positionFactor num.Decimal) (*Engine, error) {
log = log.Named(namedLogger)
log.SetLevel(cfg.Level.Get())
e := &Engine{
log: log,
feeCfg: feeCfg,
cfg: cfg,
asset: asset,
positionFactor: positionFactor,
}
return e, e.UpdateFeeFactors(e.feeCfg)
}
// ReloadConf is used in order to reload the internal configuration of
// the fee engine.
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.cfg = cfg
}
func (e *Engine) UpdateFeeFactors(fees types.Fees) error {
if fees.Factors.MakerFee.IsNegative() || fees.Factors.InfrastructureFee.IsNegative() || fees.Factors.LiquidityFee.IsNegative() {
return ErrInvalidFeeFactor
}
e.f.makerFee = fees.Factors.MakerFee
e.f.infrastructureFee = fees.Factors.InfrastructureFee
// not sure we need the IsPositive check here, that ought to be validation
if !fees.Factors.LiquidityFee.IsZero() && fees.Factors.LiquidityFee.IsPositive() {
e.f.liquidityFee = fees.Factors.LiquidityFee
}
e.feeCfg = fees
return nil
}
func (e *Engine) SetLiquidityFee(v num.Decimal) {
e.f.liquidityFee = v
}
// CalculateForContinuousMode calculate the fee for
// trades which were produced from a market running in
// in continuous trading mode.
// A single FeesTransfer is produced here as all fees
// are paid by the aggressive order.
func (e *Engine) CalculateForContinuousMode(
trades []*types.Trade,
) (events.FeesTransfer, error) {
if len(trades) <= 0 {
return nil, ErrEmptyTrades
}
var (
aggressor, maker string
totalFeeAmount = num.UintZero()
totalInfrastructureFeeAmount = num.UintZero()
totalLiquidityFeeAmount = num.UintZero()
// we allocate the len of the trades + 2
// len(trade) = number of makerFee + 1 infra fee + 1 liquidity fee
transfers = make([]*types.Transfer, 0, (len(trades)*2)+2)
transfersRecv = make([]*types.Transfer, 0, len(trades)+2)
)
for _, v := range trades {
fee := e.calculateContinuousModeFees(v)
switch v.Aggressor {
case types.SideBuy:
v.BuyerFee = fee
v.SellerFee = types.NewFee()
aggressor = v.Buyer
maker = v.Seller
case types.SideSell:
v.SellerFee = fee
v.BuyerFee = types.NewFee()
aggressor = v.Seller
maker = v.Buyer
}
totalFeeAmount.AddSum(fee.InfrastructureFee, fee.LiquidityFee, fee.MakerFee)
totalInfrastructureFeeAmount.AddSum(fee.InfrastructureFee)
totalLiquidityFeeAmount.AddSum(fee.LiquidityFee)
// create a transfer for the aggressor
transfers = append(transfers, &types.Transfer{
Owner: aggressor,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: fee.MakerFee.Clone(),
},
Type: types.TransferTypeMakerFeePay,
})
// create a transfer for the maker
transfersRecv = append(transfersRecv, &types.Transfer{
Owner: maker,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: fee.MakerFee.Clone(),
},
Type: types.TransferTypeMakerFeeReceive,
})
}
// now create transfer for the infrastructure
transfers = append(transfers, &types.Transfer{
Owner: aggressor,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: totalInfrastructureFeeAmount,
},
Type: types.TransferTypeInfrastructureFeePay,
})
// now create transfer for the liquidity
transfers = append(transfers, &types.Transfer{
Owner: aggressor,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: totalLiquidityFeeAmount,
},
Type: types.TransferTypeLiquidityFeePay,
})
return &feesTransfer{
totalFeesAmountsPerParty: map[string]*num.Uint{aggressor: totalFeeAmount, maker: num.UintZero()},
transfers: append(transfers, transfersRecv...),
}, nil
}
// CalculateForAuctionMode calculate the fee for
// trades which were produced from a market running in
// in auction trading mode.
// A list FeesTransfer is produced each containing fees transfer from a
// single party.
func (e *Engine) CalculateForAuctionMode(
trades []*types.Trade,
) (events.FeesTransfer, error) {
if len(trades) <= 0 {
return nil, ErrEmptyTrades
}
var (
totalFeesAmounts = map[string]*num.Uint{}
// we allocate for len of trades *4 as all trades generate
// 2 fees per party
transfers = make([]*types.Transfer, 0, len(trades)*4)
)
// we iterate over all trades
// for each trades both party needs to pay half of the fees
// no maker fees are to be paid here.
for _, v := range trades {
fee, newTransfers := e.getAuctionModeFeesAndTransfers(v)
totalFee := num.Sum(fee.InfrastructureFee, fee.LiquidityFee)
transfers = append(transfers, newTransfers...)
// increase the total fee for the parties
if sellerTotalFee, ok := totalFeesAmounts[v.Seller]; !ok {
totalFeesAmounts[v.Seller] = totalFee.Clone()
} else {
sellerTotalFee.AddSum(totalFee)
}
if buyerTotalFee, ok := totalFeesAmounts[v.Buyer]; !ok {
totalFeesAmounts[v.Buyer] = totalFee.Clone()
} else {
buyerTotalFee.AddSum(totalFee)
}
v.BuyerFee = fee
v.SellerFee = fee
}
return &feesTransfer{
totalFeesAmountsPerParty: totalFeesAmounts,
transfers: transfers,
}, nil
}
// CalculateForFrequentBatchesAuctionMode calculate the fee for
// trades which were produced from a market running in
// in auction trading mode.
// A list FeesTransfer is produced each containing fees transfer from a
// single party.
func (e *Engine) CalculateForFrequentBatchesAuctionMode(
trades []*types.Trade,
) (events.FeesTransfer, error) {
if len(trades) <= 0 {
return nil, ErrEmptyTrades
}
var (
totalFeesAmounts = map[string]*num.Uint{}
// we allocate for len of trades *4 as all trades generate
// at lest2 fees per party
transfers = make([]*types.Transfer, 0, len(trades)*4)
)
// we iterate over all trades
// if the parties submitted the order in the same batches,
// auction mode fees apply.
// if not then the aggressor is the party which submitted
// the order last, and continuous trading fees apply
for _, v := range trades {
var (
sellerTotalFee, buyerTotalFee *num.Uint
newTransfers []*types.Transfer
)
// we are in the same auction, normal auction fees applies
if v.BuyerAuctionBatch == v.SellerAuctionBatch {
var fee *types.Fee
fee, newTransfers = e.getAuctionModeFeesAndTransfers(v)
// clone the fees, obviously
v.SellerFee, v.BuyerFee = fee, fee.Clone()
totalFee := num.Sum(fee.InfrastructureFee, fee.LiquidityFee)
sellerTotalFee, buyerTotalFee = totalFee, totalFee.Clone()
} else {
// set the aggressor to be the side of the party
// entering the later auction
v.Aggressor = types.SideSell
if v.BuyerAuctionBatch > v.SellerAuctionBatch {
v.Aggressor = types.SideBuy
}
// fees are being assign to the trade directly
// no need to do add them there as well
ftrnsfr, _ := e.CalculateForContinuousMode([]*types.Trade{v})
newTransfers = ftrnsfr.Transfers()
buyerTotalFee = ftrnsfr.TotalFeesAmountPerParty()[v.Buyer]
sellerTotalFee = ftrnsfr.TotalFeesAmountPerParty()[v.Seller]
}
transfers = append(transfers, newTransfers...)
// increase the total fee for the parties
if prevTotalFee, ok := totalFeesAmounts[v.Seller]; !ok {
totalFeesAmounts[v.Seller] = sellerTotalFee.Clone()
} else {
prevTotalFee.AddSum(sellerTotalFee)
}
if prevTotalFee, ok := totalFeesAmounts[v.Buyer]; !ok {
totalFeesAmounts[v.Buyer] = buyerTotalFee.Clone()
} else {
prevTotalFee.AddSum(buyerTotalFee)
}
}
return &feesTransfer{
totalFeesAmountsPerParty: totalFeesAmounts,
transfers: transfers,
}, nil
}
func (e *Engine) CalculateFeeForPositionResolution(
// the trade from the good parties which 0 out the network order
trades []*types.Trade,
// the positions of the parties being closed out.
closedMPs []events.MarketPosition,
) (events.FeesTransfer, map[string]*types.Fee) {
var (
totalFeesAmounts = map[string]*num.Uint{}
partiesFees = map[string]*types.Fee{}
// this is the share of each party to be paid
partiesShare = map[string]*feeShare{}
totalAbsolutePos uint64
transfers = []*types.Transfer{}
)
// first calculate the share of all distressedParties
for _, v := range closedMPs {
size := v.Size()
if size < 0 {
size = -size
}
totalAbsolutePos += uint64(size)
partiesShare[v.Party()] = &feeShare{pos: uint64(size)}
// while we are at it, we initial the map of all fees per party
partiesFees[v.Party()] = types.NewFee()
}
// no we accumulated all the absolute position, we
// will get the share of each party
for _, v := range partiesShare {
v.share = num.DecimalFromInt64(int64(v.pos)).Div(num.DecimalFromInt64(int64(totalAbsolutePos)))
}
// now we have the share of each distressed parties
// we can iterate over the trades, and make the transfers
for _, t := range trades {
// continuous trading fees apply here
// the we'll split them in between all parties
fees := e.calculateContinuousModeFees(t)
// lets fine which side is the good party
goodParty := t.Buyer
t.SellerFee = fees
if goodParty == "network" {
goodParty = t.Seller
t.SellerFee = types.NewFee()
t.BuyerFee = fees.Clone()
}
// now we iterate over all parties,
// and create a pay for each distressed parties
for _, v := range closedMPs {
partyTransfers, fees, feesAmount := e.getPositionResolutionFeesTransfers(
v.Party(), partiesShare[v.Party()].share, fees)
if prevTotalFee, ok := totalFeesAmounts[v.Party()]; !ok {
totalFeesAmounts[v.Party()] = feesAmount.Clone()
} else {
prevTotalFee.AddSum(feesAmount)
}
transfers = append(transfers, partyTransfers...)
// increase the party full fees
pf := partiesFees[v.Party()]
pf.MakerFee.AddSum(fees.MakerFee)
pf.InfrastructureFee.AddSum(fees.InfrastructureFee)
pf.LiquidityFee.AddSum(fees.LiquidityFee)
partiesFees[v.Party()] = pf
}
// then 1 receive transfer for the good party
transfers = append(transfers, &types.Transfer{
Owner: goodParty,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: fees.MakerFee,
},
Type: types.TransferTypeMakerFeeReceive,
})
}
// calculate the
return &feesTransfer{
totalFeesAmountsPerParty: totalFeesAmounts,
transfers: transfers,
}, partiesFees
}
// BuildLiquidityFeeDistributionTransfer returns the set of transfers that will
// be used by the collateral engine to distribute the fees. As shares are
// represented in float64 and fees are uint64, shares are floored and the
// remainder is assigned to the last party on the share map. Note that the map
// is sorted lexicographically to keep determinism.
func (e *Engine) BuildLiquidityFeeDistributionTransfer(shares map[string]num.Decimal, acc *types.Account) events.FeesTransfer {
if len(shares) == 0 {
return nil
}
ft := &feesTransfer{
totalFeesAmountsPerParty: map[string]*num.Uint{},
transfers: make([]*types.Transfer, 0, len(shares)),
}
// Get all the map keys
keys := make([]string, 0, len(shares))
for key := range shares {
keys = append(keys, key)
ft.totalFeesAmountsPerParty[key] = num.UintZero()
}
sort.Strings(keys)
var floored num.Decimal
for _, key := range keys {
share := shares[key]
cs := acc.Balance.ToDecimal().Mul(share).Floor()
floored = floored.Add(cs)
amount, _ := num.UintFromDecimal(cs)
// populate the return value
ft.totalFeesAmountsPerParty[key].AddSum(amount)
ft.transfers = append(ft.transfers, &types.Transfer{
Owner: key,
Amount: &types.FinancialAmount{
Amount: amount.Clone(),
Asset: acc.Asset,
},
MinAmount: amount.Clone(),
Type: types.TransferTypeLiquidityFeeDistribute,
})
}
// last is the party who will get the remaining from ceil
last := keys[len(keys)-1]
diff, _ := num.UintFromDecimal(acc.Balance.ToDecimal().Sub(floored))
ft.totalFeesAmountsPerParty[last].AddSum(diff)
ft.transfers[len(ft.transfers)-1].Amount.Amount.AddSum(diff)
return ft
}
// this will calculate the transfer the distressed party needs
// to do.
func (e *Engine) getPositionResolutionFeesTransfers(
party string, share num.Decimal, fees *types.Fee,
) ([]*types.Transfer, *types.Fee, *num.Uint) {
makerFee, _ := num.UintFromDecimal(fees.MakerFee.ToDecimal().Mul(share).Ceil())
infraFee, _ := num.UintFromDecimal(fees.InfrastructureFee.ToDecimal().Mul(share).Ceil())
liquiFee, _ := num.UintFromDecimal(fees.LiquidityFee.ToDecimal().Mul(share).Ceil())
return []*types.Transfer{
{
Owner: party,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: makerFee.Clone(),
},
Type: types.TransferTypeMakerFeePay,
},
{
Owner: party,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: infraFee.Clone(),
},
Type: types.TransferTypeInfrastructureFeePay,
},
{
Owner: party,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: liquiFee.Clone(),
},
Type: types.TransferTypeLiquidityFeePay,
},
},
&types.Fee{
MakerFee: makerFee,
InfrastructureFee: infraFee,
LiquidityFee: liquiFee,
}, num.Sum(makerFee, infraFee, liquiFee)
}
type feeShare struct {
// the absolute position of the party which had to be recovered
pos uint64
// the share out of the total volume
share num.Decimal
}
func (e *Engine) getAuctionModeFeesAndTransfers(t *types.Trade) (*types.Fee, []*types.Transfer) {
fee := e.calculateAuctionModeFees(t)
transfers := make([]*types.Transfer, 0, 4)
transfers = append(transfers,
e.getAuctionModeFeeTransfers(
fee.InfrastructureFee, fee.LiquidityFee, t.Seller)...)
transfers = append(transfers,
e.getAuctionModeFeeTransfers(
fee.InfrastructureFee, fee.LiquidityFee, t.Buyer)...)
return fee, transfers
}
func (e *Engine) calculateContinuousModeFees(trade *types.Trade) *types.Fee {
size := num.NewUint(trade.Size)
// multiply by size
total := size.Mul(trade.Price, size).ToDecimal().Div(e.positionFactor)
mf, _ := num.UintFromDecimal(total.Mul(e.f.makerFee).Ceil())
inf, _ := num.UintFromDecimal(total.Mul(e.f.infrastructureFee).Ceil())
lf, _ := num.UintFromDecimal(total.Mul(e.f.liquidityFee).Ceil())
return &types.Fee{
MakerFee: mf,
InfrastructureFee: inf,
LiquidityFee: lf,
}
}
func (e *Engine) calculateAuctionModeFees(trade *types.Trade) *types.Fee {
fee := e.calculateContinuousModeFees(trade)
two := num.DecimalFromInt64(2)
inf, _ := num.UintFromDecimal(fee.InfrastructureFee.ToDecimal().Div(two).Ceil())
lf, _ := num.UintFromDecimal(fee.LiquidityFee.ToDecimal().Div(two).Ceil())
return &types.Fee{
MakerFee: num.UintZero(),
InfrastructureFee: inf,
LiquidityFee: lf,
}
}
func (e *Engine) getAuctionModeFeeTransfers(infraFee, liquiFee *num.Uint, p string) []*types.Transfer {
// we return both transfer for the party in a slice
// always the infrastructure fee first
return []*types.Transfer{
{
Owner: p,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: infraFee.Clone(),
},
Type: types.TransferTypeInfrastructureFeePay,
},
{
Owner: p,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: liquiFee.Clone(),
},
Type: types.TransferTypeLiquidityFeePay,
},
}
}
type feesTransfer struct {
totalFeesAmountsPerParty map[string]*num.Uint
transfers []*types.Transfer
}
func (f *feesTransfer) TotalFeesAmountPerParty() map[string]*num.Uint {
ret := make(map[string]*num.Uint, len(f.totalFeesAmountsPerParty))
for k, v := range f.totalFeesAmountsPerParty {
ret[k] = v.Clone()
}
return ret
}
func (f *feesTransfer) Transfers() []*types.Transfer { return f.transfers }
func (e *Engine) OnFeeFactorsMakerFeeUpdate(f num.Decimal) {
e.feeCfg.Factors.MakerFee = f
e.f.makerFee = f
}
func (e *Engine) OnFeeFactorsInfrastructureFeeUpdate(f num.Decimal) {
e.feeCfg.Factors.InfrastructureFee = f
e.f.infrastructureFee = f
}
func (e *Engine) GetLiquidityFee() num.Decimal {
return e.f.liquidityFee
}