ui: Update to bittrex v3 api.
This commit is contained in:
		
							parent
							
								
									cbcf90c492
								
							
						
					
					
						commit
						871bdb918e
					
				@ -5845,9 +5845,12 @@ class BasicSwap(BaseApp):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def lookupRates(self, coin_from, coin_to):
 | 
					    def lookupRates(self, coin_from, coin_to):
 | 
				
			||||||
        self.log.debug('lookupRates {}, {}'.format(coin_from, coin_to))
 | 
					        self.log.debug('lookupRates {}, {}'.format(coin_from, coin_to))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        bittrex_api_v3 = 'https://api.bittrex.com/v3'
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            self.setConnectionParameters()
 | 
					            self.setConnectionParameters()
 | 
				
			||||||
            rv = {}
 | 
					            rv = {}
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
                ci_from = self.ci(int(coin_from))
 | 
					                ci_from = self.ci(int(coin_from))
 | 
				
			||||||
                ci_to = self.ci(int(coin_to))
 | 
					                ci_to = self.ci(int(coin_to))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -5855,6 +5858,7 @@ class BasicSwap(BaseApp):
 | 
				
			|||||||
                name_from = ci_from.chainparams()['name']
 | 
					                name_from = ci_from.chainparams()['name']
 | 
				
			||||||
                name_to = ci_to.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)
 | 
					                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()
 | 
					                start = time.time()
 | 
				
			||||||
                req = urllib.request.Request(url, headers=headers)
 | 
					                req = urllib.request.Request(url, headers=headers)
 | 
				
			||||||
                js = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
					                js = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
				
			||||||
