Glanceway Glanceway
All sources

CoinGecko Crypto Markets

Finance v1.0.0

Top cryptocurrencies by market cap from CoinGecko

@codytseng #crypto #finance #markets

Configuration

Name Key Type Required Default Description
Currency CURRENCY select No usd Display currency for prices
usdeurgbpjpycny
Sort Order ORDER select No market_cap_desc How to sort the coin list
market_cap_descvolume_desc

Source Code

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
function currencySymbol(currency) {
  switch (currency) {
    case "usd":
      return "$";
    case "eur":
      return "\u20AC";
    case "gbp":
      return "\u00A3";
    case "jpy":
      return "\u00A5";
    case "cny":
      return "\u00A5";
    default:
      return "";
  }
}

module.exports = async (api) => {
  const currency = api.config.get("CURRENCY") || "usd";
  const order = api.config.get("ORDER") || "market_cap_desc";

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

    const response = await api.fetch(url);

    if (!response.ok || !response.json) {
      throw new Error(`Failed to fetch CoinGecko market data (HTTP ${response.status})`);
    }

    const now = Date.now();
    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);
  }

  await fetchData();

  return {
    refresh: fetchData,
  };
};