From 871bdb918e6eafbd7e49e3a1da595c670e3ca361 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Thu, 28 Jul 2022 17:01:11 +0200 Subject: [PATCH] ui: Update to bittrex v3 api. --- basicswap/basicswap.py | 129 ++++++++++++++++++++---------------- tests/basicswap/test_run.py | 13 ++++ 2 files changed, 84 insertions(+), 58 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 4bbf6ef..034fc9e 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -5845,77 +5845,90 @@ class BasicSwap(BaseApp): def lookupRates(self, coin_from, coin_to): self.log.debug('lookupRates {}, {}'.format(coin_from, coin_to)) + + bittrex_api_v3 = 'https://api.bittrex.com/v3' try: self.setConnectionParameters() rv = {} - ci_from = self.ci(int(coin_from)) - ci_to = self.ci(int(coin_to)) + 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) - 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 - - ticker_from = ci_from.chainparams()['ticker'] - ticker_to = ci_to.chainparams()['ticker'] - if ci_from.coin_type() == Coins.BTC: - pair = '{}-{}'.format(ticker_from, ticker_to) - url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair + 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 - 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['result']['Last']), 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' + 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' + 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 - rv['bittrex'] = js - elif ci_to.coin_type() == Coins.BTC: - pair = '{}-{}'.format(ticker_to, ticker_from) - url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair - 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['result']['Last'] - rv['bittrex'] = js - else: - pair = 'BTC-{}'.format(ticker_from) - url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair - 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 + 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' - pair = 'BTC-{}'.format(ticker_to) - url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair - 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 + 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 - try: - rate_inferred = float(js_from['result']['Last']) / float(js_to['result']['Last']) - rate_inferred = ci_to.format_amount(rate, conv_int=True, r=1) - except Exception as e: - rate_inferred = 'error' + 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 - rv['bittrex'] = {'from': js_from, 'to': js_to, 'rate_inferred': rate_inferred} + 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} + except Exception as e: + rv['bittrex_error'] = str(e) return rv finally: diff --git a/tests/basicswap/test_run.py b/tests/basicswap/test_run.py index e2cfe3b..2d55b99 100644 --- a/tests/basicswap/test_run.py +++ b/tests/basicswap/test_run.py @@ -101,6 +101,19 @@ class Test(BaseTest): if c in (Coins.PART_ANON, Coins.PART_BLIND): assert(coin['ticker'] == 'PART') + def test_002_lookup_rates(self): + rv = self.swap_clients[0].lookupRates(Coins.BTC, Coins.PART) + assert('coingecko' in rv) + assert('bittrex' in rv) + + rv = self.swap_clients[0].lookupRates(Coins.LTC, Coins.BTC) + assert('coingecko' in rv) + assert('bittrex' in rv) + + rv = self.swap_clients[0].lookupRates(Coins.LTC, Coins.PART) + assert('coingecko' in rv) + assert('bittrex' in rv) + def test_01_verifyrawtransaction(self): txn = '0200000001eb6e5c4ebba4efa32f40c7314cad456a64008e91ee30b2dd0235ab9bb67fbdbb01000000ee47304402200956933242dde94f6cf8f195a470f8d02aef21ec5c9b66c5d3871594bdb74c9d02201d7e1b440de8f4da672d689f9e37e98815fb63dbc1706353290887eb6e8f7235012103dc1b24feb32841bc2f4375da91fa97834e5983668c2a39a6b7eadb60e7033f9d205a803b28fe2f86c17db91fa99d7ed2598f79b5677ffe869de2e478c0d1c02cc7514c606382012088a8201fe90717abb84b481c2a59112414ae56ec8acc72273642ca26cc7a5812fdc8f68876a914225fbfa4cb725b75e511810ac4d6f74069bdded26703520140b27576a914207eb66b2fd6ed9924d6217efc7fa7b38dfabe666888acffffffff01e0167118020000001976a9140044e188928710cecba8311f1cf412135b98145c88ac00000000' prevout = {