-
Notifications
You must be signed in to change notification settings - Fork 19
/
collateral.go
167 lines (155 loc) · 4.94 KB
/
collateral.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
// 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 future
import (
"context"
"errors"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
)
// ErrBondSlashing - just indicates that we had to penalize the party due to insufficient funds, and as such, we have to cancel their LP.
var ErrBondSlashing = errors.New("bond slashing")
func (m *Market) transferMargins(ctx context.Context, risk []events.Risk, closed []events.MarketPosition) error {
if m.as.InAuction() {
return m.transferMarginsAuction(ctx, risk, closed)
}
return m.transferMarginsContinuous(ctx, risk)
}
func (m *Market) transferMarginsAuction(ctx context.Context, risk []events.Risk, distressed []events.MarketPosition) error {
evts := make([]events.Event, 0, len(risk))
mID := m.GetID()
// first, update the margin accounts for all parties who have enough balance
for _, re := range risk {
tr, _, err := m.collateral.MarginUpdateOnOrder(ctx, mID, re)
if err != nil {
// @TODO handle this
return err
}
if len(tr.Entries) > 0 {
evts = append(evts, events.NewLedgerMovements(ctx, []*types.LedgerMovement{tr}))
}
}
m.broker.SendBatch(evts)
rmorders, err := m.matching.RemoveDistressedOrders(distressed)
if err != nil {
return err
}
evts = make([]events.Event, 0, len(rmorders))
for _, o := range rmorders {
// cancel order
o.Status = types.OrderStatusCancelled
o.Reason = types.OrderErrorInsufficientAssetBalance
// create event
evts = append(evts, events.NewOrderEvent(ctx, o))
// remove order from positions
_ = m.position.UnregisterOrder(ctx, o)
}
m.broker.SendBatch(evts)
return nil
}
func (m *Market) transferRecheckMargins(ctx context.Context, risk []events.Risk) {
if len(risk) == 0 {
return
}
mID := m.GetID()
evts := make([]events.Event, 0, len(risk))
for _, r := range risk {
var tr *types.LedgerMovement
responses := make([]*types.LedgerMovement, 0, 1)
tr, closed, err := m.collateral.MarginUpdateOnOrder(ctx, mID, r)
if err != nil {
m.log.Warn("margin recheck failed",
logging.MarketID(m.GetID()),
logging.PartyID(r.Party()),
logging.Error(err))
}
if tr != nil {
responses = append(responses, tr)
}
if closed != nil && !closed.MarginShortFall().IsZero() {
resp, err := m.bondSlashing(ctx, closed)
if err != nil {
m.log.Panic("Bond slashing for non-distressed LP failed",
logging.String("party", closed.Party()),
logging.Error(err),
)
}
responses = append(responses, resp...)
}
if len(responses) > 0 {
evts = append(evts, events.NewLedgerMovements(ctx, responses))
}
}
m.broker.SendBatch(evts)
}
func (m *Market) transferMarginsContinuous(ctx context.Context, risk []events.Risk) error {
if len(risk) > 1 {
return errors.New("transferMarginsContinuous should not be possible when len(risk) > 1")
}
if len(risk) == 0 {
return nil
}
mID := m.GetID()
tr, closed, err := m.collateral.MarginUpdateOnOrder(ctx, mID, risk[0])
if err != nil {
return err
}
// if LP shortfall is not empty, this party will have to pay the LP penalty
responses := make([]*types.LedgerMovement, 0, len(risk))
if tr != nil {
responses = append(responses, tr)
}
// margin shortfall && liquidity provider -> bond slashing
if closed != nil && !closed.MarginShortFall().IsZero() {
// get bond penalty
resp, err := m.bondSlashing(ctx, closed)
if err != nil {
return err
}
responses = append(responses, resp...)
}
if len(responses) > 0 {
m.broker.Send(events.NewLedgerMovements(ctx, responses))
}
return nil
}
func (m *Market) bondSlashing(ctx context.Context, closed ...events.Margin) ([]*types.LedgerMovement, error) {
mID := m.GetID()
ret := make([]*types.LedgerMovement, 0, len(closed))
for _, c := range closed {
if !m.liquidityEngine.IsLiquidityProvider(c.Party()) {
continue
}
penalty, _ := num.UintFromDecimal(
num.DecimalFromUint(c.MarginShortFall()).Mul(m.bondPenaltyFactor).Floor(),
)
resp, err := m.collateral.BondUpdate(ctx, mID, &types.Transfer{
Owner: c.Party(),
Amount: &types.FinancialAmount{
Amount: penalty,
Asset: c.Asset(),
},
Type: types.TransferTypeBondSlashing,
MinAmount: num.UintZero(),
})
if err != nil {
return nil, err
}
ret = append(ret, resp)
}
return ret, nil
}