def handle_protocols( self, protocol_name: str, token_symbol: str, normalized_balance: FVal, token_address: str, token_name: str, ) -> Optional[DefiBalance]: """Special handling for price for token/protocols which are easier to do onchain or need some kind of special treatment. """ if protocol_name == 'PoolTogether': result = _handle_pooltogether(normalized_balance, token_name) if result is not None: return result underlying_asset_price = get_underlying_asset_price(token_symbol) usd_price = handle_defi_price_query(self.ethereum, token_symbol, underlying_asset_price) if usd_price is None: return None return DefiBalance( token_address=to_checksum_address(token_address), token_name=token_name, token_symbol=token_symbol, balance=Balance(amount=normalized_balance, usd_value=normalized_balance * usd_price), )
def find_usd_price(asset: Asset) -> Price: """Returns the current USD price of the asset Returns Price(ZERO) if all options have been exhausted and errors are logged in the logs """ if asset.identifier in SPECIAL_SYMBOLS: ethereum = Inquirer()._ethereum assert ethereum, 'Inquirer should never be called before the injection of ethereum' underlying_asset_price = get_underlying_asset_price( asset.identifier) usd_price = handle_defi_price_query( ethereum=ethereum, token_symbol=asset.identifier, underlying_asset_price=underlying_asset_price, ) if usd_price is None: return Price(ZERO) return Price(usd_price) try: price = Inquirer()._cryptocompare_find_usd_price(asset) except RemoteError: price = Price(ZERO) if price == Price(ZERO): try: price = Inquirer()._coingecko.simple_price(from_asset=asset, to_asset=A_USD) except RemoteError as e: log.error( f'Coingecko usd price query for {asset.identifier} failed due to {str(e)}', ) price = Price(ZERO) return price
def handle_protocols( self, token_symbol: str, normalized_balance: FVal, token_address: str, token_name: str, ) -> Optional[DefiBalance]: """Special handling for price for token/protocols which are easier to do onchain""" underlying_asset_price = get_underlying_asset_price(token_symbol) usd_price = handle_defi_price_query(self.ethereum, token_symbol, underlying_asset_price) if usd_price is None: return None return DefiBalance( token_address=to_checksum_address(token_address), token_name=token_name, token_symbol=token_symbol, balance=Balance(amount=normalized_balance, usd_value=normalized_balance * usd_price), )
def find_usd_price(asset: Asset) -> Price: """Returns the current USD price of the asset May raise: - RemoteError if the cryptocompare query has a problem """ if asset.identifier in SPECIAL_SYMBOLS: ethereum = Inquirer()._ethereum assert ethereum, 'Inquirer should never be called before the injection of ethereum' underlying_asset_price = get_underlying_asset_price( asset.identifier) usd_price = handle_defi_price_query( ethereum=ethereum, token_symbol=asset.identifier, underlying_asset_price=underlying_asset_price, ) if usd_price is None: return Price(ZERO) return Price(usd_price) try: result = Inquirer()._cryptocompare.query_endpoint_price( from_asset=asset, to_asset=A_USD, ) except PriceQueryUnsupportedAsset: log.error( 'Cryptocompare usd price for asset failed since it is not known to cryptocompare', asset=asset, ) return Price(ZERO) if 'USD' not in result: log.error('Cryptocompare usd price query failed', asset=asset) return Price(ZERO) price = Price(FVal(result['USD'])) log.debug('Got usd price from cryptocompare', asset=asset, price=price) return price