-
Notifications
You must be signed in to change notification settings - Fork 19
/
auction.go
162 lines (142 loc) · 5.13 KB
/
auction.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
// 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 events
import (
"context"
"fmt"
"time"
types "code.vegaprotocol.io/vega/protos/vega"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
)
type Auction struct {
*Base
// marketID for the market creating the auction event
marketID string
// start time in nanoseconds since 1/1/1970 of the current/last auction
auctionStart int64
// stop time in nanoseconds since 1/1/1970 of the current/last auction
auctionStop int64
// is/was this an opening auction
openingAuction bool
// are we leaving the auction (=true) or entering an auction (=false)
leave bool
// what precisely triggered the auction
trigger types.AuctionTrigger
// what component extended the ongoing auction (if any)
extension types.AuctionTrigger
}
// NewAuctionEvent creates a new auction event object.
func NewAuctionEvent(ctx context.Context, marketID string, leave bool, start, stop int64, triggers ...types.AuctionTrigger) *Auction {
if len(triggers) == 0 {
return nil
}
trigger := triggers[0]
opening := trigger == types.AuctionTrigger_AUCTION_TRIGGER_OPENING
e := &Auction{
Base: newBase(ctx, AuctionEvent),
marketID: marketID,
auctionStart: start,
auctionStop: stop,
openingAuction: opening,
leave: leave,
trigger: trigger,
}
if len(triggers) == 2 {
e.extension = triggers[1]
}
return e
}
func (a Auction) MarketID() string {
return a.marketID
}
// Auction returns the action performed (either true=leave auction, or false=entering auction).
func (a Auction) Auction() bool {
return a.leave
}
// MarketEvent - implement market event interface so we can log this event.
func (a Auction) MarketEvent() string {
// is in auction
start := time.Unix(0, a.auctionStart).Format(time.RFC3339Nano)
if a.extension != types.AuctionTrigger_AUCTION_TRIGGER_UNSPECIFIED {
return fmt.Sprintf("Market %s in auction mode (%s) started at %s (extension reason: %s)", a.marketID, a.trigger, start, a.extension)
}
stopT := time.Unix(0, a.auctionStop)
if a.leave {
if a.auctionStop == 0 {
stopT = time.Now()
}
stop := stopT.Format(time.RFC3339Nano)
if a.openingAuction {
return fmt.Sprintf("Market %s left opening auction started at %s at %s (trigger: %s)", a.marketID, start, stop, a.trigger)
}
return fmt.Sprintf("Market %s left auction started at %s at %s (trigger: %s)", a.marketID, start, stop, a.trigger)
}
if a.openingAuction {
// an opening auction will always have a STOP time
stop := stopT.Format(time.RFC3339Nano)
return fmt.Sprintf("Market %s entered opening auction at %s, will close at %s (trigger: %s)", a.marketID, start, stop, a.trigger)
}
if a.auctionStop == 0 {
return fmt.Sprintf("Market %s entered auction mode at %s (trigger: %s)", a.marketID, start, a.trigger)
}
return fmt.Sprintf("Market %s entered auction mode at %s, auction closes at %s (trigger: %s)", a.marketID, start, stopT.Format(time.RFC3339Nano), a.trigger)
}
// Proto wrap event data in a proto message.
func (a Auction) Proto() eventspb.AuctionEvent {
return eventspb.AuctionEvent{
MarketId: a.marketID,
OpeningAuction: a.openingAuction,
Leave: a.leave,
Start: a.auctionStart,
End: a.auctionStop,
Trigger: a.trigger,
ExtensionTrigger: a.extension,
}
}
// StreamMessage returns the BusEvent message for the event stream API.
func (a Auction) StreamMessage() *eventspb.BusEvent {
p := a.Proto()
busEvent := newBusEventFromBase(a.Base)
busEvent.Event = &eventspb.BusEvent_Auction{
Auction: &p,
}
return busEvent
}
// StreamMarketMessage - allows for this event to be streamed as just a market event
// containing just market ID and a string akin to a log message.
func (a Auction) StreamMarketMessage() *eventspb.BusEvent {
busEvent := newBusEventFromBase(a.Base)
busEvent.Type = eventspb.BusEventType_BUS_EVENT_TYPE_MARKET
busEvent.Event = &eventspb.BusEvent_Market{
Market: &eventspb.MarketEvent{
MarketId: a.marketID,
Payload: a.MarketEvent(),
},
}
return busEvent
}
func AuctionEventFromStream(ctx context.Context, be *eventspb.BusEvent) *Auction {
e := &Auction{
Base: newBaseFromBusEvent(ctx, AuctionEvent, be),
marketID: be.GetAuction().MarketId,
auctionStart: be.GetAuction().Start,
auctionStop: be.GetAuction().End,
openingAuction: be.GetAuction().OpeningAuction,
leave: be.GetAuction().Leave,
trigger: be.GetAuction().Trigger,
extension: be.GetAuction().ExtensionTrigger,
}
return e
}