forked from HydroProtocol/hydro-sdk-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
154 lines (125 loc) · 3.88 KB
/
message.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
package common
import (
"fmt"
"github.com/shopspring/decimal"
)
// WebsocketMessage is message unit between engine and websocket
// Engine is producer
// Websocket is consumer
const WsTypeOrderChange = "orderChange"
const WsTypeTradeChange = "tradeChange"
const WsTypeLockedBalanceChange = "lockedBalanceChange"
const WsTypeNewMarketTrade = "newMarketTrade"
//const MessageTypeAccount = "account"
//const MessageTypeMarket = "market"
type WebSocketMessage struct {
//MessageType string `json:"message_type"`
ChannelID string `json:"channel_id"`
Payload interface{} `json:"payload"`
}
type WebsocketMarketOrderChangePayload struct {
Side string `json:"side"`
Sequence uint64 `json:"sequence"`
Price string `json:"price"`
Amount string `json:"amount"`
}
type WebsocketLockedBalanceChangePayload struct {
Type string `json:"type"`
Symbol string `json:"symbol"`
Balance decimal.Decimal `json:"balance"`
}
type WebsocketOrderChangePayload struct {
Type string `json:"type"`
Order interface{} `json:"order"`
}
type WebsocketTradeChangePayload struct {
Type string `json:"type"`
Trade interface{} `json:"trade"`
}
type WebsocketMarketNewMarketTradePayload struct {
Type string `json:"type"`
Trade interface{} `json:"trade"`
}
// engine event
const (
EventNewOrder = "EVENT/NEW_ORDER"
EventCancelOrder = "EVENT/EVENT_CANCEL_ORDER"
EventRestartEngine = "EVENT/EVENT_RESTART"
EventConfirmTransaction = "EVENT/EVENT_CONFIRM_TRANSACTION"
EventOpenMarket = "EVENT/EVENT_OPEN_MARKET"
EventCloseMarket = "EVENT/EVENT_CLOSE_MARKET"
)
type Event struct {
Type string `json:"eventType"`
MarketID string `json:"marketID"`
}
type NewOrderEvent struct {
Event
Order string `json:"order"`
}
type CancelOrderEvent struct {
Event
ID string `json:"id"`
Price string `json:"price"`
Side string `json:"side"`
}
type ConfirmTransactionEvent struct {
Event
Hash string `json:"hash"`
Status string `json:"status"`
Timestamp uint64 `json:"timestamp"`
}
// channel
const MarketChannelPrefix = "Market"
const AccountChannelPrefix = "TraderAddress"
func GetAccountChannelID(address string) string {
return fmt.Sprintf("%s#%s", AccountChannelPrefix, address)
}
func GetMarketChannelID(marketID string) string {
return fmt.Sprintf("%s#%s", MarketChannelPrefix, marketID)
}
func OrderBookChangeMessage(marketID string, sequence uint64, side string, price, amount decimal.Decimal) WebSocketMessage {
payload := &WebsocketMarketOrderChangePayload{
Sequence: sequence,
Side: side,
Price: price.String(),
Amount: amount.String(),
}
return marketChannelMessage(marketID, payload)
}
func marketChannelMessage(marketID string, payload interface{}) WebSocketMessage {
return WebSocketMessage{
//MessageType: MessageTypeMarket,
ChannelID: GetMarketChannelID(marketID),
Payload: payload,
}
}
func MessagesForUpdateOrder(order *MemoryOrder) []WebSocketMessage {
updateMsg := orderUpdateMessage(order)
var balanceChangeMsg WebSocketMessage
if order.Side == "buy" {
balanceChangeMsg = lockedBalanceChangeMessage(order.Trader, order.QuoteTokenSymbol())
} else {
balanceChangeMsg = lockedBalanceChangeMessage(order.Trader, order.BaseTokenSymbol())
}
return []WebSocketMessage{updateMsg, balanceChangeMsg}
}
func orderUpdateMessage(order *MemoryOrder) WebSocketMessage {
return accountMessage(order.Trader, &WebsocketOrderChangePayload{
Type: WsTypeOrderChange,
Order: order,
})
}
func lockedBalanceChangeMessage(address, symbol string) WebSocketMessage {
return accountMessage(address, &WebsocketLockedBalanceChangePayload{
Type: WsTypeLockedBalanceChange,
Symbol: symbol,
})
}
func accountMessage(address string, payload interface{}) WebSocketMessage {
return WebSocketMessage{
//MessageType: MessageTypeAccount,
ChannelID: GetAccountChannelID(address),
Payload: payload,
}
}