-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexchange_API.py
More file actions
107 lines (97 loc) · 3.58 KB
/
exchange_API.py
File metadata and controls
107 lines (97 loc) · 3.58 KB
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
import requests
import json
import time
class memoize_timeout:
def __init__(self, timeout):
self.timeout = timeout
self.cache = {}
def __call__(self, f):
def func(*args, **kwargs):
key = (args, tuple(sorted(kwargs.items())))
v = self.cache.get(key, (0,0))
# print('cache')
if time.time() - v[1] > self.timeout:
# print('updating')
v = self.cache[key] = f(*args, **kwargs), time.time()
return v[0]
return func
@memoize_timeout(10*60)
def convert(fsym, tsyms):
eosish_factor = 1.0
swap_factor = 1.0
revesre_convert_eos = False
revesre_convert_swap = False
allowed = {'WISH', 'USD', 'ETH', 'EUR', 'BTC', 'NEO', 'EOS', 'EOSISH', 'BNB', 'TRX', 'TRONISH', 'USDT', 'WAVES', 'SWAP'}
if fsym == 'EOSISH' or tsyms == 'EOSISH':
eosish_factor = float(
requests.get('https://api.coingecko.com/api/v3/simple/price?ids=eosish&vs_currencies=eos')
.json()['eosish']['eos']
)
#requests.get('https://api.chaince.com/tickers/eosisheos/',
# headers={'accept-version': 'v1'}).json()['price']
#)
print('eosish factor', eosish_factor, flush=True)
if fsym == 'EOSISH':
fsym = 'EOS'
if tsyms == fsym:
return {'EOS': eosish_factor}
else:
tsyms = 'EOS'
if tsyms == fsym:
return {'EOSISH': 1 / eosish_factor}
revesre_convert_eos = True
eosish_factor = 1 / eosish_factor
tronish = False
if tsyms == 'TRONISH':
if fsym == 'TRX':
return {'TRONISH': 0.02}
else:
tsyms = 'TRX'
tronish = True
if fsym == 'TRONISH':
fsym = 'TRX'
answer = json.loads(requests.get(
'http://127.0.0.1:5001/convert?fsym={fsym}&tsyms={tsyms}'.format(
fsym=fsym, tsyms=tsyms)
).content.decode())
answer[tsyms] = answer[tsyms] * 0.02
return answer
if fsym == 'SWAP' or tsyms == 'SWAP':
swap_factor = float(requests.get('https://api.coingecko.com/api/v3/simple/price?ids=swaps-network&vs_currencies=eth')
.json()['swaps-network']['eth']
)
print('swap factor', swap_factor, flush=True)
if fsym == 'SWAP':
fsym = 'ETH'
if tsyms == fsym:
return {'ETH': swap_factor}
else:
tsyms = 'ETH'
if tsyms == fsym:
return {'SWAP': 1 / swap_factor}
revesre_convert_swap = True
swap_factor = 1 / swap_factor
if fsym not in allowed or any([x not in allowed for x in tsyms.split(',')]):
raise Exception('currency not allowed')
# print(fsym, tsyms)
answer = json.loads(requests.get(
'http://127.0.0.1:5001/convert?fsym={fsym}&tsyms={tsyms}'.format(fsym=fsym, tsyms=tsyms)
).content.decode())
# print('currency_proxi answer', answer, flush=True)
if revesre_convert_eos:
answer = {'EOSISH': answer['EOS']}
tsyms = 'EOSISH'
if revesre_convert_swap:
answer = {'SWAP': answer['ETH']}
tsyms = 'SWAP'
if eosish_factor != 1.0:
answer[tsyms] = answer[tsyms] * eosish_factor
if swap_factor != 1.0:
answer[tsyms] = answer[tsyms] * swap_factor
if tronish:
answer['TRONISH'] = answer['TRX'] / 0.02
return answer
def to_wish(curr, amount=1):
return amount * (convert(curr, 'WISH')['WISH'])
def swap_to_wish(amount=1):
return amount * to_wish('SWAP', amount)