-
Notifications
You must be signed in to change notification settings - Fork 19
/
engine.go
170 lines (145 loc) · 5.34 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
// Copyright (c) 2023 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 volumediscount
import (
"context"
"sort"
"time"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)
const MaximumWindowLength uint64 = 100
type Engine struct {
broker Broker
marketActivityTracker MarketActivityTracker
epochData []map[types.PartyID]*num.Uint
epochDataIndex int
parties map[types.PartyID]struct{}
avgVolumePerParty map[types.PartyID]num.Decimal
latestProgramVersion uint64
currentProgram *types.VolumeDiscountProgram
newProgram *types.VolumeDiscountProgram
programHasEnded bool
}
func New(broker Broker, marketActivityTracker MarketActivityTracker) *Engine {
return &Engine{
broker: broker,
marketActivityTracker: marketActivityTracker,
epochData: make([]map[types.PartyID]*num.Uint, MaximumWindowLength),
epochDataIndex: 0,
avgVolumePerParty: map[types.PartyID]num.Decimal{},
parties: map[types.PartyID]struct{}{},
programHasEnded: true,
}
}
func (e *Engine) OnEpoch(ctx context.Context, ep types.Epoch) {
switch ep.Action {
case vegapb.EpochAction_EPOCH_ACTION_START:
e.applyProgramUpdate(ctx, ep.StartTime)
if e.currentProgram != nil {
e.calculatePartiesVolumeForWindow(int(e.currentProgram.WindowLength))
}
case vegapb.EpochAction_EPOCH_ACTION_END:
e.updateNotionalVolumeForEpoch()
}
}
func (e *Engine) OnEpochRestore(_ context.Context, ep types.Epoch) {
}
func (e *Engine) UpdateProgram(newProgram *types.VolumeDiscountProgram) {
e.latestProgramVersion += 1
e.newProgram = newProgram
sort.Slice(e.newProgram.VolumeBenefitTiers, func(i, j int) bool {
return e.newProgram.VolumeBenefitTiers[i].MinimumRunningNotionalTakerVolume.LT(e.newProgram.VolumeBenefitTiers[j].MinimumRunningNotionalTakerVolume)
})
e.newProgram.Version = e.latestProgramVersion
}
func (e *Engine) HasProgramEnded() bool {
return e.programHasEnded
}
func (e *Engine) VolumeDiscountFactorForParty(party types.PartyID) num.Decimal {
if e.programHasEnded {
return num.DecimalZero()
}
notionalVolume := e.avgVolumePerParty[party]
tiersLen := len(e.currentProgram.VolumeBenefitTiers)
for i := tiersLen - 1; i >= 0; i-- {
tier := e.currentProgram.VolumeBenefitTiers[i]
if notionalVolume.GreaterThanOrEqual(tier.MinimumRunningNotionalTakerVolume.ToDecimal()) {
return tier.VolumeDiscountFactor
}
}
return num.DecimalZero()
}
func (e *Engine) TakerNotionalForParty(party types.PartyID) num.Decimal {
return e.avgVolumePerParty[party]
}
func (e *Engine) applyProgramUpdate(ctx context.Context, startEpochTime time.Time) {
if e.newProgram != nil {
if e.currentProgram != nil {
e.endCurrentProgram()
e.startNewProgram()
e.notifyVolumeDiscountProgramUpdated(ctx)
} else {
e.startNewProgram()
e.notifyVolumeDiscountProgramStarted(ctx)
}
}
// This handles a edge case where the new program ends before the next
// epoch starts. It can happen when the proposal updating the volume discount
// program doesn't specify an end date that is to close to the enactment
// time.
if e.currentProgram != nil && !e.currentProgram.EndOfProgramTimestamp.IsZero() && !e.currentProgram.EndOfProgramTimestamp.After(startEpochTime) {
e.notifyVolumeDiscountProgramEnded(ctx)
e.endCurrentProgram()
}
}
func (e *Engine) endCurrentProgram() {
e.programHasEnded = true
e.currentProgram = nil
}
func (e *Engine) startNewProgram() {
e.programHasEnded = false
e.currentProgram = e.newProgram
e.newProgram = nil
}
func (e *Engine) notifyVolumeDiscountProgramStarted(ctx context.Context) {
e.broker.Send(events.NewVolumeDiscountProgramStartedEvent(ctx, e.currentProgram))
}
func (e *Engine) notifyVolumeDiscountProgramUpdated(ctx context.Context) {
e.broker.Send(events.NewVolumeDiscountProgramUpdatedEvent(ctx, e.currentProgram))
}
func (e *Engine) notifyVolumeDiscountProgramEnded(ctx context.Context) {
e.broker.Send(events.NewVolumeDiscountProgramEndedEvent(ctx, e.currentProgram.Version, e.currentProgram.ID))
}
func (e *Engine) calculatePartiesVolumeForWindow(windowSize int) {
windowSizeAsDecimal := num.DecimalFromInt64(int64(windowSize))
for pi := range e.parties {
total := num.UintZero()
for i := 0; i < windowSize; i++ {
valueForEpoch, ok := e.epochData[(e.epochDataIndex+int(MaximumWindowLength)-i-1)%int(MaximumWindowLength)][pi]
if !ok {
valueForEpoch = num.UintZero()
}
total.AddSum(valueForEpoch)
}
e.avgVolumePerParty[pi] = total.ToDecimal().Div(windowSizeAsDecimal)
}
}
func (e *Engine) updateNotionalVolumeForEpoch() {
e.epochData[e.epochDataIndex] = e.marketActivityTracker.NotionalTakerVolumeForAllParties()
for pi := range e.epochData[e.epochDataIndex] {
e.parties[pi] = struct{}{}
}
e.epochDataIndex = (e.epochDataIndex + 1) % int(MaximumWindowLength)
}