/
orderbook.go
719 lines (567 loc) · 17.9 KB
/
orderbook.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package common
import (
"fmt"
"github.com/novaprotocolio/sdk-backend/utils"
"github.com/cevaris/ordered_map"
"github.com/labstack/gommon/log"
"github.com/petar/GoLLRB/llrb"
"github.com/shopspring/decimal"
"strings"
"sync"
"time"
)
type OrderbookEvent struct {
Side string
OrderID string
Price decimal.Decimal
Amount decimal.Decimal
}
type OrderbookPlugin func(event *OrderbookEvent)
type IOrderbook interface {
InsertOrder(*MemoryOrder)
RemoveOrder(*MemoryOrder)
ChangeOrder(*MemoryOrder, decimal.Decimal)
UsePlugin(plugin OrderbookPlugin)
SnapshotV2() *SnapshotV2
CanMatch(*MemoryOrder) bool
MatchOrder(*MemoryOrder, int) *MatchResult
ExecuteMatch(*MemoryOrder, int) *MatchResult
}
type (
MatchResult struct {
TakerOrder *MemoryOrder
TakerOrderIsDone bool
MatchItems []*MatchItem
TakerOrderLeftAmount decimal.Decimal
OrderbookActivities []WebSocketMessage
}
MatchItem struct {
MakerOrder *MemoryOrder
MakerOrderIsDone bool
MatchedAmount decimal.Decimal
MatchShouldBeCanceled bool
}
MemoryOrder struct {
ID string `json:"id"`
MarketID string `json:"marketID"`
Price decimal.Decimal `json:"price"`
Amount decimal.Decimal `json:"amount"`
Side string `json:"side"`
Type string `json:"type"`
Trader string `json:"trader"`
GasFeeAmount decimal.Decimal `json:"gasFeeAmount"`
MakerFeeRate decimal.Decimal `json:"makerFeeRate"`
TakerFeeRate decimal.Decimal `json:"takerFeeRate"`
}
SnapshotV2 struct {
Sequence uint64 `json:"sequence"`
Bids [][2]string `json:"bids"`
Asks [][2]string `json:"asks"`
}
)
func (order *MemoryOrder) QuoteTokenSymbol() string {
parts := strings.Split(order.MarketID, "-")
if len(parts) == 2 {
return parts[0]
} else {
return "unknown"
}
}
func (order *MemoryOrder) BaseTokenSymbol() string {
parts := strings.Split(order.MarketID, "-")
if len(parts) == 2 {
return parts[1]
} else {
return "unknown"
}
}
func (matchResult *MatchResult) QuoteTokenTotalMatchedAmt() decimal.Decimal {
quoteTokenAmt := decimal.Zero
for _, item := range matchResult.MatchItems {
quoteTokenAmt = quoteTokenAmt.Add(item.MatchedAmount.Mul(item.MakerOrder.Price))
}
return quoteTokenAmt
}
func (matchResult *MatchResult) TakerTradeFeeInQuoteToken() decimal.Decimal {
return matchResult.QuoteTokenTotalMatchedAmt().Mul(matchResult.TakerOrder.TakerFeeRate)
}
func (matchResult *MatchResult) MakerTradeFeeInQuoteToken() (sum decimal.Decimal) {
for _, item := range matchResult.MatchItems {
sum = sum.Add(item.MatchedAmount.Mul(item.MakerOrder.Price).Mul(item.MakerOrder.MakerFeeRate))
}
return
}
func (matchResult *MatchResult) BaseTokenTotalMatchedAmtWithoutCanceledMatch() decimal.Decimal {
baseTokenAmt := decimal.Zero
for _, item := range matchResult.MatchItems {
if !item.MatchShouldBeCanceled {
baseTokenAmt = baseTokenAmt.Add(item.MatchedAmount)
}
}
return baseTokenAmt
}
func (matchResult *MatchResult) SumOfGasOfMakerOrders() decimal.Decimal {
sum := decimal.Zero
for _, item := range matchResult.MatchItems {
sum = sum.Add(item.MakerOrder.GasFeeAmount)
}
return sum
}
func (matchResult MatchResult) ExistMatchToBeExecuted() bool {
for _, match := range matchResult.MatchItems {
if !match.MatchShouldBeCanceled {
return true
}
}
return false
}
type priceLevel struct {
price decimal.Decimal
totalAmount decimal.Decimal
orderMap *ordered_map.OrderedMap
}
func newPriceLevel(price decimal.Decimal) *priceLevel {
return &priceLevel{
price: price,
totalAmount: decimal.Zero,
orderMap: ordered_map.NewOrderedMap(),
}
}
func (p *priceLevel) Len() int {
return p.orderMap.Len()
}
func (p *priceLevel) InsertOrder(order *MemoryOrder) {
log.Debug("InsertOrder:", order.ID)
if _, ok := p.orderMap.Get(order.ID); ok {
panic(fmt.Errorf("can't add order which is already in this priceLevel. priceLevel: %s, orderID: %s", p.price.String(), order.ID))
}
p.orderMap.Set(order.ID, order)
p.totalAmount = p.totalAmount.Add(order.Amount)
}
func (p *priceLevel) RemoveOrder(o *MemoryOrder) {
orderItem, ok := p.orderMap.Get(o.ID)
if !ok {
panic(fmt.Errorf("can't remove order which is not in this priceLevel. priceLevel: %s", p.price.String()))
}
order := orderItem.(*MemoryOrder)
p.orderMap.Delete(order.ID)
p.totalAmount = p.totalAmount.Sub(order.Amount)
}
func (p *priceLevel) GetOrder(id string) (order *MemoryOrder, exist bool) {
orderItem, exist := p.orderMap.Get(id)
if !exist {
return nil, exist
}
return orderItem.(*MemoryOrder), exist
}
func (p *priceLevel) ChangeOrder(o *MemoryOrder, changeAmount decimal.Decimal) {
_, ok := p.orderMap.Get(o.ID)
if !ok {
panic(fmt.Errorf("can't remove order which is not in this priceLevel. priceLevel: %s", p.price.String()))
}
p.totalAmount = p.totalAmount.Add(changeAmount)
}
func (p *priceLevel) Less(item llrb.Item) bool {
another := item.(*priceLevel)
return p.price.LessThan(another.price)
}
// Orderbook ...
type Orderbook struct {
market string
plugins []OrderbookPlugin
bidsTree *llrb.LLRB
asksTree *llrb.LLRB
lock sync.RWMutex
Sequence uint64
}
// NewOrderbook return a new book
func NewOrderbook(market string) *Orderbook {
book := &Orderbook{
plugins: make([]OrderbookPlugin, 0, 3),
market: market,
bidsTree: llrb.New(),
asksTree: llrb.New(),
}
return book
}
func (book *Orderbook) SnapshotV2() *SnapshotV2 {
//startTime := time.Now()
book.lock.RLock()
defer book.lock.RUnlock()
//utils.Debug("== cost in lock, Snapshot : %f", float64(time.Since(startTime))/1000000)
bids := make([][2]string, 0, 0)
asks := make([][2]string, 0, 0)
asyncWaitGroup := sync.WaitGroup{}
asyncWaitGroup.Add(1)
go func() {
book.asksTree.AscendGreaterOrEqual(newPriceLevel(decimal.Zero), func(i llrb.Item) bool {
pl := i.(*priceLevel)
asks = append(asks, [2]string{pl.price.String(), pl.totalAmount.String()})
return true
})
asyncWaitGroup.Done()
}()
asyncWaitGroup.Add(1)
go func() {
book.bidsTree.DescendLessOrEqual(newPriceLevel(decimal.New(1, 99)), func(i llrb.Item) bool {
pl := i.(*priceLevel)
bids = append(bids, [2]string{pl.price.String(), pl.totalAmount.String()})
return true
})
asyncWaitGroup.Done()
}()
asyncWaitGroup.Wait()
res := &SnapshotV2{
Bids: bids,
Asks: asks,
}
//utils.Debugf("== cost in lock read, Snapshot : %f", float64(time.Since(startTime))/1000000)
return res
}
func (book *Orderbook) InsertOrder(order *MemoryOrder) *OrderbookEvent {
startTime := time.Now().UTC()
book.lock.Lock()
defer book.lock.Unlock()
log.Debug("cost in lock, InsertOrder :", order.ID, float64(time.Since(startTime))/1000000)
var tree *llrb.LLRB
if order.Side == "sell" {
tree = book.asksTree
} else {
tree = book.bidsTree
}
price := tree.Get(newPriceLevel(order.Price))
if price == nil {
price = newPriceLevel(order.Price)
tree.InsertNoReplace(price)
}
price.(*priceLevel).InsertOrder(order)
orderBookEvent := &OrderbookEvent{
OrderID: order.ID,
Side: order.Side,
Amount: order.Amount,
Price: order.Price,
}
book.RunPlugins(orderBookEvent)
return orderBookEvent
}
func (book *Orderbook) RemoveOrder(order *MemoryOrder) *OrderbookEvent {
book.lock.Lock()
defer book.lock.Unlock()
var tree *llrb.LLRB
if order.Side == "sell" {
tree = book.asksTree
} else {
tree = book.bidsTree
}
// log
plItem := tree.Get(newPriceLevel(order.Price))
if plItem == nil {
log.Infof("plItem is nil when RemoveOrder")
return nil
}
price := plItem.(*priceLevel)
if price == nil {
panic(fmt.Sprintf("pl is nil when RemoveOrder, book: %s, order: %+v", book.market, order))
}
price.RemoveOrder(order)
if price.Len() <= 0 {
tree.Delete(price)
}
event := &OrderbookEvent{
OrderID: order.ID,
Side: order.Side,
Amount: order.Amount.Mul(decimal.New(-1, 0)),
Price: order.Price,
}
book.RunPlugins(event)
return event
}
func (book *Orderbook) ChangeOrder(order *MemoryOrder, changeAmount decimal.Decimal) *OrderbookEvent {
book.lock.Lock()
defer book.lock.Unlock()
var tree *llrb.LLRB
if order.Side == "sell" {
tree = book.asksTree
} else {
tree = book.bidsTree
}
price := tree.Get(newPriceLevel(order.Price))
if price == nil {
fmt.Println("book snapshot:", book.SnapshotV2())
panic(fmt.Sprintf("can't change order which is not in this orderbook. book: %s, order: %+v", book.market, order))
}
price.(*priceLevel).ChangeOrder(order, changeAmount)
event := &OrderbookEvent{
OrderID: order.ID,
Side: order.Side,
Amount: changeAmount,
Price: order.Price,
}
book.RunPlugins(event)
return event
}
func (book *Orderbook) UsePlugin(plugin OrderbookPlugin) {
book.plugins = append(book.plugins, plugin)
}
func (book *Orderbook) RunPlugins(event *OrderbookEvent) {
for _, plugin := range book.plugins {
plugin(event)
}
}
func (book *Orderbook) GetOrder(id string, side string, price decimal.Decimal) (*MemoryOrder, bool) {
book.lock.Lock()
defer book.lock.Unlock()
var tree *llrb.LLRB
if side == "sell" {
tree = book.asksTree
} else {
tree = book.bidsTree
}
pl := tree.Get(newPriceLevel(price))
if pl == nil {
return nil, false
}
return pl.(*priceLevel).GetOrder(id)
}
// MaxBid ...
func (book *Orderbook) MaxBid() *decimal.Decimal {
book.lock.Lock()
defer book.lock.Unlock()
maxItem := book.bidsTree.Max()
if maxItem != nil {
return &maxItem.(*priceLevel).price
}
return nil
}
// MinAsk ...
func (book *Orderbook) MinAsk() *decimal.Decimal {
book.lock.Lock()
defer book.lock.Unlock()
minItem := book.asksTree.Min()
if minItem != nil {
return &minItem.(*priceLevel).price
}
return nil
}
func (book *Orderbook) CanMatch(order *MemoryOrder) bool {
if strings.EqualFold("buy", order.Side) {
minItem := book.asksTree.Min()
if minItem == nil {
return false
}
if order.Price.GreaterThanOrEqual(minItem.(*priceLevel).price) {
return true
}
return false
} else {
maxItem := book.bidsTree.Max()
if maxItem == nil {
return false
}
if order.Price.LessThanOrEqual(maxItem.(*priceLevel).price) {
return true
}
return false
}
}
// return matching orders in book
// will NOT modify the order book
//
// amt is quoteCurrency when order is MarketID Buy Order
// all other amount is baseCurrencyAmt
func (book *Orderbook) MatchOrder(takerOrder *MemoryOrder, marketAmountDecimals int) *MatchResult {
book.lock.Lock()
defer book.lock.Unlock()
matchedResult := make([]*MatchItem, 0)
totalMatchedAmount := decimal.NewFromFloat(0)
leftAmount := takerOrder.Amount
// This function will be called multi times
// Return false to break the loop
limitOrderIterator := func(i llrb.Item) bool {
pl := i.(*priceLevel)
if takerOrder.Side == "buy" && pl.price.GreaterThan(takerOrder.Price) {
return false
} else if takerOrder.Side == "sell" && pl.price.LessThan(takerOrder.Price) {
return false
}
iter := pl.orderMap.IterFunc()
for kv, ok := iter(); ok; kv, ok = iter() {
if leftAmount.LessThanOrEqual(decimal.Zero) {
break
}
bookOrder := kv.Value.(*MemoryOrder)
if leftAmount.GreaterThanOrEqual(bookOrder.Amount) {
matchedAmount := bookOrder.Amount
matchedItem := &MatchItem{
MatchedAmount: matchedAmount,
MakerOrder: bookOrder,
}
matchedResult = append(matchedResult, matchedItem)
totalMatchedAmount = totalMatchedAmount.Add(matchedAmount)
leftAmount = leftAmount.Sub(matchedAmount)
} else {
eatAmount := leftAmount
matchedItem := &MatchItem{
MatchedAmount: eatAmount,
MakerOrder: bookOrder,
}
matchedResult = append(matchedResult, matchedItem)
totalMatchedAmount = totalMatchedAmount.Add(eatAmount)
leftAmount = decimal.Zero
}
}
return leftAmount.GreaterThan(decimal.Zero)
}
marketOrderIterator := func(i llrb.Item) bool {
pl := i.(*priceLevel)
iter := pl.orderMap.IterFunc()
for kv, ok := iter(); ok; kv, ok = iter() {
// break when no leftAmount
if leftAmount.LessThanOrEqual(decimal.Zero) {
return false
}
// for marketOrder with price limit
if takerOrder.Price.GreaterThan(decimal.Zero) {
if takerOrder.Side == "buy" && pl.price.GreaterThan(takerOrder.Price) {
utils.Infof("market buy exit early for price bound: %s", takerOrder.Price)
return false
} else if takerOrder.Side == "sell" && pl.price.LessThan(takerOrder.Price) {
utils.Infof("market sell exit early for price bound: %s", takerOrder.Price)
return false
}
}
memoryOrder := kv.Value.(*MemoryOrder)
matchedItem := &MatchItem{
MakerOrder: memoryOrder,
}
// for market order buy, leftAmount is quoteCurrencyAmount
if takerOrder.Side == "buy" {
//price = wethAmt / hotAmt
makerQuoteCurrencyAmt := memoryOrder.Amount.Mul(memoryOrder.Price)
if leftAmount.GreaterThanOrEqual(makerQuoteCurrencyAmt) {
//can take this whole maker order
matchedItem.MatchedAmount = memoryOrder.Amount
leftAmount = leftAmount.Sub(makerQuoteCurrencyAmt)
} else {
// can take part of this order, round down with marketAmountDecimals
eatBaseCurrencyAmt := leftAmount.DivRound(memoryOrder.Price, int32(marketAmountDecimals)+1).Truncate(int32(marketAmountDecimals))
matchedItem.MatchedAmount = eatBaseCurrencyAmt
leftAmount = decimal.Zero
}
} else {
// for sell, leftAmount is baseCurrencyAmount
if leftAmount.GreaterThanOrEqual(memoryOrder.Amount) {
matchedItem.MatchedAmount = memoryOrder.Amount
leftAmount = leftAmount.Sub(memoryOrder.Amount)
} else {
matchedItem.MatchedAmount = leftAmount
leftAmount = decimal.Zero
}
}
matchedResult = append(matchedResult, matchedItem)
utils.Infof("matchedItem.MatchedAmount: %s", matchedItem.MatchedAmount)
totalMatchedAmount = totalMatchedAmount.Add(matchedItem.MatchedAmount)
}
return leftAmount.GreaterThan(decimal.Zero)
}
// decide iterator
var iterator llrb.ItemIterator
if takerOrder.Type == "market" {
iterator = marketOrderIterator
} else {
iterator = limitOrderIterator
}
if takerOrder.Side == "sell" {
book.bidsTree.DescendLessOrEqual(newPriceLevel(decimal.New(1, 99)), iterator)
} else {
book.asksTree.AscendGreaterOrEqual(newPriceLevel(decimal.Zero), iterator)
}
return &MatchResult{
MatchItems: matchedResult,
TakerOrder: takerOrder,
}
}
func (book *Orderbook) ExecuteMatch(takerOrder *MemoryOrder, marketAmountDecimals int) *MatchResult {
result := book.MatchOrder(takerOrder, marketAmountDecimals)
cancelSmallMatchesIfExist(result)
for _, item := range result.MatchItems {
var e *OrderbookEvent
// after match, gasFee is paid
if !item.MatchShouldBeCanceled && item.MatchedAmount.IsPositive() {
item.MakerOrder.GasFeeAmount = decimal.Zero
}
if makerOrderShouldBeRemovedAfterMatch(takerOrder.GasFeeAmount, takerOrder.TakerFeeRate, item) {
e = book.RemoveOrder(item.MakerOrder)
item.MakerOrder.Amount = decimal.Zero
item.MakerOrderIsDone = true
} else {
changeAmt := item.MatchedAmount
e = book.ChangeOrder(item.MakerOrder, changeAmt.Mul(decimal.New(-1, 0)))
item.MakerOrder.Amount = item.MakerOrder.Amount.Sub(changeAmt)
}
msg := OrderbookChangeMessage(book.market, book.Sequence, e.Side, e.Price, e.Amount)
result.OrderbookActivities = append(result.OrderbookActivities, msg)
}
return result
}
// when makerOrder is sell
// one cases when maker order should be removed
// 1. all matched - no remaining amount left
//
// when makerOrder is buy
// two cases when maker order should be removed
// 1. all matched - no remaining amount left
// 2. remaining amount too small
func makerOrderShouldBeRemovedAfterMatch(assumedTakerOrderGasFee, assumedTakerOrderFeeRate decimal.Decimal, item *MatchItem) bool {
remainingAmtInQuote := item.MakerOrder.Amount.Sub(item.MatchedAmount).Mul(item.MakerOrder.Price)
return orderShouldBeRemoved(assumedTakerOrderGasFee, assumedTakerOrderFeeRate, remainingAmtInQuote, item.MakerOrder.Side)
}
func TakerOrderShouldBeRemoved(taker *MemoryOrder) bool {
remainingAmtInQuote := taker.Amount.Mul(taker.Price)
return orderShouldBeRemoved(taker.GasFeeAmount, taker.TakerFeeRate, remainingAmtInQuote, taker.Side)
}
// whether order should be removed depends on it's taker order,
// so we need assumedTakerGasFee & assumedTakerFeeRate
func orderShouldBeRemoved(assumedTakerGasFee, assumedTakerFeeRate, remainingAmtInQuote decimal.Decimal, orderSide string) bool {
if orderSide == "sell" {
return remainingAmtInQuote.LessThanOrEqual(decimal.Zero)
} else {
// take away taker's gas & tradeFee
subtractAmtFromTaker := assumedTakerGasFee.Add(remainingAmtInQuote.Mul(assumedTakerFeeRate))
return remainingAmtInQuote.LessThanOrEqual(decimal.Zero) || remainingAmtInQuote.Sub(subtractAmtFromTaker).IsNegative()
}
}
// small matches should be canceled to avoid transaction revert
func cancelSmallMatchesIfExist(matchResult *MatchResult) (canceledAmtSum decimal.Decimal) {
if matchResult.TakerOrder.Side == "buy" {
// taker buy, every match > gas + tradeFee
for _, matchItem := range matchResult.MatchItems {
tradeAmt := matchItem.MatchedAmount.Mul(matchItem.MakerOrder.Price)
// subtract taker's gas & tradeFee
subtractAmt := matchItem.MakerOrder.GasFeeAmount.Add(matchItem.MakerOrder.MakerFeeRate.Mul(tradeAmt))
if tradeAmt.LessThan(subtractAmt) {
amt := cancelMatch(matchItem)
canceledAmtSum = canceledAmtSum.Add(amt)
}
}
} else {
tradeAmtInQuoteToken := matchResult.QuoteTokenTotalMatchedAmt()
// subtract taker's gas & tradeFee
subtractAmt := matchResult.TakerOrder.GasFeeAmount.Add(matchResult.TakerTradeFeeInQuoteToken())
if tradeAmtInQuoteToken.LessThan(subtractAmt) {
canceledAmtSum = cancelAllMatches(matchResult)
}
}
return
}
func cancelMatch(match *MatchItem) (cancelAmt decimal.Decimal) {
match.MatchShouldBeCanceled = true
return match.MatchedAmount
}
func cancelAllMatches(match *MatchResult) (sum decimal.Decimal) {
for _, item := range match.MatchItems {
amt := cancelMatch(item)
sum = sum.Add(amt)
}
return sum
}