-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
CoingeckoAPI for fetching the price in usd of a given token. | ||
""" | ||
# pylint: disable=logging-fstring-interpolation | ||
|
||
from typing import Optional | ||
import requests | ||
from src.helper_functions import get_logger | ||
from src.constants import ( | ||
header, | ||
REQUEST_TIMEOUT, | ||
) | ||
|
||
|
||
class CoingeckoAPI: | ||
""" | ||
Class for fetching token prices from Coingecko. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self.logger = get_logger() | ||
|
||
def get_token_price_in_usd(self, address: str) -> Optional[float]: | ||
""" | ||
Returns the Coingecko price in usd of the given token. | ||
""" | ||
coingecko_url = ( | ||
"https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=" | ||
+ address | ||
+ "&vs_currencies=usd" | ||
) | ||
try: | ||
coingecko_data = requests.get( | ||
coingecko_url, | ||
headers=header, | ||
timeout=REQUEST_TIMEOUT, | ||
) | ||
coingecko_rsp = coingecko_data.json() | ||
coingecko_price_in_usd = coingecko_rsp[address]["usd"] | ||
except requests.RequestException as err: | ||
self.logger.warning( | ||
f"Connection error while fetching Coingecko price for token {address}, error: {err}" | ||
) | ||
return None | ||
return coingecko_price_in_usd |
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,42 @@ | ||
""" | ||
KlerosAPI for fetching the Kleros token list. | ||
""" | ||
# pylint: disable=logging-fstring-interpolation | ||
|
||
import requests | ||
from src.helper_functions import get_logger | ||
from src.constants import ( | ||
header, | ||
REQUEST_TIMEOUT, | ||
) | ||
|
||
|
||
class KlerosAPI: | ||
""" | ||
Class for fetching the Kleros token list. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self.logger = get_logger() | ||
|
||
def get_token_list(self) -> list[str]: | ||
""" | ||
Returns the Kleros token list. | ||
""" | ||
kleros_url = "http://t2crtokens.eth.link" | ||
|
||
try: | ||
kleros_data = requests.get( | ||
kleros_url, | ||
headers=header, | ||
timeout=REQUEST_TIMEOUT, | ||
) | ||
kleros_rsp = kleros_data.json() | ||
kleros_list = [] | ||
for token in kleros_rsp["tokens"]: | ||
kleros_list.append(token["address"].lower()) | ||
except requests.RequestException as err: | ||
self.logger.warning( | ||
f"Connection error while fetching the Kleros token list, error: {err}" | ||
) | ||
return kleros_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