-
Notifications
You must be signed in to change notification settings - Fork 0
/
settle_position.go
142 lines (120 loc) · 3.57 KB
/
settle_position.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
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.ZETA 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 events
import (
"context"
"github.com/zeta-protocol/zeta/libs/num"
eventspb "github.com/zeta-protocol/zeta/protos/zeta/events/v1"
)
type SettlePos struct {
*Base
partyID string
marketID string
positionFactor num.Decimal
price *num.Uint
trades []TradeSettlement
ts int64
}
func NewSettlePositionEvent(ctx context.Context, partyID, marketID string, price *num.Uint, trades []TradeSettlement, ts int64, positionFactor num.Decimal) *SettlePos {
return &SettlePos{
Base: newBase(ctx, SettlePositionEvent),
partyID: partyID,
marketID: marketID,
price: price.Clone(),
trades: trades,
ts: ts,
positionFactor: positionFactor,
}
}
func (s SettlePos) MarketID() string {
return s.marketID
}
func (s SettlePos) IsParty(id string) bool {
return s.partyID == id
}
func (s SettlePos) PartyID() string {
return s.partyID
}
func (s SettlePos) Price() *num.Uint {
return s.price.Clone()
}
func (s SettlePos) Trades() []TradeSettlement {
return s.trades
}
func (s SettlePos) Timestamp() int64 {
return s.ts
}
func (s SettlePos) PositionFactor() num.Decimal {
return s.positionFactor
}
func (s SettlePos) Proto() eventspb.SettlePosition {
ts := make([]*eventspb.TradeSettlement, 0, len(s.trades))
for _, t := range s.trades {
ts = append(ts, &eventspb.TradeSettlement{
Size: t.Size(),
Price: t.Price().String(),
MarketPrice: t.MarketPrice().String(),
})
}
return eventspb.SettlePosition{
MarketId: s.marketID,
PartyId: s.partyID,
Price: s.price.String(),
PositionFactor: s.positionFactor.String(),
TradeSettlements: ts,
}
}
func (s SettlePos) StreamMessage() *eventspb.BusEvent {
p := s.Proto()
busEvent := newBusEventFromBase(s.Base)
busEvent.Event = &eventspb.BusEvent_SettlePosition{
SettlePosition: &p,
}
return busEvent
}
type settlement struct {
SettlementSize int64
SettlementPrice *num.Uint
SettlementMarketPrice *num.Uint
}
func (s settlement) Size() int64 {
return s.SettlementSize
}
func (s settlement) Price() *num.Uint {
return s.SettlementPrice
}
func (s settlement) MarketPrice() *num.Uint {
return s.SettlementMarketPrice
}
func SettlePositionEventFromStream(ctx context.Context, be *eventspb.BusEvent) *SettlePos {
sp := be.GetSettlePosition()
settlements := make([]TradeSettlement, 0, len(sp.TradeSettlements))
for _, ts := range sp.TradeSettlements {
price, _ := num.UintFromString(ts.Price, 10)
marketPrice, _ := num.UintFromString(ts.MarketPrice, 10)
settlements = append(settlements, settlement{
SettlementSize: ts.Size,
SettlementPrice: price,
SettlementMarketPrice: marketPrice,
})
}
spPrice, _ := num.UintFromString(sp.Price, 10)
positionFactor := num.MustDecimalFromString(sp.PositionFactor)
return &SettlePos{
Base: newBaseFromBusEvent(ctx, SettlePositionEvent, be),
partyID: sp.PartyId,
marketID: sp.MarketId,
price: spPrice,
trades: settlements,
positionFactor: positionFactor,
}
}