-
Notifications
You must be signed in to change notification settings - Fork 19
/
parties_should_have_the_following_position_status.go
138 lines (124 loc) · 3.62 KB
/
parties_should_have_the_following_position_status.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
// 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 steps
import (
"fmt"
"code.vegaprotocol.io/vega/core/integration/stubs"
proto "code.vegaprotocol.io/vega/protos/vega"
"github.com/cucumber/godog"
)
func PartiesShouldHaveTheFollowingPositionStatus(
broker *stubs.BrokerStub,
market string,
table *godog.Table,
) error {
return positionEvents(broker, table, market, false)
}
func PartiesShouldHaveTheFollowingPositionStatusAgg(
broker *stubs.BrokerStub,
market string,
table *godog.Table,
) error {
return positionEvents(broker, table, market, true)
}
func positionEvents(broker *stubs.BrokerStub, table *godog.Table, market string, aggregate bool) error {
orders := broker.GetDistressedOrders()
closed := broker.GetSettleDistressed()
// get a status map by party
partyStatus := map[string]proto.PositionStatus{}
for _, o := range orders {
if o.MarketID() != market {
continue
}
for _, p := range o.Parties() {
partyStatus[p] = proto.PositionStatus_POSITION_STATUS_ORDERS_CLOSED
}
}
for _, c := range closed {
if c.MarketID() != market {
continue
}
partyStatus[c.PartyID()] = proto.PositionStatus_POSITION_STATUS_CLOSED_OUT
}
lossSoc := broker.GetLossSocializationEvents()
partyLSA := map[string]int64{}
for _, le := range lossSoc {
if le.MarketID() != market {
continue
}
amt := le.AmountLost()
party := le.PartyID()
// aggregate if required
if aggregate {
amt += partyLSA[party]
}
partyLSA[party] = amt
}
for _, r := range parsePositionStatusRow(table) {
row := positionStatusRow{row: r}
party := row.Party()
if row.HasStatus() {
exp := row.Status()
// status is not unspecified (ie we expect to see an event with this information
if exp == proto.PositionStatus_POSITION_STATUS_UNSPECIFIED {
continue
}
status, ok := partyStatus[party]
if !ok {
return fmt.Errorf("no status update found for party %s", party)
}
if status != row.Status() {
return fmt.Errorf("expected status %s for party %s, instead got %s", row.Status(), party, status)
}
}
if row.HasAmount() {
exp := row.Amount()
got, ok := partyLSA[party]
if !ok && exp != 0 {
return fmt.Errorf("expected a loss socialisation amount of %d, none found", exp)
}
if got != exp {
return fmt.Errorf("expected a loss socialisation amount of %d, instead saw %d", exp, got)
}
}
}
return nil
}
func parsePositionStatusRow(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"party",
}, []string{
"status",
"amount",
})
}
type positionStatusRow struct {
row RowWrapper
}
func (p positionStatusRow) HasStatus() bool {
return p.row.HasColumn("status")
}
func (p positionStatusRow) HasAmount() bool {
return p.row.HasColumn("amount")
}
func (p positionStatusRow) Party() string {
return p.row.MustStr("party")
}
func (p positionStatusRow) Status() proto.PositionStatus {
return p.row.MustPositionStatus("status")
}
func (p positionStatusRow) Amount() int64 {
return p.row.MustI64("amount")
}