-
Notifications
You must be signed in to change notification settings - Fork 182
/
order.go
278 lines (247 loc) · 7.47 KB
/
order.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package types
import (
"encoding/json"
"fmt"
"log"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// nolint
type OrderStatus int
// nolint
const (
Open OrderStatus = iota
Filled
Cancelled
Expired
PartialFilledCancelled
PartialFilledExpired
)
func (p OrderStatus) String() string {
switch p {
case Open:
return "Open"
case Filled:
return "Filled"
case Cancelled:
return "Cancelled"
case Expired:
return "Expired"
case PartialFilledCancelled:
return "PartialFilledCancelled"
case PartialFilledExpired:
return "PartialFilledExpired"
default:
return "Unknown"
}
}
// nolint
const (
OrderStatusOpen = 0
OrderStatusFilled = 1
OrderStatusCancelled = 2
OrderStatusExpired = 3
OrderStatusPartialFilledCancelled = 4
OrderStatusPartialFilledExpired = 5
//OrderStatusPartialFilled = 6
)
// nolint
const (
OrderExtraInfoKeyNewFee = "newFee"
OrderExtraInfoKeyCancelFee = "cancelFee"
OrderExtraInfoKeyExpireFee = "expireFee"
OrderExtraInfoKeyDealFee = "dealFee"
OrderExtraInfoKeyReceiveFee = "receiveFee"
)
// nolint
type Order struct {
TxHash string `json:"txhash"` // txHash of the place order tx
OrderID string `json:"order_id"` // order id
Sender sdk.AccAddress `json:"sender"` // order maker address
Product string `json:"product"` // product for trading pair
Side string `json:"side"` // BUY/SELL
Price sdk.Dec `json:"price"` // price of the order
Quantity sdk.Dec `json:"quantity"` // quantity of the order
Status int64 `json:"status"` // order status, see OrderStatusXXX
FilledAvgPrice sdk.Dec `json:"filled_avg_price"` // filled average price
RemainQuantity sdk.Dec `json:"remain_quantity"` // Remaining quantity of the order
RemainLocked sdk.Dec `json:"remain_locked"` // Remaining locked quantity of token
Timestamp int64 `json:"timestamp"` // created timestamp
OrderExpireBlocks int64 `json:"order_expire_blocks"`
FeePerBlock sdk.SysCoin `json:"fee_per_block"`
ExtraInfo string `json:"extra_info"` // extra info of order in json format
}
// nolint
func NewOrder(txHash string, sender sdk.AccAddress, product, side string, price, quantity sdk.Dec,
timestamp int64, orderExpireBlocks int64, feePerBlock sdk.SysCoin) *Order {
order := &Order{
TxHash: txHash,
Sender: sender,
Product: product,
Side: side,
Price: price,
Quantity: quantity,
Status: OrderStatusOpen,
RemainQuantity: quantity,
Timestamp: timestamp,
OrderExpireBlocks: orderExpireBlocks,
FeePerBlock: feePerBlock,
}
if side == BuyOrder {
order.RemainLocked = price.Mul(quantity)
} else {
order.RemainLocked = quantity
}
return order
}
func (order *Order) String() string {
if orderJSON, err := json.Marshal(order); err != nil {
panic(err)
} else {
return string(orderJSON)
}
}
func (order *Order) setExtraInfo(extra map[string]string) {
if extra != nil {
bz, err := json.Marshal(extra)
if err != nil {
panic(err)
}
order.ExtraInfo = string(bz)
}
}
func (order *Order) getExtraInfo() map[string]string {
extra := make(map[string]string)
if order.ExtraInfo != "" && order.ExtraInfo != "{}" {
if err := json.Unmarshal([]byte(order.ExtraInfo), &extra); err != nil {
log.Printf("Unmarshal order extra info(%s) failed\n", order.ExtraInfo)
}
}
return extra
}
func (order *Order) setExtraInfoWithKeyValue(key, value string) {
extra := order.getExtraInfo()
extra[key] = value
order.setExtraInfo(extra)
}
// nolint
func (order *Order) GetExtraInfoWithKey(key string) string {
extra := order.getExtraInfo()
if value, ok := extra[key]; ok {
return value
}
return ""
}
// nolint
func (order *Order) RecordOrderNewFee(fee sdk.SysCoins) {
order.setExtraInfoWithKeyValue(OrderExtraInfoKeyNewFee, fee.String())
}
// nolint
func (order *Order) RecordOrderCancelFee(fee sdk.SysCoins) {
order.setExtraInfoWithKeyValue(OrderExtraInfoKeyCancelFee, fee.String())
}
func (order *Order) recordOrderExpireFee(fee sdk.SysCoins) {
order.setExtraInfoWithKeyValue(OrderExtraInfoKeyExpireFee, fee.String())
}
// nolint
func (order *Order) RecordOrderReceiveFee(fee sdk.SysCoins) {
order.setExtraInfoWithKeyValue(OrderExtraInfoKeyReceiveFee, fee.String())
}
// RecordOrderDealFee : An order may have several deals
func (order *Order) RecordOrderDealFee(fee sdk.SysCoins) {
oldValue := order.GetExtraInfoWithKey(OrderExtraInfoKeyDealFee)
if oldValue == "" {
order.setExtraInfoWithKeyValue(OrderExtraInfoKeyDealFee, fee.String())
return
}
oldFee, err := sdk.ParseDecCoins(oldValue)
if err != nil {
log.Println(err)
return
}
newFee := oldFee.Add2(fee)
order.setExtraInfoWithKeyValue(OrderExtraInfoKeyDealFee, newFee.String())
}
// nolint
func (order *Order) Fill(price, fillAmount sdk.Dec) {
filledSum := order.FilledAvgPrice.Mul(order.Quantity.Sub(order.RemainQuantity))
newFilledSum := filledSum.Add(price.Mul(fillAmount))
order.RemainQuantity = order.RemainQuantity.Sub(fillAmount)
order.FilledAvgPrice = newFilledSum.Quo(order.Quantity.Sub(order.RemainQuantity))
if order.Side == BuyOrder {
order.RemainLocked = order.RemainLocked.Sub(price.Mul(fillAmount))
} else {
order.RemainLocked = order.RemainLocked.Sub(fillAmount)
}
if order.RemainQuantity.IsZero() {
order.Status = OrderStatusFilled
}
}
// nolint
func (order *Order) Cancel() {
if order.RemainQuantity.Equal(order.Quantity) {
order.Status = OrderStatusCancelled
} else {
order.Status = OrderStatusPartialFilledCancelled
}
}
// nolint
func (order *Order) Expire() {
if order.RemainQuantity.Equal(order.Quantity) {
order.Status = OrderStatusExpired
} else {
order.Status = OrderStatusPartialFilledExpired
}
}
// NeedLockCoins : when place a new order, we should lock the coins of sender
func (order *Order) NeedLockCoins() sdk.SysCoins {
if order.Side == BuyOrder {
token := strings.Split(order.Product, "_")[1]
amount := order.Price.Mul(order.Quantity)
return sdk.SysCoins{{Denom: token, Amount: amount}}
}
token := strings.Split(order.Product, "_")[0]
amount := order.Quantity
return sdk.SysCoins{{Denom: token, Amount: amount}}
}
// NeedUnlockCoins : when order be cancelled/expired, we should unlock the coins of sender
func (order *Order) NeedUnlockCoins() sdk.SysCoins {
if order.Side == BuyOrder {
token := strings.Split(order.Product, "_")[1]
return sdk.SysCoins{{Denom: token, Amount: order.RemainLocked}}
}
token := strings.Split(order.Product, "_")[0]
return sdk.SysCoins{{Denom: token, Amount: order.RemainLocked}}
}
// nolint
func (order *Order) Unlock() {
order.RemainLocked = sdk.ZeroDec()
}
// nolint
func FormatOrderID(blockHeight, orderNum int64) string {
format := "ID%010d-%d"
if blockHeight > 9999999999 {
format = "ID%d-%d"
}
return fmt.Sprintf(format, blockHeight, orderNum)
}
// nolint
func FormatOrderIDPrefix(blockHeight int64) string {
format := "ID%010d-"
if blockHeight > 9999999999 {
format = "ID%d-"
}
return fmt.Sprintf(format, blockHeight)
}
// nolint
func GetBlockHeightFromOrderID(orderID string) int64 {
var blockHeight int64
var id int64
format := "ID%d-%d"
_, err := fmt.Sscanf(orderID, format, &blockHeight, &id)
if err != nil {
log.Println(err)
return 0
}
return blockHeight
}