-
Notifications
You must be signed in to change notification settings - Fork 19
/
liquidation.go
147 lines (131 loc) · 3.98 KB
/
liquidation.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
// 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 types
import (
"time"
"code.vegaprotocol.io/vega/libs/num"
vegapb "code.vegaprotocol.io/vega/protos/vega"
snapshot "code.vegaprotocol.io/vega/protos/vega/snapshot/v1"
)
type LiquidationStrategy struct {
DisposalTimeStep time.Duration
DisposalFraction num.Decimal
FullDisposalSize uint64
MaxFractionConsumed num.Decimal
}
type LiquidationNode struct {
MarketID string
NetworkPos int64
NextStep time.Time
Config *LiquidationStrategy
}
func (l *LiquidationNode) isPayload() {}
func (l *LiquidationNode) plToProto() interface{} {
return &snapshot.Payload_Liquidation{
Liquidation: l.IntoProto(),
}
}
func (l *LiquidationNode) Namespace() SnapshotNamespace {
return LiquidationSnapshot
}
func (l *LiquidationNode) Key() string {
return l.MarketID
}
func (l *LiquidationNode) IntoProto() *snapshot.Liquidation {
var cfg *vegapb.LiquidationStrategy
if l.Config != nil {
cfg = l.Config.IntoProto()
}
var ns int64
if !l.NextStep.IsZero() {
ns = l.NextStep.UnixNano()
}
return &snapshot.Liquidation{
MarketId: l.MarketID,
NetworkPos: l.NetworkPos,
NextStep: ns,
Config: cfg,
}
}
func PayloadLiquidationNodeFromProto(p *snapshot.Payload_Liquidation) *LiquidationNode {
node, err := LiquidationSnapshotFromProto(p.Liquidation)
if err != nil {
// @TODO figure out what to do with this error
panic("invalid liquidation snapshot payload: " + err.Error())
}
return node
}
func LiquidationSnapshotFromProto(p *snapshot.Liquidation) (*LiquidationNode, error) {
var s *LiquidationStrategy
if p.Config != nil {
st, err := LiquidationStrategyFromProto(p.Config)
if err != nil {
return nil, err
}
s = st
}
var ns time.Time
if p.NextStep > 0 {
ns = time.Unix(0, p.NextStep)
}
return &LiquidationNode{
MarketID: p.MarketId,
NetworkPos: p.NetworkPos,
NextStep: ns,
Config: s,
}, nil
}
func LiquidationStrategyFromProto(p *vegapb.LiquidationStrategy) (*LiquidationStrategy, error) {
df, err := num.DecimalFromString(p.DisposalFraction)
if err != nil {
return nil, err
}
mfc, err := num.DecimalFromString(p.MaxFractionConsumed)
if err != nil {
return nil, err
}
return &LiquidationStrategy{
DisposalTimeStep: time.Second * time.Duration(p.DisposalTimeStep),
DisposalFraction: df,
FullDisposalSize: p.FullDisposalSize,
MaxFractionConsumed: mfc,
}, nil
}
func (l *LiquidationStrategy) IntoProto() *vegapb.LiquidationStrategy {
return &vegapb.LiquidationStrategy{
DisposalTimeStep: int64(l.DisposalTimeStep / time.Second),
DisposalFraction: l.DisposalFraction.String(),
FullDisposalSize: l.FullDisposalSize,
MaxFractionConsumed: l.MaxFractionConsumed.String(),
}
}
func (l *LiquidationStrategy) DeepClone() *LiquidationStrategy {
cpy := *l
return &cpy
}
func (l *LiquidationStrategy) EQ(l2 *LiquidationStrategy) bool {
// if the memory address is the same, then they are obviously the same
if l == l2 {
return true
}
if l2 == nil {
return false
}
// this should be fine, there's no pointer fields to think about
// but just in case we end up switching the decimal types out
// return *l == *l2
return l.DisposalTimeStep == l2.DisposalTimeStep && l.FullDisposalSize == l2.FullDisposalSize &&
l.DisposalFraction.Equals(l2.DisposalFraction) && l.MaxFractionConsumed.Equals(l2.MaxFractionConsumed)
}