From 0da2afb1caafac30fa3d1ac21c6fff61f9f10975 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Wed, 21 Aug 2019 12:15:04 +0300 Subject: [PATCH] use CoinGecko instead of CoinMarketcap for exchange rates CoinMarketcap deprecated and took offline its v1 API. They have v2 API which has free plan and requires API keys. But as part of https://github.com/poanetwork/blockscout/issues/234 we alresy implemented fetching from CoinGecko. Now, coin id should be set for CoinGecko for the required coin. ``` config :explorer, Explorer.ExchangeRates.Source.CoinGecko, coin_id: System.get_env("COIN_GECKO_ID", "poa-network") ``` Or the `COIN_GECKO_ID` env var should be set. For example, COIN_GECKO_ID=poa-network --- apps/explorer/config/config.exs | 2 + apps/explorer/lib/explorer/chain.ex | 2 +- .../{coin_market_cap.ex => exchange_rate.ex} | 4 +- .../lib/explorer/exchange_rates/source.ex | 2 +- .../exchange_rates/source/coin_gecko.ex | 38 +++++++++++-------- 5 files changed, 28 insertions(+), 20 deletions(-) rename apps/explorer/lib/explorer/chain/supply/{coin_market_cap.ex => exchange_rate.ex} (72%) diff --git a/apps/explorer/config/config.exs b/apps/explorer/config/config.exs index 95c80ba445..87b57d5f82 100644 --- a/apps/explorer/config/config.exs +++ b/apps/explorer/config/config.exs @@ -34,6 +34,8 @@ config :explorer, Explorer.Chain.Cache.BlockNumber, enabled: true config :explorer, Explorer.ExchangeRates.Source.CoinMarketCap, pages: String.to_integer(System.get_env("COINMARKETCAP_PAGES") || "10") +config :explorer, Explorer.ExchangeRates.Source.CoinGecko, coin_id: System.get_env("COIN_GECKO_ID", "poa-network") + balances_update_interval = if System.get_env("ADDRESS_WITH_BALANCES_UPDATE_INTERVAL") do case Integer.parse(System.get_env("ADDRESS_WITH_BALANCES_UPDATE_INTERVAL")) do diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 58253c0f27..90df61749c 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -2717,7 +2717,7 @@ defmodule Explorer.Chain do end defp supply_module do - Application.get_env(:explorer, :supply, Explorer.Chain.Supply.CoinMarketCap) + Application.get_env(:explorer, :supply, Explorer.Chain.Supply.ExchangeRate) end @doc """ diff --git a/apps/explorer/lib/explorer/chain/supply/coin_market_cap.ex b/apps/explorer/lib/explorer/chain/supply/exchange_rate.ex similarity index 72% rename from apps/explorer/lib/explorer/chain/supply/coin_market_cap.ex rename to apps/explorer/lib/explorer/chain/supply/exchange_rate.ex index ebaadb3c47..d45a8edc02 100644 --- a/apps/explorer/lib/explorer/chain/supply/coin_market_cap.ex +++ b/apps/explorer/lib/explorer/chain/supply/exchange_rate.ex @@ -1,6 +1,6 @@ -defmodule Explorer.Chain.Supply.CoinMarketCap do +defmodule Explorer.Chain.Supply.ExchangeRate do @moduledoc """ - Defines the supply API for calculating supply for coins from coinmarketcap. + Defines the supply API for calculating supply for coins from exchange_rate.. """ use Explorer.Chain.Supply diff --git a/apps/explorer/lib/explorer/exchange_rates/source.ex b/apps/explorer/lib/explorer/exchange_rates/source.ex index 41b87c1e29..d733b22222 100644 --- a/apps/explorer/lib/explorer/exchange_rates/source.ex +++ b/apps/explorer/lib/explorer/exchange_rates/source.ex @@ -83,7 +83,7 @@ defmodule Explorer.ExchangeRates.Source do @spec exchange_rates_source() :: module() defp exchange_rates_source do - config(:source) || Explorer.ExchangeRates.Source.CoinMarketCap + config(:source) || Explorer.ExchangeRates.Source.CoinGecko end @spec config(atom()) :: term diff --git a/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex b/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex index 4e59537bf6..f12eaca844 100644 --- a/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex +++ b/apps/explorer/lib/explorer/exchange_rates/source/coin_gecko.ex @@ -15,39 +15,45 @@ defmodule Explorer.ExchangeRates.Source.CoinGecko do {:ok, price} = get_btc_price() btc_price = to_decimal(price) - for item <- decode_json(data), - not is_nil(item["total_supply"]) and not is_nil(item["current_price"]) do - {:ok, last_updated, 0} = DateTime.from_iso8601(item["last_updated"]) + json_data = decode_json(data) - current_price = to_decimal(item["current_price"]) + market_data = json_data["market_data"] + {:ok, last_updated, 0} = DateTime.from_iso8601(market_data["last_updated"]) - id = item["id"] - btc_value = if id != "btc", do: Decimal.div(current_price, btc_price), else: 1 + current_price = to_decimal(market_data["current_price"]["usd"]) + id = json_data["id"] + btc_value = if id != "btc", do: Decimal.div(current_price, btc_price), else: 1 + + [ %Token{ - available_supply: to_decimal(item["total_supply"]), - total_supply: to_decimal(item["total_supply"]), + available_supply: to_decimal(market_data["circulating_supply"]), + total_supply: to_decimal(market_data["total_supply"]), btc_value: btc_value, - id: id, + id: json_data["id"], last_updated: last_updated, - market_cap_usd: to_decimal(item["market_cap"]), - name: item["name"], - symbol: item["symbol"], + market_cap_usd: to_decimal(market_data["market_cap"]["usd"]), + name: json_data["name"], + symbol: String.upcase(json_data["symbol"]), usd_value: current_price, - volume_24h_usd: to_decimal(item["total_volume"]) + volume_24h_usd: to_decimal(market_data["total_volume"]["usd"]) } - end + ] end @impl Source - def source_url(currency \\ "usd") do - "#{base_url()}/coins/markets?vs_currency=#{currency}" + def source_url do + "#{base_url()}/coins/#{coin_id()}" end defp base_url do config(:base_url) || "https://api.coingecko.com/api/v3" end + defp coin_id do + Application.get_env(:explorer, __MODULE__)[:coin_id] + end + defp get_btc_price(currency \\ "usd") do url = "#{base_url()}/exchange_rates"