From 75a887c9edcf1b5082ce11320aadb47288fb20f2 Mon Sep 17 00:00:00 2001 From: HVbajoria Date: Tue, 7 Oct 2025 01:12:29 +0530 Subject: [PATCH 1/2] Implemented ETH to USD script --- conversions/crypto_price.py | 77 +++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 78 insertions(+) create mode 100644 conversions/crypto_price.py diff --git a/conversions/crypto_price.py b/conversions/crypto_price.py new file mode 100644 index 000000000000..c0084d438aac --- /dev/null +++ b/conversions/crypto_price.py @@ -0,0 +1,77 @@ +""" +Convert ETH (Ethereum) to USD using live or static conversion rates. + +Fetches current ETH price from CoinGecko API or uses fallback rate. +Source: https://api.coingecko.com/api/v3/simple/price +""" + + +def eth_to_usd(eth_amount: float, eth_price_usd: float = 2000.0) -> float: + """ + Convert ETH amount to USD equivalent. + + Args: + eth_amount: Amount of ETH to convert + eth_price_usd: Price of 1 ETH in USD (default: 2000.0) + + Returns: + USD equivalent of the ETH amount + + Raises: + ValueError: If eth_amount or eth_price_usd is negative + + >>> eth_to_usd(1.0, 2000.0) + 2000.0 + >>> eth_to_usd(0.5, 3000.0) + 1500.0 + >>> eth_to_usd(0, 2000.0) + 0.0 + >>> eth_to_usd(-1, 2000.0) + Traceback (most recent call last): + ... + ValueError: ETH amount cannot be negative + >>> eth_to_usd(1, -100) + Traceback (most recent call last): + ... + ValueError: ETH price cannot be negative + """ + if eth_amount < 0: + raise ValueError("ETH amount cannot be negative") + if eth_price_usd < 0: + raise ValueError("ETH price cannot be negative") + + return eth_amount * eth_price_usd + + +def get_live_eth_price() -> float: + """ + Fetch current ETH price from CoinGecko API. + + Returns: + Current ETH price in USD, or 2000.0 if request fails + + >>> price = get_live_eth_price() + >>> isinstance(price, float) + True + """ + try: + import requests + response = requests.get( + "https://api.coingecko.com/api/v3/simple/price", + params={"ids": "ethereum", "vs_currencies": "usd"}, + timeout=10 + ) + response.raise_for_status() + return response.json()["ethereum"]["usd"] + except (ImportError, requests.RequestException, KeyError): + return 2000.0 + + +if __name__ == "__main__": + import doctest + doctest.testmod() + + # Interactive example + live_price = get_live_eth_price() + result = eth_to_usd(1.0, live_price) + print(f"1 ETH = ${result:,.2f} USD (${live_price:,.2f}/ETH)") diff --git a/requirements.txt b/requirements.txt index 66b5d8a6b94e..7a5978bca065 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,6 +9,7 @@ numpy opencv-python pandas pillow +requests rich scikit-learn sphinx-pyproject From 0ad3f7932beb2450a12806310fa0229e61354779 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 19:53:34 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/crypto_price.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/conversions/crypto_price.py b/conversions/crypto_price.py index c0084d438aac..908bb73a19ee 100644 --- a/conversions/crypto_price.py +++ b/conversions/crypto_price.py @@ -56,10 +56,11 @@ def get_live_eth_price() -> float: """ try: import requests + response = requests.get( "https://api.coingecko.com/api/v3/simple/price", params={"ids": "ethereum", "vs_currencies": "usd"}, - timeout=10 + timeout=10, ) response.raise_for_status() return response.json()["ethereum"]["usd"] @@ -69,6 +70,7 @@ def get_live_eth_price() -> float: if __name__ == "__main__": import doctest + doctest.testmod() # Interactive example