-
Notifications
You must be signed in to change notification settings - Fork 21
/
functions.py
593 lines (450 loc) · 14.3 KB
/
functions.py
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
from config import *
from model import User, Trade, Dispute, Affiliate, session
import random
import string
from datetime import datetime
import cryptocompare
client = Client(API_KEY, API_SECRET)
accounts = client.get_accounts()
eth_account = accounts.data[4]
btc_account = accounts.data[6]
ltc_account = accounts.data[1]
xrp_account = accounts.data[2]
bch_account = accounts.data[5]
def get_user(msg):
"Returns or creates a new user"
chat = msg.message.chat.id
id = msg.from_user.id
user = session.query(User).filter_by(id=id).first()
if user:
return user
else:
user = User(id=id, chat=chat)
session.add(user)
session.commit()
return user
def get_received_msg(msg):
"Delete This Message"
message_id = msg.message_id
chat = msg.chat
return chat, message_id
def get_coin_price(coin_code, currency_code):
"""
Returning the current btc/eth price for specified currency
"""
data = cryptocompare.get_price(coin_code, currency_code)
return data[coin_code][currency_code]
def generate_id():
"Return unique id"
u_id = ""
lower_case = string.ascii_lowercase
upper_case = string.ascii_uppercase
digits = string.digits
option = lower_case + upper_case + digits
for i in range(10):
u_id += str(random.choice(option))
return u_id
def get_trade(id):
"Return the trade"
try:
trade = session.query(Trade).filter(Trade.id == id).first()
return trade
except:
return "Not Found"
def get_recent_trade(user):
"""
Return a trade matching a seller
"""
trades = session.query(Trade).filter(Trade.seller == user.id)
if trades.count() != 0:
dates = [trade.updated_at for trade in trades]
position = dates.index(max(dates))
return trades[position]
else:
trades = session.query(Trade).filter(Trade.buyer == user.id)
dates = [trade.updated_at for trade in trades]
position = dates.index(max(dates))
return trades[position]
def create_affiliate(user, id):
"Return a newly created affilate object"
check = Affiliate().check_affiliate(id)
if check == None:
affiliate = Affiliate(
id = id,
admin = user,
)
session.add(affiliate)
session.commit()
return affiliate
else:
return "Already Exists"
def get_affiliate(id):
"Returns affiliate"
affiliate = Affiliate().check_affiliate(id)
if affiliate != None:
return affiliate
else:
return None
def add_affiliate_btc(id, wallet):
affiliate = session.query(Affiliate).filter_by(id=id).first()
affiliate.btc_wallet = wallet
session.add(affiliate)
def add_affiliate_eth(id, wallet):
affiliate = session.query(Affiliate).filter_by(id=id).first()
affiliate.eth_wallet = wallet
session.add(affiliate)
# session.commit()
def add_affiliate_ltc(id, wallet):
affiliate = session.query(Affiliate).filter_by(id=id).first()
affiliate.ltc_wallet = wallet
session.add(affiliate)
def add_affiliate_xrp(id, wallet):
affiliate = session.query(Affiliate).filter_by(id=id).first()
affiliate.xrp_wallet = wallet
session.add(affiliate)
def add_affiliate_bch(id, wallet):
affiliate = session.query(Affiliate).filter_by(id=id).first()
affiliate.bch_wallet = wallet
session.add(affiliate)
session.commit()
def open_new_trade(user, currency):
"""
Returns a new trade
"""
user = get_user(msg=user)
affiliate = get_affiliate(user.chat)
if affiliate != None:
affiliate = affiliate.id
trade = Trade(
id = generate_id(),
seller = user.id,
currency = currency,
payment_status = False,
created_at = str(datetime.now()),
updated_at = str(datetime.now()),
is_open = True,
affiliate_id = affiliate,
)
session.add(trade)
session.commit()
def add_coin(user, coin):
"""
Update trade instance with coin preference
"""
trade = get_recent_trade(user)
trade.coin = str(coin)
if coin == "BTC":
trade.receive_address_id = btc_account.create_address().id
elif coin == "ETH":
trade.receive_address_id = eth_account.create_address().id
elif coin == "LTC":
trade.receive_address_id = ltc_account.create_address().id
elif coin == "XRP":
trade.receive_address_id = xrp_account.create_address().id
elif coin == "BCH":
trade.receive_address_id = bch_account.create_address().id
else:
pass
session.add(trade)
def add_price(user, price):
"""
Update trade instance with price of service
"""
trade = get_recent_trade(user)
trade.price = float(price)
session.add(trade)
def add_wallet(user, address):
"""
Update trade instance with wallet for seller
"""
trade = get_recent_trade(user)
trade.wallet = str(address)
trade.updated_at = str(datetime.now())
session.add(trade)
session.commit()
def add_buyer(trade, buyer):
"Add Buyer To Trade"
trade.buyer = buyer.id
trade.updated_at = str(datetime.now())
session.add(trade)
session.commit()
def get_receive_address(trade):
"Return the receive address"
if trade.coin == "BTC":
wallet = btc_account.get_address(trade.receive_address_id).address
elif trade.coin == "ETH":
wallet = eth_account.get_address(trade.receive_address_id).address
elif trade.coin == "LTC":
wallet = ltc_account.get_address(trade.receive_address_id).address
elif trade.coin == "XRP":
wallet = xrp_account.get_address(trade.receive_address_id).address
elif trade.coin == "BCH":
wallet = bch_account.get_address(trade.receive_address_id).address
else:
return "ERROR!"
return wallet
def delete_trade(trade_id):
"Delete Trade"
trade = session.query(Trade).filter(Trade.id == trade_id).delete()
if trade == None:
return "Not Found!"
else:
session.commit()
return "Complete!"
def check_trade(user, trade_id):
"Return trade info"
trade = session.query(Trade).filter(Trade.id == trade_id).first()
if trade == None:
return "Not Found"
elif trade.buyer == trade.seller:
return "Not Permitted"
else:
add_buyer(
trade=trade,
buyer=user
)
return trade
def get_trades(user):
"Retrun list of trades the user is in"
sells = session.query(Trade).filter(Trade.seller == user.id).all()
buys = session.query(Trade).filter(Trade.buyer == user.id).all()
return sells, buys
def confirm_pay(trade):
"Confirm Payment"
trade.payment_status = True
trade.updated_at = str(datetime.now())
session.add(trade)
session.commit()
def check_payment(trade, hash):
"Returns Status Of Payment"
try:
tx = blockexplorer.get_tx(hash)
#Check if it is the same
if trade.coin == "BTC":
transaction_hash = btc_account.get_address_transactions(trade.receive_address_id).data[-1].network.hash
elif trade.coin == "ETH":
transaction_hash = eth_account.get_address_transactions(trade.receive_address_id).data[-1].network.hash
elif trade.coin == "LTC":
transaction_hash = ltc_account.get_address_transactions(trade.receive_address_id).data[-1].network.hash
elif trade.coin == "XRP":
transaction_hash = xrp_account.get_address_transactions(trade.receive_address_id).data[-1].network.hash
elif trade.coin == "BCH":
transaction_hash = bch_account.get_address_transactions(trade.receive_address_id).data[-1].network.hash
else:
transaction_hash = ""
if transaction_hash == tx.hash:
confirm_pay(trade)
return "Approved"
except:
return "Pending"
def pay_funds_to_seller(trade):
"Calculate Fees And Send Funds To Seller"
affiliate = Affiliate().check_affiliate(trade.affiliate_id)
coin_price = get_coin_price(
coin_code=trade.coin,
currency_code=trade.currency
)
value = float(trade.price)/float(coin_price)
service_charge = 0.01 * float(value)
fees = 0.0149 * value
pay_price = float(value) - service_charge + fees
price = "%.4f" % pay_price
a_price = "%.4f" % service_charge # Affiliate pay
if trade.coin == "BTC":
btc_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "BTC"
)
if affiliate != None:
btc_account.send_money(
to = affiliate.btc_wallet,
amount = str(a_price),
currency = "BTC"
)
close_trade(trade)
elif trade.coin == "ETH":
eth_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "ETH",
)
if affiliate != None:
eth_account.send_money(
to = affiliate.eth_wallet,
amount = str(a_price),
currency = "ETH"
)
close_trade(trade)
elif trade.coin == "LTC":
ltc_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "LTC",
)
if affiliate != None:
ltc_account.send_money(
to = affiliate.ltc_wallet,
amount = str(a_price),
currency = "LTC"
)
close_trade(trade)
elif trade.coin == "XRP":
xrp_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "XRP",
)
if affiliate != None:
xrp_account.send_money(
to = affiliate.xrp_wallet,
amount = str(a_price),
currency = "XRP"
)
close_trade(trade)
elif trade.coin == "BCH":
bch_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "BCH",
)
if affiliate != None:
bch_account.send_money(
to = affiliate.bch_wallet,
amount = str(a_price),
currency = "BCH"
)
close_trade(trade)
else:
pass
def close_trade(trade):
"Closing The Trade"
trade.is_open = False
trade.updated_at = str(datetime.now())
session.add(trade)
session.commit()
def pay_to_buyer(trade, wallet):
"Send Funds To Buyer"
affiliate = Affiliate().check_affiliate(trade.affiliate_id)
coin_price = get_coin_price(
coin_code=trade.coin,
currency_code=trade.currency
)
value = float(trade.price)/float(coin_price)
service_charge = 0.01 * float(value)
fees = 0.0149 * value
pay_price = float(value) - service_charge + fees
price = "%.4f" % pay_price
a_price = "%.4f" % service_charge # Affiliate pay
if trade.coin == "BTC":
btc_account.send_money(
to = wallet,
amount = str(price),
currency = "BTC"
)
if affiliate != None:
btc_account.send_money(
to = affiliate.btc_wallet,
amount = str(a_price),
currency = "BTC"
)
close_trade(trade)
elif trade.coin == "ETH":
eth_account.send_money(
to = wallet,
amount = str(price),
currency = "ETH",
)
if affiliate != None:
eth_account.send_money(
to = affiliate.eth_wallet,
amount = str(a_price),
currency = "ETH"
)
close_trade(trade)
elif trade.coion == "LTC":
ltc_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "LTC",
)
if affiliate != None:
ltc_account.send_money(
to = affiliate.ltc_wallet,
amount = str(a_price),
currency = "LTC"
)
close_trade(trade)
elif trade.coion == "XRP":
xrp_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "XRP",
)
if affiliate != None:
xrp_account.send_money(
to = affiliate.xrp_wallet,
amount = str(a_price),
currency = "XRP"
)
close_trade(trade)
elif trade.coion == "BCH":
bch_account.send_money(
to = trade.wallet,
amount = str(price),
currency = "BCH",
)
if affiliate != None:
bch_account.send_money(
to = affiliate.bch_wallet,
amount = str(a_price),
currency = "BCH"
)
close_trade(trade)
else:
pass
#######################DISPUTE############################
def get_dispute(id):
"Returns the dispute on attached to this user"
user_id = id
dispute = session.query(Dispute).filter(Dispute.user == user_id)
if dispute == None:
return "No Dispute"
elif dispute.count() >= 1:
return dispute[-1]
else:
return dispute
def get_dispute_by_id(id):
"Return The Dispute By ID"
dispute = session.query(Dispute).filter(Dispute.id == id).first()
return dispute
def create_dispute(user, trade):
"Returns a newly created disput to a trade"
dispute = Dispute(
id = generate_id(),
user = user.id,
created_on = str(datetime.now()),
trade = trade,
)
trade.dispute.append(dispute)
if user.id == trade.seller and user.id == trade.buyer:
dispute.is_buyer = True
dispute.is_seller = True
elif user.id != trade.seller and user.id == trade.buyer:
dispute.is_buyer = True
dispute.is_seller = False
elif user.id == trade.seller and user.id != trade.buyer:
dispute.is_buyer = False
dispute.is_seller = True
else:
dispute.is_seller = False
dispute.is_buyer = False
session.add(dispute)
session.add(trade)
session.commit()
return dispute
def add_complaint(dispute, text):
"Add Complaint Message"
dispute.complaint = text
session.add(dispute)
session.commit()