Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

feat: Extend CoinGecko wrapper with 5 new endpoints

rudrashethcbb8ccc4b445
diff --git a/fincept-terminal-desktop/src-tauri/resources/scripts/coingecko_data.py b/fincept-terminal-desktop/src-tauri/resources/scripts/coingecko_data.py
index 24cc7fab5..33272fe85 100644
--- a/fincept-terminal-desktop/src-tauri/resources/scripts/coingecko_data.py
+++ b/fincept-terminal-desktop/src-tauri/resources/scripts/coingecko_data.py
@@ -10,8 +10,7 @@ import os
import requests
from typing import Dict, Any, List
-# 1. CONFIGURATION (from your guide)
-# The public CoinGecko API doesn't require a key, but we'll use their pro URL if a key is found.
+# 1. CONFIGURATION
API_KEY = os.environ.get('COINGECKO_API_KEY')
if API_KEY:
BASE_URL = "https://pro-api.coingecko.com/api/v3"
@@ -22,127 +21,144 @@ def _make_request(endpoint: str, params: Dict[str, Any] = None) -> Any:
"""A private helper function to handle all API requests and errors."""
full_url = f"{BASE_URL}/{endpoint}"
- # Add API key to params if using the pro version
if API_KEY:
if params is None:
params = {}
params['x_cg_pro_api_key'] = API_KEY
try:
- response = requests.get(full_url, params=params, timeout=10)
- response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
+ response = requests.get(full_url, params=params, timeout=15)
+ response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as e:
- # Return a standardized error format
return {"error": f"HTTP Error: {e.response.status_code} - {e.response.text}"}
except requests.exceptions.RequestException as e:
return {"error": f"Network or request error: {str(e)}"}
except json.JSONDecodeError:
return {"error": "Failed to decode API response."}
-# 2. CORE FUNCTIONS (from your guide)
+# 2. CORE FUNCTIONS
def get_ping() -> Dict[str, Any]:
- """Checks if the CoinGecko API is responsive."""
+ """1. Checks if the CoinGecko API is responsive."""
return _make_request("ping")
def get_prices(coin_ids: List[str]) -> Dict[str, Any]:
- """
- Fetches the current price for one or more cryptocurrencies.
-
- Args:
- coin_ids: A list of coin IDs (e.g., ['bitcoin', 'ethereum']).
-
- Returns:
- A dictionary with formatted price data or an error.
- """
+ """2. Fetches the current price for one or more cryptocurrencies."""
if not coin_ids:
return {"error": "No coin IDs provided."}
-
- params = {
- 'ids': ",".join(coin_ids), # API expects a comma-separated string
- 'vs_currencies': 'usd'
- }
-
+ params = {'ids': ",".join(coin_ids), 'vs_currencies': 'usd'}
data = _make_request("simple/price", params=params)
if data and "error" not in data:
- # Format the data into the standardized response format
- results = []
- for coin_id, prices in data.items():
- results.append({
- "id": coin_id,
- "price_usd": prices.get('usd', 0)
- })
+ results = [{"id": coin_id, "price_usd": prices.get('usd', 0)} for coin_id, prices in data.items()]
return {"data": results}
- return data # Return the error response if something went wrong
+ return data
def get_market_info(coin_id: str) -> Dict[str, Any]:
- """
- Fetches detailed market information for a single cryptocurrency.
-
- Args:
- coin_id: The ID of the coin (e.g., 'bitcoin').
-
- Returns:
- A dictionary with formatted market data or an error.
- """
- params = {
- 'vs_currency': 'usd',
- 'ids': coin_id
- }
+ """3. Fetches detailed market information for a single cryptocurrency."""
+ params = {'vs_currency': 'usd', 'ids': coin_id}
data = _make_request("coins/markets", params=params)
- # API returns a list, we only want the first item
if data and "error" not in data and isinstance(data, list) and len(data) > 0:
- coin_data = data[0]
- # Format into a standardized response
- return {
- "id": coin_data.get('id'),
- "symbol": coin_data.get('symbol'),
- "name": coin_data.get('name'),
- "image": coin_data.get('image'),
- "current_price": coin_data.get('current_price'),
- "market_cap": coin_data.get('market_cap'),
- "total_volume": coin_data.get('total_volume'),
- "price_change_24h": coin_data.get('price_change_percentage_24h')
- }
+ return data[0] # The raw API response is already well-formatted
elif isinstance(data, list) and len(data) == 0:
return {"error": f"No market data found for ID: {coin_id}"}
- return data # Return original error or empty list
+ return data
-# 3. CLI INTERFACE (from your guide)
+def get_historical_data(coin_id: str, date: str) -> Dict[str, Any]:
+ """4. Fetches historical data for a coin on a specific date."""
+ params = {'date': date} # API expects DD-MM-YYYY format
+ data = _make_request(f"coins/{coin_id}/history", params=params)
+
+ if data and "error" not in data:
+ # Standardize the response
+ return {
+ "id": data.get('id'),
+ "symbol": data.get('symbol'),
+ "date": date,
+ "price_usd": data.get('market_data', {}).get('current_price', {}).get('usd'),
+ "market_cap_usd": data.get('market_data', {}).get('market_cap', {}).get('usd'),
+ "volume_usd": data.get('market_data', {}).get('total_volume', {}).get('usd')
+ }
+ return data
+
+def get_market_chart(coin_id: str, days: int) -> Dict[str, Any]:
+ """5. Fetches market chart data (prices, market caps, volumes) for a coin."""
+ params = {'vs_currency': 'usd', 'days': days}
+ data = _make_request(f"coins/{coin_id}/market_chart", params=params)
+ return data # The raw response is already well-structured for charting
+
+def get_trending_coins() -> Dict[str, Any]:
+ """6. Fetches the top-7 trending coins on CoinGecko."""
+ data = _make_request("search/trending")
+ if data and "error" not in data:
+ # The API returns a list of items inside a 'coins' key
+ return {"trending": data.get("coins", [])}
+ return data
+
+def search_coins(query: str) -> Dict[str, Any]:
+ """7. Searches for coins by name or symbol."""
+ params = {'query': query}
+ data = _make_request("search", params=params)
+ if data and "error" not in data:
+ return {"results": data.get("coins", [])}
+ return data
+
+def get_global_market_data() -> Dict[str, Any]:
+ """8. Fetches global crypto market data (e.g., BTC dominance)."""
+ response = _make_request("global")
+ if response and "error" not in response:
+ return response.get("data", {}) # Data is nested under a 'data' key
+ return response
+
+# 3. CLI INTERFACE
def main():
"""Main Command-Line Interface entry point."""
if len(sys.argv) < 2:
print(json.dumps({
"error": "Usage: python coingecko_data.py <command> [args]",
- "commands": ["ping", "price <id1> [id2...]", "info <id>"]
+ "commands": [
+ "ping",
+ "price <id1> [id2...]",
+ "info <id>",
+ "history <id> <dd-mm-yyyy>",
+ "chart <id> <days>",
+ "trending",
+ "search <query>",
+ "global"
+ ]
}, indent=2))
sys.exit(1)
command = sys.argv[1].lower()
result = {}
- if command == "ping":
- result = get_ping()
- elif command == "price":
- if len(sys.argv) < 3:
- result = {"error": "Usage: python coingecko_data.py price <coin_id_1> [coin_id_2]..."}
- else:
- coin_ids = sys.argv[2:]
- result = get_prices(coin_ids)
- elif command == "info":
- if len(sys.argv) != 3:
- result = {"error": "Usage: python coingecko_data.py info <coin_id>"}
+ try:
+ if command == "ping":
+ result = get_ping()
+ elif command == "price":
+ result = get_prices(sys.argv[2:])
+ elif command == "info":
+ result = get_market_info(sys.argv[2])
+ elif command == "history":
+ result = get_historical_data(sys.argv[2], sys.argv[3])
+ elif command == "chart":
+ result = get_market_chart(sys.argv[2], int(sys.argv[3]))
+ elif command == "trending":
+ result = get_trending_coins()
+ elif command == "search":
+ result = search_coins(" ".join(sys.argv[2:]))
+ elif command == "global":
+ result = get_global_market_data()
else:
- coin_id = sys.argv[2]
- result = get_market_info(coin_id)
- else:
- result = {"error": f"Unknown command: {command}"}
+ result = {"error": f"Unknown command: {command}"}
+ except IndexError:
+ result = {"error": "Missing arguments for command."}
+ except Exception as e:
+ result = {"error": f"An unexpected error occurred: {e}"}
- # Print the final JSON output
print(json.dumps(result, indent=2))
if __name__ == "__main__":