mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-06 10:48:11 +01:00
ui: rateslist returns js list of rates.
This commit is contained in:
@@ -5870,92 +5870,131 @@ class BasicSwap(BaseApp):
|
||||
return {'Error': 'Not Initialised'}
|
||||
return self._network.get_info()
|
||||
|
||||
def lookupRates(self, coin_from, coin_to):
|
||||
def lookupRates(self, coin_from, coin_to, output_array=False):
|
||||
self.log.debug('lookupRates {}, {}'.format(coin_from, coin_to))
|
||||
|
||||
bittrex_api_v3 = 'https://api.bittrex.com/v3'
|
||||
rate_sources = self.settings.get('rate_sources', {})
|
||||
ci_from = self.ci(int(coin_from))
|
||||
ci_to = self.ci(int(coin_to))
|
||||
name_from = ci_from.chainparams()['name']
|
||||
name_to = ci_to.chainparams()['name']
|
||||
ticker_from = ci_from.chainparams()['ticker']
|
||||
ticker_to = ci_to.chainparams()['ticker']
|
||||
headers = {'Connection': 'close'}
|
||||
try:
|
||||
self.setConnectionParameters()
|
||||
rv = {}
|
||||
try:
|
||||
ci_from = self.ci(int(coin_from))
|
||||
ci_to = self.ci(int(coin_to))
|
||||
|
||||
headers = {'Connection': 'close'}
|
||||
name_from = ci_from.chainparams()['name']
|
||||
name_to = ci_to.chainparams()['name']
|
||||
url = 'https://api.coingecko.com/api/v3/simple/price?ids={},{}&vs_currencies=usd'.format(name_from, name_to)
|
||||
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
|
||||
rate = float(js[name_from]['usd']) / float(js[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)
|
||||
|
||||
try:
|
||||
ticker_from = ci_from.chainparams()['ticker']
|
||||
ticker_to = ci_to.chainparams()['ticker']
|
||||
if ci_from.coin_type() == Coins.BTC:
|
||||
pair = f'{ticker_to}-{ticker_from}'
|
||||
url = f'{bittrex_api_v3}/markets/{pair}/ticker'
|
||||
if rate_sources.get('coingecko.com', True):
|
||||
try:
|
||||
url = 'https://api.coingecko.com/api/v3/simple/price?ids={},{}&vs_currencies=usd,btc'.format(name_from, name_to)
|
||||
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
|
||||
rate = float(js[name_from]['usd']) / float(js[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)
|
||||
|
||||
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'
|
||||
if rate_sources.get('bittrex.com', True):
|
||||
bittrex_api_v3 = 'https://api.bittrex.com/v3'
|
||||
try:
|
||||
if ci_from.coin_type() == Coins.BTC:
|
||||
pair = f'{ticker_to}-{ticker_from}'
|
||||
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'{ticker_from}-{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'{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
|
||||
|
||||
rv['bittrex'] = js
|
||||
elif ci_to.coin_type() == Coins.BTC:
|
||||
pair = f'{ticker_from}-{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']
|
||||
rv['bittrex'] = js
|
||||
else:
|
||||
pair = f'{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'{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
|
||||
|
||||
pair = f'{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:
|
||||
rate_inferred = float(js_from['lastTradeRate']) / float(js_to['lastTradeRate'])
|
||||
rate_inferred = ci_to.format_amount(rate, conv_int=True, r=1)
|
||||
except Exception as e:
|
||||
rate_inferred = 'error'
|
||||
|
||||
try:
|
||||
rate_inferred = float(js_from['lastTradeRate']) / float(js_to['lastTradeRate'])
|
||||
rate_inferred = ci_to.format_amount(rate, conv_int=True, r=1)
|
||||
except Exception as e:
|
||||
rate_inferred = 'error'
|
||||
rv['bittrex'] = {
|
||||
'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)
|
||||
|
||||
rv['bittrex'] = {'from': js_from, 'to': js_to, 'rate_inferred': rate_inferred}
|
||||
except Exception as e:
|
||||
rv['bittrex_error'] = str(e)
|
||||
if output_array:
|
||||
|
||||
def format_float(f):
|
||||
return '{:.12f}'.format(f).rstrip('0').rstrip('.')
|
||||
|
||||
rv_array = []
|
||||
if 'coingecko_error' in rv:
|
||||
rv_array.append(('coingecko.com', 'error', rv['coingecko_error']))
|
||||
if 'coingecko' in rv:
|
||||
js = rv['coingecko']
|
||||
rv_array.append((
|
||||
'coingecko.com',
|
||||
ticker_from,
|
||||
ticker_to,
|
||||
format_float(float(js[name_from]['usd'])),
|
||||
format_float(float(js[name_to]['usd'])),
|
||||
format_float(float(js[name_from]['btc'])),
|
||||
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, '', '', js['from_btc'], js['to_btc'], format_float(float(rate))))
|
||||
return rv_array
|
||||
|
||||
return rv
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user