@ -5862,12 +5866,16 @@ class BasicSwap(BaseApp):
 | 
				
			|||||||
                rate = float(js[name_from]['usd']) / float(js[name_to]['usd'])
 | 
					                rate = float(js[name_from]['usd']) / float(js[name_to]['usd'])
 | 
				
			||||||
                js['rate_inferred'] = ci_to.format_amount(rate, conv_int=True, r=1)
 | 
					                js['rate_inferred'] = ci_to.format_amount(rate, conv_int=True, r=1)
 | 
				
			||||||
                rv['coingecko'] = js
 | 
					                rv['coingecko'] = js
 | 
				
			||||||
 | 
					            except Exception as e:
 | 
				
			||||||
 | 
					                rv['coingecko_error'] = str(e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
                ticker_from = ci_from.chainparams()['ticker']
 | 
					                ticker_from = ci_from.chainparams()['ticker']
 | 
				
			||||||
                ticker_to = ci_to.chainparams()['ticker']
 | 
					                ticker_to = ci_to.chainparams()['ticker']
 | 
				
			||||||
                if ci_from.coin_type() == Coins.BTC:
 | 
					                if ci_from.coin_type() == Coins.BTC:
 | 
				
			||||||
                pair = '{}-{}'.format(ticker_from, ticker_to)
 | 
					                    pair = f'{ticker_to}-{ticker_from}'
 | 
				
			||||||
                url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair
 | 
					                    url = f'{bittrex_api_v3}/markets/{pair}/ticker'
 | 
				
			||||||
 | 
					                    self.log.debug(f'lookupRates: {url}')
 | 
				
			||||||
                    start = time.time()
 | 
					                    start = time.time()
 | 
				
			||||||
                    req = urllib.request.Request(url, headers=headers)
 | 
					                    req = urllib.request.Request(url, headers=headers)
 | 
				
			||||||
                    js = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
					                    js = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
				
			||||||
@ -5875,7 +5883,7 @@ class BasicSwap(BaseApp):
 | 
				
			|||||||
                    js['pair'] = pair
 | 
					                    js['pair'] = pair
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    try:
 | 
					                    try:
 | 
				
			||||||
                    rate_inverted = ci_from.make_int(1.0 / float(js['result']['Last']), r=1)
 | 
					                        rate_inverted = ci_from.make_int(1.0 / float(js['lastTradeRate']), r=1)
 | 
				
			||||||
                        js['rate_inferred'] = ci_to.format_amount(rate_inverted)
 | 
					                        js['rate_inferred'] = ci_to.format_amount(rate_inverted)
 | 
				
			||||||
                    except Exception as e:
 | 
					                    except Exception as e:
 | 
				
			||||||
                        self.log.warning('lookupRates error: %s', str(e))
 | 
					                        self.log.warning('lookupRates error: %s', str(e))
 | 
				
			||||||
@ -5883,26 +5891,29 @@ class BasicSwap(BaseApp):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                    rv['bittrex'] = js
 | 
					                    rv['bittrex'] = js
 | 
				
			||||||
                elif ci_to.coin_type() == Coins.BTC:
 | 
					                elif ci_to.coin_type() == Coins.BTC:
 | 
				
			||||||
                pair = '{}-{}'.format(ticker_to, ticker_from)
 | 
					                    pair = f'{ticker_from}-{ticker_to}'
 | 
				
			||||||
                url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair
 | 
					                    url = f'{bittrex_api_v3}/markets/{pair}/ticker'
 | 
				
			||||||
 | 
					                    self.log.debug(f'lookupRates: {url}')
 | 
				
			||||||
                    start = time.time()
 | 
					                    start = time.time()
 | 
				
			||||||
                    req = urllib.request.Request(url, headers=headers)
 | 
					                    req = urllib.request.Request(url, headers=headers)
 | 
				
			||||||
                    js = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
					                    js = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
				
			||||||
                    js['time_taken'] = time.time() - start
 | 
					                    js['time_taken'] = time.time() - start
 | 
				
			||||||
                    js['pair'] = pair
 | 
					                    js['pair'] = pair
 | 
				
			||||||
                js['rate_last'] = js['result']['Last']
 | 
					                    js['rate_last'] = js['lastTradeRate']
 | 
				
			||||||
                    rv['bittrex'] = js
 | 
					                    rv['bittrex'] = js
 | 
				
			||||||
                else:
 | 
					                else:
 | 
				
			||||||
                pair = 'BTC-{}'.format(ticker_from)
 | 
					                    pair = f'{ticker_from}-BTC'
 | 
				
			||||||
                url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair
 | 
					                    url = f'{bittrex_api_v3}/markets/{pair}/ticker'
 | 
				
			||||||
 | 
					                    self.log.debug(f'lookupRates: {url}')
 | 
				
			||||||
                    start = time.time()
 | 
					                    start = time.time()
 | 
				
			||||||
                    req = urllib.request.Request(url, headers=headers)
 | 
					                    req = urllib.request.Request(url, headers=headers)
 | 
				
			||||||
                    js_from = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
					                    js_from = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
				
			||||||
                    js_from['time_taken'] = time.time() - start
 | 
					                    js_from['time_taken'] = time.time() - start
 | 
				
			||||||
                    js_from['pair'] = pair
 | 
					                    js_from['pair'] = pair
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                pair = 'BTC-{}'.format(ticker_to)
 | 
					                    pair = f'{ticker_to}-BTC'
 | 
				
			||||||
                url = 'https://api.bittrex.com/api/v1.1/public/getticker?market=' + pair
 | 
					                    url = f'{bittrex_api_v3}/markets/{pair}/ticker'
 | 
				
			||||||
 | 
					                    self.log.debug(f'lookupRates: {url}')
 | 
				
			||||||
                    start = time.time()
 | 
					                    start = time.time()
 | 
				
			||||||
                    req = urllib.request.Request(url, headers=headers)
 | 
					                    req = urllib.request.Request(url, headers=headers)
 | 
				
			||||||
                    js_to = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
					                    js_to = json.loads(urllib.request.urlopen(req, timeout=10).read())
 | 
				
			||||||
@ -5910,12 +5921,14 @@ class BasicSwap(BaseApp):
 | 
				
			|||||||
                    js_to['pair'] = pair
 | 
					                    js_to['pair'] = pair
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    try:
 | 
					                    try:
 | 
				
			||||||
                    rate_inferred = float(js_from['result']['Last']) / float(js_to['result']['Last'])
 | 
					                        rate_inferred = float(js_from['lastTradeRate']) / float(js_to['lastTradeRate'])
 | 
				
			||||||
                        rate_inferred = ci_to.format_amount(rate, conv_int=True, r=1)
 | 
					                        rate_inferred = ci_to.format_amount(rate, conv_int=True, r=1)
 | 
				
			||||||
                    except Exception as e:
 | 
					                    except Exception as e:
 | 
				
			||||||
                        rate_inferred = 'error'
 | 
					                        rate_inferred = 'error'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    rv['bittrex'] = {'from': js_from, 'to': js_to, 'rate_inferred': rate_inferred}
 | 
					                    rv['bittrex'] = {'from': js_from, 'to': js_to, 'rate_inferred': rate_inferred}
 | 
				
			||||||
 | 
					            except Exception as e:
 | 
				
			||||||
 | 
					                rv['bittrex_error'] = str(e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return rv
 | 
					            return rv
 | 
				
			||||||
        finally:
 | 
					        finally:
 | 
				
			||||||
 | 
				
			|||||||
@ -101,6 +101,19 @@ class Test(BaseTest):
 | 
				
			|||||||
            if c in (Coins.PART_ANON, Coins.PART_BLIND):
 | 
					            if c in (Coins.PART_ANON, Coins.PART_BLIND):
 | 
				
			||||||
                assert(coin['ticker'] == 'PART')
 | 
					                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):
 | 
					    def test_01_verifyrawtransaction(self):
 | 
				
			||||||
        txn = '0200000001eb6e5c4ebba4efa32f40c7314cad456a64008e91ee30b2dd0235ab9bb67fbdbb01000000ee47304402200956933242dde94f6cf8f195a470f8d02aef21ec5c9b66c5d3871594bdb74c9d02201d7e1b440de8f4da672d689f9e37e98815fb63dbc1706353290887eb6e8f7235012103dc1b24feb32841bc2f4375da91fa97834e5983668c2a39a6b7eadb60e7033f9d205a803b28fe2f86c17db91fa99d7ed2598f79b5677ffe869de2e478c0d1c02cc7514c606382012088a8201fe90717abb84b481c2a59112414ae56ec8acc72273642ca26cc7a5812fdc8f68876a914225fbfa4cb725b75e511810ac4d6f74069bdded26703520140b27576a914207eb66b2fd6ed9924d6217efc7fa7b38dfabe666888acffffffff01e0167118020000001976a9140044e188928710cecba8311f1cf412135b98145c88ac00000000'
 | 
					        txn = '0200000001eb6e5c4ebba4efa32f40c7314cad456a64008e91ee30b2dd0235ab9bb67fbdbb01000000ee47304402200956933242dde94f6cf8f195a470f8d02aef21ec5c9b66c5d3871594bdb74c9d02201d7e1b440de8f4da672d689f9e37e98815fb63dbc1706353290887eb6e8f7235012103dc1b24feb32841bc2f4375da91fa97834e5983668c2a39a6b7eadb60e7033f9d205a803b28fe2f86c17db91fa99d7ed2598f79b5677ffe869de2e478c0d1c02cc7514c606382012088a8201fe90717abb84b481c2a59112414ae56ec8acc72273642ca26cc7a5812fdc8f68876a914225fbfa4cb725b75e511810ac4d6f74069bdded26703520140b27576a914207eb66b2fd6ed9924d6217efc7fa7b38dfabe666888acffffffff01e0167118020000001976a9140044e188928710cecba8311f1cf412135b98145c88ac00000000'
 | 
				
			||||||
        prevout = {
 | 
					        prevout = {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user