def query_etherscan_for_transactions( accounts: List[EthAddress]) -> List[EthereumTransaction]: transactions: List[EthereumTransaction] = list() for account in accounts: transactions.extend( retry_calls( 5, 'etherscan', 'query_ethereum_txlist', query_ethereum_txlist, account, False, ), ) transactions.extend( retry_calls( 5, 'etherscan', 'query_ethereum_txlist_internal', query_ethereum_txlist, account, True, ), ) transactions.sort(key=lambda tx: tx.timestamp) return transactions
def query_etherscan_for_transactions(accounts): transactions = list() for account in accounts: transactions.extend( retry_calls(5, 'etherscan', 'query_txlist', query_txlist, account, False)) transactions.extend( retry_calls(5, 'etherscan', 'query_txlist_internal', query_txlist, account, True)) transactions.sort(key=lambda tx: tx.timestamp) return transactions
def find_usd_price(self, asset, asset_btc_price=None): if self.kraken and self.kraken.first_connection_made and asset_btc_price is not None: return self.query_kraken_for_price(asset, asset_btc_price) # Adjust some ETH tokens to how cryptocompare knows them if asset == 'RDN': asset = 'RDN*' # temporary if asset == 'DATAcoin': asset = 'DATA' resp = retry_calls( 5, 'find_usd_price', 'urllib2.urlopen', urlopen, Request( u'https://min-api.cryptocompare.com/data/price?fsym={}&tsyms=USD'.format( asset )) ) resp = rlk_jsonloads(resp.read()) # If there is an error in the response skip this token if 'USD' not in resp: if resp['Response'] == 'Error': print('Could not query USD price for {}. Error: "{}"'.format( asset, resp['Message']), ) else: print('Could not query USD price for {}'.format(asset)) return FVal(0) return FVal(resp['USD'])
def query_cryptocompare_for_fiat_price(asset: Asset) -> FVal: log.debug('Get usd price from cryptocompare', asset=asset) asset = world_to_cryptocompare(asset) resp = retry_calls( 5, 'find_usd_price', 'requests.get', requests.get, u'https://min-api.cryptocompare.com/data/price?' 'fsym={}&tsyms=USD'.format(asset), ) if resp.status_code != 200: raise RemoteError( 'Cant reach cryptocompare to get USD value of {}'.format(asset)) resp = rlk_jsonloads_dict(resp.text) # If there is an error in the response skip this token if 'USD' not in resp: error_message = '' if resp['Response'] == 'Error': error_message = resp['Message'] log.error( 'Cryptocompare usd price query failed', asset=asset, error=error_message, ) return FVal(0) price = FVal(resp['USD']) log.debug('Got usd price from cryptocompare', asset=asset, price=price) return price
def find_usd_price(self, asset, asset_btc_price=None): if self.kraken and self.kraken.first_connection_made and asset_btc_price is not None: return self.query_kraken_for_price(asset, asset_btc_price) # Adjust some ETH tokens to how cryptocompare knows them if asset == 'RDN': asset = 'RDN*' # temporary if asset == 'DATAcoin': asset = 'DATA' resp = retry_calls( 5, 'find_usd_price', 'requests.get', requests.get, u'https://min-api.cryptocompare.com/data/price?' 'fsym={}&tsyms=USD'.format(asset)) if resp.status_code != 200: raise RemoteError( 'Cant reach cryptocompare to get USD value of {}'.format( asset)) resp = rlk_jsonloads(resp.text) # If there is an error in the response skip this token if 'USD' not in resp: if resp['Response'] == 'Error': print( 'Could not query USD price for {}. Error: "{}"'.format( asset, resp['Message']), ) else: print('Could not query USD price for {}'.format(asset)) return FVal(0) return FVal(resp['USD'])
def api_query(self, command: str, req: Optional[Dict] = None) -> Dict: result = retry_calls(5, 'poloniex', command, self._api_query, command, req) if 'error' in result: raise PoloniexError( 'Poloniex query for "{}" returned error: {}'.format( command, result['error'])) return result
def api_query(self, command, req={}): result = retry_calls(5, 'poloniex', command, self._api_query, command, req) if 'error' in result: raise ValueError( 'Poloniex query for "{}" returned error: {}'.format( command, result['error'])) return result
def query_historical_fiat_exchange_rates( self, from_currency: FiatAsset, to_currency: FiatAsset, timestamp: Timestamp, ) -> Optional[FVal]: date = tsToDate(timestamp, formatstr='%Y-%m-%d') rate = self._get_cached_forex_data(date, from_currency, to_currency) if rate: return rate log.debug( 'Querying exchangeratesapi', from_currency=from_currency, to_currency=to_currency, timestamp=timestamp, ) query_str = (f'https://api.exchangeratesapi.io/{date}?' f'base={from_currency}') resp = retry_calls( 5, 'query_exchangeratesapi', 'requests.get', requests.get, query_str, ) if resp.status_code != 200: return None try: result = rlk_jsonloads_dict(resp.text) except JSONDecodeError: return None if 'rates' not in result or to_currency not in result['rates']: return None if date not in self.cached_forex_data: self.cached_forex_data[date] = {} if from_currency not in self.cached_forex_data[date]: self.cached_forex_data[date][from_currency] = {} for key, value in result['rates'].items(): self.cached_forex_data[date][from_currency][key] = FVal(value) rate = FVal(result['rates'][to_currency]) log.debug('Exchangeratesapi query succesful', rate=rate) return rate
def find_usd_price( self, asset: typing.Asset, asset_btc_price: Optional[FVal] = None, ) -> FVal: if self.kraken and self.kraken.first_connection_made and asset_btc_price is not None: price = self.query_kraken_for_price(asset, asset_btc_price) log.debug('Get usd price from kraken', asset=asset, price=price) return price log.debug('Get usd price from cryptocompare', asset=asset) asset = world_to_cryptocompare(asset) resp = retry_calls( 5, 'find_usd_price', 'requests.get', requests.get, u'https://min-api.cryptocompare.com/data/price?' 'fsym={}&tsyms=USD'.format(asset)) if resp.status_code != 200: raise RemoteError( 'Cant reach cryptocompare to get USD value of {}'.format( asset)) resp = rlk_jsonloads(resp.text) # If there is an error in the response skip this token if 'USD' not in resp: error_message = '' if resp['Response'] == 'Error': error_message = resp['Message'] log.error( 'Cryptocompare usd price query failed', asset=asset, error=error_message, ) return FVal(0) price = FVal(resp['USD']) log.debug('Got usd price from cryptocompare', asset=asset, price=price) return price
def find_usd_price( self, asset: typing.Asset, asset_btc_price: Optional[FVal] = None, ) -> FVal: if self.kraken and self.kraken.first_connection_made and asset_btc_price is not None: return self.query_kraken_for_price(asset, asset_btc_price) # Adjust some ETH tokens to how cryptocompare knows them if asset == S_RDN: # remove this if cryptocompare changes the symbol asset = cast(typing.EthToken, 'RDN*') if asset == S_DATACOIN: asset = cast(typing.NonEthTokenBlockchainAsset, 'DATA') resp = retry_calls( 5, 'find_usd_price', 'requests.get', requests.get, u'https://min-api.cryptocompare.com/data/price?' 'fsym={}&tsyms=USD'.format(asset)) if resp.status_code != 200: raise RemoteError( 'Cant reach cryptocompare to get USD value of {}'.format( asset)) resp = rlk_jsonloads(resp.text) # If there is an error in the response skip this token if 'USD' not in resp: if resp['Response'] == 'Error': print( 'Could not query USD price for {}. Error: "{}"'.format( asset, resp['Message']), ) else: print('Could not query USD price for {}'.format(asset)) return FVal(0) return FVal(resp['USD'])
def query_private(self, method, req={}): return retry_calls(5, 'kraken', method, self._query_private, method, req)
def query_public(self, method, req={}): return retry_calls(5, 'kraken', method, self._query_public, method, req)
def query_private(self, method: str, req: Optional[dict] = None) -> dict: return retry_calls(5, 'kraken', method, self._query_private, method, req)