forked from ProofSuite/amp-matching-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
203 lines (179 loc) · 7.7 KB
/
main_test.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
package dex
import (
"testing"
)
// func setupTestingEnvironment() {
// client := NewClient()
// }
func TestOrderbook(t *testing.T) {
// done := make(chan bool)
// engine := NewTradingEngine()
// BTC_USDT := NewPair("BTC", "USDT")
// engine.CreateNewOrderBook(BTC_USDT, done)
// engine.AddOrder(&Order{OrderType: SELL, Id: 1, Price: 50, Amount: 50})
// engine.AddOrder(&Order{OrderType: SELL, Id: 1, Price: 50, Amount: 50})
// engine.AddOrder(&Order{OrderType: SELL, Id: 2, Price: 45, Amount: 25})
// engine.AddOrder(&Order{OrderType: SELL, Id: 3, Price: 45, Amount: 25})
// engine.AddOrder(&Order{OrderType: BUY, Id: 4, Price: 55, Amount: 75})
// engine.CancelOrder(1, BTC_USDT)
// engine.AddOrder(&Order{OrderType: BUY, Id: 5, Price: 55, Amount: 20})
// engine.AddOrder(&Order{OrderType: BUY, Id: 6, Price: 50, Amount: 15})
// engine.AddOrder(&Order{OrderType: SELL, Id: 7, Price: 45, Amount: 25})
// engine.CloseOrderBook(BTC_USDT)
// <-done
// expected := []*Action{
// &Action{AT_SELL, BTC_USDT, 1, 0, 50, 50},
// &Action{AT_SELL, BTC_USDT, 2, 0, 25, 45},
// &Action{AT_SELL, BTC_USDT, 3, 0, 25, 45},
// &Action{AT_BUY, BTC_USDT, 4, 0, 75, 55},
// &Action{AT_PARTIAL_FILLED, BTC_USDT, 4, 2, 25, 45},
// &Action{AT_PARTIAL_FILLED, BTC_USDT, 4, 3, 25, 45},
// &Action{AT_FILLED, BTC_USDT, 4, 1, 25, 50},
// &Action{AT_CANCEL, BTC_USDT, 1, 0, 0, 0},
// &Action{AT_CANCELLED, BTC_USDT, 1, 0, 0, 0},
// &Action{AT_BUY, BTC_USDT, 5, 0, 20, 55},
// &Action{AT_BUY, BTC_USDT, 6, 0, 15, 50},
// &Action{AT_SELL, BTC_USDT, 7, 0, 25, 45},
// &Action{AT_PARTIAL_FILLED, BTC_USDT, 7, 5, 20, 55},
// &Action{AT_FILLED, BTC_USDT, 7, 6, 5, 50},
// &Action{AT_DONE, BTC_USDT, 0, 0, 0, 0},
// }
// logs := engine.orderbooks[BTC_USDT]
// if !reflect.DeepEqual(logs, expected) {
// t.Error("\n\nExpected:\n\n", expected, "\n\nGot:\n\n", logs, "\n\n")
// }
}
// func TestSingleOrderbook(t *testing.T) {
// actions := make(chan *Action)
// done := make(chan bool)
// engine := NewTradingEngine()
// pair := NewPair
// engine.CreateNewOrderBook("ETHEOS", actions)
// log := make([]*Action, 0)
// go func() {
// for {
// action := <-actions
// log = append(log, action)
// if action.actionType == AT_DONE {
// done <- true
// return
// }
// }
// }()
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 1, price: 50, amount: 50})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 2, price: 45, amount: 25})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 3, price: 45, amount: 25})
// engine.AddOrder(&Order{orderType: BUY, symbol: "ETHEOS", id: 4, price: 55, amount: 75})
// engine.CancelOrder(1, "ETHEOS")
// engine.AddOrder(&Order{orderType: BUY, symbol: "ETHEOS", id: 5, price: 55, amount: 20})
// engine.AddOrder(&Order{orderType: BUY, symbol: "ETHEOS", id: 6, price: 50, amount: 15})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 7, price: 45, amount: 25})
// engine.CloseOrderBook("ETHEOS")
// <-done
// expected := []*Action{
// &Action{AT_SELL, "ETHEOS", 1, 0, 50, 50},
// &Action{AT_SELL, "ETHEOS", 2, 0, 25, 45},
// &Action{AT_SELL, "ETHEOS", 3, 0, 25, 45},
// &Action{AT_BUY, "ETHEOS", 4, 0, 75, 55},
// &Action{AT_PARTIAL_FILLED, "ETHEOS", 4, 2, 25, 45},
// &Action{AT_PARTIAL_FILLED, "ETHEOS", 4, 3, 25, 45},
// &Action{AT_FILLED, "ETHEOS", 4, 1, 25, 50},
// &Action{AT_CANCEL, "ETHEOS", 1, 0, 0, 0},
// &Action{AT_CANCELLED, "ETHEOS", 1, 0, 0, 0},
// &Action{AT_BUY, "ETHEOS", 5, 0, 20, 55},
// &Action{AT_BUY, "ETHEOS", 6, 0, 15, 50},
// &Action{AT_SELL, "ETHEOS", 7, 0, 25, 45},
// &Action{AT_PARTIAL_FILLED, "ETHEOS", 7, 5, 20, 55},
// &Action{AT_FILLED, "ETHEOS", 7, 6, 5, 50},
// &Action{AT_DONE, "", 0, 0, 0, 0},
// }
// if !reflect.DeepEqual(log, expected) {
// t.Error("\n\nExpected:\n\n", expected, "\n\nGot:\n\n", log, "\n\n")
// }
// }
// func TestMultipleOrderbook(t *testing.T) {
// var wg sync.WaitGroup
// wg.Add(2)
// actions1 := make(chan *Action)
// actions2 := make(chan *Action)
// engine := NewTradingEngine()
// engine.CreateNewOrderBook("ETHEOS", actions1)
// engine.CreateNewOrderBook("ZRXEOS", actions2)
// log1 := make([]*Action, 0)
// log2 := make([]*Action, 0)
// go func() {
// for {
// select {
// case action := <-actions1:
// log1 = append(log1, action)
// if action.actionType == AT_DONE {
// wg.Done()
// }
// case action := <-actions2:
// log2 = append(log2, action)
// if action.actionType == AT_DONE {
// wg.Done()
// }
// }
// }
// }()
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 1, price: 50, amount: 50})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 2, price: 45, amount: 25})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ZRXEOS", id: 1, price: 50, amount: 50})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ZRXEOS", id: 2, price: 45, amount: 25})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 3, price: 45, amount: 25})
// engine.AddOrder(&Order{orderType: BUY, symbol: "ETHEOS", id: 4, price: 55, amount: 75})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ZRXEOS", id: 3, price: 45, amount: 25})
// engine.AddOrder(&Order{orderType: BUY, symbol: "ZRXEOS", id: 4, price: 55, amount: 75})
// engine.CancelOrder(1, "ETHEOS")
// engine.AddOrder(&Order{orderType: BUY, symbol: "ETHEOS", id: 5, price: 55, amount: 20})
// engine.CancelOrder(1, "ZRXEOS")
// engine.AddOrder(&Order{orderType: BUY, symbol: "ZRXEOS", id: 5, price: 55, amount: 20})
// engine.AddOrder(&Order{orderType: BUY, symbol: "ZRXEOS", id: 6, price: 50, amount: 15})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ZRXEOS", id: 7, price: 45, amount: 25})
// engine.AddOrder(&Order{orderType: BUY, symbol: "ETHEOS", id: 6, price: 50, amount: 15})
// engine.AddOrder(&Order{orderType: SELL, symbol: "ETHEOS", id: 7, price: 45, amount: 25})
// engine.CloseOrderBook("ETHEOS")
// engine.CloseOrderBook("ZRXEOS")
// wg.Wait()
// expected1 := []*Action{
// &Action{AT_SELL, "ETHEOS", 1, 0, 50, 50},
// &Action{AT_SELL, "ETHEOS", 2, 0, 25, 45},
// &Action{AT_SELL, "ETHEOS", 3, 0, 25, 45},
// &Action{AT_BUY, "ETHEOS", 4, 0, 75, 55},
// &Action{AT_PARTIAL_FILLED, "ETHEOS", 4, 2, 25, 45},
// &Action{AT_PARTIAL_FILLED, "ETHEOS", 4, 3, 25, 45},
// &Action{AT_FILLED, "ETHEOS", 4, 1, 25, 50},
// &Action{AT_CANCEL, "ETHEOS", 1, 0, 0, 0},
// &Action{AT_CANCELLED, "ETHEOS", 1, 0, 0, 0},
// &Action{AT_BUY, "ETHEOS", 5, 0, 20, 55},
// &Action{AT_BUY, "ETHEOS", 6, 0, 15, 50},
// &Action{AT_SELL, "ETHEOS", 7, 0, 25, 45},
// &Action{AT_PARTIAL_FILLED, "ETHEOS", 7, 5, 20, 55},
// &Action{AT_FILLED, "ETHEOS", 7, 6, 5, 50},
// &Action{AT_DONE, "", 0, 0, 0, 0},
// }
// expected2 := []*Action{
// &Action{AT_SELL, "ZRXEOS", 1, 0, 50, 50},
// &Action{AT_SELL, "ZRXEOS", 2, 0, 25, 45},
// &Action{AT_SELL, "ZRXEOS", 3, 0, 25, 45},
// &Action{AT_BUY, "ZRXEOS", 4, 0, 75, 55},
// &Action{AT_PARTIAL_FILLED, "ZRXEOS", 4, 2, 25, 45},
// &Action{AT_PARTIAL_FILLED, "ZRXEOS", 4, 3, 25, 45},
// &Action{AT_FILLED, "ZRXEOS", 4, 1, 25, 50},
// &Action{AT_CANCEL, "ZRXEOS", 1, 0, 0, 0},
// &Action{AT_CANCELLED, "ZRXEOS", 1, 0, 0, 0},
// &Action{AT_BUY, "ZRXEOS", 5, 0, 20, 55},
// &Action{AT_BUY, "ZRXEOS", 6, 0, 15, 50},
// &Action{AT_SELL, "ZRXEOS", 7, 0, 25, 45},
// &Action{AT_PARTIAL_FILLED, "ZRXEOS", 7, 5, 20, 55},
// &Action{AT_FILLED, "ZRXEOS", 7, 6, 5, 50},
// &Action{AT_DONE, "", 0, 0, 0, 0},
// }
// if !reflect.DeepEqual(log1, expected1) {
// t.Error("\n\nExpected:\n\n", expected1, "\n\nGot:\n\n", log1, "\n\n")
// }
// if !reflect.DeepEqual(log2, expected2) {
// t.Error("\n\nExpected:\n\n", expected2, "\n\nGot:\n\n", log2, "\n\n")
// }
// }