-
Notifications
You must be signed in to change notification settings - Fork 19
/
margin_levels.go
183 lines (155 loc) · 5.54 KB
/
margin_levels.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package entities
import (
"context"
"encoding/json"
"fmt"
"time"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"code.vegaprotocol.io/vega/protos/vega"
"github.com/shopspring/decimal"
)
type MarginLevels struct {
AccountID AccountID
MaintenanceMargin decimal.Decimal
SearchLevel decimal.Decimal
InitialMargin decimal.Decimal
CollateralReleaseLevel decimal.Decimal
Timestamp time.Time
TxHash TxHash
VegaTime time.Time
}
func MarginLevelsFromProto(ctx context.Context, margin *vega.MarginLevels, accountSource AccountSource, txHash TxHash, vegaTime time.Time) (MarginLevels, error) {
var (
maintenanceMargin, searchLevel, initialMargin, collateralReleaseLevel decimal.Decimal
err error
)
marginAccount, err := GetAccountFromMarginLevel(ctx, margin, accountSource, txHash, vegaTime)
if err != nil {
return MarginLevels{}, fmt.Errorf("failed to obtain account for margin level: %w", err)
}
if maintenanceMargin, err = decimal.NewFromString(margin.MaintenanceMargin); err != nil {
return MarginLevels{}, fmt.Errorf("invalid maintenance margin: %w", err)
}
if searchLevel, err = decimal.NewFromString(margin.SearchLevel); err != nil {
return MarginLevels{}, fmt.Errorf("invalid search level: %w", err)
}
if initialMargin, err = decimal.NewFromString(margin.InitialMargin); err != nil {
return MarginLevels{}, fmt.Errorf("invalid initial margin: %w", err)
}
if collateralReleaseLevel, err = decimal.NewFromString(margin.CollateralReleaseLevel); err != nil {
return MarginLevels{}, fmt.Errorf("invalid collateralReleaseLevel: %w", err)
}
return MarginLevels{
AccountID: marginAccount.ID,
MaintenanceMargin: maintenanceMargin,
SearchLevel: searchLevel,
InitialMargin: initialMargin,
CollateralReleaseLevel: collateralReleaseLevel,
Timestamp: time.Unix(0, vegaTime.UnixNano()),
TxHash: txHash,
VegaTime: vegaTime,
}, nil
}
func GetAccountFromMarginLevel(ctx context.Context, margin *vega.MarginLevels, accountSource AccountSource, txHash TxHash, vegaTime time.Time) (Account, error) {
marginAccount := Account{
ID: "",
PartyID: PartyID(margin.PartyId),
AssetID: AssetID(margin.Asset),
MarketID: MarketID(margin.MarketId),
Type: vega.AccountType_ACCOUNT_TYPE_MARGIN,
TxHash: txHash,
VegaTime: vegaTime,
}
err := accountSource.Obtain(ctx, &marginAccount)
return marginAccount, err
}
func (ml *MarginLevels) ToProto(ctx context.Context, accountSource AccountSource) (*vega.MarginLevels, error) {
marginAccount, err := accountSource.GetByID(ctx, ml.AccountID)
if err != nil {
return nil, fmt.Errorf("getting from account for transfer proto:%w", err)
}
return &vega.MarginLevels{
MaintenanceMargin: ml.MaintenanceMargin.String(),
SearchLevel: ml.SearchLevel.String(),
InitialMargin: ml.InitialMargin.String(),
CollateralReleaseLevel: ml.CollateralReleaseLevel.String(),
PartyId: marginAccount.PartyID.String(),
MarketId: marginAccount.MarketID.String(),
Asset: marginAccount.AssetID.String(),
Timestamp: ml.Timestamp.UnixNano(),
}, nil
}
func (ml MarginLevels) Cursor() *Cursor {
cursor := MarginCursor{
VegaTime: ml.VegaTime,
AccountID: ml.AccountID,
}
return NewCursor(cursor.String())
}
func (ml MarginLevels) ToProtoEdge(input ...any) (*v2.MarginEdge, error) {
if len(input) != 2 {
return nil, fmt.Errorf("expected account source and context argument")
}
ctx, ok := input[0].(context.Context)
if !ok {
return nil, fmt.Errorf("first argument must be a context.Context, got: %v", input[0])
}
as, ok := input[1].(AccountSource)
if !ok {
return nil, fmt.Errorf("second argument must be an AccountSource, got: %v", input[1])
}
mlProto, err := ml.ToProto(ctx, as)
if err != nil {
return nil, err
}
return &v2.MarginEdge{
Node: mlProto,
Cursor: ml.Cursor().Encode(),
}, nil
}
type MarginLevelsKey struct {
AccountID AccountID
VegaTime time.Time
}
func (ml MarginLevels) Key() MarginLevelsKey {
return MarginLevelsKey{ml.AccountID, ml.VegaTime}
}
func (ml MarginLevels) ToRow() []interface{} {
return []interface{}{
ml.AccountID, ml.Timestamp, ml.MaintenanceMargin,
ml.SearchLevel, ml.InitialMargin, ml.CollateralReleaseLevel, ml.TxHash, ml.VegaTime,
}
}
var MarginLevelsColumns = []string{
"account_id", "timestamp", "maintenance_margin",
"search_level", "initial_margin", "collateral_release_level", "tx_hash", "vega_time",
}
type MarginCursor struct {
VegaTime time.Time
AccountID AccountID
}
func (mc MarginCursor) String() string {
bs, err := json.Marshal(mc)
if err != nil {
// This should never happen
panic(fmt.Errorf("failed to marshal margin cursor: %w", err))
}
return string(bs)
}
func (mc *MarginCursor) Parse(cursorString string) error {
if cursorString == "" {
return nil
}
return json.Unmarshal([]byte(cursorString), mc)
}