-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/load official tokens list #327
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4a423df
(feat) Added logic to load all tokens from the official tokens file i…
89b0f01
(fix) Updated changelog file
f59766b
(fix) Fixed failing unit tests
0d41383
(fix) Added exception handling to prevent connection issues from brak…
9e8fc8a
(fix) Updated dependencis in poetry lock file
355c3a9
(fix) Added unit test for AsyncClient tokens loading logic, to improv…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from typing import Dict, List | ||
|
||
import aiohttp | ||
|
||
from pyinjective.core.token import Token | ||
from pyinjective.utils.logger import LoggerProvider | ||
|
||
|
||
class TokensFileLoader: | ||
def load_json(self, json: List[Dict]) -> List[Token]: | ||
loaded_tokens = [] | ||
|
||
for token_info in json: | ||
token = Token( | ||
name=token_info["name"], | ||
symbol=token_info["symbol"], | ||
denom=token_info["denom"], | ||
address=token_info["address"], | ||
decimals=token_info["decimals"], | ||
logo=token_info["logo"], | ||
updated=-1, | ||
) | ||
|
||
loaded_tokens.append(token) | ||
|
||
return loaded_tokens | ||
|
||
async def load_tokens(self, tokens_file_url: str) -> List[Token]: | ||
tokens_list = [] | ||
try: | ||
async with aiohttp.ClientSession() as session: | ||
async with session.get(tokens_file_url) as response: | ||
if response.ok: | ||
tokens_list = await response.json(content_type=None) | ||
except Exception as e: | ||
LoggerProvider().logger_for_class(logging_class=self.__class__).warning( | ||
f"there was an error fetching the list of official tokens: {e}" | ||
) | ||
|
||
return self.load_json(tokens_list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import pytest | ||
|
||
from pyinjective.core.tokens_file_loader import TokensFileLoader | ||
|
||
|
||
class TestTokensFileLoader: | ||
def test_load_tokens(self): | ||
tokens_list = [ | ||
{ | ||
"address": "", | ||
"isNative": True, | ||
"decimals": 9, | ||
"symbol": "SOL", | ||
"name": "Solana", | ||
"logo": "https://imagedelivery.net/DYKOWp0iCc0sIkF-2e4dNw/2aa4deed-fa31-4d1a-ba0a-d698b84f3800/public", | ||
"coinGeckoId": "solana", | ||
"denom": "", | ||
"tokenType": "spl", | ||
"tokenVerification": "verified", | ||
"externalLogo": "solana.png", | ||
}, | ||
{ | ||
"address": "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", | ||
"isNative": False, | ||
"decimals": 18, | ||
"symbol": "WMATIC", | ||
"logo": "https://imagedelivery.net/DYKOWp0iCc0sIkF-2e4dNw/0d061e1e-a746-4b19-1399-8187b8bb1700/public", | ||
"name": "Wrapped Matic", | ||
"coinGeckoId": "wmatic", | ||
"denom": "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", | ||
"tokenType": "evm", | ||
"tokenVerification": "verified", | ||
"externalLogo": "polygon.png", | ||
}, | ||
] | ||
|
||
loader = TokensFileLoader() | ||
|
||
loaded_tokens = loader.load_json(json=tokens_list) | ||
|
||
assert len(loaded_tokens) == 2 | ||
|
||
for token, token_info in zip(loaded_tokens, tokens_list): | ||
assert token.name == token_info["name"] | ||
assert token.symbol == token_info["symbol"] | ||
assert token.denom == token_info["denom"] | ||
assert token.address == token_info["address"] | ||
assert token.decimals == token_info["decimals"] | ||
assert token.logo == token_info["logo"] | ||
|
||
@pytest.mark.asyncio | ||
async def test_load_tokens_from_url(self, aioresponses): | ||
loader = TokensFileLoader() | ||
tokens_list = [ | ||
{ | ||
"address": "", | ||
"isNative": True, | ||
"decimals": 9, | ||
"symbol": "SOL", | ||
"name": "Solana", | ||
"logo": "https://imagedelivery.net/DYKOWp0iCc0sIkF-2e4dNw/2aa4deed-fa31-4d1a-ba0a-d698b84f3800/public", | ||
"coinGeckoId": "solana", | ||
"denom": "", | ||
"tokenType": "spl", | ||
"tokenVerification": "verified", | ||
"externalLogo": "solana.png", | ||
}, | ||
{ | ||
"address": "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", | ||
"isNative": False, | ||
"decimals": 18, | ||
"symbol": "WMATIC", | ||
"logo": "https://imagedelivery.net/DYKOWp0iCc0sIkF-2e4dNw/0d061e1e-a746-4b19-1399-8187b8bb1700/public", | ||
"name": "Wrapped Matic", | ||
"coinGeckoId": "wmatic", | ||
"denom": "0x0d500b1d8e8ef31e21c99d1db9a6444d3adf1270", | ||
"tokenType": "evm", | ||
"tokenVerification": "verified", | ||
"externalLogo": "polygon.png", | ||
}, | ||
] | ||
|
||
aioresponses.get( | ||
"https://github.com/InjectiveLabs/injective-lists/raw/master/tokens/mainnet.json", payload=tokens_list | ||
) | ||
loaded_tokens = await loader.load_tokens( | ||
tokens_file_url="https://github.com/InjectiveLabs/injective-lists/raw/master/tokens/mainnet.json" | ||
) | ||
|
||
assert len(loaded_tokens) == 2 | ||
|
||
for token, token_info in zip(loaded_tokens, tokens_list): | ||
assert token.name == token_info["name"] | ||
assert token.symbol == token_info["symbol"] | ||
assert token.denom == token_info["denom"] | ||
assert token.address == token_info["address"] | ||
assert token.decimals == token_info["decimals"] | ||
assert token.logo == token_info["logo"] | ||
|
||
@pytest.mark.asyncio | ||
async def test_load_tokens_from_url_returns_nothing_when_request_fails(self, aioresponses): | ||
loader = TokensFileLoader() | ||
|
||
aioresponses.get( | ||
"https://github.com/InjectiveLabs/injective-lists/raw/master/tokens/mainnet.json", | ||
status=404, | ||
) | ||
loaded_tokens = await loader.load_tokens( | ||
tokens_file_url="https://github.com/InjectiveLabs/injective-lists/raw/master/tokens/mainnet.json" | ||
) | ||
|
||
assert len(loaded_tokens) == 0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using list comprehension to optimize the
load_json
method.Committable suggestion