-
Notifications
You must be signed in to change notification settings - Fork 19
/
lp_events.go
134 lines (121 loc) · 3.49 KB
/
lp_events.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
// 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/events"
"code.vegaprotocol.io/vega/core/integration/stubs"
"code.vegaprotocol.io/vega/libs/num"
"github.com/cucumber/godog"
)
func TheFollowingLPEventsShouldBeEmitted(broker *stubs.BrokerStub, table *godog.Table) error {
lpEvts := broker.GetLPEvents()
evtsByPartyID := map[string]map[string][]events.LiquidityProvision{}
for _, e := range lpEvts {
party := e.PartyID()
lpID := e.LiquidityProvision().Reference
m, ok := evtsByPartyID[party]
if !ok {
m = map[string][]events.LiquidityProvision{}
}
s, ok := m[lpID]
if !ok {
s = []events.LiquidityProvision{}
}
m[lpID] = append(s, e)
evtsByPartyID[party] = m
}
for _, row := range parseLPEventTable(table) {
lpe := LPEventWrapper{
row: row,
}
party, id, version, final, amt := lpe.Party(), lpe.ID(), lpe.Version(), lpe.Final(), lpe.CommitmentAmount()
lpIDMap, ok := evtsByPartyID[party]
if !ok {
return fmt.Errorf("no LP events found for party %s", party)
}
evts, ok := lpIDMap[id]
if !ok {
return fmt.Errorf("no LP events found for LP ID %s (party %s)", id, party)
}
if final {
if err := checkLPEvent(evts[len(evts)-1], party, id, amt, version); err != nil {
return err
}
continue
}
// find matching event in slice
var err error
// find matching event
for _, e := range evts {
if err = checkLPEvent(e, party, id, amt, version); err == nil {
// match found, break
break
}
}
if err != nil {
return err
}
}
return nil
}
func checkLPEvent(last events.LiquidityProvision, party, id string, amt *num.Uint, version uint64) error {
if last.LiquidityProvision().Version != version {
return fmt.Errorf("version %d is not the last version for LP %s (party %s), last is %d", version, id, party, last.LiquidityProvision().Version)
}
if amt != nil && last.LiquidityProvision().CommitmentAmount != amt.String() {
return fmt.Errorf("commitment amount was %s, expected %s for last event for LP %s, party %s",
last.LiquidityProvision().CommitmentAmount,
amt,
id,
party,
)
}
return nil
}
type LPEventWrapper struct {
row RowWrapper
}
func parseLPEventTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"party",
"id",
"version",
}, []string{
"commitment amount",
"final",
})
}
func (l LPEventWrapper) Party() string {
return l.row.MustStr("party")
}
func (l LPEventWrapper) ID() string {
return l.row.MustStr("id")
}
func (l LPEventWrapper) Version() uint64 {
return l.row.MustU64("version")
}
func (l LPEventWrapper) CommitmentAmount() *num.Uint {
if !l.row.HasColumn("commitment amount") {
return nil
}
return l.row.MustUint("commitment amount")
}
func (l LPEventWrapper) Final() bool {
if !l.row.HasColumn("final") {
return false
}
return l.row.MustBool("final")
}