def get_ethereum_token( address: ChecksumEthAddress ) -> Optional[EthereumToken]: # noqa: E501 """Gets all details for an ethereum token by its address If no token for the given address can be found None is returned. """ cursor = GlobalDBHandler()._conn.cursor() query = cursor.execute( 'SELECT A.identifier, B.address, B.decimals, A.name, A.symbol, A.started, ' 'A.swapped_for, A.coingecko, A.cryptocompare, B.protocol ' 'FROM ethereum_tokens AS B LEFT OUTER JOIN ' 'assets AS A ON B.address = A.details_reference WHERE address=?;', (address, ), ) results = query.fetchall() if len(results) == 0: return None token_data = results[0] underlying_tokens = GlobalDBHandler().fetch_underlying_tokens(address) try: return EthereumToken.deserialize_from_db( entry=token_data, underlying_tokens=underlying_tokens, ) except UnknownAsset as e: log.error( f'Found unknown swapped_for asset {str(e)} in ' f'the DB when deserializing an EthereumToken', ) return None
def get_ethereum_tokens( exceptions: Optional[List[ChecksumEthAddress]] = None, protocol: Optional[str] = None, ) -> List[EthereumToken]: """Gets all ethereum tokens from the DB Can also accept filtering parameters. - List of addresses to ignore via exceptions - Protocol for which to return tokens """ cursor = GlobalDBHandler()._conn.cursor() querystr = ( 'SELECT A.identifier, B.address, B.decimals, A.name, A.symbol, A.started, ' 'A.swapped_for, A.coingecko, A.cryptocompare, B.protocol ' 'FROM ethereum_tokens as B LEFT OUTER JOIN ' 'assets AS A on B.address = A.details_reference ') if exceptions is not None or protocol is not None: bindings_list: List[Union[str, ChecksumEthAddress]] = [] querystr_additions = [] if exceptions is not None: questionmarks = '?' * len(exceptions) querystr_additions.append( f'WHERE B.address NOT IN ({",".join(questionmarks)}) ') bindings_list.extend(exceptions) if protocol is not None: querystr_additions.append('WHERE B.protocol=? ') bindings_list.append(protocol) querystr += 'AND '.join(querystr_additions) + ';' bindings = tuple(bindings_list) else: querystr += ';' bindings = () query = cursor.execute(querystr, bindings) tokens = [] for entry in query: underlying_tokens = GlobalDBHandler().fetch_underlying_tokens( entry[1]) try: token = EthereumToken.deserialize_from_db( entry, underlying_tokens) # noqa: E501 tokens.append(token) except UnknownAsset as e: log.error( f'Found unknown swapped_for asset {str(e)} in ' f'the DB when deserializing an EthereumToken', ) return tokens