-
Notifications
You must be signed in to change notification settings - Fork 19
/
amendments.go
120 lines (100 loc) · 3.7 KB
/
amendments.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
// 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 liquidity
import (
"context"
"errors"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
)
var ErrPartyHaveNoLiquidityProvision = errors.New("party have no liquidity provision")
func (e *Engine) AmendLiquidityProvision(
ctx context.Context,
lpa *types.LiquidityProvisionAmendment,
party string,
isCancel bool,
) (bool, error) {
if err := e.CanAmend(lpa, party, !isCancel); err != nil {
return false, err
}
// LP exists, checked in the previous func.
lp, _ := e.provisions.Get(party)
if lp == nil {
lp, _ = e.pendingProvisions.Get(party)
}
// If we are cancelling the LP, preserve the reference field
if lpa.CommitmentAmount.IsZero() {
lpa.Reference = lp.Reference
}
updatedLp := e.createAmendedProvision(lp, lpa)
// add to pending provision since the change in CommitmentAmount should be reflected at the beginning of next epoch
// if it's not opening auction.
if lp.CommitmentAmount.NEQ(lpa.CommitmentAmount) && !e.auctionState.IsOpeningAuction() {
e.pendingProvisions.Set(updatedLp)
e.broker.Send(events.NewLiquidityProvisionEvent(ctx, updatedLp))
return false, nil
}
// cancel immediately during opening auction.
if e.auctionState.IsOpeningAuction() && isCancel {
if err := e.CancelLiquidityProvision(ctx, party); err != nil {
return false, err
}
return true, nil
}
// update immediately since either the commitment amount has not changed.
updatedLp.Status = types.LiquidityProvisionStatusActive
e.broker.Send(events.NewLiquidityProvisionEvent(ctx, updatedLp))
e.provisions.Set(party, updatedLp)
return true, nil
}
func (e *Engine) createAmendedProvision(
currentProvision *types.LiquidityProvision,
amendment *types.LiquidityProvisionAmendment,
) *types.LiquidityProvision {
return &types.LiquidityProvision{
ID: currentProvision.ID,
MarketID: currentProvision.MarketID,
Party: currentProvision.Party,
CreatedAt: currentProvision.CreatedAt,
Status: types.LiquidityProvisionStatusPending,
Fee: amendment.Fee,
Reference: amendment.Reference,
Version: currentProvision.Version + 1,
CommitmentAmount: amendment.CommitmentAmount.Clone(),
UpdatedAt: e.timeService.GetTimeNow().UnixNano(),
}
}
func (e *Engine) CanAmend(lps *types.LiquidityProvisionAmendment, party string, shouldValidate bool) error {
if !e.IsLiquidityProvider(party) {
return ErrPartyHaveNoLiquidityProvision
}
if !shouldValidate {
return nil
}
if err := e.ValidateLiquidityProvisionAmendment(lps); err != nil {
return err
}
return nil
}
func (e *Engine) ValidateLiquidityProvisionAmendment(lp *types.LiquidityProvisionAmendment) error {
if lp.Fee.IsZero() && (lp.CommitmentAmount == nil || lp.CommitmentAmount.IsZero()) {
return errors.New("empty liquidity provision amendment content")
}
// If orders fee is provided, we need it to be valid
if lp.Fee.IsNegative() || lp.Fee.GreaterThan(e.maxFee) {
return errors.New("invalid liquidity provision fee")
}
return nil
}