-
Notifications
You must be signed in to change notification settings - Fork 19
/
parties_submit_liquidity_provision.go
199 lines (173 loc) · 5.54 KB
/
parties_submit_liquidity_provision.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// 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 (
"context"
"encoding/hex"
"errors"
"fmt"
"sort"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/num"
"github.com/cucumber/godog"
)
type LPUpdate struct {
MarketID string
CommitmentAmount *num.Uint
Fee num.Decimal
Reference string
LpType string
Err string
}
func PartyCancelsTheirLiquidityProvision(exec Execution, marketID, party string) error {
cancel := types.LiquidityProvisionCancellation{
MarketID: marketID,
}
if err := exec.CancelLiquidityProvision(context.Background(), &cancel, party); err != nil {
return errCancelLiquidityProvision(party, marketID, err)
}
return nil
}
func PartiesSubmitLiquidityProvision(exec Execution, table *godog.Table) error {
lps := map[string]*LPUpdate{}
parties := map[string]string{}
keys := []string{}
var errRow ErroneousRow
for _, r := range parseSubmitLiquidityProvisionTable(table) {
row := submitLiquidityProvisionRow{row: r}
if errRow == nil || row.ExpectError() {
errRow = row
}
id := row.ID()
ref := id
if _, ok := lps[id]; !ok {
lp := &LPUpdate{
MarketID: row.MarketID(),
CommitmentAmount: row.CommitmentAmount(),
Fee: row.Fee(),
Reference: ref,
LpType: row.LpType(),
Err: row.Error(),
}
parties[id] = row.Party()
lps[id] = lp
keys = append(keys, id)
}
}
// ensure we always submit in the same order
sort.Strings(keys)
for _, id := range keys {
lp, ok := lps[id]
if !ok {
return errors.New("LP not found")
}
party, ok := parties[id]
if !ok {
return errors.New("party for LP not found")
}
if lp.LpType == "amendment" {
lpa := &types.LiquidityProvisionAmendment{
MarketID: lp.MarketID,
CommitmentAmount: lp.CommitmentAmount,
Fee: lp.Fee,
Reference: lp.Reference,
}
err := exec.AmendLiquidityProvision(context.Background(), lpa, party)
if ceerr := checkExpectedError(errRow, err, errAmendingLiquidityProvision(lpa, party, err)); ceerr != nil {
return ceerr
}
} else if lp.LpType == "submission" {
sub := &types.LiquidityProvisionSubmission{
MarketID: lp.MarketID,
CommitmentAmount: lp.CommitmentAmount,
Fee: lp.Fee,
Reference: lp.Reference,
}
deterministicID := hex.EncodeToString(crypto.Hash([]byte(id + party + lp.MarketID)))
err := exec.SubmitLiquidityProvision(context.Background(), sub, party, id, deterministicID)
if ceerr := checkExpectedError(errRow, err, errSubmittingLiquidityProvision(sub, party, id, err)); ceerr != nil {
return ceerr
}
} else if lp.LpType == "cancellation" {
cancel := types.LiquidityProvisionCancellation{
MarketID: lp.MarketID,
}
err := exec.CancelLiquidityProvision(context.Background(), &cancel, party)
if ceerr := checkExpectedError(errRow, err, errCancelLiquidityProvision(party, lp.MarketID, err)); ceerr != nil {
return ceerr
}
}
}
return nil
}
func errSubmittingLiquidityProvision(lp *types.LiquidityProvisionSubmission, party, id string, err error) error {
return fmt.Errorf("failed to submit [%v] for party %s and id %s: %v", lp, party, id, err)
}
func errCancelLiquidityProvision(party, market string, err error) error {
return fmt.Errorf("failed to cancel LP for party %s on market %s: %v", party, market, err)
}
func errAmendingLiquidityProvision(lp *types.LiquidityProvisionAmendment, party string, err error) error {
return fmt.Errorf("failed to amend [%v] for party %s : %v", lp, party, err)
}
func parseSubmitLiquidityProvisionTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"id",
"party",
"market id",
"commitment amount",
"fee",
"lp type",
}, []string{
"reference",
"error",
})
}
type submitLiquidityProvisionRow struct {
row RowWrapper
}
func (r submitLiquidityProvisionRow) ID() string {
return r.row.MustStr("id")
}
func (r submitLiquidityProvisionRow) Party() string {
return r.row.MustStr("party")
}
func (r submitLiquidityProvisionRow) MarketID() string {
return r.row.MustStr("market id")
}
func (r submitLiquidityProvisionRow) Side() types.Side {
if len(r.row.Str("side")) == 0 {
return types.SideUnspecified
}
return r.row.MustSide("side")
}
func (r submitLiquidityProvisionRow) CommitmentAmount() *num.Uint {
return r.row.MustUint("commitment amount")
}
func (r submitLiquidityProvisionRow) Fee() num.Decimal {
return r.row.MustDecimal("fee")
}
func (r submitLiquidityProvisionRow) LpType() string {
return r.row.MustStr("lp type")
}
func (r submitLiquidityProvisionRow) Reference() string {
return r.row.Str("reference")
}
func (r submitLiquidityProvisionRow) Error() string {
return r.row.Str("error")
}
func (r submitLiquidityProvisionRow) ExpectError() bool {
return r.row.HasColumn("error")
}