-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from InjectiveLabs/feat/load_official_tokens_…
…list Feat/load official tokens list
- Loading branch information
Showing
10 changed files
with
544 additions
and
253 deletions.
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.