Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Access CoinGecko crypto market data: spot prices, OHLC, trending coins, exchange listings, NFTs, and global stats.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
tools/infrastructure.py
1#!/usr/bin/env python32"""3CoinGecko Infrastructure Tools45Tools for fetching reference data: asset platforms, exchange rates, supported currencies, and categories list.6"""78import os9from dotenv import load_dotenv10import json11import argparse12from typing import Dict, Any, Optional1314from core.http_client import proxied_get1516# Load environment variables17load_dotenv()181920def get_api_key() -> str:21"""Get CoinGecko API key from environment."""22api_key = os.getenv("COINGECKO_API_KEY")23if not api_key:24raise ValueError("COINGECKO_API_KEY environment variable is required")25return api_key262728def get_asset_platforms(filter: Optional[str] = None) -> Dict[str, Any]:29"""30Get all blockchain networks/asset platforms.3132Args:33filter: Filter platforms (e.g., "nft" for NFT-supporting platforms)3435Returns:36Dictionary with list of asset platforms37"""38api_key = get_api_key()3940url = "https://pro-api.coingecko.com/api/v3/asset_platforms"41headers = {"x-cg-pro-api-key": api_key}42params = {}43if filter:44params["filter"] = filter4546response = proxied_get(url, headers=headers, params=params, timeout=30)47response.raise_for_status()48data = response.json()4950platforms = []51for platform in data:52platforms.append({53"id": platform.get("id", ""),54"chain_identifier": platform.get("chain_identifier"),55"name": platform.get("name", ""),56"shortname": platform.get("shortname"),57"native_coin_id": platform.get("native_coin_id"),58"image": platform.get("image", {})59})6061return {62"platforms": platforms,63"count": len(platforms),64"filter": filter65}666768def get_exchange_rates() -> Dict[str, Any]:69"""70Get BTC exchange rates to all fiat currencies.7172Returns:73Dictionary with BTC exchange rates74"""75api_key = get_api_key()7677url = "https://pro-api.coingecko.com/api/v3/exchange_rates"78headers = {"x-cg-pro-api-key": api_key}7980response = proxied_get(url, headers=headers, timeout=30)81response.raise_for_status()82data = response.json()8384rates = data.get("rates", {})85formatted_rates = {}86for currency, info in rates.items():87formatted_rates[currency] = {88"name": info.get("name", ""),89"unit": info.get("unit", ""),90"value": info.get("value"),91"type": info.get("type", "")92}9394return {95"base": "btc",96"rates": formatted_rates,97"count": len(formatted_rates)98}99100101def get_vs_currencies() -> Dict[str, Any]:102"""103Get list of supported quote currencies (vs_currencies).104105Returns:106Dictionary with list of supported currencies107"""108api_key = get_api_key()109110url = "https://pro-api.coingecko.com/api/v3/simple/supported_vs_currencies"111headers = {"x-cg-pro-api-key": api_key}112113response = proxied_get(url, headers=headers, timeout=30)114response.raise_for_status()115data = response.json()116117return {118"currencies": data,119"count": len(data)120}121122123def get_categories_list() -> Dict[str, Any]:124"""125Get lightweight list of category names and IDs (no market data).126127Returns:128Dictionary with list of categories129"""130api_key = get_api_key()131132url = "https://pro-api.coingecko.com/api/v3/coins/categories/list"133headers = {"x-cg-pro-api-key": api_key}134135response = proxied_get(url, headers=headers, timeout=30)136response.raise_for_status()137data = response.json()138139categories = []140for category in data:141categories.append({142"category_id": category.get("category_id", ""),143"name": category.get("name", "")144})145146return {147"categories": categories,148"count": len(categories)149}150151152def main():153"""CLI interface for infrastructure tools."""154parser = argparse.ArgumentParser(155description="CoinGecko Infrastructure Tools",156formatter_class=argparse.RawDescriptionHelpFormatter157)158159subparsers = parser.add_subparsers(dest="command")160161# Asset platforms command162platforms_parser = subparsers.add_parser("platforms", help="Get all asset platforms")163platforms_parser.add_argument("--filter", help="Filter platforms (e.g., nft)")164165# Exchange rates command166subparsers.add_parser("exchange-rates", help="Get BTC exchange rates")167168# Supported currencies command169subparsers.add_parser("currencies", help="Get supported vs_currencies")170171# Categories list command172subparsers.add_parser("categories", help="Get category names and IDs")173174args = parser.parse_args()175176try:177if args.command == "platforms":178result = get_asset_platforms(getattr(args, 'filter', None))179elif args.command == "exchange-rates":180result = get_exchange_rates()181elif args.command == "currencies":182result = get_vs_currencies()183elif args.command == "categories":184result = get_categories_list()185else:186parser.print_help()187return 0188189print(json.dumps(result, indent=2, ensure_ascii=False))190return 0191192except Exception as e:193print(json.dumps({"error": str(e)}, indent=2))194return 1195196197if __name__ == "__main__":198exit(main())199