Glanceway Glanceway
جميع المصادر

CoinGecko Crypto Markets

المالية v1.0.0

Top cryptocurrencies by market cap from CoinGecko

@codytseng #crypto #finance #markets

الإعدادات

الاسم المفتاح النوع مطلوب القيمة الافتراضية الوصف
Currency CURRENCY select لا usd Display currency for prices
usdeurgbpjpycny
Sort Order ORDER select لا market_cap_desc How to sort the coin list
market_cap_descvolume_desc

الشيفرة المصدرية

version: 1.0.0
name: CoinGecko Crypto Markets
description: Top cryptocurrencies by market cap from CoinGecko
author: codytseng
author_url: https://github.com/codytseng
category: Finance
tags:
  - crypto
  - finance
  - markets

config:
  - key: CURRENCY
    name: Currency
    type: select
    required: false
    default: usd
    description: Display currency for prices
    options:
      - usd
      - eur
      - gbp
      - jpy
      - cny
  - key: ORDER
    name: Sort Order
    type: select
    required: false
    default: market_cap_desc
    description: How to sort the coin list
    options:
      - market_cap_desc
      - volume_desc
import type { GlancewayAPI, SourceMethods } from "../../types";

interface CoinMarket {
  id: string;
  symbol: string;
  name: string;
  current_price: number;
  price_change_percentage_24h: number | null;
  last_updated: string;
}

export default (api: GlancewayAPI): SourceMethods => {
  const currency = api.config.get("CURRENCY") || "usd";
  const order = api.config.get("ORDER") || "market_cap_desc";

  return {
    async refresh() {
      const url = `https://api.coingecko.com/api/v3/coins/markets?vs_currency=${currency}&order=${order}&per_page=25&sparkline=false`;

      const response = await api.fetch<CoinMarket[]>(url);

      const now = Date.now();
      if (response.ok && response.json) {
        const items = response.json.map((item, i) => {
          const change = item.price_change_percentage_24h;
          const changeStr =
            change != null
              ? ` ${change > 0 ? "+" : ""}${change.toFixed(2)}%`
              : "";

          return {
            id: item.id,
            title: `${item.name} (${item.symbol.toUpperCase()})`,
            subtitle: `${currencySymbol(currency)}${item.current_price.toLocaleString("en-US")}${changeStr}`,
            url: `https://www.coingecko.com/en/coins/${item.id}`,
            timestamp: now - i,
          };
        });

        api.emit(items);
      }
    },
  };
};

function currencySymbol(currency: string): string {
  switch (currency) {
    case "usd":
      return "$";
    case "eur":
      return "\u20AC";
    case "gbp":
      return "\u00A3";
    case "jpy":
      return "\u00A5";
    case "cny":
      return "\u00A5";
    default:
      return "";
  }
}