def query_balances(self) -> ExchangeQueryBalances: try: kraken_balances = self.api_query('Balance', req={}) except RemoteError as e: if "Missing key: 'result'" in str(e): # handle https://github.com/rotki/rotki/issues/946 kraken_balances = {} else: msg = ( 'Kraken API request failed. Could not reach kraken due ' 'to {}'.format(e) ) log.error(msg) return None, msg assets_balance: DefaultDict[Asset, Balance] = defaultdict(Balance) for kraken_name, amount_ in kraken_balances.items(): amount = FVal(amount_) if amount == ZERO: continue try: our_asset = asset_from_kraken(kraken_name) except UnknownAsset as e: self.msg_aggregator.add_warning( f'Found unsupported/unknown kraken asset {e.asset_name}. ' f' Ignoring its balance query.', ) continue except DeserializationError: self.msg_aggregator.add_error( f'Found kraken asset with non-string type {type(kraken_name)}. ' f' Ignoring its balance query.', ) continue balance = Balance(amount=amount) if our_asset.identifier != 'KFEE': # There is no price value for KFEE. TODO: Shouldn't we then just skip the balance? try: usd_price = Inquirer().find_usd_price(our_asset) except RemoteError as e: self.msg_aggregator.add_error( f'Error processing kraken balance entry due to inability to ' f'query USD price: {str(e)}. Skipping balance entry', ) continue balance.usd_value = balance.amount * usd_price assets_balance[our_asset] += balance log.debug( 'kraken balance query result', sensitive_log=True, currency=our_asset, amount=balance.amount, usd_value=balance.usd_value, ) return dict(assets_balance), ''
def process_gitcoin( self, from_ts: Optional[Timestamp], to_ts: Optional[Timestamp], grant_id: Optional[int], ) -> Tuple[Asset, Dict[int, GitcoinReport]]: """Processess gitcoin transactions in the given time range and creates a report""" actions = self.db_ledger.get_gitcoin_grant_events( grant_id=grant_id, from_ts=from_ts, to_ts=to_ts, ) profit_currency = self.db.get_main_currency() reports: DefaultDict[int, GitcoinReport] = defaultdict(GitcoinReport) for entry in actions: balance = Balance(amount=entry.amount) if entry.rate_asset is None or entry.rate is None: log.error( f'Found gitcoin ledger action for {entry.amount} {entry.asset} ' f'without a rate asset. Should not happen. Entry was ' f'possibly edited by hand. Skipping.', ) continue report = reports[entry.extra_data.grant_id] # type: ignore rate = entry.rate if entry.rate_asset != profit_currency: try: profit_currency_in_rate_asset = PriceHistorian().query_historical_price( from_asset=profit_currency, to_asset=entry.rate_asset, timestamp=entry.timestamp, ) except NoPriceForGivenTimestamp as e: self.db.msg_aggregator.add_error( f'{str(e)} when processing gitcoin entry. Skipping entry.', ) continue rate = entry.rate / profit_currency_in_rate_asset # type: ignore # checked above value_in_profit_currency = entry.amount * rate balance.usd_value = value_in_profit_currency report.per_asset[entry.asset] += balance report.total += value_in_profit_currency return self.db.get_main_currency(), reports