-
Notifications
You must be signed in to change notification settings - Fork 19
/
distressed_positions.go
174 lines (152 loc) · 4.69 KB
/
distressed_positions.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
// 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 events
import (
"context"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)
// DistressedPositions contains the market and parties that needed to have their orders closed in order
// to maintain their open positions on the market.
type DistressedPositions struct {
*Base
dpIDs map[string]struct{}
spIDs map[string]struct{}
pb eventspb.DistressedPositions
}
func NewDistressedPositionsEvent(ctx context.Context, marketID string, dParties, sParties []string) *DistressedPositions {
dk := make(map[string]struct{}, len(dParties))
sk := make(map[string]struct{}, len(sParties))
ret := &DistressedPositions{
Base: newBase(ctx, DistressedPositionsEvent),
dpIDs: dk,
spIDs: sk,
pb: eventspb.DistressedPositions{
MarketId: marketID,
DistressedParties: make([]string, 0, len(dParties)),
SafeParties: make([]string, 0, len(sParties)),
},
}
ret.AddDistressedParties(dParties...)
ret.AddSafeParties(sParties...)
return ret
}
func (d *DistressedPositions) AddDistressedParties(parties ...string) {
// ensure parties cannot be marked as both safe and distressed
d.rmSafe(parties)
for _, p := range parties {
// party already registered as distressed
if _, ok := d.dpIDs[p]; ok {
continue
}
// mark party as distressed, ensure we don't mark a party as both distressed and no-longer-distressed
d.dpIDs[p] = struct{}{}
d.pb.DistressedParties = append(d.pb.DistressedParties, p)
}
}
func (d *DistressedPositions) AddSafeParties(parties ...string) {
// ensure these parties aren't marked as distressed
d.rmDistressed(parties)
for _, p := range parties {
if _, ok := d.spIDs[p]; ok {
continue
}
d.spIDs[p] = struct{}{}
d.pb.SafeParties = append(d.pb.SafeParties, p)
}
}
func (d DistressedPositions) MarketID() string {
return d.pb.MarketId
}
func (d *DistressedPositions) rmSafe(parties []string) {
for _, p := range parties {
if _, ok := d.spIDs[p]; ok {
delete(d.spIDs, p)
for i := 0; i < len(d.pb.SafeParties); i++ {
if d.pb.SafeParties[i] == p {
d.pb.SafeParties = append(d.pb.SafeParties[:i], d.pb.SafeParties[i+1:]...)
break
}
}
}
}
}
func (d *DistressedPositions) rmDistressed(parties []string) {
for _, p := range parties {
if _, ok := d.dpIDs[p]; ok {
delete(d.dpIDs, p)
for i := 0; i < len(d.pb.DistressedParties); i++ {
if d.pb.DistressedParties[i] == p {
d.pb.DistressedParties = append(d.pb.DistressedParties[:i], d.pb.DistressedParties[i+1:]...)
break
}
}
}
}
}
func (d DistressedPositions) DistressedParties() []string {
return d.pb.DistressedParties
}
func (d DistressedPositions) SafeParties() []string {
return d.pb.SafeParties
}
func (d DistressedPositions) IsMarket(marketID string) bool {
return d.pb.MarketId == marketID
}
func (d DistressedPositions) IsParty(partyID string) bool {
if _, ok := d.dpIDs[partyID]; ok {
return true
}
_, ok := d.spIDs[partyID]
return ok
}
func (d DistressedPositions) IsDistressedParty(p string) bool {
_, ok := d.dpIDs[p]
return ok
}
func (d DistressedPositions) IsSafeParty(p string) bool {
_, ok := d.spIDs[p]
return ok
}
func (d DistressedPositions) Proto() eventspb.DistressedPositions {
return d.pb
}
func (d DistressedPositions) StreamMessage() *eventspb.BusEvent {
busEvent := newBusEventFromBase(d.Base)
cpy := d.pb
busEvent.Event = &eventspb.BusEvent_DistressedPositions{
DistressedPositions: &cpy,
}
return busEvent
}
func (d DistressedPositions) StreamMarketMessage() *eventspb.BusEvent {
return d.StreamMessage()
}
func DistressedPositionsEventFromStream(ctx context.Context, be *eventspb.BusEvent) *DistressedPositions {
m := be.GetDistressedPositions()
dk := make(map[string]struct{}, len(m.DistressedParties))
for _, p := range m.DistressedParties {
dk[p] = struct{}{}
}
sk := make(map[string]struct{}, len(m.SafeParties))
for _, p := range m.SafeParties {
sk[p] = struct{}{}
}
return &DistressedPositions{
Base: newBaseFromBusEvent(ctx, DistressedPositionsEvent, be),
dpIDs: dk,
spIDs: sk,
pb: *m,
}
}