-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathengine.go
156 lines (131 loc) · 3 KB
/
engine.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
package engine
import (
"encoding/json"
"github.com/tomochain/tomox-sdk/errors"
"github.com/tomochain/tomox-sdk/ethereum"
"github.com/tomochain/tomox-sdk/interfaces"
"github.com/tomochain/tomox-sdk/rabbitmq"
"github.com/tomochain/tomox-sdk/types"
"github.com/tomochain/tomox-sdk/utils"
)
// Engine contains daos required for engine to work
type Engine struct {
orderbooks map[string]*OrderBook
rabbitMQConn *rabbitmq.Connection
orderDao interfaces.OrderDao
tradeDao interfaces.TradeDao
pairDao interfaces.PairDao
provider *ethereum.EthereumProvider
}
var logger = utils.Logger
// NewEngine initializes the engine singleton instance
func NewEngine(
rabbitMQConn *rabbitmq.Connection,
orderDao interfaces.OrderDao,
tradeDao interfaces.TradeDao,
pairDao interfaces.PairDao,
provider *ethereum.EthereumProvider,
) *Engine {
pairs, err := pairDao.GetAll()
if err != nil {
panic(err)
}
obs := map[string]*OrderBook{}
for _, p := range pairs {
ob := NewOrderBook(rabbitMQConn, orderDao, tradeDao, p)
obs[p.Code()] = ob
}
engine := &Engine{obs, rabbitMQConn, orderDao, tradeDao, pairDao, provider}
return engine
}
// Provider : implement engine interface
func (e *Engine) Provider() interfaces.EthereumProvider {
return e.provider
}
func (e *Engine) getObs() (map[string]*OrderBook, error) {
pairs, err := e.pairDao.GetAll()
if err != nil {
return nil, err
}
obs := map[string]*OrderBook{}
for _, p := range pairs {
ob := NewOrderBook(e.rabbitMQConn, e.orderDao, e.tradeDao, p)
obs[p.Code()] = ob
}
return obs, nil
}
// HandleOrders parses incoming rabbitmq order messages and redirects them to the appropriate
// engine function
func (e *Engine) HandleOrders(msg *rabbitmq.Message) error {
switch msg.Type {
case "NEW_ORDER":
err := e.handleNewOrder(msg.Data)
if err != nil {
logger.Error(err)
return err
}
case "CANCEL_ORDER":
err := e.handleCancelOrder(msg.Data)
if err != nil {
logger.Error(err)
return err
}
default:
logger.Error("Unknown message", msg)
}
return nil
}
func (e *Engine) handleNewOrder(bytes []byte) error {
o := &types.Order{}
err := json.Unmarshal(bytes, o)
if err != nil {
logger.Error(err)
return err
}
code, err := o.PairCode()
if err != nil {
logger.Error(err)
return err
}
obs, err := e.getObs()
if err != nil {
return errors.New("Orderbook error")
}
ob := obs[code]
if ob == nil {
return errors.New("Orderbook error")
}
err = ob.newOrder(o)
if err != nil {
logger.Error(err)
return err
}
return nil
}
func (e *Engine) handleCancelOrder(bytes []byte) error {
o := &types.Order{}
err := json.Unmarshal(bytes, o)
if err != nil {
logger.Error(err)
return err
}
code, err := o.PairCode()
if err != nil {
logger.Error(err)
return err
}
obs, err := e.getObs()
if err != nil {
return errors.New("Orderbook error")
}
ob := obs[code]
if ob == nil {
return errors.New("Orderbook error")
}
err = ob.cancelOrder(o)
if err != nil {
logger.Error(err)
return err
}
return nil
}