-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain
384 lines (321 loc) · 13.6 KB
/
main
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
import MySQLdb
from dateutil import parser
import datetime
import sys
# function to parse the input
# price update
def parsePriceUpdate(price):
splitedPriceTmp = price.split()
if len(splitedPriceTmp) != 6:
return False
else:
splitedPrice = {}
splitedPrice["timestamp"] = splitedPriceTmp[0]
splitedPrice["source_exchange"] = splitedPriceTmp[1]
splitedPrice["source_currency"] = splitedPriceTmp[2]
splitedPrice["destination_currency"] = splitedPriceTmp[3]
splitedPrice["forward_factor"] = splitedPriceTmp[4]
splitedPrice["backward_factor"] = splitedPriceTmp[5]
return splitedPrice
# main function to process a new price update
def processPriceUpdate(priceUpdate):
try:
parsedPriceUpdate = parsePriceUpdate(priceUpdate)
if parsedPriceUpdate:
# ---insert if not exists / update if it does---
priceExist = checkIfPriceExists(parsedPriceUpdate)
# if price exist update else insert
if not priceExist:
print "Inserting to db.."
insertPriceUpdate(parsedPriceUpdate)
addWeightIfCanCreated(parsedPriceUpdate)
else:
print "Already in db, updating ..."
updatePriceUpdate(priceExist, parsedPriceUpdate)
priceExistRevert = checkIfPriceExistsRevert(parsedPriceUpdate)
# if price exist update else insert
if not priceExistRevert:
print "Inserting to db.."
insertPriceUpdateRevert(parsedPriceUpdate)
else:
print "Already in db, updating ..."
updatePriceUpdateRevert(priceExistRevert, parsedPriceUpdate)
else:
return "Wrong format"
except:
print "Unexpected error:", sys.exc_info()[0]
# add edge and weights in db if possible
def addWeightIfCanCreated(price):
db = getMysqlCursor()
mysqlCur = db.cursor()
mysqlCur.execute(
"""SELECT source_exchange, source_currency FROM frateData.edgeWeights
where source_exchange != %s and source_currency = %s""",
[price['source_exchange'], price['source_currency']])
# gets the number of rows affected by the command executed
row_count = mysqlCur.rowcount
if not row_count == 0:
existingPair = list(mysqlCur.fetchone())
try:
mysqlCur.execute("""INSERT INTO `frateData`.`edgeWeights`
(`source_exchange`, `source_currency`, `destination_exchange`,
`destination_currency`, `edge`, `rate_time_ex`)
VALUES (%s, %s, %s, %s, %s, %s)""",
[price['source_exchange'], price['source_currency'],
existingPair[0],
existingPair[1], 1, datetime.datetime.now()])
db.commit()
except:
db.rollback()
try:
mysqlCur.execute("""INSERT INTO `frateData`.`edgeWeights`
(`source_exchange`, `source_currency`, `destination_exchange`,
`destination_currency`, `edge`, `rate_time_ex`)
VALUES (%s, %s, %s, %s, %s, %s)""",
[existingPair[0], price['source_currency'],
price['source_exchange'],
existingPair[1], 1, datetime.datetime.now()])
db.commit()
except:
db.rollback()
mysqlCur.execute(
"""SELECT source_exchange, destination_currency FROM frateData.edgeWeights
where source_exchange != %s and destination_currency = %s""",
[price['source_exchange'], price['destination_currency']])
# gets the number of rows affected by the command executed
row_count = mysqlCur.rowcount
if not row_count == 0:
existingPair2 = list(mysqlCur.fetchone())
try:
mysqlCur.execute("""INSERT INTO `frateData`.`edgeWeights`
(`source_exchange`, `source_currency`, `destination_exchange`,
`destination_currency`, `edge`, `rate_time_ex`)
VALUES (%s, %s, %s, %s, %s, %s)""",
[price['source_exchange'], price['destination_currency'],
existingPair2[0],
existingPair2[1], 1, datetime.datetime.now()])
db.commit()
except:
print "Unexpected error, insert failed:", sys.exc_info()[0]
db.rollback()
try:
mysqlCur.execute("""INSERT INTO `frateData`.`edgeWeights`
(`source_exchange`, `source_currency`, `destination_exchange`,
`destination_currency`, `edge`, `rate_time_ex`)
VALUES (%s, %s, %s, %s, %s, %s)""",
[existingPair2[0], price['destination_currency'],
price['source_exchange'],
existingPair2[1], 1, datetime.datetime.now()])
db.commit()
except:
print "Unexpected error, insert failed:", sys.exc_info()[0]
db.rollback()
db.close()
# update the price update already in db
def updatePriceUpdate(priceUpdateId, priceUpdateData):
db = getMysqlCursor()
mysqlCur = db.cursor()
try:
mysqlCur.execute("""UPDATE `frateData`.`edgeWeights`
SET `edge`= %s, `rate_time_ex`= %s WHERE `id`= %s;""",
[priceUpdateData['forward_factor'], parser.parse(priceUpdateData['timestamp']), priceUpdateId])
db.commit()
except:
print "Unexpected error, update failed:", sys.exc_info()[0]
db.rollback()
db.close()
# update the revert update
def updatePriceUpdateRevert(priceUpdateId, priceUpdateData):
db = getMysqlCursor()
mysqlCur = db.cursor()
try:
mysqlCur.execute("""UPDATE `frateData`.`edgeWeights`
SET `edge`= %s, `rate_time_ex`= %s WHERE `id`= %s;""",
[priceUpdateData['backward_factor'], parser.parse(priceUpdateData['timestamp']),
priceUpdateId])
db.commit()
except:
print "Unexpected error, update failed:", sys.exc_info()[0]
db.rollback()
db.close()
# check if price update already in db to avoid duplicates
def checkIfPriceExists(price):
db = getMysqlCursor()
mysqlCur = db.cursor()
mysqlCur.execute(
"""SELECT id FROM frateData.edgeWeights
where source_exchange = %s and source_currency = %s and destination_exchange = %s and destination_currency = %s""",
[price['source_exchange'], price['source_currency'], price['source_exchange'], price['destination_currency']])
# gets the number of rows affected by the command executed
row_count = mysqlCur.rowcount
db.close()
if row_count == 0:
return False
else:
id = mysqlCur.fetchone()[0]
return id
# check if price update revert already in db to avoid duplicates
def checkIfPriceExistsRevert(price):
db = getMysqlCursor()
mysqlCur = db.cursor()
mysqlCur.execute(
"""SELECT id FROM frateData.edgeWeights
where source_exchange = %s and source_currency = %s and destination_exchange = %s and destination_currency = %s""",
[price['source_exchange'], price['destination_currency'], price['source_exchange'], price['source_currency']])
# gets the number of rows affected by the command executed
row_count = mysqlCur.rowcount
db.close()
if row_count == 0:
return False
else:
id = mysqlCur.fetchone()[0]
return id
# insert price update in db
def insertPriceUpdate(price):
db = getMysqlCursor()
mysqlCur = db.cursor()
try:
mysqlCur.execute("""INSERT INTO `frateData`.`edgeWeights`
(`source_exchange`, `source_currency`, `destination_exchange`,
`destination_currency`, `edge`, `rate_time_ex`)
VALUES (%s, %s, %s, %s, %s, %s)""",
[price['source_exchange'], price['source_currency'],
price['source_exchange'],
price['destination_currency'], price['forward_factor'],
parser.parse(price['timestamp'])])
db.commit()
except:
print "Unexpected error, insert failed:", sys.exc_info()[0]
db.rollback()
db.close()
# insert revert price update in db
def insertPriceUpdateRevert(price):
db = getMysqlCursor()
mysqlCur = db.cursor()
try:
mysqlCur.execute("""INSERT INTO `frateData`.`edgeWeights`
(`source_exchange`, `source_currency`, `destination_exchange`,
`destination_currency`, `edge`, `rate_time_ex`)
VALUES (%s, %s, %s, %s, %s, %s)""",
[price['source_exchange'], price['destination_currency'],
price['source_exchange'],
price['source_currency'], price['backward_factor'],
parser.parse(price['timestamp'])])
db.commit()
except:
print "Unexpected error, insert failed:", sys.exc_info()[0]
db.rollback()
db.close()
# parse incoming request for rate
def parseRequest(input):
try:
if input.startswith("EXCHANGE_RATE_REQUEST"):
splitedRequestTmp = input.split()
spitedRequest = {}
spitedRequest["source_exchange"] = splitedRequestTmp[1]
spitedRequest["source_currency"] = splitedRequestTmp[2]
spitedRequest["destination_exchange"] = splitedRequestTmp[3]
spitedRequest["destination_currency"] = splitedRequestTmp[4]
return spitedRequest
return False
except:
print "Something is wrong with the request, please ensure that you provided correct format"
# get edges from db
def getEdges():
db = getMysqlCursor()
mysqlCur = db.cursor(MySQLdb.cursors.DictCursor)
mysqlCur.execute("""SELECT * FROM frateData.edgeWeights""")
db.close()
# gets the number of rows affected by the command executed
row_count = mysqlCur.rowcount
if row_count == 0:
return False
else:
weights = mysqlCur.fetchall()
return weights
# What is the best exchange rate for converting source_currency
# on source_exchange into destination_currency on destination_exchange,
# and what trades and transfers need to be made to achieve that rate?
def processRequest(request):
parsedRequest = parseRequest(request)
weights = getEdges()
rate = {}
nextr = {}
V = []
for weight in weights:
# print weight
u = weight['source_exchange'] + weight['source_currency']
v = weight['destination_exchange'] + weight['destination_currency']
V.append(u)
V.append(v)
rate[u, v] = float(weight['edge'])
rate[u, u] = 0
rate[v, v] = 0
nextr[u, v] = v
#Floyd Warshall algorithm
for k in range(0, len(V)):
for i in range(0, len(V)):
for j in range(0, len(V)):
try:
if rate[V[i], V[j]] < rate[V[i], V[k]] * rate[V[k], V[j]]:
rate[V[i], V[j]] = rate[V[i], V[k]] * rate[V[k], V[j]]
nextr[V[i], V[j]] = nextr[V[i], V[k]]
except:
continue
path = []
def findPath(u, v):
try:
if nextr[u, v] == None:
return False
while u != v:
u = nextr[u, v]
path.append(u)
return path
except:
return False
path = findPath(parsedRequest['source_exchange'] + parsedRequest['source_currency'],
parsedRequest['destination_exchange'] + parsedRequest['destination_currency'])
ratef = float(0)
if path:
if len(path) == 1:
ratef = ratef + rate[path[0], path[0]]
else:
for i in range(0, len(path) - 1):
ratef = ratef + rate[path[i], path[i + 1]]
print "BEST_RATES_BEGIN " + parsedRequest['source_exchange'] + " " + parsedRequest['source_currency'] + " " + \
parsedRequest['destination_exchange'] + " " + parsedRequest['destination_currency'] + " " + str(ratef)
for p in path:
print p + "\n"
print "BEST_RATES_END"
else:
print "BEST_RATES_BEGIN " + parsedRequest['source_exchange'] + " " + parsedRequest['source_currency'] + " " + \
parsedRequest['destination_exchange'] + " " + parsedRequest[
'destination_currency'] + " " + 'no possible path fount'
print "BEST_RATES_END"
# main function, get user's input and proceeds accordingly
def main():
try:
request = str(
input("press 1 for price update input \n i.e. '2017-11-01T09:43:23+00:00 GDAX BTC USD 1001.0 0.0008'"
" \n or 2 for exchange rate request \n i.e. "
"'EXCHANGE_RATE_REQUEST KRAKEN BTC GDAX BTC' \n \n"))
if request not in [1, 2, '1', '2']:
print "Sorry I don't understand " + str(request)
exit()
if request in [1, '1']:
price = input("Please enter the price update \n")
processPriceUpdate(price)
elif request in [2, '2']:
exchangeReq = input("Please enter an exchange request \n")
processRequest(exchangeReq)
except:
print "Ooops, something went wrong, please note that your input is given in single quotes"
# returns the mysql cursor - replace config accordingly
def getMysqlCursor():
db = MySQLdb.connect(host="localhost", # your host
user="root", # username
passwd="password", # password
db="frateData") # name of the database
return db
if __name__ == '__main__':
main()