|
|
@ -16,7 +16,6 @@ import random |
|
|
|
import shutil |
|
|
|
import shutil |
|
|
|
import string |
|
|
|
import string |
|
|
|
import struct |
|
|
|
import struct |
|
|
|
import urllib.request |
|
|
|
|
|
|
|
import hashlib |
|
|
|
import hashlib |
|
|
|
import secrets |
|
|
|
import secrets |
|
|
|
import datetime as dt |
|
|
|
import datetime as dt |
|
|
@ -6397,151 +6396,142 @@ class BasicSwap(BaseApp): |
|
|
|
ticker_from = ci_from.chainparams()['ticker'] |
|
|
|
ticker_from = ci_from.chainparams()['ticker'] |
|
|
|
ticker_to = ci_to.chainparams()['ticker'] |
|
|
|
ticker_to = ci_to.chainparams()['ticker'] |
|
|
|
headers = {'Connection': 'close'} |
|
|
|
headers = {'Connection': 'close'} |
|
|
|
try: |
|
|
|
rv = {} |
|
|
|
self.setConnectionParameters() |
|
|
|
|
|
|
|
rv = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if rate_sources.get('coingecko.com', True): |
|
|
|
if rate_sources.get('coingecko.com', True): |
|
|
|
try: |
|
|
|
try: |
|
|
|
url = 'https://api.coingecko.com/api/v3/simple/price?ids={},{}&vs_currencies=usd,btc'.format(exchange_name_from, exchange_name_to) |
|
|
|
url = 'https://api.coingecko.com/api/v3/simple/price?ids={},{}&vs_currencies=usd,btc'.format(exchange_name_from, exchange_name_to) |
|
|
|
|
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
|
|
|
|
start = time.time() |
|
|
|
|
|
|
|
js = json.loads(self.readURL(url, timeout=10, headers=headers)) |
|
|
|
|
|
|
|
js['time_taken'] = time.time() - start |
|
|
|
|
|
|
|
rate = float(js[exchange_name_from]['usd']) / float(js[exchange_name_to]['usd']) |
|
|
|
|
|
|
|
js['rate_inferred'] = ci_to.format_amount(rate, conv_int=True, r=1) |
|
|
|
|
|
|
|
rv['coingecko'] = js |
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
rv['coingecko_error'] = str(e) |
|
|
|
|
|
|
|
if self.debug: |
|
|
|
|
|
|
|
self.log.error(traceback.format_exc()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if exchange_name_from != name_from: |
|
|
|
|
|
|
|
js[name_from] = js[exchange_name_from] |
|
|
|
|
|
|
|
js.pop(exchange_name_from) |
|
|
|
|
|
|
|
if exchange_name_to != name_to: |
|
|
|
|
|
|
|
js[name_to] = js[exchange_name_to] |
|
|
|
|
|
|
|
js.pop(exchange_name_to) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if rate_sources.get('bittrex.com', True): |
|
|
|
|
|
|
|
bittrex_api_v3 = 'https://api.bittrex.com/v3' |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
exchange_ticker_to = ci_to.getExchangeTicker('bittrex.com') |
|
|
|
|
|
|
|
exchange_ticker_from = ci_from.getExchangeTicker('bittrex.com') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
USDT_coins = (Coins.FIRO,) |
|
|
|
|
|
|
|
# TODO: How to compare USDT pairs with BTC pairs |
|
|
|
|
|
|
|
if ci_from.coin_type() in USDT_coins: |
|
|
|
|
|
|
|
raise ValueError('No BTC pair') |
|
|
|
|
|
|
|
if ci_to.coin_type() in USDT_coins: |
|
|
|
|
|
|
|
raise ValueError('No BTC pair') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ci_from.coin_type() == Coins.BTC: |
|
|
|
|
|
|
|
pair = f'{exchange_ticker_to}-{exchange_ticker_from}' |
|
|
|
|
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
start = time.time() |
|
|
|
start = time.time() |
|
|
|
req = urllib.request.Request(url, headers=headers) |
|
|
|
js = json.loads(self.readURL(url, timeout=10, headers=headers)) |
|
|
|
js = json.loads(urllib.request.urlopen(req, timeout=10).read()) |
|
|
|
|
|
|
|
js['time_taken'] = time.time() - start |
|
|
|
js['time_taken'] = time.time() - start |
|
|
|
rate = float(js[exchange_name_from]['usd']) / float(js[exchange_name_to]['usd']) |
|
|
|
js['pair'] = pair |
|
|
|
js['rate_inferred'] = ci_to.format_amount(rate, conv_int=True, r=1) |
|
|
|
try: |
|
|
|
rv['coingecko'] = js |
|
|
|
rate_inverted = ci_from.make_int(1.0 / float(js['lastTradeRate']), r=1) |
|
|
|
except Exception as e: |
|
|
|
js['rate_inferred'] = ci_to.format_amount(rate_inverted) |
|
|
|
rv['coingecko_error'] = str(e) |
|
|
|
except Exception as e: |
|
|
|
if self.debug: |
|
|
|
self.log.warning('lookupRates error: %s', str(e)) |
|
|
|
self.log.error(traceback.format_exc()) |
|
|
|
js['rate_inferred'] = 'error' |
|
|
|
|
|
|
|
js['from_btc'] = 1.0 |
|
|
|
if exchange_name_from != name_from: |
|
|
|
js['to_btc'] = js['lastTradeRate'] |
|
|
|
js[name_from] = js[exchange_name_from] |
|
|
|
rv['bittrex'] = js |
|
|
|
js.pop(exchange_name_from) |
|
|
|
elif ci_to.coin_type() == Coins.BTC: |
|
|
|
if exchange_name_to != name_to: |
|
|
|
pair = f'{exchange_ticker_from}-{exchange_ticker_to}' |
|
|
|
js[name_to] = js[exchange_name_to] |
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
js.pop(exchange_name_to) |
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
|
|
|
|
start = time.time() |
|
|
|
if rate_sources.get('bittrex.com', True): |
|
|
|
js = json.loads(self.readURL(url, timeout=10, headers=headers)) |
|
|
|
bittrex_api_v3 = 'https://api.bittrex.com/v3' |
|
|
|
js['time_taken'] = time.time() - start |
|
|
|
try: |
|
|
|
js['pair'] = pair |
|
|
|
exchange_ticker_to = ci_to.getExchangeTicker('bittrex.com') |
|
|
|
js['rate_last'] = js['lastTradeRate'] |
|
|
|
exchange_ticker_from = ci_from.getExchangeTicker('bittrex.com') |
|
|
|
js['from_btc'] = js['lastTradeRate'] |
|
|
|
|
|
|
|
js['to_btc'] = 1.0 |
|
|
|
USDT_coins = (Coins.FIRO,) |
|
|
|
rv['bittrex'] = js |
|
|
|
# TODO: How to compare USDT pairs with BTC pairs |
|
|
|
else: |
|
|
|
if ci_from.coin_type() in USDT_coins: |
|
|
|
pair = f'{exchange_ticker_from}-BTC' |
|
|
|
raise ValueError('No BTC pair') |
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
if ci_to.coin_type() in USDT_coins: |
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
raise ValueError('No BTC pair') |
|
|
|
start = time.time() |
|
|
|
|
|
|
|
js_from = json.loads(self.readURL(url, timeout=10, headers=headers)) |
|
|
|
if ci_from.coin_type() == Coins.BTC: |
|
|
|
js_from['time_taken'] = time.time() - start |
|
|
|
pair = f'{exchange_ticker_to}-{exchange_ticker_from}' |
|
|
|
js_from['pair'] = pair |
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
|
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
|
|
|
|
start = time.time() |
|
|
|
|
|
|
|
req = urllib.request.Request(url, headers=headers) |
|
|
|
|
|
|
|
js = json.loads(urllib.request.urlopen(req, timeout=10).read()) |
|
|
|
|
|
|
|
js['time_taken'] = time.time() - start |
|
|
|
|
|
|
|
js['pair'] = pair |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
rate_inverted = ci_from.make_int(1.0 / float(js['lastTradeRate']), r=1) |
|
|
|
|
|
|
|
js['rate_inferred'] = ci_to.format_amount(rate_inverted) |
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
self.log.warning('lookupRates error: %s', str(e)) |
|
|
|
|
|
|
|
js['rate_inferred'] = 'error' |
|
|
|
|
|
|
|
js['from_btc'] = 1.0 |
|
|
|
|
|
|
|
js['to_btc'] = js['lastTradeRate'] |
|
|
|
|
|
|
|
rv['bittrex'] = js |
|
|
|
|
|
|
|
elif ci_to.coin_type() == Coins.BTC: |
|
|
|
|
|
|
|
pair = f'{exchange_ticker_from}-{exchange_ticker_to}' |
|
|
|
|
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
|
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
|
|
|
|
start = time.time() |
|
|
|
|
|
|
|
req = urllib.request.Request(url, headers=headers) |
|
|
|
|
|
|
|
js = json.loads(urllib.request.urlopen(req, timeout=10).read()) |
|
|
|
|
|
|
|
js['time_taken'] = time.time() - start |
|
|
|
|
|
|
|
js['pair'] = pair |
|
|
|
|
|
|
|
js['rate_last'] = js['lastTradeRate'] |
|
|
|
|
|
|
|
js['from_btc'] = js['lastTradeRate'] |
|
|
|
|
|
|
|
js['to_btc'] = 1.0 |
|
|
|
|
|
|
|
rv['bittrex'] = js |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
pair = f'{exchange_ticker_from}-BTC' |
|
|
|
|
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
|
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
|
|
|
|
start = time.time() |
|
|
|
|
|
|
|
req = urllib.request.Request(url, headers=headers) |
|
|
|
|
|
|
|
js_from = json.loads(urllib.request.urlopen(req, timeout=10).read()) |
|
|
|
|
|
|
|
js_from['time_taken'] = time.time() - start |
|
|
|
|
|
|
|
js_from['pair'] = pair |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pair = f'{exchange_ticker_to}-BTC' |
|
|
|
|
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
|
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
|
|
|
|
start = time.time() |
|
|
|
|
|
|
|
req = urllib.request.Request(url, headers=headers) |
|
|
|
|
|
|
|
js_to = json.loads(urllib.request.urlopen(req, timeout=10).read()) |
|
|
|
|
|
|
|
js_to['time_taken'] = time.time() - start |
|
|
|
|
|
|
|
js_to['pair'] = pair |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
pair = f'{exchange_ticker_to}-BTC' |
|
|
|
rate_inferred = float(js_from['lastTradeRate']) / float(js_to['lastTradeRate']) |
|
|
|
url = f'{bittrex_api_v3}/markets/{pair}/ticker' |
|
|
|
rate_inferred = ci_to.format_amount(rate, conv_int=True, r=1) |
|
|
|
self.log.debug(f'lookupRates: {url}') |
|
|
|
except Exception as e: |
|
|
|
start = time.time() |
|
|
|
rate_inferred = 'error' |
|
|
|
js_to = json.loads(self.readURL(url, timeout=10, headers=headers)) |
|
|
|
|
|
|
|
js_to['time_taken'] = time.time() - start |
|
|
|
rv['bittrex'] = { |
|
|
|
js_to['pair'] = pair |
|
|
|
'from': js_from, |
|
|
|
|
|
|
|
'to': js_to, |
|
|
|
|
|
|
|
'rate_inferred': rate_inferred, |
|
|
|
|
|
|
|
'from_btc': js_from['lastTradeRate'], |
|
|
|
|
|
|
|
'to_btc': js_to['lastTradeRate'] |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
rv['bittrex_error'] = str(e) |
|
|
|
|
|
|
|
if self.debug: |
|
|
|
|
|
|
|
self.log.error(traceback.format_exc()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if output_array: |
|
|
|
try: |
|
|
|
|
|
|
|
rate_inferred = float(js_from['lastTradeRate']) / float(js_to['lastTradeRate']) |
|
|
|
def format_float(f): |
|
|
|
rate_inferred = ci_to.format_amount(rate, conv_int=True, r=1) |
|
|
|
return '{:.12f}'.format(f).rstrip('0').rstrip('.') |
|
|
|
except Exception as e: |
|
|
|
|
|
|
|
rate_inferred = 'error' |
|
|
|
rv_array = [] |
|
|
|
|
|
|
|
if 'coingecko_error' in rv: |
|
|
|
rv['bittrex'] = { |
|
|
|
rv_array.append(('coingecko.com', 'error', rv['coingecko_error'])) |
|
|
|
'from': js_from, |
|
|
|
if 'coingecko' in rv: |
|
|
|
'to': js_to, |
|
|
|
js = rv['coingecko'] |
|
|
|
'rate_inferred': rate_inferred, |
|
|
|
rv_array.append(( |
|
|
|
'from_btc': js_from['lastTradeRate'], |
|
|
|
'coingecko.com', |
|
|
|
'to_btc': js_to['lastTradeRate'] |
|
|
|
ticker_from, |
|
|
|
} |
|
|
|
ticker_to, |
|
|
|
except Exception as e: |
|
|
|
format_float(float(js[name_from]['usd'])), |
|
|
|
rv['bittrex_error'] = str(e) |
|
|
|
format_float(float(js[name_to]['usd'])), |
|
|
|
if self.debug: |
|
|
|
format_float(float(js[name_from]['btc'])), |
|
|
|
self.log.error(traceback.format_exc()) |
|
|
|
format_float(float(js[name_to]['btc'])), |
|
|
|
|
|
|
|
format_float(float(js['rate_inferred'])), |
|
|
|
if output_array: |
|
|
|
)) |
|
|
|
|
|
|
|
if 'bittrex_error' in rv: |
|
|
|
def format_float(f): |
|
|
|
rv_array.append(('bittrex.com', 'error', rv['bittrex_error'])) |
|
|
|
return '{:.12f}'.format(f).rstrip('0').rstrip('.') |
|
|
|
if 'bittrex' in rv: |
|
|
|
|
|
|
|
js = rv['bittrex'] |
|
|
|
rv_array = [] |
|
|
|
rate = js['rate_last'] if 'rate_last' in js else js['rate_inferred'] |
|
|
|
if 'coingecko_error' in rv: |
|
|
|
rv_array.append(( |
|
|
|
rv_array.append(('coingecko.com', 'error', rv['coingecko_error'])) |
|
|
|
'bittrex.com', |
|
|
|
if 'coingecko' in rv: |
|
|
|
ticker_from, |
|
|
|
js = rv['coingecko'] |
|
|
|
ticker_to, |
|
|
|
rv_array.append(( |
|
|
|
'', |
|
|
|
'coingecko.com', |
|
|
|
'', |
|
|
|
ticker_from, |
|
|
|
format_float(float(js['from_btc'])), |
|
|
|
ticker_to, |
|
|
|
format_float(float(js['to_btc'])), |
|
|
|
format_float(float(js[name_from]['usd'])), |
|
|
|
format_float(float(rate)) |
|
|
|
format_float(float(js[name_to]['usd'])), |
|
|
|
)) |
|
|
|
format_float(float(js[name_from]['btc'])), |
|
|
|
return rv_array |
|
|
|
format_float(float(js[name_to]['btc'])), |
|
|
|
|
|
|
|
format_float(float(js['rate_inferred'])), |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
if 'bittrex_error' in rv: |
|
|
|
|
|
|
|
rv_array.append(('bittrex.com', 'error', rv['bittrex_error'])) |
|
|
|
|
|
|
|
if 'bittrex' in rv: |
|
|
|
|
|
|
|
js = rv['bittrex'] |
|
|
|
|
|
|
|
rate = js['rate_last'] if 'rate_last' in js else js['rate_inferred'] |
|
|
|
|
|
|
|
rv_array.append(( |
|
|
|
|
|
|
|
'bittrex.com', |
|
|
|
|
|
|
|
ticker_from, |
|
|
|
|
|
|
|
ticker_to, |
|
|
|
|
|
|
|
'', |
|
|
|
|
|
|
|
'', |
|
|
|
|
|
|
|
format_float(float(js['from_btc'])), |
|
|
|
|
|
|
|
format_float(float(js['to_btc'])), |
|
|
|
|
|
|
|
format_float(float(rate)) |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
return rv_array |
|
|
|
|
|
|
|
|
|
|
|
return rv |
|
|
|
return rv |
|
|
|
finally: |
|
|
|
|
|
|
|
self.popConnectionParameters() |
|
|
|
|
|
|
|