def get_token_info(self, token_address: str) -> Optional[Erc721InfoWithLogo]: """ :param token_address: :return: Erc721 name and symbol. If it cannot be found, `name=''` and `symbol=''` """ try: token = Token.objects.get(address=token_address) return Erc721InfoWithLogo.from_token(token) except Token.DoesNotExist: logo_uri = '' trusted = False if token_address in self.ENS_CONTRACTS_WITH_TLD: token_info = Erc721Info('Ethereum Name Service', 'ENS') logo_uri = self.ENS_IMAGE_FILENAME trusted = True else: token_info = self.retrieve_token_info(token_address) # If symbol is way bigger than name, swap them (e.g. POAP) if token_info: if (len(token_info.name) - len(token_info.symbol)) < -5: token_info = Erc721Info(token_info.symbol, token_info.name) token = Token.objects.create(address=token_address, name=token_info.name, symbol=token_info.symbol, decimals=0, logo_uri=logo_uri, trusted=trusted) return Erc721InfoWithLogo.from_token(token) return token_info
def test_get_token_info(self, get_info_mock: MagicMock): collectibles_service = CollectiblesServiceProvider() random_address = Account.create().address # No DB, no blockchain source get_info_mock.side_effect = InvalidERC721Info self.assertFalse(collectibles_service.cache_token_info) self.assertIsNone(collectibles_service.get_token_info(random_address)) self.assertTrue( collectibles_service.cache_token_info ) # Cache works for not found tokens too # Add DB source token = TokenFactory() self.assertEqual( collectibles_service.get_token_info(token.address), Erc721InfoWithLogo.from_token(token), ) # Just Blockchain source random_address = Account.create().address self.assertEqual(Token.objects.count(), 1) get_info_mock.side_effect = None get_info_mock.return_value = Erc721Info("Uxio Collectible Card", "UCC") token_info = collectibles_service.get_token_info(random_address) self.assertIsInstance(token_info, Erc721InfoWithLogo) self.assertEqual(token_info.name, get_info_mock.return_value.name) self.assertEqual(token_info.symbol, get_info_mock.return_value.symbol) self.assertEqual(Token.objects.count(), 2) # Test switch name-symbol when symbol is way longer than name random_address = Account.create().address get_info_mock.return_value = Erc721Info( "POAP", "The Proof of Attendance Protocol" ) token_info = collectibles_service.get_token_info(random_address) self.assertIsInstance(token_info, Erc721InfoWithLogo) self.assertEqual(token_info.symbol, get_info_mock.return_value.name) self.assertEqual(token_info.name, get_info_mock.return_value.symbol) self.assertEqual(Token.objects.count(), 3) self.assertEqual( len(collectibles_service.cache_token_info), 4 ) # Cache works for not found tokens too # Test ENS (hardcoded) get_info_mock.return_value = None ens_token_address = list(ENS_CONTRACTS_WITH_TLD.keys())[0] token_info = collectibles_service.get_token_info(ens_token_address) self.assertIsNotNone(token_info) ens_logo_uri = "/media/tokens/logos/ENS.png" self.assertEqual(token_info.logo_uri, ens_logo_uri) self.assertEqual(Token.objects.count(), 4) self.assertEqual( Token.objects.get(address=ens_token_address).logo.url, ens_logo_uri )
def test_get_token_info(self, retrieve_token_info_mock: MagicMock): collectibles_service = CollectiblesService(self.ethereum_client) random_address = Account.create().address # No DB, no blockchain source retrieve_token_info_mock.return_value = None self.assertFalse(collectibles_service.cache_token_info) self.assertIsNone(collectibles_service.get_token_info(random_address)) self.assertTrue(collectibles_service.cache_token_info ) # Cache works for not found tokens too # Add DB source token = TokenFactory() self.assertEqual(collectibles_service.get_token_info(token.address), Erc721InfoWithLogo.from_token(token)) # Just Blockchain source random_address = Account.create().address self.assertEqual(Token.objects.count(), 1) retrieve_token_info_mock.return_value = Erc721Info( 'Uxio Collectible Card', 'UCC') token_info = collectibles_service.get_token_info(random_address) self.assertIsInstance(token_info, Erc721InfoWithLogo) self.assertEqual(token_info.name, retrieve_token_info_mock.return_value.name) self.assertEqual(token_info.symbol, retrieve_token_info_mock.return_value.symbol) self.assertEqual(Token.objects.count(), 2) # Test switch name-symbol when symbol is way longer than name random_address = Account.create().address retrieve_token_info_mock.return_value = Erc721Info( 'POAP', 'The Proof of Attendance Protocol') token_info = collectibles_service.get_token_info(random_address) self.assertIsInstance(token_info, Erc721InfoWithLogo) self.assertEqual(token_info.symbol, retrieve_token_info_mock.return_value.name) self.assertEqual(token_info.name, retrieve_token_info_mock.return_value.symbol) self.assertEqual(Token.objects.count(), 3) self.assertEqual(len(collectibles_service.cache_token_info), 4) # Cache works for not found tokens too # Test ENS (hardcoded) retrieve_token_info_mock.return_value = None token_info = collectibles_service.get_token_info( list(collectibles_service.ENS_CONTRACTS_WITH_TLD.keys())[0]) self.assertIsNotNone(token_info) self.assertEqual(Token.objects.count(), 4)
def get_token_info(self, token_address: str) -> Erc721Info: """ :param token_address: :return: Erc721 name and symbol. If it cannot be found, `name=''` and `symbol=''` """ if token_address in self.ENS_CONTRACTS_WITH_TLD: token_info = Erc721Info('Ethereum Name Service', 'ENS') else: token_info = self.retrieve_token_info(token_address) # If symbol is way bigger than name, swap them (e.g. POAP) if token_info and (len(token_info.name) - len(token_info.symbol)) < -5: token_info = Erc721Info(token_info.symbol, token_info.name) elif not token_info: token_info = Erc721Info('', '') return token_info
def test_get_token_info(self, retrieve_token_info_mock: MagicMock): collectibles_service = CollectiblesService(self.ethereum_client) random_address = Account.create().address retrieve_token_info_mock.return_value = Erc721Info( 'POAP', 'The Proof of Attendance Protocol') self.assertEqual( collectibles_service.get_token_info(random_address), Erc721Info('The Proof of Attendance Protocol', 'POAP')) retrieve_token_info_mock.return_value = Erc721Info( 'Uxio Collectible Card', 'UCC') self.assertEqual(collectibles_service.get_token_info(random_address), Erc721Info('Uxio Collectible Card', 'UCC')) retrieve_token_info_mock.return_value = None self.assertEqual(collectibles_service.get_token_info(random_address), Erc721Info('', ''))