-
Notifications
You must be signed in to change notification settings - Fork 19
/
parties_should_have_the_following_margin_levels.go
95 lines (85 loc) · 2.62 KB
/
parties_should_have_the_following_margin_levels.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
// 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 steps
import (
"fmt"
"github.com/cucumber/godog"
"code.vegaprotocol.io/vega/core/integration/stubs"
types "code.vegaprotocol.io/vega/protos/vega"
)
func ThePartiesShouldHaveTheFollowingMarginLevels(
broker *stubs.BrokerStub,
table *godog.Table,
) error {
for _, row := range parseExpectedMarginsTable(table) {
partyID := row.MustStr("party")
marketID := row.MustStr("market id")
maintenance := row.MustU64("maintenance")
search := row.MustU64("search")
initial := row.MustU64("initial")
release := row.MustU64("release")
levels, err := broker.GetMarginByPartyAndMarket(partyID, marketID)
if err != nil {
return errCannotGetMarginLevelsForPartyAndMarket(partyID, marketID, err)
}
var hasError bool
if stringToU64(levels.MaintenanceMargin) != maintenance {
hasError = true
}
if stringToU64(levels.SearchLevel) != search {
hasError = true
}
if stringToU64(levels.InitialMargin) != initial {
hasError = true
}
if stringToU64(levels.CollateralReleaseLevel) != release {
hasError = true
}
if hasError {
return errInvalidMargins(maintenance, search, initial, release, levels, partyID)
}
}
return nil
}
func errCannotGetMarginLevelsForPartyAndMarket(partyID, market string, err error) error {
return fmt.Errorf("couldn't get margin levels for party(%s) and market(%s): %s", partyID, market, err.Error())
}
func errInvalidMargins(
maintenance, search, initial, release uint64,
levels types.MarginLevels,
partyID string,
) error {
return formatDiff(fmt.Sprintf("invalid margins for party \"%s\"", partyID),
map[string]string{
"maintenance": u64ToS(maintenance),
"search": u64ToS(search),
"initial": u64ToS(initial),
"release": u64ToS(release),
},
map[string]string{
"maintenance": levels.MaintenanceMargin,
"search": levels.SearchLevel,
"initial": levels.InitialMargin,
"release": levels.CollateralReleaseLevel,
},
)
}
func parseExpectedMarginsTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"party",
"market id",
"maintenance",
"search",
"initial",
"release",
}, []string{})
}