-
Notifications
You must be signed in to change notification settings - Fork 19
/
liquidity_provision_submission.go
137 lines (114 loc) · 5.18 KB
/
liquidity_provision_submission.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
// 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 commands
import (
"errors"
"math/big"
"strconv"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)
var (
ErrOrderInShapeWithoutReference = errors.New("order in shape without reference")
ErrOrderInShapeWithoutProportion = errors.New("order in shape without a proportion")
ErrOrderInBuySideShapeWithBestAskPrice = errors.New("order in buy side shape with best ask price reference")
ErrOrderInBuySideShapeOffsetInf0 = errors.New("order in buy side shape offset must be >= 0")
ErrOrderInBuySideShapeOffsetInfEq0 = errors.New("order in buy side shape offset must be > 0")
ErrOrderInSellSideShapeOffsetInf0 = errors.New("order in sell shape offset must be >= 0")
ErrOrderInSellSideShapeWithBestBidPrice = errors.New("order in sell side shape with best bid price reference")
ErrOrderInSellSideShapeOffsetInfEq0 = errors.New("order in sell shape offset must be > 0")
)
func CheckLiquidityProvisionSubmission(cmd *commandspb.LiquidityProvisionSubmission) error {
return checkLiquidityProvisionSubmission(cmd).ErrorOrNil()
}
func checkLiquidityProvisionSubmission(cmd *commandspb.LiquidityProvisionSubmission) Errors {
errs := NewErrors()
if cmd == nil {
return errs.FinalAddForProperty("liquidity_provision_submission", ErrIsRequired)
}
if len(cmd.MarketId) <= 0 {
errs.AddForProperty("liquidity_provision_submission.market_id", ErrIsRequired)
} else if !IsVegaID(cmd.MarketId) {
errs.AddForProperty("liquidity_provision_submission.market_id", ErrShouldBeAValidVegaID)
}
if len(cmd.Reference) > ReferenceMaxLen {
errs.AddForProperty("liquidity_provision_submission.reference", ErrReferenceTooLong)
}
// if the commitment amount is 0, then the command should be interpreted as
// a cancellation of the liquidity provision. As a result, the validation
// shouldn't be made on the rest of the field.
// However, since the user might by sending an blank command to probe the
// validation, we want to return as many error message as possible.
// A cancellation is only valid if a market is specified, and the commitment is
// 0. In any case the core will consider that as a cancellation, so we return
// the error that we go from the market id check.
if len(cmd.CommitmentAmount) > 0 {
if commitment, ok := big.NewInt(0).SetString(cmd.CommitmentAmount, 10); !ok {
errs.AddForProperty("liquidity_provision_submission.commitment_amount", ErrNotAValidInteger)
} else if commitment.Cmp(big.NewInt(0)) <= 0 {
return errs.FinalAddForProperty("liquidity_provision_submission.commitment_amount", ErrIsNotValidNumber)
}
} else {
errs.AddForProperty("liquidity_provision_submission.commitment_amount", ErrIsRequired)
}
if len(cmd.Fee) <= 0 {
errs.AddForProperty("liquidity_provision_submission.fee", ErrIsRequired)
} else {
if fee, err := strconv.ParseFloat(cmd.Fee, 64); err != nil {
errs.AddForProperty(
"liquidity_provision_submission.fee",
ErrIsNotValid,
)
} else if fee < 0 {
errs.AddForProperty("liquidity_provision_submission.fee", ErrMustBePositive)
}
}
return errs
}
func CheckLiquidityProvisionCancellation(cmd *commandspb.LiquidityProvisionCancellation) error {
return checkLiquidityProvisionCancellation(cmd).ErrorOrNil()
}
func checkLiquidityProvisionCancellation(cmd *commandspb.LiquidityProvisionCancellation) Errors {
errs := NewErrors()
if cmd == nil {
return errs.FinalAddForProperty("liquidity_provision_cancellation", ErrIsRequired)
}
if len(cmd.MarketId) <= 0 {
return errs.FinalAddForProperty("liquidity_provision_cancellation.market_id", ErrIsRequired)
} else if !IsVegaID(cmd.MarketId) {
errs.AddForProperty("liquidity_provision_cancellation.market_id", ErrShouldBeAValidVegaID)
}
return errs
}
func CheckLiquidityProvisionAmendment(cmd *commandspb.LiquidityProvisionAmendment) error {
return checkLiquidityProvisionAmendment(cmd).ErrorOrNil()
}
func checkLiquidityProvisionAmendment(cmd *commandspb.LiquidityProvisionAmendment) Errors {
errs := NewErrors()
if cmd == nil {
return errs.FinalAddForProperty("liquidity_provision_amendment", ErrIsRequired)
}
if len(cmd.MarketId) <= 0 {
return errs.FinalAddForProperty("liquidity_provision_amendment.market_id", ErrIsRequired)
}
if len(cmd.CommitmentAmount) <= 0 &&
len(cmd.Fee) <= 0 &&
len(cmd.Reference) <= 0 {
return errs.FinalAddForProperty("liquidity_provision_amendment", ErrIsRequired)
}
if len(cmd.Reference) > ReferenceMaxLen {
errs.AddForProperty("liquidity_provision_amendment.reference", ErrReferenceTooLong)
}
return errs
}